1 //===- unittests/AST/ASTTraverserTest.h------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/AST/ASTContext.h"
10 #include "clang/AST/ASTNodeTraverser.h"
11 #include "clang/AST/TextNodeDumper.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 #include "clang/Tooling/Tooling.h"
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
17 
18 using namespace clang::tooling;
19 using namespace clang::ast_matchers;
20 
21 namespace clang {
22 
23 class NodeTreePrinter : public TextTreeStructure {
24   llvm::raw_ostream &OS;
25 
26 public:
27   NodeTreePrinter(llvm::raw_ostream &OS)
28       : TextTreeStructure(OS, /* showColors */ false), OS(OS) {}
29 
30   void Visit(const Decl *D) { OS << D->getDeclKindName() << "Decl"; }
31 
32   void Visit(const Stmt *S) { OS << S->getStmtClassName(); }
33 
34   void Visit(QualType QT) {
35     OS << "QualType " << QT.split().Quals.getAsString();
36   }
37 
38   void Visit(const Type *T) { OS << T->getTypeClassName() << "Type"; }
39 
40   void Visit(const comments::Comment *C, const comments::FullComment *FC) {
41     OS << C->getCommentKindName();
42   }
43 
44   void Visit(const CXXCtorInitializer *Init) { OS << "CXXCtorInitializer"; }
45 
46   void Visit(const Attr *A) {
47     switch (A->getKind()) {
48 #define ATTR(X)                                                                \
49   case attr::X:                                                                \
50     OS << #X;                                                                  \
51     break;
52 #include "clang/Basic/AttrList.inc"
53     }
54     OS << "Attr";
55   }
56 
57   void Visit(const OMPClause *C) { OS << "OMPClause"; }
58   void Visit(const TemplateArgument &A, SourceRange R = {},
59              const Decl *From = nullptr, const char *Label = nullptr) {
60     OS << "TemplateArgument";
61   }
62 
63   template <typename... T> void Visit(T...) {}
64 };
65 
66 class TestASTDumper : public ASTNodeTraverser<TestASTDumper, NodeTreePrinter> {
67 
68   NodeTreePrinter MyNodeRecorder;
69 
70 public:
71   TestASTDumper(llvm::raw_ostream &OS) : MyNodeRecorder(OS) {}
72   NodeTreePrinter &doGetNodeDelegate() { return MyNodeRecorder; }
73 };
74 
75 template <typename... NodeType> std::string dumpASTString(NodeType &&... N) {
76   std::string Buffer;
77   llvm::raw_string_ostream OS(Buffer);
78 
79   TestASTDumper Dumper(OS);
80 
81   OS << "\n";
82 
83   Dumper.Visit(std::forward<NodeType &&>(N)...);
84 
85   return OS.str();
86 }
87 
88 const FunctionDecl *getFunctionNode(clang::ASTUnit *AST,
89                                     const std::string &Name) {
90   auto Result = ast_matchers::match(functionDecl(hasName(Name)).bind("fn"),
91                                     AST->getASTContext());
92   EXPECT_EQ(Result.size(), 1u);
93   return Result[0].getNodeAs<FunctionDecl>("fn");
94 }
95 
96 template <typename T> struct Verifier {
97   static void withDynNode(T Node, const std::string &DumpString) {
98     EXPECT_EQ(dumpASTString(ast_type_traits::DynTypedNode::create(Node)),
99               DumpString);
100   }
101 };
102 
103 template <typename T> struct Verifier<T *> {
104   static void withDynNode(T *Node, const std::string &DumpString) {
105     EXPECT_EQ(dumpASTString(ast_type_traits::DynTypedNode::create(*Node)),
106               DumpString);
107   }
108 };
109 
110 template <typename T>
111 void verifyWithDynNode(T Node, const std::string &DumpString) {
112   EXPECT_EQ(dumpASTString(Node), DumpString);
113 
114   Verifier<T>::withDynNode(Node, DumpString);
115 }
116 
117 TEST(Traverse, Dump) {
118 
119   auto AST = buildASTFromCode(R"cpp(
120 struct A {
121   int m_number;
122 
123   /// CTor
124   A() : m_number(42) {}
125 
126   [[nodiscard]] const int func() {
127     return 42;
128   }
129 
130 };
131 
132 template<typename T>
133 struct templ
134 {
135 };
136 
137 template<>
138 struct templ<int>
139 {
140 };
141 
142 )cpp");
143 
144   const FunctionDecl *Func = getFunctionNode(AST.get(), "func");
145 
146   verifyWithDynNode(Func,
147                     R"cpp(
148 CXXMethodDecl
149 |-CompoundStmt
150 | `-ReturnStmt
151 |   `-IntegerLiteral
152 `-WarnUnusedResultAttr
153 )cpp");
154 
155   Stmt *Body = Func->getBody();
156 
157   verifyWithDynNode(Body,
158                     R"cpp(
159 CompoundStmt
160 `-ReturnStmt
161   `-IntegerLiteral
162 )cpp");
163 
164   QualType QT = Func->getType();
165 
166   verifyWithDynNode(QT,
167                     R"cpp(
168 FunctionProtoType
169 `-QualType const
170   `-BuiltinType
171 )cpp");
172 
173   const FunctionDecl *CTorFunc = getFunctionNode(AST.get(), "A");
174 
175   verifyWithDynNode(CTorFunc->getType(),
176                     R"cpp(
177 FunctionProtoType
178 `-BuiltinType
179 )cpp");
180 
181   Attr *A = *Func->attr_begin();
182 
183   {
184     std::string expectedString = R"cpp(
185 WarnUnusedResultAttr
186 )cpp";
187 
188     EXPECT_EQ(dumpASTString(A), expectedString);
189   }
190 
191   auto *CTor = dyn_cast<CXXConstructorDecl>(CTorFunc);
192   const CXXCtorInitializer *Init = *CTor->init_begin();
193 
194   verifyWithDynNode(Init,
195                     R"cpp(
196 CXXCtorInitializer
197 `-IntegerLiteral
198 )cpp");
199 
200   const comments::FullComment *Comment =
201       AST->getASTContext().getLocalCommentForDeclUncached(CTorFunc);
202   {
203     std::string expectedString = R"cpp(
204 FullComment
205 `-ParagraphComment
206   `-TextComment
207 )cpp";
208     EXPECT_EQ(dumpASTString(Comment, Comment), expectedString);
209   }
210 
211   auto Result = ast_matchers::match(
212       classTemplateSpecializationDecl(hasName("templ")).bind("fn"),
213       AST->getASTContext());
214   EXPECT_EQ(Result.size(), 1u);
215   auto Templ = Result[0].getNodeAs<ClassTemplateSpecializationDecl>("fn");
216 
217   TemplateArgument TA = Templ->getTemplateArgs()[0];
218 
219   verifyWithDynNode(TA,
220                     R"cpp(
221 TemplateArgument
222 )cpp");
223 }
224 } // namespace clang
225