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
.