1 //===-- DumpASTTests.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "Annotations.h"
12 #include "clang/AST/ASTTypeTraits.h"
13 #include "llvm/Support/ScopedPrinter.h"
14 #include "gmock/gmock.h"
15 #include "gtest/gtest.h"
20 using testing::Contains
;
22 using testing::SizeIs
;
24 MATCHER_P(withDetail
, str
, "") { return arg
.detail
== str
; }
26 TEST(DumpASTTests
, BasicInfo
) {
27 std::pair
</*Code=*/std::string
, /*Expected=*/std::string
> Cases
[] = {
34 declaration: Function - root
37 declaration: ParmVar - x
42 expression: ImplicitCast - IntegralToFloating
43 expression: BinaryOperator - +
44 expression: ImplicitCast - LValueToRValue
45 expression: UnaryOperator - *
46 expression: ImplicitCast - LValueToRValue
47 expression: DeclRef - x
48 expression: IntegerLiteral - 1
52 struct S { static const int x = 0; };
53 int y = S::x + root::S().x;
57 declaration: Namespace - root
58 declaration: CXXRecord - S
60 type: Qualified - const
62 expression: IntegerLiteral - 0
63 declaration: CXXConstructor
64 declaration: CXXConstructor
65 declaration: CXXConstructor
66 declaration: CXXDestructor
69 expression: ExprWithCleanups
70 expression: BinaryOperator - +
71 expression: ImplicitCast - LValueToRValue
72 expression: DeclRef - x
75 expression: ImplicitCast - LValueToRValue
76 expression: Member - x
77 expression: MaterializeTemporary - rvalue
78 expression: CXXTemporaryObject - S
80 specifier: Namespace - root::
85 template <typename T> int tmpl() {
86 (void)tmpl<unsigned>();
92 declaration: Namespace - root
93 declaration: FunctionTemplate - tmpl
94 declaration: TemplateTypeParm - T
95 declaration: Function - tmpl
99 expression: CStyleCast - ToVoid
102 expression: ImplicitCast - FunctionToPointerDecay
103 expression: DeclRef - tmpl
104 template argument: Type
105 type: Builtin - unsigned int
107 expression: DependentScopeDeclRef - value
109 type: TemplateTypeParm - T
112 struct Foo { char operator+(int); };
113 char root = Foo() + 42;
116 declaration: Var - root
118 expression: ExprWithCleanups
119 expression: CXXOperatorCall
120 expression: ImplicitCast - FunctionToPointerDecay
121 expression: DeclRef - operator+
122 expression: MaterializeTemporary - lvalue
123 expression: CXXTemporaryObject - Foo
126 expression: IntegerLiteral - 42
137 declaration: CXXMethod - root
142 expression: ImplicitCast - LValueToRValue
143 expression: Member - x
144 expression: CXXThis - const, implicit
147 for (const auto &Case
: Cases
) {
148 ParsedAST AST
= TestTU::withCode(Case
.first
).build();
149 auto Node
= dumpAST(DynTypedNode::create(findUnqualifiedDecl(AST
, "root")),
150 AST
.getTokens(), AST
.getASTContext());
151 EXPECT_EQ(llvm::StringRef(Case
.second
).trim(),
152 llvm::StringRef(llvm::to_string(Node
)).trim());
156 TEST(DumpASTTests
, Range
) {
157 Annotations
Case("$var[[$type[[int]] x]];");
158 ParsedAST AST
= TestTU::withCode(Case
.code()).build();
159 auto Node
= dumpAST(DynTypedNode::create(findDecl(AST
, "x")), AST
.getTokens(),
160 AST
.getASTContext());
161 EXPECT_EQ(Node
.range
, Case
.range("var"));
162 ASSERT_THAT(Node
.children
, SizeIs(1)) << "Expected one child typeloc";
163 EXPECT_EQ(Node
.children
.front().range
, Case
.range("type"));
166 TEST(DumpASTTests
, NoRange
) {
167 auto TU
= TestTU::withHeaderCode("void funcFromHeader();");
168 TU
.Code
= "int varFromSource;";
169 ParsedAST AST
= TU
.build();
171 DynTypedNode::create(*AST
.getASTContext().getTranslationUnitDecl()),
172 AST
.getTokens(), AST
.getASTContext());
173 ASSERT_THAT(Node
.children
, Contains(withDetail("varFromSource")));
174 ASSERT_THAT(Node
.children
, Not(Contains(withDetail("funcFromHeader"))));
175 EXPECT_THAT(Node
.arcana
, testing::StartsWith("TranslationUnitDecl "));
176 ASSERT_FALSE(Node
.range
) << "Expected no range for translation unit";
179 TEST(DumpASTTests
, Arcana
) {
180 ParsedAST AST
= TestTU::withCode("int x;").build();
181 auto Node
= dumpAST(DynTypedNode::create(findDecl(AST
, "x")), AST
.getTokens(),
182 AST
.getASTContext());
183 EXPECT_THAT(Node
.arcana
, testing::StartsWith("VarDecl "));
184 EXPECT_THAT(Node
.arcana
, testing::EndsWith(" 'int'"));
185 ASSERT_THAT(Node
.children
, SizeIs(1)) << "Expected one child typeloc";
186 EXPECT_THAT(Node
.children
.front().arcana
, testing::StartsWith("QualType "));
190 } // namespace clangd