[PATCH] D95526: [Syntax] Add syntax-tree-dump in clang-check.
Haojian Wu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 27 07:17:34 PST 2021
hokein created this revision.
hokein added a reviewer: sammccall.
Herald added a subscriber: mgorny.
hokein requested review of this revision.
Herald added a project: clang.
This is useful to experiment/develop syntax trees.
Repository:
rG LLVM Github Monorepo
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 BuildSyntaxTree : 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() {
@@ -182,6 +211,8 @@
FrontendFactory = newFrontendActionFactory<clang::ento::AnalysisAction>();
else if (Fixit)
FrontendFactory = newFrontendActionFactory<ClangCheckFixItAction>();
+ else if (SyntaxTreeDump)
+ FrontendFactory = newFrontendActionFactory<BuildSyntaxTree>();
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.319568.patch
Type: text/x-patch
Size: 3187 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210127/9c4dc972/attachment.bin>
More information about the cfe-commits
mailing list