12#include "clang/AST/ASTTypeTraits.h"
13#include "llvm/Support/ScopedPrinter.h"
14#include "gmock/gmock.h"
15#include "gtest/gtest.h"
20using testing::Contains;
26TEST(DumpASTTests, BasicInfo) {
27 std::pair<std::string, std::string> Cases[] = {
34declaration: 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
52struct S { static const int x = 0; ~S(); };
53int y = S::x + root::S().x;
57declaration: Namespace - root
58 declaration: CXXRecord - S
60 type: Qualified - const
62 expression: IntegerLiteral - 0
63 declaration: CXXDestructor
67 declaration: CXXConstructor
68 declaration: CXXConstructor
71 expression: ExprWithCleanups
72 expression: BinaryOperator - +
73 expression: ImplicitCast - LValueToRValue
74 expression: DeclRef - x
77 expression: ImplicitCast - LValueToRValue
78 expression: Member - x
79 expression: CXXBindTemporary
80 expression: CXXTemporaryObject - S
82 specifier: Namespace - root::
86struct S { static const int x = 0; };
87int y = S::x + root::S().x;
91declaration: Namespace - root
92 declaration: CXXRecord - S
94 type: Qualified - const
96 expression: IntegerLiteral - 0
97 declaration: CXXConstructor
98 declaration: CXXConstructor
99 declaration: CXXConstructor
100 declaration: CXXDestructor
103 expression: BinaryOperator - +
104 expression: ImplicitCast - LValueToRValue
105 expression: DeclRef - x
108 expression: ImplicitCast - LValueToRValue
109 expression: Member - x
110 expression: CXXTemporaryObject - S
112 specifier: Namespace - root::
116template <typename T> int tmpl() {
117 (void)tmpl<unsigned>();
123declaration: Namespace - root
124 declaration: FunctionTemplate - tmpl
125 declaration: TemplateTypeParm - T
126 declaration: Function - tmpl
130 expression: CStyleCast - ToVoid
133 expression: ImplicitCast - FunctionToPointerDecay
134 expression: DeclRef - tmpl
135 template argument: Type
136 type: Builtin - unsigned int
138 expression: DependentScopeDeclRef - value
140 type: TemplateTypeParm - T
143struct Foo { char operator+(int); };
144char root = Foo() + 42;
147declaration: Var - root
149 expression: ExprWithCleanups
150 expression: CXXOperatorCall
151 expression: ImplicitCast - FunctionToPointerDecay
152 expression: DeclRef - operator+
153 expression: MaterializeTemporary - lvalue
154 expression: CXXTemporaryObject - Foo
156 expression: IntegerLiteral - 42
167declaration: CXXMethod - root
172 expression: ImplicitCast - LValueToRValue
173 expression: Member - x
174 expression: CXXThis - const, implicit
177 for (
const auto &Case : Cases) {
180 AST.getTokens(),
AST.getASTContext());
181 EXPECT_EQ(llvm::StringRef(Case.second).trim(),
182 llvm::StringRef(llvm::to_string(Node)).trim());
190 AST.getASTContext());
191 EXPECT_EQ(Node.range, Case.range(
"var"));
192 ASSERT_THAT(Node.children, SizeIs(1)) <<
"Expected one child typeloc";
193 EXPECT_EQ(Node.children.front().range, Case.range(
"type"));
196TEST(DumpASTTests, NoRange) {
198 TU.Code =
"int varFromSource;";
201 DynTypedNode::create(*
AST.getASTContext().getTranslationUnitDecl()),
202 AST.getTokens(),
AST.getASTContext());
203 ASSERT_THAT(Node.children, Contains(withDetail(
"varFromSource")));
204 ASSERT_THAT(Node.children, Not(Contains(withDetail(
"funcFromHeader"))));
205 EXPECT_THAT(Node.arcana, testing::StartsWith(
"TranslationUnitDecl "));
206 ASSERT_FALSE(Node.range) <<
"Expected no range for translation unit";
209TEST(DumpASTTests, Arcana) {
212 AST.getASTContext());
213 EXPECT_THAT(Node.arcana, testing::StartsWith(
"VarDecl "));
214 EXPECT_THAT(Node.arcana, testing::EndsWith(
" 'int'"));
215 ASSERT_THAT(Node.children, SizeIs(1)) <<
"Expected one child typeloc";
216 EXPECT_THAT(Node.children.front().arcana, testing::StartsWith(
"QualType "));
219TEST(DumpASTTests, UnbalancedBraces) {
223 Annotations Case(
"/*error-ok*/ $func[[int main() {]]");
226 AST.getTokens(),
AST.getASTContext());
227 ASSERT_EQ(Node.range, Case.range(
"func"));
Same as llvm::Annotations, but adjusts functions to LSP-specific types for positions and ranges.
Stores and provides access to parsed AST.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
ASTNode dumpAST(const DynTypedNode &N, const syntax::TokenBuffer &Tokens, const ASTContext &Ctx)
TEST(BackgroundQueueTest, Priority)
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static TestTU withHeaderCode(llvm::StringRef HeaderCode)
static TestTU withCode(llvm::StringRef Code)