summaryrefslogtreecommitdiffstats
path: root/unittests/clangd/SymbolInfoTests.cpp
blob: cbd178e6853829149800ff47ad66c6c03ba1b7d3 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//===-- SymbolInfoTests.cpp  -----------------------*- C++ -*--------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Annotations.h"
#include "ClangdUnit.h"
#include "Compiler.h"
#include "Matchers.h"
#include "SyncAPI.h"
#include "TestFS.h"
#include "TestTU.h"
#include "XRefs.h"
#include "index/FileIndex.h"
#include "index/SymbolCollector.h"
#include "clang/Index/IndexingAction.h"
#include "llvm/Support/Path.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace clang {
namespace clangd {
namespace {

using testing::ElementsAreArray;

auto CreateExpectedSymbolDetails = [](const std::string &name,
                                      const std::string &container,
                                      const std::string &USR) {
  return SymbolDetails{name, container, USR, SymbolID(USR)};
};

TEST(SymbolInfoTests, All) {
  std::pair<const char *, std::vector<SymbolDetails>>
      TestInputExpectedOutput[] = {
          {
              R"cpp( // Simple function reference - declaration
          void foo();
          int bar() {
            fo^o();
          }
        )cpp",
              {CreateExpectedSymbolDetails("foo", "", "c:@F@foo#")}},
          {
              R"cpp( // Simple function reference - definition
          void foo() {}
          int bar() {
            fo^o();
          }
        )cpp",
              {CreateExpectedSymbolDetails("foo", "", "c:@F@foo#")}},
          {
              R"cpp( // Function in namespace reference
          namespace bar {
            void foo();
            int baz() {
              fo^o();
            }
          }
        )cpp",
              {CreateExpectedSymbolDetails("foo", "bar::", "c:@N@bar@F@foo#")}},
          {
              R"cpp( // Function in different namespace reference
          namespace bar {
            void foo();
          }
          namespace barbar {
            int baz() {
              bar::fo^o();
            }
          }
        )cpp",
              {CreateExpectedSymbolDetails("foo", "bar::", "c:@N@bar@F@foo#")}},
          {
              R"cpp( // Function in global namespace reference
          void foo();
          namespace Nbar {
            namespace Nbaz {
              int baz() {
                ::fo^o();
              }
            }
          }
        )cpp",
              {CreateExpectedSymbolDetails("foo", "", "c:@F@foo#")}},
          {
              R"cpp( // Function in anonymous namespace reference
          namespace {
            void foo();
          }
          namespace barbar {
            int baz() {
              fo^o();
            }
          }
        )cpp",
              {CreateExpectedSymbolDetails("foo", "(anonymous)",
                                           "c:TestTU.cpp@aN@F@foo#")}},
          {
              R"cpp( // Function reference - ADL
          namespace bar {
            struct BarType {};
            void foo(const BarType&);
          }
          namespace barbar {
            int baz() {
              bar::BarType b;
              fo^o(b);
            }
          }
        )cpp",
              {CreateExpectedSymbolDetails(
                  "foo", "bar::", "c:@N@bar@F@foo#&1$@N@bar@S@BarType#")}},
          {
              R"cpp( // Global value reference
          int value;
          void foo(int) { }
          void bar() {
            foo(val^ue);
          }
        )cpp",
              {CreateExpectedSymbolDetails("value", "", "c:@value")}},
          {
              R"cpp( // Local value reference
          void foo() { int aaa; int bbb = aa^a; }
        )cpp",
              {CreateExpectedSymbolDetails("aaa", "foo",
                                           "c:TestTU.cpp@49@F@foo#@aaa")}},
          {
              R"cpp( // Function param
          void bar(int aaa) {
            int bbb = a^aa;
          }
        )cpp",
              {CreateExpectedSymbolDetails("aaa", "bar",
                                           "c:TestTU.cpp@38@F@bar#I#@aaa")}},
          {
              R"cpp( // Lambda capture
          int ii;
          auto lam = [ii]() {
            return i^i;
          };
        )cpp",
              {CreateExpectedSymbolDetails("ii", "", "c:@ii")}},
          {
              R"cpp( // Macro reference
          #define MACRO 5\nint i = MAC^RO;
        )cpp",
              {CreateExpectedSymbolDetails("MACRO", "",
                                           "c:TestTU.cpp@55@macro@MACRO")}},
          {
              R"cpp( // Multiple symbols returned - using overloaded function name
          void foo() {}
          void foo(bool) {}
          void foo(int) {}
          namespace bar {
            using ::fo^o;
          }
        )cpp",
              {CreateExpectedSymbolDetails("foo", "", "c:@F@foo#"),
               CreateExpectedSymbolDetails("foo", "", "c:@F@foo#b#"),
               CreateExpectedSymbolDetails("foo", "", "c:@F@foo#I#")}},
          {
              R"cpp( // Multiple symbols returned - implicit conversion
          struct foo {};
          struct bar {
            bar(const foo&) {}
          };
          void func_baz1(bar) {}
          void func_baz2() {
            foo ff;
            func_baz1(f^f);
          }
        )cpp",
              {
                  CreateExpectedSymbolDetails(
                      "ff", "func_baz2", "c:TestTU.cpp@218@F@func_baz2#@ff"),
                  CreateExpectedSymbolDetails(
                      "bar", "bar::", "c:@S@bar@F@bar#&1$@S@foo#"),
              }},
          {
              R"cpp( // Type reference - declaration
          struct foo;
          void bar(fo^o*);
        )cpp",
              {CreateExpectedSymbolDetails("foo", "", "c:@S@foo")}},
          {
              R"cpp( // Type reference - definition
          struct foo {};
          void bar(fo^o*);
        )cpp",
              {CreateExpectedSymbolDetails("foo", "", "c:@S@foo")}},
          {
              R"cpp( // Type Reference - template argumen
          struct foo {};
          template<class T> struct bar {};
          void baz() {
            bar<fo^o> b;
          }
        )cpp",
              {CreateExpectedSymbolDetails("foo", "", "c:@S@foo")}},
          {
              R"cpp( // Template parameter reference - type param
          template<class TT> struct bar {
            T^T t;
          };
        )cpp",
              {/* not implemented */}},
          {
              R"cpp( // Template parameter reference - type param
          template<int NN> struct bar {
            int a = N^N;
          };
        )cpp",
              {/* not implemented */}},
          {
              R"cpp( // Class member reference - objec
          struct foo {
            int aa;
          };
          void bar() {
            foo f;
            f.a^a;
          }
        )cpp",
              {CreateExpectedSymbolDetails("aa", "foo::", "c:@S@foo@FI@aa")}},
          {
              R"cpp( // Class member reference - pointer
          struct foo {
            int aa;
          };
          void bar() {
            &foo::a^a;
          }
        )cpp",
              {CreateExpectedSymbolDetails("aa", "foo::", "c:@S@foo@FI@aa")}},
          {
              R"cpp( // Class method reference - objec
          struct foo {
            void aa() {}
          };
          void bar() {
            foo f;
            f.a^a();
          }
        )cpp",
              {CreateExpectedSymbolDetails("aa", "foo::", "c:@S@foo@F@aa#")}},
          {
              R"cpp( // Class method reference - pointer
          struct foo {
            void aa() {}
          };
          void bar() {
            &foo::a^a;
          }
        )cpp",
              {CreateExpectedSymbolDetails("aa", "foo::", "c:@S@foo@F@aa#")}},
          {
              R"cpp( // Typedef
          typedef int foo;
          void bar() {
            fo^o a;
          }
        )cpp",
              {CreateExpectedSymbolDetails("foo", "", "c:TestTU.cpp@T@foo")}},
          {
              R"cpp( // Type alias
          using foo = int;
          void bar() {
            fo^o a;
          }
        )cpp",
              {CreateExpectedSymbolDetails("foo", "", "c:@foo")}},
          {
              R"cpp( // Namespace reference
          namespace foo {}
          using namespace fo^o;
        )cpp",
              {CreateExpectedSymbolDetails("foo", "", "c:@N@foo")}},
          {
              R"cpp( // Enum value reference
          enum foo { bar, baz };
          void f() {
            foo fff = ba^r;
          }
        )cpp",
              {CreateExpectedSymbolDetails("bar", "foo", "c:@E@foo@bar")}},
          {
              R"cpp( // Enum class value reference
          enum class foo { bar, baz };
          void f() {
            foo fff = foo::ba^r;
          }
        )cpp",
              {CreateExpectedSymbolDetails("bar", "foo::", "c:@E@foo@bar")}},
          {
              R"cpp( // Type inferrence with auto keyword
          struct foo {};
          foo getfoo() { return foo{}; }
          void f() {
            au^to a = getfoo();
          }
        )cpp",
              {/* not implemented */}},
          {
              R"cpp( // decltype
          struct foo {};
          void f() {
            foo f;
            declt^ype(f);
          }
        )cpp",
              {/* not implemented */}},
      };

  for (const auto &T : TestInputExpectedOutput) {
    Annotations TestInput(T.first);
    auto AST = TestTU::withCode(TestInput.code()).build();

    EXPECT_THAT(getSymbolInfo(AST, TestInput.point()),
                ElementsAreArray(T.second))
        << T.first;
  }
}

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