[PATCH] D95526: [Syntax] Add syntax-tree-dump in clang-check.
Haojian Wu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 29 05:20:05 PST 2021
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe90e455d2a0c: [Syntax] Add syntax-tree-dump in clang-check. (authored by hokein).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D95526/new/
https://reviews.llvm.org/D95526
Files:
clang/test/Tooling/clang-check-syntax-tree-dump.cpp
clang/tools/clang-check/CMakeLists.txt
clang/tools/clang-check/ClangCheck.cpp
Index: clang/tools/clang-check/ClangCheck.cpp
===================================================================
--- clang/tools/clang-check/ClangCheck.cpp
+++ clang/tools/clang-check/ClangCheck.cpp
@@ -24,6 +24,9 @@
#include "clang/Rewrite/Frontend/FrontendActions.h"
#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
+#include "clang/Tooling/Syntax/BuildTree.h"
+#include "clang/Tooling/Syntax/Tokens.h"
+#include "clang/Tooling/Syntax/Tree.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Option/OptTable.h"
@@ -82,6 +85,10 @@
cl::desc(Options.getOptionHelpText(options::OPT_fix_what_you_can)),
cl::cat(ClangCheckCategory));
+static cl::opt<bool> SyntaxTreeDump("syntax-tree-dump",
+ cl::desc("dump the syntax tree"),
+ cl::cat(ClangCheckCategory));
+
namespace {
// FIXME: Move FixItRewriteInPlace from lib/Rewrite/Frontend/FrontendActions.cpp
@@ -131,6 +138,28 @@
}
};
+class DumpSyntaxTree : public clang::ASTFrontendAction {
+public:
+ std::unique_ptr<clang::ASTConsumer>
+ CreateASTConsumer(clang::CompilerInstance &CI, StringRef InFile) override {
+ class Consumer : public clang::ASTConsumer {
+ public:
+ Consumer(clang::CompilerInstance &CI) : Collector(CI.getPreprocessor()) {}
+
+ void HandleTranslationUnit(clang::ASTContext &AST) override {
+ clang::syntax::TokenBuffer TB = std::move(Collector).consume();
+ clang::syntax::Arena A(AST.getSourceManager(), AST.getLangOpts(), TB);
+ llvm::outs() << clang::syntax::buildSyntaxTree(A, AST)->dump(
+ AST.getSourceManager());
+ }
+
+ private:
+ clang::syntax::TokenCollector Collector;
+ };
+ return std::make_unique<Consumer>(CI);
+ }
+};
+
class ClangCheckActionFactory {
public:
std::unique_ptr<clang::ASTConsumer> newASTConsumer() {
@@ -188,6 +217,8 @@
FrontendFactory = newFrontendActionFactory<clang::ento::AnalysisAction>();
else if (Fixit)
FrontendFactory = newFrontendActionFactory<ClangCheckFixItAction>();
+ else if (SyntaxTreeDump)
+ FrontendFactory = newFrontendActionFactory<DumpSyntaxTree>();
else
FrontendFactory = newFrontendActionFactory(&CheckFactory);
Index: clang/tools/clang-check/CMakeLists.txt
===================================================================
--- clang/tools/clang-check/CMakeLists.txt
+++ clang/tools/clang-check/CMakeLists.txt
@@ -18,4 +18,5 @@
clangSerialization
clangStaticAnalyzerFrontend
clangTooling
+ clangToolingSyntax
)
Index: clang/test/Tooling/clang-check-syntax-tree-dump.cpp
===================================================================
--- /dev/null
+++ clang/test/Tooling/clang-check-syntax-tree-dump.cpp
@@ -0,0 +1,9 @@
+// RUN: clang-check -syntax-tree-dump "%s" -- 2>&1 | FileCheck %s
+int abc;
+// CHECK: TranslationUnit Detached
+// CHECK-NEXT: `-SimpleDeclaration
+// CHECK-NEXT: |-'int'
+// CHECK-NEXT: |-DeclaratorList Declarators
+// CHECK-NEXT: | `-SimpleDeclarator ListElement
+// CHECK-NEXT: | `-'abc'
+// CHECK-NEXT: `-';'
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95526.320113.patch
Type: text/x-patch
Size: 3185 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210129/43984c99/attachment.bin>
More information about the cfe-commits
mailing list