0

I have an LibTooling (TimeFlag), which is used to add an flag for every forstmt/whilestmt. And I use ./TimeFlag lalala.cpp -- to insert flags in lalala.cpp

Unfortunately, this tool also will change the headers, even system library.

So is there some ways letting LibTooling just handle the input file?

Aries_Liu
  • 95
  • 1
  • 10

1 Answers1

1

Here are two possibilities: if using a RecursiveASTVisitor, one could use the SourceManager to determine if the location of the statement or declaration is in the main expansion file:

clang::SourceManager &sm(astContext->getSourceManager());
bool const inMainFile(
  sm.isInMainFile( sm.getExpansionLoc( stmt->getLocStart())));
if(inMainFile){
  /* process decl or stmt */
}
else{
  std::cout << "'" << stmt->getNameAsString() << "' is not in main file\n";
}  

There are several similar methods in SourceManager, such as isInSystemHeader to assist with this task.

If you are using AST matchers, you can use isExpansionInMainFile to narrow which nodes it matches:

auto matcher = forStmt( isExpansionInMainFile());

There is a similar matcher, isExpansionInSystemHeader.

  • Thank u very mach for this. In fact I 'm using the file path to identify system headers which seems so foolish. I will use isInSystemHeader to improve my code. Thank U again. – Aries_Liu Jul 21 '17 at 11:42