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

2017-08-09 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman accepted this revision.
arphaman added a comment.
This revision is now accepted and ready to land.

LGTM


https://reviews.llvm.org/D36180



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2017-08-03 Thread Johannes Altmanninger via Phabricator via cfe-commits
johannes updated this revision to Diff 109538.
johannes added a comment.

merge update


https://reviews.llvm.org/D36180

Files:
  test/Tooling/clang-diff-ast.cpp
  test/Tooling/clang-diff-json.cpp
  tools/clang-diff/ClangDiff.cpp

Index: tools/clang-diff/ClangDiff.cpp
===
--- tools/clang-diff/ClangDiff.cpp
+++ tools/clang-diff/ClangDiff.cpp
@@ -25,9 +25,14 @@
 
 static cl::opt
 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 ASTDumpJson(
+"ast-dump-json",
+cl::desc("Print the internal representation of the AST as JSON."),
+cl::init(false), cl::cat(ClangDiffCategory));
+
 static cl::opt SourcePath(cl::Positional, cl::desc(""),
cl::Required,
cl::cat(ClangDiffCategory));
@@ -166,6 +171,15 @@
   OS << "(" << Id << ")";
 }
 
+static void printTree(raw_ostream , diff::SyntaxTree ) {
+  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 , diff::ASTDiff ,
diff::SyntaxTree , diff::SyntaxTree ,
diff::NodeId Dst) {
@@ -213,15 +227,19 @@
 
   addExtraArgs(CommonCompilations);
 
-  if (ASTDump) {
+  if (ASTDump || ASTDumpJson) {
 if (!DestinationPath.empty()) {
   llvm::errs() << "Error: Please specify exactly one filename.\n";
   return 1;
 }
 std::unique_ptr AST = getAST(CommonCompilations, SourcePath);
 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":)";
Index: test/Tooling/clang-diff-json.cpp
===
--- test/Tooling/clang-diff-json.cpp
+++ test/Tooling/clang-diff-json.cpp
@@ -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;
 };
Index: test/Tooling/clang-diff-ast.cpp
===
--- /dev/null
+++ test/Tooling/clang-diff-ast.cpp
@@ -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;
+  }
+};
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2017-08-02 Thread Johannes Altmanninger via Phabricator via cfe-commits
johannes updated this revision to Diff 109419.
johannes added a comment.
Herald added a subscriber: klimek.

add a test


https://reviews.llvm.org/D36180

Files:
  test/Tooling/clang-diff-ast.cpp
  test/Tooling/clang-diff-json.cpp
  tools/clang-diff/ClangDiff.cpp

Index: tools/clang-diff/ClangDiff.cpp
===
--- tools/clang-diff/ClangDiff.cpp
+++ tools/clang-diff/ClangDiff.cpp
@@ -25,9 +25,14 @@
 
 static cl::opt
 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 ASTDumpJson(
+"ast-dump-json",
+cl::desc("Print the internal representation of the AST as JSON."),
+cl::init(false), cl::cat(ClangDiffCategory));
+
 static cl::opt SourcePath(cl::Positional, cl::desc(""),
cl::Required,
cl::cat(ClangDiffCategory));
@@ -171,6 +176,15 @@
   OS << "(" << Id << ")";
 }
 
+static void printTree(raw_ostream , diff::SyntaxTree ) {
+  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 , diff::ASTDiff ,
diff::SyntaxTree , diff::SyntaxTree ,
diff::NodeId Dst) {
@@ -218,15 +232,19 @@
 
   addExtraArgs(CommonCompilations);
 
-  if (ASTDump) {
+  if (ASTDump || ASTDumpJson) {
 if (!DestinationPath.empty()) {
   llvm::errs() << "Error: Please specify exactly one filename.\n";
   return 1;
 }
 std::unique_ptr AST = getAST(CommonCompilations, SourcePath);
 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":)";
Index: test/Tooling/clang-diff-json.cpp
===
--- test/Tooling/clang-diff-json.cpp
+++ test/Tooling/clang-diff-json.cpp
@@ -1,8 +1,8 @@
-// RUN: clang-diff -ast-dump %s -- | python -m json.tool | FileCheck %s
+// RUN: clang-diff -ast-dump-json %s -- | python -m json.tool | FileCheck %s
 
 // CHECK: "type": "CXXRecordDecl",
-// CHECK: "begin": 185,
-// CHECK: "end": 205,
+// CHECK: "begin": 190,
+// CHECK: "end": 210,
 // CHECK: "type": "FieldDecl",
 class A {
   int x;
Index: test/Tooling/clang-diff-ast.cpp
===
--- /dev/null
+++ test/Tooling/clang-diff-ast.cpp
@@ -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;
+  }
+};
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits