File: DumpASTTests.cpp

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (190 lines) | stat: -rw-r--r-- 6,018 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//===-- DumpASTTests.cpp --------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "Annotations.h"
#include "DumpAST.h"
#include "TestTU.h"
#include "llvm/Support/ScopedPrinter.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace clang {
namespace clangd {
namespace {
using testing::Contains;
using testing::Not;
using testing::SizeIs;

MATCHER_P(WithDetail, str, "") { return arg.detail == str; }

TEST(DumpASTTests, BasicInfo) {
  std::pair</*Code=*/std::string, /*Expected=*/std::string> Cases[] = {
      {R"cpp(
float root(int *x) {
  return *x + 1;
}
      )cpp",
       R"(
declaration: Function - root
  type: FunctionProto
    type: Builtin - float
    declaration: ParmVar - x
      type: Pointer
        type: Builtin - int
  statement: Compound
    statement: Return
      expression: ImplicitCast - IntegralToFloating
        expression: BinaryOperator - +
          expression: ImplicitCast - LValueToRValue
            expression: UnaryOperator - *
              expression: ImplicitCast - LValueToRValue
                expression: DeclRef - x
          expression: IntegerLiteral - 1
      )"},
      {R"cpp(
namespace root {
struct S { static const int x = 0; };
int y = S::x + root::S().x;
}
      )cpp",
       R"(
declaration: Namespace - root
  declaration: CXXRecord - S
    declaration: Var - x
      type: Qualified - const
        type: Builtin - int
      expression: IntegerLiteral - 0
    declaration: CXXConstructor
    declaration: CXXConstructor
    declaration: CXXConstructor
    declaration: CXXDestructor
  declaration: Var - y
    type: Builtin - int
    expression: ExprWithCleanups
      expression: BinaryOperator - +
        expression: ImplicitCast - LValueToRValue
          expression: DeclRef - x
            specifier: TypeSpec
              type: Record - S
        expression: ImplicitCast - LValueToRValue
          expression: Member - x
            expression: MaterializeTemporary - rvalue
              expression: CXXTemporaryObject - S
                type: Elaborated
                  specifier: Namespace - root::
                  type: Record - S
      )"},
      {R"cpp(
namespace root {
template <typename T> int tmpl() {
  (void)tmpl<unsigned>();
  return T::value;
}
}
      )cpp",
       R"(
declaration: Namespace - root
  declaration: FunctionTemplate - tmpl
    declaration: TemplateTypeParm - T
    declaration: Function - tmpl
      type: FunctionProto
        type: Builtin - int
      statement: Compound
        expression: CStyleCast - ToVoid
          type: Builtin - void
          expression: Call
            expression: ImplicitCast - FunctionToPointerDecay
              expression: DeclRef - tmpl
                template argument: Type
                  type: Builtin - unsigned int
        statement: Return
          expression: DependentScopeDeclRef - value
            specifier: TypeSpec
              type: TemplateTypeParm - T
      )"},
      {R"cpp(
struct Foo { char operator+(int); };
char root = Foo() + 42;
      )cpp",
       R"(
declaration: Var - root
  type: Builtin - char
  expression: ExprWithCleanups
    expression: CXXOperatorCall
      expression: ImplicitCast - FunctionToPointerDecay
        expression: DeclRef - operator+
      expression: MaterializeTemporary - lvalue
        expression: CXXTemporaryObject - Foo
          type: Record - Foo
      expression: IntegerLiteral - 42
      )"},
      {R"cpp(
struct Bar {
  int x;
  int root() const {
    return x;
  }
};
      )cpp",
       R"(
declaration: CXXMethod - root
  type: FunctionProto
    type: Builtin - int
  statement: Compound
    statement: Return
      expression: ImplicitCast - LValueToRValue
        expression: Member - x
          expression: CXXThis - const, implicit
      )"},
  };
  for (const auto &Case : Cases) {
    ParsedAST AST = TestTU::withCode(Case.first).build();
    auto Node = dumpAST(DynTypedNode::create(findUnqualifiedDecl(AST, "root")),
                        AST.getTokens(), AST.getASTContext());
    EXPECT_EQ(llvm::StringRef(Case.second).trim(),
              llvm::StringRef(llvm::to_string(Node)).trim());
  }
}

TEST(DumpASTTests, Range) {
  Annotations Case("$var[[$type[[int]] x]];");
  ParsedAST AST = TestTU::withCode(Case.code()).build();
  auto Node = dumpAST(DynTypedNode::create(findDecl(AST, "x")), AST.getTokens(),
                      AST.getASTContext());
  EXPECT_EQ(Node.range, Case.range("var"));
  ASSERT_THAT(Node.children, SizeIs(1)) << "Expected one child typeloc";
  EXPECT_EQ(Node.children.front().range, Case.range("type"));
}

TEST(DumpASTTests, NoRange) {
  auto TU = TestTU::withHeaderCode("void funcFromHeader();");
  TU.Code = "int varFromSource;";
  ParsedAST AST = TU.build();
  auto Node = dumpAST(
      DynTypedNode::create(*AST.getASTContext().getTranslationUnitDecl()),
      AST.getTokens(), AST.getASTContext());
  ASSERT_THAT(Node.children, Contains(WithDetail("varFromSource")));
  ASSERT_THAT(Node.children, Not(Contains(WithDetail("funcFromHeader"))));
  EXPECT_THAT(Node.arcana, testing::StartsWith("TranslationUnitDecl "));
  ASSERT_FALSE(Node.range.hasValue())
      << "Expected no range for translation unit";
}

TEST(DumpASTTests, Arcana) {
  ParsedAST AST = TestTU::withCode("int x;").build();
  auto Node = dumpAST(DynTypedNode::create(findDecl(AST, "x")), AST.getTokens(),
                      AST.getASTContext());
  EXPECT_THAT(Node.arcana, testing::StartsWith("VarDecl "));
  EXPECT_THAT(Node.arcana, testing::EndsWith(" 'int'"));
  ASSERT_THAT(Node.children, SizeIs(1)) << "Expected one child typeloc";
  EXPECT_THAT(Node.children.front().arcana, testing::StartsWith("QualType "));
}

} // namespace
} // namespace clangd
} // namespace clang