diff options
author | Johannes Altmanninger <aclopte@gmail.com> | 2017-08-20 10:22:32 +0000 |
---|---|---|
committer | Johannes Altmanninger <aclopte@gmail.com> | 2017-08-20 10:22:32 +0000 |
commit | d5b56a8619bd0d8fee3ead9541e02cfe070e032d (patch) | |
tree | 37f86d1d2ee3f50a438f1e0ef9943f827d33687d | |
parent | 88a3d5c8551d8778c354aa3e3cfdf1c75f49c859 (diff) | |
download | bcm5719-llvm-d5b56a8619bd0d8fee3ead9541e02cfe070e032d.tar.gz bcm5719-llvm-d5b56a8619bd0d8fee3ead9541e02cfe070e032d.zip |
[clang-diff] Filter AST nodes
Summary:
Ignore macros and implicit AST nodes, as well as anything outside of the
main source file.
Reviewers: arphaman
Subscribers: klimek
Differential Revision: https://reviews.llvm.org/D36184
llvm-svn: 311280
-rw-r--r-- | clang/lib/Tooling/ASTDiff/ASTDiff.cpp | 17 | ||||
-rw-r--r-- | clang/test/Tooling/clang-diff-ast.cpp | 24 | ||||
-rw-r--r-- | clang/test/Tooling/clang-diff-json.cpp | 2 |
3 files changed, 40 insertions, 3 deletions
diff --git a/clang/lib/Tooling/ASTDiff/ASTDiff.cpp b/clang/lib/Tooling/ASTDiff/ASTDiff.cpp index 43c19a72260..ada7cbcfc55 100644 --- a/clang/lib/Tooling/ASTDiff/ASTDiff.cpp +++ b/clang/lib/Tooling/ASTDiff/ASTDiff.cpp @@ -158,12 +158,23 @@ private: void setLeftMostDescendants(); }; +static bool isSpecializedNodeExcluded(const Decl *D) { return D->isImplicit(); } +static bool isSpecializedNodeExcluded(const Stmt *S) { return false; } + template <class T> static bool isNodeExcluded(const SourceManager &SrcMgr, T *N) { if (!N) return true; SourceLocation SLoc = N->getLocStart(); - return SLoc.isValid() && SrcMgr.isInSystemHeader(SLoc); + if (SLoc.isValid()) { + // Ignore everything from other files. + if (!SrcMgr.isInMainFile(SLoc)) + return true; + // Ignore macros. + if (SLoc != SrcMgr.getSpellingLoc(SLoc)) + return true; + } + return isSpecializedNodeExcluded(N); } namespace { @@ -180,6 +191,8 @@ struct NodeCountVisitor : public RecursiveASTVisitor<NodeCountVisitor> { return true; } bool TraverseStmt(Stmt *S) { + if (S) + S = S->IgnoreImplicit(); if (isNodeExcluded(Tree.AST.getSourceManager(), S)) return true; ++Count; @@ -242,6 +255,8 @@ struct PreorderVisitor : public RecursiveASTVisitor<PreorderVisitor> { return true; } bool TraverseStmt(Stmt *S) { + if (S) + S = S->IgnoreImplicit(); if (isNodeExcluded(Tree.AST.getSourceManager(), S)) return true; auto SavedState = PreTraverse(S); diff --git a/clang/test/Tooling/clang-diff-ast.cpp b/clang/test/Tooling/clang-diff-ast.cpp index a79648d3f61..970031a95b9 100644 --- a/clang/test/Tooling/clang-diff-ast.cpp +++ b/clang/test/Tooling/clang-diff-ast.cpp @@ -12,7 +12,8 @@ void f() { // 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 @@ class X : Base { if (i == 0) // CHECK: StringLiteral: foo( return "foo"; + // CHECK-NOT: ImplicitCastExpr return 0; } @@ -48,3 +50,23 @@ public: 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); +} + +#ifndef GUARD +#define GUARD +// CHECK-NEXT: NamespaceDecl +namespace world { +// nodes from other files are excluded, there should be no output here +#include "clang-diff-ast.cpp" +} +// CHECK-NEXT: FunctionDecl: sentinel +void sentinel(); +#endif diff --git a/clang/test/Tooling/clang-diff-json.cpp b/clang/test/Tooling/clang-diff-json.cpp index cbbd48ea84e..9aac6fa8b15 100644 --- a/clang/test/Tooling/clang-diff-json.cpp +++ b/clang/test/Tooling/clang-diff-json.cpp @@ -3,9 +3,9 @@ // RUN: | FileCheck %s // CHECK: "begin": 299, -// CHECK: "type": "CXXRecordDecl", // CHECK: "type": "FieldDecl", // CHECK: "end": 319, +// CHECK: "type": "CXXRecordDecl", class A { int x; }; |