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; };
53int y = S::x + root::S().x;
57declaration: 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::
85template <typename T> int tmpl() {
86 (void)tmpl<unsigned>();
92declaration: 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
112struct Foo { char operator+(int); };
113char root = Foo() + 42;
116declaration: 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
137declaration: CXXMethod - root
142 expression: ImplicitCast - LValueToRValue
143 expression: Member - x
144 expression: CXXThis - const, implicit
147 for (
const auto &Case : Cases) {
150 AST.getTokens(),
AST.getASTContext());
151 EXPECT_EQ(llvm::StringRef(Case.second).trim(),
152 llvm::StringRef(llvm::to_string(Node)).trim());
156TEST(DumpASTTests, Range) {
157 Annotations Case(
"$var[[$type[[int]] x]];");
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"));
166TEST(DumpASTTests, NoRange) {
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";
179TEST(DumpASTTests, Arcana) {
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 "));
189TEST(DumpASTTests, UnbalancedBraces) {
193 Annotations Case(
"/*error-ok*/ $func[[int main() {]]");
196 AST.getTokens(),
AST.getASTContext());
197 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)