Index: lib/Tooling/ASTDiff/ASTDiff.cpp =================================================================== --- lib/Tooling/ASTDiff/ASTDiff.cpp +++ lib/Tooling/ASTDiff/ASTDiff.cpp @@ -162,9 +162,20 @@ if (!N) return true; SourceLocation SLoc = N->getLocStart(); - return SLoc.isValid() && SrcMgr.isInSystemHeader(SLoc); + if (!SLoc.isValid()) + return false; + // Ignore everything from other files. + if (!SrcMgr.isInMainFile(SLoc)) + return true; + // Ignore macros. + if (N->getLocStart() != SrcMgr.getSpellingLoc(N->getLocStart())) + return true; + return false; } +static bool isDeclExcluded(const Decl *D) { return D->isImplicit(); } +static bool isStmtExcluded(const Stmt *S) { return false; } + namespace { /// Counts the number of nodes that will be compared. struct NodeCountVisitor : public RecursiveASTVisitor { @@ -172,14 +183,16 @@ const SyntaxTree::Impl &Tree; NodeCountVisitor(const SyntaxTree::Impl &Tree) : Tree(Tree) {} bool TraverseDecl(Decl *D) { - if (isNodeExcluded(Tree.AST.getSourceManager(), D)) + if (isNodeExcluded(Tree.AST.getSourceManager(), D) || isDeclExcluded(D)) return true; ++Count; RecursiveASTVisitor::TraverseDecl(D); return true; } bool TraverseStmt(Stmt *S) { - if (isNodeExcluded(Tree.AST.getSourceManager(), S)) + if (S) + S = S->IgnoreImplicit(); + if (isNodeExcluded(Tree.AST.getSourceManager(), S) || isStmtExcluded(S)) return true; ++Count; RecursiveASTVisitor::TraverseStmt(S); @@ -233,7 +246,7 @@ N.Height = std::max(N.Height, 1 + Tree.getNode(Child).Height); } bool TraverseDecl(Decl *D) { - if (isNodeExcluded(Tree.AST.getSourceManager(), D)) + if (isNodeExcluded(Tree.AST.getSourceManager(), D) || isDeclExcluded(D)) return true; auto SavedState = PreTraverse(D); RecursiveASTVisitor::TraverseDecl(D); @@ -241,7 +254,9 @@ return true; } bool TraverseStmt(Stmt *S) { - if (isNodeExcluded(Tree.AST.getSourceManager(), S)) + if (S) + S = S->IgnoreImplicit(); + if (isNodeExcluded(Tree.AST.getSourceManager(), S) || isStmtExcluded(S)) return true; auto SavedState = PreTraverse(S); RecursiveASTVisitor::TraverseStmt(S); Index: test/Tooling/clang-diff-ast.cpp =================================================================== --- test/Tooling/clang-diff-ast.cpp +++ test/Tooling/clang-diff-ast.cpp @@ -12,7 +12,8 @@ // CHECK: IntegerLiteral: 1 auto i = 1; // CHECK: CallExpr( - // CHECK: DeclRefExpr: f( + // CHECK-NOT: ImplicitCastExpr + // CHECK-NEXT: DeclRefExpr: f( f(); // CHECK: BinaryOperator: =( i = i; @@ -37,6 +38,7 @@ if (i == 0) // CHECK: StringLiteral: foo( return "foo"; + // CHECK-NOT: ImplicitCastExpr return 0; } @@ -48,3 +50,22 @@ int x = m; } }; + +#define M (void)1 +#define MA(a, b) (void)a, b +// CHECK: FunctionDecl +// CHECK-NEXT: CompoundStmt +void macros() { + M; + MA(1, 2); +} +// CHECK-NEXT: NamespaceDecl + +#ifndef GUARD +#define GUARD +namespace world { +// nodes from other files are excluded +// CHECK-NOT {{.}} +#include "clang-diff-ast.cpp" +} +#endif