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;
24MATCHER_P(withDetail, str,
"") {
return arg.detail == str; }
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::
87struct S { static const int x = 0; };
88int y = S::x + root::S().x;
92declaration: Namespace - root
93 declaration: CXXRecord - S
95 type: Qualified - const
97 expression: IntegerLiteral - 0
98 declaration: CXXConstructor
99 declaration: CXXConstructor
100 declaration: CXXConstructor
101 declaration: CXXDestructor
104 expression: BinaryOperator - +
105 expression: ImplicitCast - LValueToRValue
106 expression: DeclRef - x
109 expression: ImplicitCast - LValueToRValue
110 expression: Member - x
111 expression: CXXTemporaryObject - S
113 specifier: Namespace - root::
118template <typename T> int tmpl() {
119 (void)tmpl<unsigned>();
125declaration: Namespace - root
126 declaration: FunctionTemplate - tmpl
127 declaration: TemplateTypeParm - T
128 declaration: Function - tmpl
132 expression: CStyleCast - ToVoid
135 expression: ImplicitCast - FunctionToPointerDecay
136 expression: DeclRef - tmpl
137 template argument: Type
138 type: Builtin - unsigned int
140 expression: DependentScopeDeclRef - value
142 type: TemplateTypeParm - T
145struct Foo { char operator+(int); };
146char root = Foo() + 42;
149declaration: Var - root
151 expression: ExprWithCleanups
152 expression: CXXOperatorCall
153 expression: ImplicitCast - FunctionToPointerDecay
154 expression: DeclRef - operator+
155 expression: MaterializeTemporary - lvalue
156 expression: CXXTemporaryObject - Foo
159 expression: IntegerLiteral - 42
170declaration: CXXMethod - root
175 expression: ImplicitCast - LValueToRValue
176 expression: Member - x
177 expression: CXXThis - const, implicit
180 for (
const auto &Case : Cases) {
183 AST.getTokens(),
AST.getASTContext());
184 EXPECT_EQ(llvm::StringRef(Case.second).trim(),
185 llvm::StringRef(llvm::to_string(Node)).trim());
189TEST(DumpASTTests, Range) {
190 Annotations Case(
"$var[[$type[[int]] x]];");
193 AST.getASTContext());
194 EXPECT_EQ(
Node.range, Case.range(
"var"));
195 ASSERT_THAT(
Node.children, SizeIs(1)) <<
"Expected one child typeloc";
196 EXPECT_EQ(
Node.children.front().range, Case.range(
"type"));
199TEST(DumpASTTests, NoRange) {
201 TU.Code =
"int varFromSource;";
202 ParsedAST
AST = TU.build();
204 DynTypedNode::create(*
AST.getASTContext().getTranslationUnitDecl()),
205 AST.getTokens(),
AST.getASTContext());
206 ASSERT_THAT(
Node.children, Contains(withDetail(
"varFromSource")));
207 ASSERT_THAT(
Node.children, Not(Contains(withDetail(
"funcFromHeader"))));
208 EXPECT_THAT(
Node.arcana, testing::StartsWith(
"TranslationUnitDecl "));
209 ASSERT_FALSE(
Node.range) <<
"Expected no range for translation unit";
212TEST(DumpASTTests, Arcana) {
215 AST.getASTContext());
216 EXPECT_THAT(
Node.arcana, testing::StartsWith(
"VarDecl "));
217 EXPECT_THAT(
Node.arcana, testing::EndsWith(
" 'int'"));
218 ASSERT_THAT(
Node.children, SizeIs(1)) <<
"Expected one child typeloc";
219 EXPECT_THAT(
Node.children.front().arcana, testing::StartsWith(
"QualType "));
222TEST(DumpASTTests, UnbalancedBraces) {
226 Annotations Case(
"/*error-ok*/ $func[[int main() {]]");
229 AST.getTokens(),
AST.getASTContext());
230 ASSERT_EQ(
Node.range, Case.range(
"func"));
::clang::DynTypedNode Node
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)