[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / clangd / unittests / FindSymbolsTests.cpp
blobb1b8b4ccd184c2bc42ac8ac4b182420dffc6c69e
1 //===-- FindSymbolsTests.cpp -------------------------*- C++ -*------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "Annotations.h"
9 #include "FindSymbols.h"
10 #include "TestFS.h"
11 #include "TestTU.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "gmock/gmock.h"
14 #include "gtest/gtest.h"
16 namespace clang {
17 namespace clangd {
19 namespace {
21 using ::testing::AllOf;
22 using ::testing::ElementsAre;
23 using ::testing::ElementsAreArray;
24 using ::testing::Field;
25 using ::testing::IsEmpty;
26 using ::testing::UnorderedElementsAre;
28 // GMock helpers for matching SymbolInfos items.
29 MATCHER_P(qName, Name, "") {
30 if (arg.containerName.empty())
31 return arg.name == Name;
32 return (arg.containerName + "::" + arg.name) == Name;
34 MATCHER_P(withName, N, "") { return arg.name == N; }
35 MATCHER_P(withKind, Kind, "") { return arg.kind == Kind; }
36 MATCHER_P(withDetail, Detail, "") { return arg.detail == Detail; }
37 MATCHER_P(symRange, Range, "") { return arg.range == Range; }
39 // GMock helpers for matching DocumentSymbol.
40 MATCHER_P(symNameRange, Range, "") { return arg.selectionRange == Range; }
41 template <class... ChildMatchers>
42 ::testing::Matcher<DocumentSymbol> children(ChildMatchers... ChildrenM) {
43 return Field(&DocumentSymbol::children, UnorderedElementsAre(ChildrenM...));
46 std::vector<SymbolInformation> getSymbols(TestTU &TU, llvm::StringRef Query,
47 int Limit = 0) {
48 auto SymbolInfos = getWorkspaceSymbols(Query, Limit, TU.index().get(),
49 testPath(TU.Filename));
50 EXPECT_TRUE(bool(SymbolInfos)) << "workspaceSymbols returned an error";
51 return *SymbolInfos;
54 TEST(WorkspaceSymbols, Macros) {
55 TestTU TU;
56 TU.Code = R"cpp(
57 #define MACRO X
58 )cpp";
60 // LSP's SymbolKind doesn't have a "Macro" kind, and
61 // indexSymbolKindToSymbolKind() currently maps macros
62 // to SymbolKind::String.
63 EXPECT_THAT(getSymbols(TU, "macro"),
64 ElementsAre(AllOf(qName("MACRO"), withKind(SymbolKind::String))));
67 TEST(WorkspaceSymbols, NoLocals) {
68 TestTU TU;
69 TU.Code = R"cpp(
70 void test(int FirstParam, int SecondParam) {
71 struct LocalClass {};
72 int local_var;
73 })cpp";
74 EXPECT_THAT(getSymbols(TU, "l"), ElementsAre(qName("LocalClass")));
75 EXPECT_THAT(getSymbols(TU, "p"), IsEmpty());
78 TEST(WorkspaceSymbols, Globals) {
79 TestTU TU;
80 TU.AdditionalFiles["foo.h"] = R"cpp(
81 int global_var;
83 int global_func();
85 struct GlobalStruct {};)cpp";
86 TU.Code = R"cpp(
87 #include "foo.h"
88 )cpp";
89 EXPECT_THAT(getSymbols(TU, "global"),
90 UnorderedElementsAre(
91 AllOf(qName("GlobalStruct"), withKind(SymbolKind::Struct)),
92 AllOf(qName("global_func"), withKind(SymbolKind::Function)),
93 AllOf(qName("global_var"), withKind(SymbolKind::Variable))));
96 TEST(WorkspaceSymbols, Unnamed) {
97 TestTU TU;
98 TU.AdditionalFiles["foo.h"] = R"cpp(
99 struct {
100 int InUnnamed;
101 } UnnamedStruct;)cpp";
102 TU.Code = R"cpp(
103 #include "foo.h"
104 )cpp";
105 EXPECT_THAT(getSymbols(TU, "UnnamedStruct"),
106 ElementsAre(AllOf(qName("UnnamedStruct"),
107 withKind(SymbolKind::Variable))));
108 EXPECT_THAT(getSymbols(TU, "InUnnamed"),
109 ElementsAre(AllOf(qName("(anonymous struct)::InUnnamed"),
110 withKind(SymbolKind::Field))));
113 TEST(WorkspaceSymbols, InMainFile) {
114 TestTU TU;
115 TU.Code = R"cpp(
116 int test() {}
117 static void test2() {}
118 )cpp";
119 EXPECT_THAT(getSymbols(TU, "test"),
120 ElementsAre(qName("test"), qName("test2")));
123 TEST(WorkspaceSymbols, Namespaces) {
124 TestTU TU;
125 TU.AdditionalFiles["foo.h"] = R"cpp(
126 namespace ans1 {
127 int ai1;
128 namespace ans2 {
129 int ai2;
130 namespace ans3 {
131 int ai3;
135 )cpp";
136 TU.Code = R"cpp(
137 #include "foo.h"
138 )cpp";
139 EXPECT_THAT(getSymbols(TU, "a"),
140 UnorderedElementsAre(
141 qName("ans1"), qName("ans1::ai1"), qName("ans1::ans2"),
142 qName("ans1::ans2::ai2"), qName("ans1::ans2::ans3"),
143 qName("ans1::ans2::ans3::ai3")));
144 EXPECT_THAT(getSymbols(TU, "::"), ElementsAre(qName("ans1")));
145 EXPECT_THAT(getSymbols(TU, "::a"), ElementsAre(qName("ans1")));
146 EXPECT_THAT(getSymbols(TU, "ans1::"),
147 UnorderedElementsAre(qName("ans1::ai1"), qName("ans1::ans2"),
148 qName("ans1::ans2::ai2"),
149 qName("ans1::ans2::ans3"),
150 qName("ans1::ans2::ans3::ai3")));
151 EXPECT_THAT(getSymbols(TU, "ans2::"),
152 UnorderedElementsAre(qName("ans1::ans2::ai2"),
153 qName("ans1::ans2::ans3"),
154 qName("ans1::ans2::ans3::ai3")));
155 EXPECT_THAT(getSymbols(TU, "::ans1"), ElementsAre(qName("ans1")));
156 EXPECT_THAT(getSymbols(TU, "::ans1::"),
157 UnorderedElementsAre(qName("ans1::ai1"), qName("ans1::ans2")));
158 EXPECT_THAT(getSymbols(TU, "::ans1::ans2"), ElementsAre(qName("ans1::ans2")));
159 EXPECT_THAT(getSymbols(TU, "::ans1::ans2::"),
160 ElementsAre(qName("ans1::ans2::ai2"), qName("ans1::ans2::ans3")));
162 // Ensure sub-sequence matching works.
163 EXPECT_THAT(getSymbols(TU, "ans1::ans3::ai"),
164 UnorderedElementsAre(qName("ans1::ans2::ans3::ai3")));
167 TEST(WorkspaceSymbols, AnonymousNamespace) {
168 TestTU TU;
169 TU.Code = R"cpp(
170 namespace {
171 void test() {}
173 )cpp";
174 EXPECT_THAT(getSymbols(TU, "test"), ElementsAre(qName("test")));
177 TEST(WorkspaceSymbols, MultiFile) {
178 TestTU TU;
179 TU.AdditionalFiles["foo.h"] = R"cpp(
180 int foo() {
182 )cpp";
183 TU.AdditionalFiles["foo2.h"] = R"cpp(
184 int foo2() {
186 )cpp";
187 TU.Code = R"cpp(
188 #include "foo.h"
189 #include "foo2.h"
190 )cpp";
191 EXPECT_THAT(getSymbols(TU, "foo"),
192 UnorderedElementsAre(qName("foo"), qName("foo2")));
195 TEST(WorkspaceSymbols, GlobalNamespaceQueries) {
196 TestTU TU;
197 TU.AdditionalFiles["foo.h"] = R"cpp(
198 int foo() {
200 class Foo {
201 int a;
203 namespace ns {
204 int foo2() {
207 )cpp";
208 TU.Code = R"cpp(
209 #include "foo.h"
210 )cpp";
211 EXPECT_THAT(getSymbols(TU, "::"),
212 UnorderedElementsAre(
213 AllOf(qName("Foo"), withKind(SymbolKind::Class)),
214 AllOf(qName("foo"), withKind(SymbolKind::Function)),
215 AllOf(qName("ns"), withKind(SymbolKind::Namespace))));
216 EXPECT_THAT(getSymbols(TU, ":"), IsEmpty());
217 EXPECT_THAT(getSymbols(TU, ""),
218 UnorderedElementsAre(qName("foo"), qName("Foo"), qName("Foo::a"),
219 qName("ns"), qName("ns::foo2")));
222 TEST(WorkspaceSymbols, Enums) {
223 TestTU TU;
224 TU.AdditionalFiles["foo.h"] = R"cpp(
225 enum {
228 enum Color {
229 Green
231 enum class Color2 {
232 Yellow
234 namespace ns {
235 enum {
236 Black
238 enum Color3 {
239 Blue
241 enum class Color4 {
242 White
245 )cpp";
246 TU.Code = R"cpp(
247 #include "foo.h"
248 )cpp";
249 EXPECT_THAT(getSymbols(TU, "Red"), ElementsAre(qName("Red")));
250 EXPECT_THAT(getSymbols(TU, "::Red"), ElementsAre(qName("Red")));
251 EXPECT_THAT(getSymbols(TU, "Green"), ElementsAre(qName("Green")));
252 EXPECT_THAT(getSymbols(TU, "Green"), ElementsAre(qName("Green")));
253 EXPECT_THAT(getSymbols(TU, "Color2::Yellow"),
254 ElementsAre(qName("Color2::Yellow")));
255 EXPECT_THAT(getSymbols(TU, "Yellow"), ElementsAre(qName("Color2::Yellow")));
257 EXPECT_THAT(getSymbols(TU, "ns::Black"), ElementsAre(qName("ns::Black")));
258 EXPECT_THAT(getSymbols(TU, "ns::Blue"), ElementsAre(qName("ns::Blue")));
259 EXPECT_THAT(getSymbols(TU, "ns::Color4::White"),
260 ElementsAre(qName("ns::Color4::White")));
263 TEST(WorkspaceSymbols, Ranking) {
264 TestTU TU;
265 TU.AdditionalFiles["foo.h"] = R"cpp(
266 namespace ns{}
267 void func();
268 )cpp";
269 TU.Code = R"cpp(
270 #include "foo.h"
271 )cpp";
272 EXPECT_THAT(getSymbols(TU, "::"), ElementsAre(qName("func"), qName("ns")));
275 TEST(WorkspaceSymbols, RankingPartialNamespace) {
276 TestTU TU;
277 TU.Code = R"cpp(
278 namespace ns1 {
279 namespace ns2 { struct Foo {}; }
281 namespace ns2 { struct FooB {}; })cpp";
282 EXPECT_THAT(getSymbols(TU, "ns2::f"),
283 ElementsAre(qName("ns2::FooB"), qName("ns1::ns2::Foo")));
286 TEST(WorkspaceSymbols, WithLimit) {
287 TestTU TU;
288 TU.AdditionalFiles["foo.h"] = R"cpp(
289 int foo;
290 int foo2;
291 )cpp";
292 TU.Code = R"cpp(
293 #include "foo.h"
294 )cpp";
295 // Foo is higher ranked because of exact name match.
296 EXPECT_THAT(getSymbols(TU, "foo"),
297 UnorderedElementsAre(
298 AllOf(qName("foo"), withKind(SymbolKind::Variable)),
299 AllOf(qName("foo2"), withKind(SymbolKind::Variable))));
301 EXPECT_THAT(getSymbols(TU, "foo", 1), ElementsAre(qName("foo")));
304 TEST(WorkspaceSymbols, TempSpecs) {
305 TestTU TU;
306 TU.ExtraArgs = {"-xc++"};
307 TU.Code = R"cpp(
308 template <typename T, typename U, int X = 5> class Foo {};
309 template <typename T> class Foo<int, T> {};
310 template <> class Foo<bool, int> {};
311 template <> class Foo<bool, int, 3> {};
312 )cpp";
313 // Foo is higher ranked because of exact name match.
314 EXPECT_THAT(
315 getSymbols(TU, "Foo"),
316 UnorderedElementsAre(
317 AllOf(qName("Foo"), withKind(SymbolKind::Class)),
318 AllOf(qName("Foo<int, T>"), withKind(SymbolKind::Class)),
319 AllOf(qName("Foo<bool, int>"), withKind(SymbolKind::Class)),
320 AllOf(qName("Foo<bool, int, 3>"), withKind(SymbolKind::Class))));
323 std::vector<DocumentSymbol> getSymbols(ParsedAST AST) {
324 auto SymbolInfos = getDocumentSymbols(AST);
325 EXPECT_TRUE(bool(SymbolInfos)) << "documentSymbols returned an error";
326 return *SymbolInfos;
329 TEST(DocumentSymbols, BasicSymbols) {
330 TestTU TU;
331 Annotations Main(R"(
332 class Foo;
333 class Foo {
334 Foo() {}
335 Foo(int a) {}
336 void $decl[[f]]();
337 friend void f1();
338 friend class Friend;
339 Foo& operator=(const Foo&);
340 ~Foo();
341 class Nested {
342 void f();
345 class Friend {
348 void f1();
349 inline void f2() {}
350 static const int KInt = 2;
351 const char* kStr = "123";
353 void f1() {}
355 namespace foo {
356 // Type alias
357 typedef int int32;
358 using int32_t = int32;
360 // Variable
361 int v1;
363 // Namespace
364 namespace bar {
365 int v2;
367 // Namespace alias
368 namespace baz = bar;
370 using bar::v2;
371 } // namespace foo
372 )");
374 TU.Code = Main.code().str();
375 EXPECT_THAT(
376 getSymbols(TU.build()),
377 ElementsAreArray(
378 {AllOf(withName("Foo"), withKind(SymbolKind::Class),
379 withDetail("class"), children()),
380 AllOf(withName("Foo"), withKind(SymbolKind::Class),
381 withDetail("class"),
382 children(
383 AllOf(withName("Foo"), withKind(SymbolKind::Constructor),
384 withDetail("()"), children()),
385 AllOf(withName("Foo"), withKind(SymbolKind::Constructor),
386 withDetail("(int)"), children()),
387 AllOf(withName("f"), withKind(SymbolKind::Method),
388 withDetail("void ()"), children()),
389 AllOf(withName("operator="), withKind(SymbolKind::Method),
390 withDetail("Foo &(const Foo &)"), children()),
391 AllOf(withName("~Foo"), withKind(SymbolKind::Constructor),
392 withDetail(""), children()),
393 AllOf(withName("Nested"), withKind(SymbolKind::Class),
394 withDetail("class"),
395 children(AllOf(
396 withName("f"), withKind(SymbolKind::Method),
397 withDetail("void ()"), children()))))),
398 AllOf(withName("Friend"), withKind(SymbolKind::Class),
399 withDetail("class"), children()),
400 AllOf(withName("f1"), withKind(SymbolKind::Function),
401 withDetail("void ()"), children()),
402 AllOf(withName("f2"), withKind(SymbolKind::Function),
403 withDetail("void ()"), children()),
404 AllOf(withName("KInt"), withKind(SymbolKind::Variable),
405 withDetail("const int"), children()),
406 AllOf(withName("kStr"), withKind(SymbolKind::Variable),
407 withDetail("const char *"), children()),
408 AllOf(withName("f1"), withKind(SymbolKind::Function),
409 withDetail("void ()"), children()),
410 AllOf(
411 withName("foo"), withKind(SymbolKind::Namespace), withDetail(""),
412 children(AllOf(withName("int32"), withKind(SymbolKind::Class),
413 withDetail("type alias"), children()),
414 AllOf(withName("int32_t"), withKind(SymbolKind::Class),
415 withDetail("type alias"), children()),
416 AllOf(withName("v1"), withKind(SymbolKind::Variable),
417 withDetail("int"), children()),
418 AllOf(withName("bar"), withKind(SymbolKind::Namespace),
419 withDetail(""),
420 children(AllOf(withName("v2"),
421 withKind(SymbolKind::Variable),
422 withDetail("int"), children()))),
423 AllOf(withName("baz"), withKind(SymbolKind::Namespace),
424 withDetail(""), children()),
425 AllOf(withName("v2"), withKind(SymbolKind::Namespace),
426 withDetail(""))))}));
429 TEST(DocumentSymbols, DeclarationDefinition) {
430 TestTU TU;
431 Annotations Main(R"(
432 class Foo {
433 void $decl[[f]]();
435 void Foo::$def[[f]]() {
437 )");
439 TU.Code = Main.code().str();
440 EXPECT_THAT(
441 getSymbols(TU.build()),
442 ElementsAre(
443 AllOf(withName("Foo"), withKind(SymbolKind::Class),
444 withDetail("class"),
445 children(AllOf(withName("f"), withKind(SymbolKind::Method),
446 withDetail("void ()"),
447 symNameRange(Main.range("decl"))))),
448 AllOf(withName("Foo::f"), withKind(SymbolKind::Method),
449 withDetail("void ()"), symNameRange(Main.range("def")))));
452 TEST(DocumentSymbols, Concepts) {
453 TestTU TU;
454 TU.ExtraArgs = {"-std=c++20"};
455 TU.Code = "template <typename T> concept C = requires(T t) { t.foo(); };";
457 EXPECT_THAT(getSymbols(TU.build()),
458 ElementsAre(AllOf(withName("C"), withDetail("concept"))));
461 TEST(DocumentSymbols, ExternSymbol) {
462 TestTU TU;
463 TU.AdditionalFiles["foo.h"] = R"cpp(
464 extern int var;
465 )cpp";
466 TU.Code = R"cpp(
467 #include "foo.h"
468 )cpp";
470 EXPECT_THAT(getSymbols(TU.build()), IsEmpty());
473 TEST(DocumentSymbols, ExternContext) {
474 TestTU TU;
475 TU.Code = R"cpp(
476 extern "C" {
477 void foo();
478 class Foo {};
480 namespace ns {
481 extern "C" {
482 void bar();
483 class Bar {};
485 })cpp";
487 EXPECT_THAT(getSymbols(TU.build()),
488 ElementsAre(withName("foo"), withName("Foo"),
489 AllOf(withName("ns"),
490 children(withName("bar"), withName("Bar")))));
493 TEST(DocumentSymbols, ExportContext) {
494 TestTU TU;
495 TU.ExtraArgs = {"-std=c++20"};
496 TU.Code = R"cpp(
497 export module test;
498 export {
499 void foo();
500 class Foo {};
501 })cpp";
503 EXPECT_THAT(getSymbols(TU.build()),
504 ElementsAre(withName("foo"), withName("Foo")));
507 TEST(DocumentSymbols, NoLocals) {
508 TestTU TU;
509 TU.Code = R"cpp(
510 void test(int FirstParam, int SecondParam) {
511 struct LocalClass {};
512 int local_var;
513 })cpp";
514 EXPECT_THAT(getSymbols(TU.build()), ElementsAre(withName("test")));
517 TEST(DocumentSymbols, Unnamed) {
518 TestTU TU;
519 TU.Code = R"cpp(
520 struct {
521 int InUnnamed;
522 } UnnamedStruct;
523 )cpp";
524 EXPECT_THAT(
525 getSymbols(TU.build()),
526 ElementsAre(AllOf(withName("(anonymous struct)"),
527 withKind(SymbolKind::Struct), withDetail("struct"),
528 children(AllOf(withName("InUnnamed"),
529 withKind(SymbolKind::Field),
530 withDetail("int"), children()))),
531 AllOf(withName("UnnamedStruct"),
532 withKind(SymbolKind::Variable),
533 withDetail("struct (unnamed)"), children())));
536 TEST(DocumentSymbols, InHeaderFile) {
537 TestTU TU;
538 TU.AdditionalFiles["bar.h"] = R"cpp(
539 int foo() {
541 )cpp";
542 TU.Code = R"cpp(
543 int i; // declaration to finish preamble
544 #include "bar.h"
545 int test() {
547 )cpp";
548 EXPECT_THAT(getSymbols(TU.build()),
549 ElementsAre(withName("i"), withName("test")));
552 TEST(DocumentSymbols, Template) {
553 TestTU TU;
554 TU.Code = R"(
555 template <class T> struct Tmpl {T x = 0;};
556 template <> struct Tmpl<int> {
557 int y = 0;
559 extern template struct Tmpl<float>;
560 template struct Tmpl<double>;
562 template <class T, class U, class Z = float>
563 int funcTmpl(U a);
564 template <>
565 int funcTmpl<int>(double a);
567 template <class T, class U = double>
568 int varTmpl = T();
569 template <>
570 double varTmpl<int> = 10.0;
572 EXPECT_THAT(
573 getSymbols(TU.build()),
574 ElementsAre(
575 AllOf(withName("Tmpl"), withKind(SymbolKind::Struct),
576 withDetail("template struct"),
577 children(AllOf(withName("x"), withKind(SymbolKind::Field),
578 withDetail("T")))),
579 AllOf(withName("Tmpl<int>"), withKind(SymbolKind::Struct),
580 withDetail("struct"),
581 children(AllOf(withName("y"), withDetail("int")))),
582 AllOf(withName("Tmpl<float>"), withKind(SymbolKind::Struct),
583 withDetail("struct"), children()),
584 AllOf(withName("Tmpl<double>"), withKind(SymbolKind::Struct),
585 withDetail("struct"), children()),
586 AllOf(withName("funcTmpl"), withDetail("template int (U)"),
587 children()),
588 AllOf(withName("funcTmpl<int>"), withDetail("int (double)"),
589 children()),
590 AllOf(withName("varTmpl"), withDetail("template int"), children()),
591 AllOf(withName("varTmpl<int>"), withDetail("double"), children())));
594 TEST(DocumentSymbols, Namespaces) {
595 TestTU TU;
596 TU.Code = R"cpp(
597 namespace ans1 {
598 int ai1;
599 namespace ans2 {
600 int ai2;
603 namespace {
604 void test() {}
607 namespace na {
608 inline namespace nb {
609 class Foo {};
612 namespace na {
613 // This is still inlined.
614 namespace nb {
615 class Bar {};
618 )cpp";
619 EXPECT_THAT(
620 getSymbols(TU.build()),
621 ElementsAreArray<::testing::Matcher<DocumentSymbol>>(
622 {AllOf(withName("ans1"),
623 children(AllOf(withName("ai1"), children()),
624 AllOf(withName("ans2"), children(withName("ai2"))))),
625 AllOf(withName("(anonymous namespace)"), children(withName("test"))),
626 AllOf(withName("na"),
627 children(AllOf(withName("nb"), children(withName("Foo"))))),
628 AllOf(withName("na"),
629 children(AllOf(withName("nb"), children(withName("Bar")))))}));
632 TEST(DocumentSymbols, Enums) {
633 TestTU TU;
634 TU.Code = R"(
635 enum {
638 enum Color {
639 Green
641 enum class Color2 {
642 Yellow
644 namespace ns {
645 enum {
646 Black
650 EXPECT_THAT(
651 getSymbols(TU.build()),
652 ElementsAre(
653 AllOf(withName("(anonymous enum)"), withDetail("enum"),
654 children(AllOf(withName("Red"), withDetail("(unnamed)")))),
655 AllOf(withName("Color"), withDetail("enum"),
656 children(AllOf(withName("Green"), withDetail("Color")))),
657 AllOf(withName("Color2"), withDetail("enum"),
658 children(AllOf(withName("Yellow"), withDetail("Color2")))),
659 AllOf(withName("ns"),
660 children(AllOf(withName("(anonymous enum)"), withDetail("enum"),
661 children(AllOf(withName("Black"),
662 withDetail("(unnamed)"))))))));
665 TEST(DocumentSymbols, Macro) {
666 struct Test {
667 const char *Code;
668 testing::Matcher<DocumentSymbol> Matcher;
669 } Tests[] = {
671 R"cpp(
672 // Basic macro that generates symbols.
673 #define DEFINE_FLAG(X) bool FLAGS_##X; bool FLAGS_no##X
674 DEFINE_FLAG(pretty);
675 )cpp",
676 AllOf(withName("DEFINE_FLAG"), withDetail("(pretty)"),
677 children(withName("FLAGS_pretty"), withName("FLAGS_nopretty"))),
680 R"cpp(
681 // Hierarchy is determined by primary (name) location.
682 #define ID(X) X
683 namespace ID(ns) { int ID(y); }
684 )cpp",
685 AllOf(withName("ID"), withDetail("(ns)"),
686 children(AllOf(withName("ns"),
687 children(AllOf(withName("ID"), withDetail("(y)"),
688 children(withName("y"))))))),
691 R"cpp(
692 // More typical example where macro only generates part of a decl.
693 #define TEST(A, B) class A##_##B { void go(); }; void A##_##B::go()
694 TEST(DocumentSymbols, Macro) { }
695 )cpp",
696 AllOf(withName("TEST"), withDetail("(DocumentSymbols, Macro)"),
697 children(AllOf(withName("DocumentSymbols_Macro"),
698 children(withName("go"))),
699 withName("DocumentSymbols_Macro::go"))),
702 R"cpp(
703 // Nested macros.
704 #define NAMESPACE(NS, BODY) namespace NS { BODY }
705 NAMESPACE(a, NAMESPACE(b, int x;))
706 )cpp",
707 AllOf(
708 withName("NAMESPACE"), withDetail("(a, NAMESPACE(b, int x;))"),
709 children(AllOf(
710 withName("a"),
711 children(AllOf(withName("NAMESPACE"),
712 // FIXME: nested expansions not in TokenBuffer
713 withDetail(""),
714 children(AllOf(withName("b"),
715 children(withName("x"))))))))),
718 R"cpp(
719 // Macro invoked from body is not exposed.
720 #define INNER(X) int X
721 #define OUTER(X) INNER(X)
722 OUTER(foo);
723 )cpp",
724 AllOf(withName("OUTER"), withDetail("(foo)"),
725 children(withName("foo"))),
728 for (const Test &T : Tests) {
729 auto TU = TestTU::withCode(T.Code);
730 EXPECT_THAT(getSymbols(TU.build()), ElementsAre(T.Matcher)) << T.Code;
734 TEST(DocumentSymbols, RangeFromMacro) {
735 TestTU TU;
736 Annotations Main(R"(
737 #define FF(name) \
738 class name##_Test {};
740 $expansion1[[FF]](abc);
742 #define FF2() \
743 class Test {}
745 $expansion2parens[[$expansion2[[FF2]]()]];
747 #define FF3() \
748 void waldo()
750 $fullDef[[FF3() {
751 int var = 42;
753 )");
754 TU.Code = Main.code().str();
755 EXPECT_THAT(
756 getSymbols(TU.build()),
757 ElementsAre(
758 AllOf(withName("FF"), withDetail("(abc)"),
759 children(AllOf(withName("abc_Test"), withDetail("class"),
760 symNameRange(Main.range("expansion1"))))),
761 AllOf(withName("FF2"), withDetail("()"),
762 symNameRange(Main.range("expansion2")),
763 symRange(Main.range("expansion2parens")),
764 children(AllOf(withName("Test"), withDetail("class"),
765 symNameRange(Main.range("expansion2"))))),
766 AllOf(withName("FF3"), withDetail("()"),
767 symRange(Main.range("fullDef")),
768 children(AllOf(withName("waldo"), withDetail("void ()"),
769 symRange(Main.range("fullDef")))))));
772 TEST(DocumentSymbols, FuncTemplates) {
773 TestTU TU;
774 Annotations Source(R"cpp(
775 template <class T>
776 T foo() {}
778 auto x = foo<int>();
779 auto y = foo<double>();
780 )cpp");
781 TU.Code = Source.code().str();
782 // Make sure we only see the template declaration, not instantiations.
783 EXPECT_THAT(getSymbols(TU.build()),
784 ElementsAre(AllOf(withName("foo"), withDetail("template T ()")),
785 AllOf(withName("x"), withDetail("int")),
786 AllOf(withName("y"), withDetail("double"))));
789 TEST(DocumentSymbols, UsingDirectives) {
790 TestTU TU;
791 Annotations Source(R"cpp(
792 namespace ns {
793 int foo;
796 namespace ns_alias = ns;
798 using namespace ::ns; // check we don't loose qualifiers.
799 using namespace ns_alias; // and namespace aliases.
800 )cpp");
801 TU.Code = Source.code().str();
802 EXPECT_THAT(getSymbols(TU.build()),
803 ElementsAre(withName("ns"), withName("ns_alias"),
804 withName("using namespace ::ns"),
805 withName("using namespace ns_alias")));
808 TEST(DocumentSymbols, TempSpecs) {
809 TestTU TU;
810 TU.Code = R"cpp(
811 template <typename T, typename U, int X = 5> class Foo {};
812 template <typename T> class Foo<int, T> {};
813 template <> class Foo<bool, int> {};
814 template <> class Foo<bool, int, 3> {};
815 )cpp";
816 // Foo is higher ranked because of exact name match.
817 EXPECT_THAT(getSymbols(TU.build()),
818 UnorderedElementsAre(
819 AllOf(withName("Foo"), withKind(SymbolKind::Class),
820 withDetail("template class")),
821 AllOf(withName("Foo<int, T>"), withKind(SymbolKind::Class),
822 withDetail("template class")),
823 AllOf(withName("Foo<bool, int>"), withKind(SymbolKind::Class),
824 withDetail("class")),
825 AllOf(withName("Foo<bool, int, 3>"),
826 withKind(SymbolKind::Class), withDetail("class"))));
829 TEST(DocumentSymbols, Qualifiers) {
830 TestTU TU;
831 TU.Code = R"cpp(
832 namespace foo { namespace bar {
833 struct Cls;
835 int func1();
836 int func2();
837 int func3();
838 int func4();
841 struct foo::bar::Cls { };
843 int foo::bar::func1() { return 10; }
844 int ::foo::bar::func2() { return 20; }
846 using namespace foo;
847 int bar::func3() { return 30; }
849 namespace alias = foo::bar;
850 int ::alias::func4() { return 40; }
851 )cpp";
853 // All the qualifiers should be preserved exactly as written.
854 EXPECT_THAT(getSymbols(TU.build()),
855 UnorderedElementsAre(
856 withName("foo"), withName("foo::bar::Cls"),
857 withName("foo::bar::func1"), withName("::foo::bar::func2"),
858 withName("using namespace foo"), withName("bar::func3"),
859 withName("alias"), withName("::alias::func4")));
862 TEST(DocumentSymbols, QualifiersWithTemplateArgs) {
863 TestTU TU;
864 TU.Code = R"cpp(
865 template <typename T, typename U = double> class Foo;
867 template <>
868 class Foo<int, double> {
869 int method1();
870 int method2();
871 int method3();
874 using int_type = int;
876 // Typedefs should be preserved!
877 int Foo<int_type, double>::method1() { return 10; }
879 // Default arguments should not be shown!
880 int Foo<int>::method2() { return 20; }
882 using Foo_type = Foo<int>;
883 // If the whole type is aliased, this should be preserved too!
884 int Foo_type::method3() { return 30; }
885 )cpp";
886 EXPECT_THAT(getSymbols(TU.build()),
887 UnorderedElementsAre(
888 AllOf(withName("Foo"), withDetail("template class")),
889 AllOf(withName("Foo<int, double>"), withDetail("class")),
890 AllOf(withName("int_type"), withDetail("type alias")),
891 AllOf(withName("Foo<int_type, double>::method1"),
892 withDetail("int ()")),
893 AllOf(withName("Foo<int>::method2"), withDetail("int ()")),
894 AllOf(withName("Foo_type"), withDetail("type alias")),
895 AllOf(withName("Foo_type::method3"), withDetail("int ()"))));
898 TEST(DocumentSymbolsTest, Ranges) {
899 TestTU TU;
900 Annotations Main(R"(
901 $foo[[int foo(bool Argument) {
902 return 42;
905 $variable[[char GLOBAL_VARIABLE]];
907 $ns[[namespace ns {
908 $bar[[class Bar {
909 public:
910 $ctor[[Bar() {}]]
911 $dtor[[~Bar()]];
913 private:
914 $field[[unsigned Baz]];
916 $getbaz[[unsigned getBaz() { return Baz; }]]
917 }]];
918 }]] // namespace ns
920 $forwardclass[[class ForwardClassDecl]];
922 $struct[[struct StructDefinition {
923 $structfield[[int *Pointer = nullptr]];
924 }]];
925 $forwardstruct[[struct StructDeclaration]];
927 $forwardfunc[[void forwardFunctionDecl(int Something)]];
928 )");
929 TU.Code = Main.code().str();
930 EXPECT_THAT(
931 getSymbols(TU.build()),
932 UnorderedElementsAre(
933 AllOf(withName("foo"), withKind(SymbolKind::Function),
934 withDetail("int (bool)"), symRange(Main.range("foo"))),
935 AllOf(withName("GLOBAL_VARIABLE"), withKind(SymbolKind::Variable),
936 withDetail("char"), symRange(Main.range("variable"))),
937 AllOf(
938 withName("ns"), withKind(SymbolKind::Namespace),
939 symRange(Main.range("ns")),
940 children(AllOf(
941 withName("Bar"), withKind(SymbolKind::Class),
942 withDetail("class"), symRange(Main.range("bar")),
943 children(
944 AllOf(withName("Bar"), withKind(SymbolKind::Constructor),
945 withDetail("()"), symRange(Main.range("ctor"))),
946 AllOf(withName("~Bar"), withKind(SymbolKind::Constructor),
947 withDetail(""), symRange(Main.range("dtor"))),
948 AllOf(withName("Baz"), withKind(SymbolKind::Field),
949 withDetail("unsigned int"),
950 symRange(Main.range("field"))),
951 AllOf(withName("getBaz"), withKind(SymbolKind::Method),
952 withDetail("unsigned int ()"),
953 symRange(Main.range("getbaz"))))))),
954 AllOf(withName("ForwardClassDecl"), withKind(SymbolKind::Class),
955 withDetail("class"), symRange(Main.range("forwardclass"))),
956 AllOf(withName("StructDefinition"), withKind(SymbolKind::Struct),
957 withDetail("struct"), symRange(Main.range("struct")),
958 children(AllOf(withName("Pointer"), withKind(SymbolKind::Field),
959 withDetail("int *"),
960 symRange(Main.range("structfield"))))),
961 AllOf(withName("StructDeclaration"), withKind(SymbolKind::Struct),
962 withDetail("struct"), symRange(Main.range("forwardstruct"))),
963 AllOf(withName("forwardFunctionDecl"), withKind(SymbolKind::Function),
964 withDetail("void (int)"),
965 symRange(Main.range("forwardfunc")))));
968 TEST(DocumentSymbolsTest, DependentType) {
969 TestTU TU;
970 TU.Code = R"(
971 template <typename T> auto plus(T x, T y) -> decltype(x + y) { return x + y; }
973 template <typename Key, typename Value> class Pair {};
975 template <typename Key, typename Value>
976 struct Context : public Pair<Key, Value> {
977 using Pair<Key, Value>::Pair;
980 EXPECT_THAT(
981 getSymbols(TU.build()),
982 ElementsAre(
983 AllOf(withName("plus"),
984 withDetail("template auto (T, T) -> decltype(x + y)")),
985 AllOf(withName("Pair"), withDetail("template class")),
986 AllOf(withName("Context"), withDetail("template struct"),
987 children(AllOf(
988 withName("Pair<type-parameter-0-0, type-parameter-0-1>"),
989 withDetail("<dependent type>"))))));
992 TEST(DocumentSymbolsTest, ObjCCategoriesAndClassExtensions) {
993 TestTU TU;
994 TU.ExtraArgs = {"-xobjective-c++", "-Wno-objc-root-class"};
995 Annotations Main(R"cpp(
996 $Cat[[@interface Cat
997 + (id)sharedCat;
998 @end]]
999 $SneakyCat[[@interface Cat (Sneaky)
1000 - (id)sneak:(id)behavior;
1001 @end]]
1003 $MeowCat[[@interface Cat ()
1004 - (void)meow;
1005 @end]]
1006 $PurCat[[@interface Cat ()
1007 - (void)pur;
1008 @end]]
1009 )cpp");
1010 TU.Code = Main.code().str();
1011 EXPECT_THAT(
1012 getSymbols(TU.build()),
1013 ElementsAre(
1014 AllOf(withName("Cat"), symRange(Main.range("Cat")),
1015 children(AllOf(withName("+sharedCat"),
1016 withKind(SymbolKind::Method)))),
1017 AllOf(withName("Cat(Sneaky)"), symRange(Main.range("SneakyCat")),
1018 children(
1019 AllOf(withName("-sneak:"), withKind(SymbolKind::Method)))),
1020 AllOf(
1021 withName("Cat()"), symRange(Main.range("MeowCat")),
1022 children(AllOf(withName("-meow"), withKind(SymbolKind::Method)))),
1023 AllOf(withName("Cat()"), symRange(Main.range("PurCat")),
1024 children(
1025 AllOf(withName("-pur"), withKind(SymbolKind::Method))))));
1028 TEST(DocumentSymbolsTest, PragmaMarkGroups) {
1029 TestTU TU;
1030 TU.ExtraArgs = {"-xobjective-c++", "-Wno-objc-root-class"};
1031 Annotations Main(R"cpp(
1032 $DogDef[[@interface Dog
1033 @end]]
1035 $DogImpl[[@implementation Dog
1037 + (id)sharedDoggo { return 0; }
1039 #pragma $Overrides[[mark - Overrides
1041 - (id)init {
1042 return self;
1044 - (void)bark {}]]
1046 #pragma $Specifics[[mark - Dog Specifics
1048 - (int)isAGoodBoy {
1049 return 1;
1051 @]]end // FIXME: Why doesn't this include the 'end'?
1053 #pragma $End[[mark - End
1055 )cpp");
1056 TU.Code = Main.code().str();
1057 EXPECT_THAT(
1058 getSymbols(TU.build()),
1059 UnorderedElementsAre(
1060 AllOf(withName("Dog"), symRange(Main.range("DogDef"))),
1061 AllOf(withName("Dog"), symRange(Main.range("DogImpl")),
1062 children(AllOf(withName("+sharedDoggo"),
1063 withKind(SymbolKind::Method)),
1064 AllOf(withName("Overrides"),
1065 symRange(Main.range("Overrides")),
1066 children(AllOf(withName("-init"),
1067 withKind(SymbolKind::Method)),
1068 AllOf(withName("-bark"),
1069 withKind(SymbolKind::Method)))),
1070 AllOf(withName("Dog Specifics"),
1071 symRange(Main.range("Specifics")),
1072 children(AllOf(withName("-isAGoodBoy"),
1073 withKind(SymbolKind::Method)))))),
1074 AllOf(withName("End"), symRange(Main.range("End")))));
1077 TEST(DocumentSymbolsTest, PragmaMarkGroupsNesting) {
1078 TestTU TU;
1079 TU.ExtraArgs = {"-xobjective-c++", "-Wno-objc-root-class"};
1080 Annotations Main(R"cpp(
1081 #pragma mark - Foo
1082 struct Foo {
1083 #pragma mark - Bar
1084 void bar() {
1085 #pragma mark - NotTopDecl
1088 void bar() {}
1089 )cpp");
1090 TU.Code = Main.code().str();
1091 EXPECT_THAT(
1092 getSymbols(TU.build()),
1093 UnorderedElementsAre(AllOf(
1094 withName("Foo"),
1095 children(AllOf(withName("Foo"),
1096 children(AllOf(withName("Bar"),
1097 children(AllOf(withName("bar"),
1098 children(withName(
1099 "NotTopDecl"))))))),
1100 withName("bar")))));
1103 TEST(DocumentSymbolsTest, PragmaMarkGroupsNoNesting) {
1104 TestTU TU;
1105 TU.ExtraArgs = {"-xobjective-c++", "-Wno-objc-root-class"};
1106 Annotations Main(R"cpp(
1107 #pragma mark Helpers
1108 void helpA(id obj) {}
1110 #pragma mark -
1111 #pragma mark Core
1113 void coreMethod() {}
1114 )cpp");
1115 TU.Code = Main.code().str();
1116 EXPECT_THAT(getSymbols(TU.build()),
1117 UnorderedElementsAre(withName("Helpers"), withName("helpA"),
1118 withName("(unnamed group)"),
1119 withName("Core"), withName("coreMethod")));
1122 } // namespace
1123 } // namespace clangd
1124 } // namespace clang