r311232 - [clang-diff] Add option to dump the AST, one node per line

Johannes Altmanninger via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 19 02:36:15 PDT 2017


Author: krobelus
Date: Sat Aug 19 02:36:14 2017
New Revision: 311232

URL: http://llvm.org/viewvc/llvm-project?rev=311232&view=rev
Log:
[clang-diff] Add option to dump the AST, one node per line

Summary:
This is done with -ast-dump; the JSON variant has been renamed to
-ast-dump-json.

Reviewers: arphaman

Differential Revision: https://reviews.llvm.org/D36180

Added:
    cfe/trunk/test/Tooling/clang-diff-ast.cpp
Modified:
    cfe/trunk/test/Tooling/clang-diff-json.cpp
    cfe/trunk/tools/clang-diff/ClangDiff.cpp

Added: cfe/trunk/test/Tooling/clang-diff-ast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/clang-diff-ast.cpp?rev=311232&view=auto
==============================================================================
--- cfe/trunk/test/Tooling/clang-diff-ast.cpp (added)
+++ cfe/trunk/test/Tooling/clang-diff-ast.cpp Sat Aug 19 02:36:14 2017
@@ -0,0 +1,50 @@
+// RUN: clang-diff -ast-dump %s -- -std=c++11 | FileCheck %s
+
+
+// CHECK: {{^}}TranslationUnitDecl(0)
+// CHECK: {{^}} NamespaceDecl: test;(
+namespace test {
+
+// CHECK: {{^}}  FunctionDecl: f(
+// CHECK: CompoundStmt(
+void f() {
+  // CHECK: VarDecl: i(int)(
+  // CHECK: IntegerLiteral: 1
+  auto i = 1;
+  // CHECK: CallExpr(
+  // CHECK: DeclRefExpr: f(
+  f();
+  // CHECK: BinaryOperator: =(
+  i = i;
+}
+
+} // end namespace test
+
+// CHECK: TypedefDecl: nat;unsigned int;(
+typedef unsigned nat;
+// CHECK: TypeAliasDecl: real;double;(
+using real = double;
+
+class Base {
+};
+
+// CHECK: CXXRecordDecl: X;class X;(
+class X : Base {
+  int m;
+  // CHECK: CXXMethodDecl: foo(const char *(int))(
+  // CHECK: ParmVarDecl: i(int)(
+  const char *foo(int i) {
+    if (i == 0)
+      // CHECK: StringLiteral: foo(
+      return "foo";
+    return 0;
+  }
+
+  // CHECK: AccessSpecDecl: public(
+public:
+  // CHECK: CXXConstructorDecl: X(void (char, int))(
+  X(char, int) : Base(), m(0) {
+    // CHECK: MemberExpr(
+    int x = m;
+  }
+};

Modified: cfe/trunk/test/Tooling/clang-diff-json.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/clang-diff-json.cpp?rev=311232&r1=311231&r2=311232&view=diff
==============================================================================
--- cfe/trunk/test/Tooling/clang-diff-json.cpp (original)
+++ cfe/trunk/test/Tooling/clang-diff-json.cpp Sat Aug 19 02:36:14 2017
@@ -1,11 +1,11 @@
-// RUN: clang-diff -ast-dump %s -- \
+// RUN: clang-diff -ast-dump-json %s -- \
 // RUN: | %python -c 'import json, sys; json.dump(json.loads(sys.stdin.read()), sys.stdout, sort_keys=True, indent=2)' \
 // RUN: | FileCheck %s
 
-// CHECK: "begin": 294,
+// CHECK: "begin": 299,
 // CHECK: "type": "CXXRecordDecl",
 // CHECK: "type": "FieldDecl",
-// CHECK: "end": 314,
+// CHECK: "end": 319,
 class A {
   int x;
 };

Modified: cfe/trunk/tools/clang-diff/ClangDiff.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-diff/ClangDiff.cpp?rev=311232&r1=311231&r2=311232&view=diff
==============================================================================
--- cfe/trunk/tools/clang-diff/ClangDiff.cpp (original)
+++ cfe/trunk/tools/clang-diff/ClangDiff.cpp Sat Aug 19 02:36:14 2017
@@ -25,9 +25,14 @@ static cl::OptionCategory ClangDiffCateg
 
 static cl::opt<bool>
     ASTDump("ast-dump",
-            cl::desc("Print the internal representation of the AST as JSON."),
+            cl::desc("Print the internal representation of the AST."),
             cl::init(false), cl::cat(ClangDiffCategory));
 
+static cl::opt<bool> ASTDumpJson(
+    "ast-dump-json",
+    cl::desc("Print the internal representation of the AST as JSON."),
+    cl::init(false), cl::cat(ClangDiffCategory));
+
 static cl::opt<std::string> SourcePath(cl::Positional, cl::desc("<source>"),
                                        cl::Required,
                                        cl::cat(ClangDiffCategory));
@@ -166,6 +171,15 @@ static void printNode(raw_ostream &OS, d
   OS << "(" << Id << ")";
 }
 
+static void printTree(raw_ostream &OS, diff::SyntaxTree &Tree) {
+  for (diff::NodeId Id : Tree) {
+    for (int I = 0; I < Tree.getNode(Id).Depth; ++I)
+      OS << " ";
+    printNode(OS, Tree, Id);
+    OS << "\n";
+  }
+}
+
 static void printDstChange(raw_ostream &OS, diff::ASTDiff &Diff,
                            diff::SyntaxTree &SrcTree, diff::SyntaxTree &DstTree,
                            diff::NodeId Dst) {
@@ -213,7 +227,7 @@ int main(int argc, const char **argv) {
 
   addExtraArgs(CommonCompilations);
 
-  if (ASTDump) {
+  if (ASTDump || ASTDumpJson) {
     if (!DestinationPath.empty()) {
       llvm::errs() << "Error: Please specify exactly one filename.\n";
       return 1;
@@ -222,6 +236,10 @@ int main(int argc, const char **argv) {
     if (!AST)
       return 1;
     diff::SyntaxTree Tree(AST->getASTContext());
+    if (ASTDump) {
+      printTree(llvm::outs(), Tree);
+      return 0;
+    }
     llvm::outs() << R"({"filename":")";
     printJsonString(llvm::outs(), SourcePath);
     llvm::outs() << R"(","root":)";




More information about the cfe-commits mailing list