1 //===-- FindTargetTests.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 "FindTarget.h"
9
10 #include "Selection.h"
11 #include "TestTU.h"
12 #include "clang/AST/Decl.h"
13 #include "clang/AST/DeclTemplate.h"
14 #include "clang/Basic/SourceLocation.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Testing/Support/Annotations.h"
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include <initializer_list>
22
23 namespace clang {
24 namespace clangd {
25 namespace {
26
27 // A referenced Decl together with its DeclRelationSet, for assertions.
28 //
29 // There's no great way to assert on the "content" of a Decl in the general case
30 // that's both expressive and unambiguous (e.g. clearly distinguishes between
31 // templated decls and their specializations).
32 //
33 // We use the result of pretty-printing the decl, with the {body} truncated.
34 struct PrintedDecl {
PrintedDeclclang::clangd::__anon925c2fef0111::PrintedDecl35 PrintedDecl(const char *Name, DeclRelationSet Relations = {})
36 : Name(Name), Relations(Relations) {}
PrintedDeclclang::clangd::__anon925c2fef0111::PrintedDecl37 PrintedDecl(const NamedDecl *D, DeclRelationSet Relations = {})
38 : Relations(Relations) {
39 std::string S;
40 llvm::raw_string_ostream OS(S);
41 D->print(OS);
42 llvm::StringRef FirstLine =
__anon925c2fef0202(char C) 43 llvm::StringRef(OS.str()).take_until([](char C) { return C == '\n'; });
44 FirstLine = FirstLine.rtrim(" {");
45 Name = std::string(FirstLine.rtrim(" {"));
46 }
47
48 std::string Name;
49 DeclRelationSet Relations;
50 };
operator ==(const PrintedDecl & L,const PrintedDecl & R)51 bool operator==(const PrintedDecl &L, const PrintedDecl &R) {
52 return std::tie(L.Name, L.Relations) == std::tie(R.Name, R.Relations);
53 }
operator <<(llvm::raw_ostream & OS,const PrintedDecl & D)54 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const PrintedDecl &D) {
55 return OS << D.Name << " Rel=" << D.Relations;
56 }
57
58 // The test cases in for targetDecl() take the form
59 // - a piece of code (Code = "...")
60 // - Code should have a single AST node marked as a [[range]]
61 // - an EXPECT_DECLS() assertion that verify the type of node selected, and
62 // all the decls that targetDecl() considers it to reference
63 // Despite the name, these cases actually test allTargetDecls() for brevity.
64 class TargetDeclTest : public ::testing::Test {
65 protected:
66 using Rel = DeclRelation;
67 std::string Code;
68 std::vector<std::string> Flags;
69
70 // Asserts that `Code` has a marked selection of a node `NodeType`,
71 // and returns allTargetDecls() as PrintedDecl structs.
72 // Use via EXPECT_DECLS().
assertNodeAndPrintDecls(const char * NodeType)73 std::vector<PrintedDecl> assertNodeAndPrintDecls(const char *NodeType) {
74 llvm::Annotations A(Code);
75 auto TU = TestTU::withCode(A.code());
76 TU.ExtraArgs = Flags;
77 auto AST = TU.build();
78 llvm::Annotations::Range R = A.range();
79 auto Selection = SelectionTree::createRight(
80 AST.getASTContext(), AST.getTokens(), R.Begin, R.End);
81 const SelectionTree::Node *N = Selection.commonAncestor();
82 if (!N) {
83 ADD_FAILURE() << "No node selected!\n" << Code;
84 return {};
85 }
86 EXPECT_EQ(N->kind(), NodeType) << Selection;
87
88 std::vector<PrintedDecl> ActualDecls;
89 for (const auto &Entry : allTargetDecls(N->ASTNode))
90 ActualDecls.emplace_back(Entry.first, Entry.second);
91 return ActualDecls;
92 }
93 };
94
95 // This is a macro to preserve line numbers in assertion failures.
96 // It takes the expected decls as varargs to work around comma-in-macro issues.
97 #define EXPECT_DECLS(NodeType, ...) \
98 EXPECT_THAT(assertNodeAndPrintDecls(NodeType), \
99 ::testing::UnorderedElementsAreArray( \
100 std::vector<PrintedDecl>({__VA_ARGS__}))) \
101 << Code
102 using ExpectedDecls = std::vector<PrintedDecl>;
103
TEST_F(TargetDeclTest,Exprs)104 TEST_F(TargetDeclTest, Exprs) {
105 Code = R"cpp(
106 int f();
107 int x = [[f]]();
108 )cpp";
109 EXPECT_DECLS("DeclRefExpr", "int f()");
110
111 Code = R"cpp(
112 struct S { S operator+(S) const; };
113 auto X = S() [[+]] S();
114 )cpp";
115 EXPECT_DECLS("DeclRefExpr", "S operator+(S) const");
116
117 Code = R"cpp(
118 int foo();
119 int s = foo[[()]];
120 )cpp";
121 EXPECT_DECLS("CallExpr", "int foo()");
122
123 Code = R"cpp(
124 struct X {
125 void operator()(int n);
126 };
127 void test() {
128 X x;
129 x[[(123)]];
130 }
131 )cpp";
132 EXPECT_DECLS("CXXOperatorCallExpr", "void operator()(int n)");
133
134 Code = R"cpp(
135 void test() {
136 goto [[label]];
137 label:
138 return;
139 }
140 )cpp";
141 EXPECT_DECLS("GotoStmt", "label:");
142 Code = R"cpp(
143 void test() {
144 [[label]]:
145 return;
146 }
147 )cpp";
148 EXPECT_DECLS("LabelStmt", "label:");
149 }
150
TEST_F(TargetDeclTest,RecoveryForC)151 TEST_F(TargetDeclTest, RecoveryForC) {
152 Flags = {"-xc", "-Xclang", "-frecovery-ast"};
153 Code = R"cpp(
154 // error-ok: testing behavior on broken code
155 // int f();
156 int f(int);
157 int x = [[f]]();
158 )cpp";
159 EXPECT_DECLS("DeclRefExpr", "int f(int)");
160 }
161
TEST_F(TargetDeclTest,Recovery)162 TEST_F(TargetDeclTest, Recovery) {
163 Code = R"cpp(
164 // error-ok: testing behavior on broken code
165 int f();
166 int f(int, int);
167 int x = [[f]](42);
168 )cpp";
169 EXPECT_DECLS("UnresolvedLookupExpr", "int f()", "int f(int, int)");
170 }
171
TEST_F(TargetDeclTest,RecoveryType)172 TEST_F(TargetDeclTest, RecoveryType) {
173 Code = R"cpp(
174 // error-ok: testing behavior on broken code
175 struct S { int member; };
176 S overloaded(int);
177 void foo() {
178 // No overload matches, but we have recovery-expr with the correct type.
179 overloaded().[[member]];
180 }
181 )cpp";
182 EXPECT_DECLS("MemberExpr", "int member");
183 }
184
TEST_F(TargetDeclTest,UsingDecl)185 TEST_F(TargetDeclTest, UsingDecl) {
186 Code = R"cpp(
187 namespace foo {
188 int f(int);
189 int f(char);
190 }
191 using foo::f;
192 int x = [[f]](42);
193 )cpp";
194 // f(char) is not referenced!
195 EXPECT_DECLS("DeclRefExpr", {"using foo::f", Rel::Alias}, {"int f(int)"});
196
197 Code = R"cpp(
198 namespace foo {
199 int f(int);
200 int f(char);
201 }
202 [[using foo::f]];
203 )cpp";
204 // All overloads are referenced.
205 EXPECT_DECLS("UsingDecl", {"using foo::f", Rel::Alias}, {"int f(int)"},
206 {"int f(char)"});
207
208 Code = R"cpp(
209 struct X {
210 int foo();
211 };
212 struct Y : X {
213 using X::foo;
214 };
215 int x = Y().[[foo]]();
216 )cpp";
217 EXPECT_DECLS("MemberExpr", {"using X::foo", Rel::Alias}, {"int foo()"});
218
219 Code = R"cpp(
220 template <typename T>
221 struct Base {
222 void waldo() {}
223 };
224 template <typename T>
225 struct Derived : Base<T> {
226 using Base<T>::[[waldo]];
227 };
228 )cpp";
229 EXPECT_DECLS("UnresolvedUsingValueDecl", {"using Base<T>::waldo", Rel::Alias},
230 {"void waldo()"});
231 }
232
TEST_F(TargetDeclTest,ConstructorInitList)233 TEST_F(TargetDeclTest, ConstructorInitList) {
234 Code = R"cpp(
235 struct X {
236 int a;
237 X() : [[a]](42) {}
238 };
239 )cpp";
240 EXPECT_DECLS("CXXCtorInitializer", "int a");
241
242 Code = R"cpp(
243 struct X {
244 X() : [[X]](1) {}
245 X(int);
246 };
247 )cpp";
248 EXPECT_DECLS("RecordTypeLoc", "struct X");
249 }
250
TEST_F(TargetDeclTest,DesignatedInit)251 TEST_F(TargetDeclTest, DesignatedInit) {
252 Flags = {"-xc"}; // array designators are a C99 extension.
253 Code = R"c(
254 struct X { int a; };
255 struct Y { int b; struct X c[2]; };
256 struct Y y = { .c[0].[[a]] = 1 };
257 )c";
258 EXPECT_DECLS("DesignatedInitExpr", "int a");
259 }
260
TEST_F(TargetDeclTest,NestedNameSpecifier)261 TEST_F(TargetDeclTest, NestedNameSpecifier) {
262 Code = R"cpp(
263 namespace a { namespace b { int c; } }
264 int x = a::[[b::]]c;
265 )cpp";
266 EXPECT_DECLS("NestedNameSpecifierLoc", "namespace b");
267
268 Code = R"cpp(
269 namespace a { struct X { enum { y }; }; }
270 int x = a::[[X::]]y;
271 )cpp";
272 EXPECT_DECLS("NestedNameSpecifierLoc", "struct X");
273
274 Code = R"cpp(
275 template <typename T>
276 int x = [[T::]]y;
277 )cpp";
278 EXPECT_DECLS("NestedNameSpecifierLoc", "typename T");
279
280 Code = R"cpp(
281 namespace a { int x; }
282 namespace b = a;
283 int y = [[b]]::x;
284 )cpp";
285 EXPECT_DECLS("NestedNameSpecifierLoc", {"namespace b = a", Rel::Alias},
286 {"namespace a", Rel::Underlying});
287 }
288
TEST_F(TargetDeclTest,Types)289 TEST_F(TargetDeclTest, Types) {
290 Code = R"cpp(
291 struct X{};
292 [[X]] x;
293 )cpp";
294 EXPECT_DECLS("RecordTypeLoc", "struct X");
295
296 Code = R"cpp(
297 struct S{};
298 typedef S X;
299 [[X]] x;
300 )cpp";
301 EXPECT_DECLS("TypedefTypeLoc", {"typedef S X", Rel::Alias},
302 {"struct S", Rel::Underlying});
303 Code = R"cpp(
304 namespace ns { struct S{}; }
305 typedef ns::S X;
306 [[X]] x;
307 )cpp";
308 EXPECT_DECLS("TypedefTypeLoc", {"typedef ns::S X", Rel::Alias},
309 {"struct S", Rel::Underlying});
310
311 // FIXME: Auto-completion in a template requires disabling delayed template
312 // parsing.
313 Flags = {"-fno-delayed-template-parsing"};
314 Code = R"cpp(
315 template<class T>
316 void foo() { [[T]] x; }
317 )cpp";
318 EXPECT_DECLS("TemplateTypeParmTypeLoc", "class T");
319 Flags.clear();
320
321 // FIXME: Auto-completion in a template requires disabling delayed template
322 // parsing.
323 Flags = {"-fno-delayed-template-parsing"};
324 Code = R"cpp(
325 template<template<typename> class T>
326 void foo() { [[T<int>]] x; }
327 )cpp";
328 EXPECT_DECLS("TemplateSpecializationTypeLoc", "template <typename> class T");
329 Flags.clear();
330
331 Code = R"cpp(
332 struct S{};
333 S X;
334 [[decltype]](X) Y;
335 )cpp";
336 EXPECT_DECLS("DecltypeTypeLoc", {"struct S", Rel::Underlying});
337
338 Code = R"cpp(
339 struct S{};
340 [[auto]] X = S{};
341 )cpp";
342 // FIXME: deduced type missing in AST. https://llvm.org/PR42914
343 EXPECT_DECLS("AutoTypeLoc");
344
345 Code = R"cpp(
346 template <typename... E>
347 struct S {
348 static const int size = sizeof...([[E]]);
349 };
350 )cpp";
351 EXPECT_DECLS("SizeOfPackExpr", "typename ...E");
352
353 Code = R"cpp(
354 template <typename T>
355 class Foo {
356 void f([[Foo]] x);
357 };
358 )cpp";
359 EXPECT_DECLS("InjectedClassNameTypeLoc", "class Foo");
360 }
361
TEST_F(TargetDeclTest,ClassTemplate)362 TEST_F(TargetDeclTest, ClassTemplate) {
363 Code = R"cpp(
364 // Implicit specialization.
365 template<int x> class Foo{};
366 [[Foo<42>]] B;
367 )cpp";
368 EXPECT_DECLS("TemplateSpecializationTypeLoc",
369 {"template<> class Foo<42>", Rel::TemplateInstantiation},
370 {"class Foo", Rel::TemplatePattern});
371
372 Code = R"cpp(
373 template<typename T> class Foo {};
374 // The "Foo<int>" SpecializationDecl is incomplete, there is no
375 // instantiation happening.
376 void func([[Foo<int>]] *);
377 )cpp";
378 EXPECT_DECLS("TemplateSpecializationTypeLoc",
379 {"class Foo", Rel::TemplatePattern},
380 {"template<> class Foo<int>", Rel::TemplateInstantiation});
381
382 Code = R"cpp(
383 // Explicit specialization.
384 template<int x> class Foo{};
385 template<> class Foo<42>{};
386 [[Foo<42>]] B;
387 )cpp";
388 EXPECT_DECLS("TemplateSpecializationTypeLoc", "template<> class Foo<42>");
389
390 Code = R"cpp(
391 // Partial specialization.
392 template<typename T> class Foo{};
393 template<typename T> class Foo<T*>{};
394 [[Foo<int*>]] B;
395 )cpp";
396 EXPECT_DECLS("TemplateSpecializationTypeLoc",
397 {"template<> class Foo<int *>", Rel::TemplateInstantiation},
398 {"template <typename T> class Foo<T *>", Rel::TemplatePattern});
399
400 Code = R"cpp(
401 // Template template argument.
402 template<typename T> struct Vector {};
403 template <template <typename> class Container>
404 struct A {};
405 A<[[Vector]]> a;
406 )cpp";
407 EXPECT_DECLS("TemplateArgumentLoc", {"template <typename T> struct Vector"});
408
409 Flags.push_back("-std=c++17"); // for CTAD tests
410
411 Code = R"cpp(
412 // Class template argument deduction
413 template <typename T>
414 struct Test {
415 Test(T);
416 };
417 void foo() {
418 [[Test]] a(5);
419 }
420 )cpp";
421 EXPECT_DECLS("DeducedTemplateSpecializationTypeLoc",
422 {"struct Test", Rel::TemplatePattern});
423
424 Code = R"cpp(
425 // Deduction guide
426 template <typename T>
427 struct Test {
428 template <typename I>
429 Test(I, I);
430 };
431 template <typename I>
432 [[Test]](I, I) -> Test<typename I::type>;
433 )cpp";
434 EXPECT_DECLS("CXXDeductionGuideDecl", {"template <typename T> struct Test"});
435 }
436
TEST_F(TargetDeclTest,Concept)437 TEST_F(TargetDeclTest, Concept) {
438 Flags.push_back("-std=c++20");
439
440 // FIXME: Should we truncate the pretty-printed form of a concept decl
441 // somewhere?
442
443 Code = R"cpp(
444 template <typename T>
445 concept Fooable = requires (T t) { t.foo(); };
446
447 template <typename T> requires [[Fooable]]<T>
448 void bar(T t) {
449 t.foo();
450 }
451 )cpp";
452 EXPECT_DECLS(
453 "ConceptSpecializationExpr",
454 {"template <typename T> concept Fooable = requires (T t) { t.foo(); };"});
455
456 // trailing requires clause
457 Code = R"cpp(
458 template <typename T>
459 concept Fooable = true;
460
461 template <typename T>
462 void foo() requires [[Fooable]]<T>;
463 )cpp";
464 EXPECT_DECLS("ConceptSpecializationExpr",
465 {"template <typename T> concept Fooable = true;"});
466
467 // constrained-parameter
468 Code = R"cpp(
469 template <typename T>
470 concept Fooable = true;
471
472 template <[[Fooable]] T>
473 void bar(T t);
474 )cpp";
475 EXPECT_DECLS("ConceptSpecializationExpr",
476 {"template <typename T> concept Fooable = true;"});
477
478 // partial-concept-id
479 Code = R"cpp(
480 template <typename T, typename U>
481 concept Fooable = true;
482
483 template <[[Fooable]]<int> T>
484 void bar(T t);
485 )cpp";
486 EXPECT_DECLS("ConceptSpecializationExpr",
487 {"template <typename T, typename U> concept Fooable = true;"});
488 }
489
TEST_F(TargetDeclTest,FunctionTemplate)490 TEST_F(TargetDeclTest, FunctionTemplate) {
491 Code = R"cpp(
492 // Implicit specialization.
493 template<typename T> bool foo(T) { return false; };
494 bool x = [[foo]](42);
495 )cpp";
496 EXPECT_DECLS("DeclRefExpr",
497 {"template<> bool foo<int>(int)", Rel::TemplateInstantiation},
498 {"bool foo(T)", Rel::TemplatePattern});
499
500 Code = R"cpp(
501 // Explicit specialization.
502 template<typename T> bool foo(T) { return false; };
503 template<> bool foo<int>(int) { return false; };
504 bool x = [[foo]](42);
505 )cpp";
506 EXPECT_DECLS("DeclRefExpr", "template<> bool foo<int>(int)");
507 }
508
TEST_F(TargetDeclTest,VariableTemplate)509 TEST_F(TargetDeclTest, VariableTemplate) {
510 // Pretty-printer doesn't do a very good job of variable templates :-(
511 Code = R"cpp(
512 // Implicit specialization.
513 template<typename T> int foo;
514 int x = [[foo]]<char>;
515 )cpp";
516 EXPECT_DECLS("DeclRefExpr", {"int foo", Rel::TemplateInstantiation},
517 {"int foo", Rel::TemplatePattern});
518
519 Code = R"cpp(
520 // Explicit specialization.
521 template<typename T> int foo;
522 template <> bool foo<char>;
523 int x = [[foo]]<char>;
524 )cpp";
525 EXPECT_DECLS("DeclRefExpr", "bool foo");
526
527 Code = R"cpp(
528 // Partial specialization.
529 template<typename T> int foo;
530 template<typename T> bool foo<T*>;
531 bool x = [[foo]]<char*>;
532 )cpp";
533 EXPECT_DECLS("DeclRefExpr", {"bool foo", Rel::TemplateInstantiation},
534 {"bool foo", Rel::TemplatePattern});
535 }
536
TEST_F(TargetDeclTest,TypeAliasTemplate)537 TEST_F(TargetDeclTest, TypeAliasTemplate) {
538 Code = R"cpp(
539 template<typename T, int X> class SmallVector {};
540 template<typename U> using TinyVector = SmallVector<U, 1>;
541 [[TinyVector<int>]] X;
542 )cpp";
543 EXPECT_DECLS("TemplateSpecializationTypeLoc",
544 {"template<> class SmallVector<int, 1>",
545 Rel::TemplateInstantiation | Rel::Underlying},
546 {"class SmallVector", Rel::TemplatePattern | Rel::Underlying},
547 {"using TinyVector = SmallVector<U, 1>",
548 Rel::Alias | Rel::TemplatePattern});
549 }
550
TEST_F(TargetDeclTest,MemberOfTemplate)551 TEST_F(TargetDeclTest, MemberOfTemplate) {
552 Code = R"cpp(
553 template <typename T> struct Foo {
554 int x(T);
555 };
556 int y = Foo<int>().[[x]](42);
557 )cpp";
558 EXPECT_DECLS("MemberExpr", {"int x(int)", Rel::TemplateInstantiation},
559 {"int x(T)", Rel::TemplatePattern});
560
561 Code = R"cpp(
562 template <typename T> struct Foo {
563 template <typename U>
564 int x(T, U);
565 };
566 int y = Foo<char>().[[x]]('c', 42);
567 )cpp";
568 EXPECT_DECLS("MemberExpr",
569 {"template<> int x<int>(char, int)", Rel::TemplateInstantiation},
570 {"int x(T, U)", Rel::TemplatePattern});
571 }
572
TEST_F(TargetDeclTest,Lambda)573 TEST_F(TargetDeclTest, Lambda) {
574 Code = R"cpp(
575 void foo(int x = 42) {
576 auto l = [ [[x]] ]{ return x + 1; };
577 };
578 )cpp";
579 EXPECT_DECLS("DeclRefExpr", "int x = 42");
580
581 // It seems like this should refer to another var, with the outer param being
582 // an underlying decl. But it doesn't seem to exist.
583 Code = R"cpp(
584 void foo(int x = 42) {
585 auto l = [x]{ return [[x]] + 1; };
586 };
587 )cpp";
588 EXPECT_DECLS("DeclRefExpr", "int x = 42");
589
590 Code = R"cpp(
591 void foo() {
592 auto l = [x = 1]{ return [[x]] + 1; };
593 };
594 )cpp";
595 // FIXME: why both auto and int?
596 EXPECT_DECLS("DeclRefExpr", "auto int x = 1");
597 }
598
TEST_F(TargetDeclTest,OverloadExpr)599 TEST_F(TargetDeclTest, OverloadExpr) {
600 // FIXME: Auto-completion in a template requires disabling delayed template
601 // parsing.
602 Flags = {"-fno-delayed-template-parsing"};
603 Flags.push_back("--target=x86_64-pc-linux-gnu");
604
605 Code = R"cpp(
606 void func(int*);
607 void func(char*);
608
609 template <class T>
610 void foo(T t) {
611 [[func]](t);
612 };
613 )cpp";
614 EXPECT_DECLS("UnresolvedLookupExpr", "void func(int *)", "void func(char *)");
615
616 Code = R"cpp(
617 struct X {
618 void func(int*);
619 void func(char*);
620 };
621
622 template <class T>
623 void foo(X x, T t) {
624 x.[[func]](t);
625 };
626 )cpp";
627 EXPECT_DECLS("UnresolvedMemberExpr", "void func(int *)", "void func(char *)");
628
629 Code = R"cpp(
630 struct X {
631 static void *operator new(unsigned long);
632 };
633 auto* k = [[new]] X();
634 )cpp";
635 EXPECT_DECLS("CXXNewExpr", "static void *operator new(unsigned long)");
636 Code = R"cpp(
637 void *operator new(unsigned long);
638 auto* k = [[new]] int();
639 )cpp";
640 EXPECT_DECLS("CXXNewExpr", "void *operator new(unsigned long)");
641
642 Code = R"cpp(
643 struct X {
644 static void operator delete(void *) noexcept;
645 };
646 void k(X* x) {
647 [[delete]] x;
648 }
649 )cpp";
650 EXPECT_DECLS("CXXDeleteExpr", "static void operator delete(void *) noexcept");
651 Code = R"cpp(
652 void operator delete(void *) noexcept;
653 void k(int* x) {
654 [[delete]] x;
655 }
656 )cpp";
657 EXPECT_DECLS("CXXDeleteExpr", "void operator delete(void *) noexcept");
658 }
659
TEST_F(TargetDeclTest,DependentExprs)660 TEST_F(TargetDeclTest, DependentExprs) {
661 Flags = {"-fno-delayed-template-parsing"};
662
663 // Heuristic resolution of method of dependent field
664 Code = R"cpp(
665 struct A { void foo() {} };
666 template <typename T>
667 struct B {
668 A a;
669 void bar() {
670 this->a.[[foo]]();
671 }
672 };
673 )cpp";
674 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void foo()");
675
676 // Similar to above but base expression involves a function call.
677 Code = R"cpp(
678 struct A {
679 void foo() {}
680 };
681 struct B {
682 A getA();
683 };
684 template <typename T>
685 struct C {
686 B c;
687 void bar() {
688 this->c.getA().[[foo]]();
689 }
690 };
691 )cpp";
692 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void foo()");
693
694 // Similar to above but uses a function pointer.
695 Code = R"cpp(
696 struct A {
697 void foo() {}
698 };
699 struct B {
700 using FPtr = A(*)();
701 FPtr fptr;
702 };
703 template <typename T>
704 struct C {
705 B c;
706 void bar() {
707 this->c.fptr().[[foo]]();
708 }
709 };
710 )cpp";
711 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void foo()");
712
713 // Base expression involves a member access into this.
714 Code = R"cpp(
715 struct Bar {
716 int aaaa;
717 };
718 template <typename T> struct Foo {
719 Bar func(int);
720 void test() {
721 func(1).[[aaaa]];
722 }
723 };
724 )cpp";
725 EXPECT_DECLS("CXXDependentScopeMemberExpr", "int aaaa");
726
727 Code = R"cpp(
728 class Foo {
729 public:
730 static Foo k(int);
731 template <typename T> T convert() const;
732 };
733 template <typename T>
734 void test() {
735 Foo::k(T()).template [[convert]]<T>();
736 }
737 )cpp";
738 EXPECT_DECLS("CXXDependentScopeMemberExpr",
739 "template <typename T> T convert() const");
740 }
741
TEST_F(TargetDeclTest,DependentTypes)742 TEST_F(TargetDeclTest, DependentTypes) {
743 Flags = {"-fno-delayed-template-parsing"};
744
745 // Heuristic resolution of dependent type name
746 Code = R"cpp(
747 template <typename>
748 struct A { struct B {}; };
749
750 template <typename T>
751 void foo(typename A<T>::[[B]]);
752 )cpp";
753 EXPECT_DECLS("DependentNameTypeLoc", "struct B");
754
755 // Heuristic resolution of dependent type name which doesn't get a TypeLoc
756 Code = R"cpp(
757 template <typename>
758 struct A { struct B { struct C {}; }; };
759
760 template <typename T>
761 void foo(typename A<T>::[[B]]::C);
762 )cpp";
763 EXPECT_DECLS("NestedNameSpecifierLoc", "struct B");
764
765 // Heuristic resolution of dependent type name whose qualifier is also
766 // dependent
767 Code = R"cpp(
768 template <typename>
769 struct A { struct B { struct C {}; }; };
770
771 template <typename T>
772 void foo(typename A<T>::B::[[C]]);
773 )cpp";
774 EXPECT_DECLS("DependentNameTypeLoc", "struct C");
775
776 // Heuristic resolution of dependent template name
777 Code = R"cpp(
778 template <typename>
779 struct A {
780 template <typename> struct B {};
781 };
782
783 template <typename T>
784 void foo(typename A<T>::template [[B]]<int>);
785 )cpp";
786 EXPECT_DECLS("DependentTemplateSpecializationTypeLoc",
787 "template <typename> struct B");
788 }
789
TEST_F(TargetDeclTest,ObjC)790 TEST_F(TargetDeclTest, ObjC) {
791 Flags = {"-xobjective-c"};
792 Code = R"cpp(
793 @interface Foo {}
794 -(void)bar;
795 @end
796 void test(Foo *f) {
797 [f [[bar]] ];
798 }
799 )cpp";
800 EXPECT_DECLS("ObjCMessageExpr", "- (void)bar");
801
802 Code = R"cpp(
803 @interface Foo { @public int bar; }
804 @end
805 int test(Foo *f) {
806 return [[f->bar]];
807 }
808 )cpp";
809 EXPECT_DECLS("ObjCIvarRefExpr", "int bar");
810
811 Code = R"cpp(
812 @interface Foo {}
813 -(int) x;
814 -(void) setX:(int)x;
815 @end
816 void test(Foo *f) {
817 [[f.x]] = 42;
818 }
819 )cpp";
820 EXPECT_DECLS("ObjCPropertyRefExpr", "- (void)setX:(int)x");
821
822 Code = R"cpp(
823 @interface I {}
824 @property(retain) I* x;
825 @property(retain) I* y;
826 @end
827 void test(I *f) {
828 [[f.x]].y = 0;
829 }
830 )cpp";
831 EXPECT_DECLS("ObjCPropertyRefExpr",
832 "@property(atomic, retain, readwrite) I *x");
833
834 Code = R"cpp(
835 @interface MYObject
836 @end
837 @interface Interface
838 @property(retain) [[MYObject]] *x;
839 @end
840 )cpp";
841 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface MYObject");
842
843 Code = R"cpp(
844 @interface MYObject2
845 @end
846 @interface Interface
847 @property(retain, nonnull) [[MYObject2]] *x;
848 @end
849 )cpp";
850 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface MYObject2");
851
852 Code = R"cpp(
853 @protocol Foo
854 @end
855 id test() {
856 return [[@protocol(Foo)]];
857 }
858 )cpp";
859 EXPECT_DECLS("ObjCProtocolExpr", "@protocol Foo");
860
861 Code = R"cpp(
862 @interface Foo
863 @end
864 void test([[Foo]] *p);
865 )cpp";
866 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface Foo");
867
868 Code = R"cpp(// Don't consider implicit interface as the target.
869 @implementation [[Implicit]]
870 @end
871 )cpp";
872 EXPECT_DECLS("ObjCImplementationDecl", "@implementation Implicit");
873
874 Code = R"cpp(
875 @interface Foo
876 @end
877 @implementation [[Foo]]
878 @end
879 )cpp";
880 EXPECT_DECLS("ObjCImplementationDecl", "@interface Foo");
881
882 Code = R"cpp(
883 @interface Foo
884 @end
885 @interface Foo (Ext)
886 @end
887 @implementation [[Foo]] (Ext)
888 @end
889 )cpp";
890 EXPECT_DECLS("ObjCCategoryImplDecl", "@interface Foo(Ext)");
891
892 Code = R"cpp(
893 @protocol Foo
894 @end
895 void test([[id<Foo>]] p);
896 )cpp";
897 EXPECT_DECLS("ObjCObjectTypeLoc", "@protocol Foo");
898
899 Code = R"cpp(
900 @class C;
901 @protocol Foo
902 @end
903 void test(C<[[Foo]]> *p);
904 )cpp";
905 // FIXME: there's no AST node corresponding to 'Foo', so we're stuck.
906 EXPECT_DECLS("ObjCObjectTypeLoc");
907 }
908
909 class FindExplicitReferencesTest : public ::testing::Test {
910 protected:
911 struct AllRefs {
912 std::string AnnotatedCode;
913 std::string DumpedReferences;
914 };
915
916 /// Parses \p Code, finds function or namespace '::foo' and annotates its body
917 /// with results of findExplicitReferences.
918 /// See actual tests for examples of annotation format.
annotateReferencesInFoo(llvm::StringRef Code)919 AllRefs annotateReferencesInFoo(llvm::StringRef Code) {
920 TestTU TU;
921 TU.Code = std::string(Code);
922
923 // FIXME: Auto-completion in a template requires disabling delayed template
924 // parsing.
925 TU.ExtraArgs.push_back("-fno-delayed-template-parsing");
926 TU.ExtraArgs.push_back("-std=c++20");
927 TU.ExtraArgs.push_back("-xobjective-c++");
928
929 auto AST = TU.build();
930 auto *TestDecl = &findDecl(AST, "foo");
931 if (auto *T = llvm::dyn_cast<FunctionTemplateDecl>(TestDecl))
932 TestDecl = T->getTemplatedDecl();
933
934 std::vector<ReferenceLoc> Refs;
935 if (const auto *Func = llvm::dyn_cast<FunctionDecl>(TestDecl))
936 findExplicitReferences(Func->getBody(), [&Refs](ReferenceLoc R) {
937 Refs.push_back(std::move(R));
938 });
939 else if (const auto *NS = llvm::dyn_cast<NamespaceDecl>(TestDecl))
940 findExplicitReferences(NS, [&Refs, &NS](ReferenceLoc R) {
941 // Avoid adding the namespace foo decl to the results.
942 if (R.Targets.size() == 1 && R.Targets.front() == NS)
943 return;
944 Refs.push_back(std::move(R));
945 });
946 else
947 ADD_FAILURE() << "Failed to find ::foo decl for test";
948
949 auto &SM = AST.getSourceManager();
950 llvm::sort(Refs, [&](const ReferenceLoc &L, const ReferenceLoc &R) {
951 return SM.isBeforeInTranslationUnit(L.NameLoc, R.NameLoc);
952 });
953
954 std::string AnnotatedCode;
955 unsigned NextCodeChar = 0;
956 for (unsigned I = 0; I < Refs.size(); ++I) {
957 auto &R = Refs[I];
958
959 SourceLocation Pos = R.NameLoc;
960 assert(Pos.isValid());
961 if (Pos.isMacroID()) // FIXME: figure out how to show macro locations.
962 Pos = SM.getExpansionLoc(Pos);
963 assert(Pos.isFileID());
964
965 FileID File;
966 unsigned Offset;
967 std::tie(File, Offset) = SM.getDecomposedLoc(Pos);
968 if (File == SM.getMainFileID()) {
969 // Print the reference in a source code.
970 assert(NextCodeChar <= Offset);
971 AnnotatedCode += Code.substr(NextCodeChar, Offset - NextCodeChar);
972 AnnotatedCode += "$" + std::to_string(I) + "^";
973
974 NextCodeChar = Offset;
975 }
976 }
977 AnnotatedCode += Code.substr(NextCodeChar);
978
979 std::string DumpedReferences;
980 for (unsigned I = 0; I < Refs.size(); ++I)
981 DumpedReferences += std::string(llvm::formatv("{0}: {1}\n", I, Refs[I]));
982
983 return AllRefs{std::move(AnnotatedCode), std::move(DumpedReferences)};
984 }
985 };
986
TEST_F(FindExplicitReferencesTest,All)987 TEST_F(FindExplicitReferencesTest, All) {
988 std::pair</*Code*/ llvm::StringRef, /*References*/ llvm::StringRef> Cases[] =
989 {
990 // Simple expressions.
991 {R"cpp(
992 int global;
993 int func();
994 void foo(int param) {
995 $0^global = $1^param + $2^func();
996 }
997 )cpp",
998 "0: targets = {global}\n"
999 "1: targets = {param}\n"
1000 "2: targets = {func}\n"},
1001 {R"cpp(
1002 struct X { int a; };
1003 void foo(X x) {
1004 $0^x.$1^a = 10;
1005 }
1006 )cpp",
1007 "0: targets = {x}\n"
1008 "1: targets = {X::a}\n"},
1009 {R"cpp(
1010 // error-ok: testing with broken code
1011 int bar();
1012 int foo() {
1013 return $0^bar() + $1^bar(42);
1014 }
1015 )cpp",
1016 "0: targets = {bar}\n"
1017 "1: targets = {bar}\n"},
1018 // Namespaces and aliases.
1019 {R"cpp(
1020 namespace ns {}
1021 namespace alias = ns;
1022 void foo() {
1023 using namespace $0^ns;
1024 using namespace $1^alias;
1025 }
1026 )cpp",
1027 "0: targets = {ns}\n"
1028 "1: targets = {alias}\n"},
1029 // Using declarations.
1030 {R"cpp(
1031 namespace ns { int global; }
1032 void foo() {
1033 using $0^ns::$1^global;
1034 }
1035 )cpp",
1036 "0: targets = {ns}\n"
1037 "1: targets = {ns::global}, qualifier = 'ns::'\n"},
1038 // Simple types.
1039 {R"cpp(
1040 struct Struct { int a; };
1041 using Typedef = int;
1042 void foo() {
1043 $0^Struct $1^x;
1044 $2^Typedef $3^y;
1045 static_cast<$4^Struct*>(0);
1046 }
1047 )cpp",
1048 "0: targets = {Struct}\n"
1049 "1: targets = {x}, decl\n"
1050 "2: targets = {Typedef}\n"
1051 "3: targets = {y}, decl\n"
1052 "4: targets = {Struct}\n"},
1053 // Name qualifiers.
1054 {R"cpp(
1055 namespace a { namespace b { struct S { typedef int type; }; } }
1056 void foo() {
1057 $0^a::$1^b::$2^S $3^x;
1058 using namespace $4^a::$5^b;
1059 $6^S::$7^type $8^y;
1060 }
1061 )cpp",
1062 "0: targets = {a}\n"
1063 "1: targets = {a::b}, qualifier = 'a::'\n"
1064 "2: targets = {a::b::S}, qualifier = 'a::b::'\n"
1065 "3: targets = {x}, decl\n"
1066 "4: targets = {a}\n"
1067 "5: targets = {a::b}, qualifier = 'a::'\n"
1068 "6: targets = {a::b::S}\n"
1069 "7: targets = {a::b::S::type}, qualifier = 'struct S::'\n"
1070 "8: targets = {y}, decl\n"},
1071 {R"cpp(
1072 void foo() {
1073 $0^ten: // PRINT "HELLO WORLD!"
1074 goto $1^ten;
1075 }
1076 )cpp",
1077 "0: targets = {ten}, decl\n"
1078 "1: targets = {ten}\n"},
1079 // Simple templates.
1080 {R"cpp(
1081 template <class T> struct vector { using value_type = T; };
1082 template <> struct vector<bool> { using value_type = bool; };
1083 void foo() {
1084 $0^vector<int> $1^vi;
1085 $2^vector<bool> $3^vb;
1086 }
1087 )cpp",
1088 "0: targets = {vector<int>}\n"
1089 "1: targets = {vi}, decl\n"
1090 "2: targets = {vector<bool>}\n"
1091 "3: targets = {vb}, decl\n"},
1092 // Template type aliases.
1093 {R"cpp(
1094 template <class T> struct vector { using value_type = T; };
1095 template <> struct vector<bool> { using value_type = bool; };
1096 template <class T> using valias = vector<T>;
1097 void foo() {
1098 $0^valias<int> $1^vi;
1099 $2^valias<bool> $3^vb;
1100 }
1101 )cpp",
1102 "0: targets = {valias}\n"
1103 "1: targets = {vi}, decl\n"
1104 "2: targets = {valias}\n"
1105 "3: targets = {vb}, decl\n"},
1106 // Injected class name.
1107 {R"cpp(
1108 namespace foo {
1109 template <typename $0^T>
1110 class $1^Bar {
1111 ~$2^Bar();
1112 void $3^f($4^Bar);
1113 };
1114 }
1115 )cpp",
1116 "0: targets = {foo::Bar::T}, decl\n"
1117 "1: targets = {foo::Bar}, decl\n"
1118 "2: targets = {foo::Bar}\n"
1119 "3: targets = {foo::Bar::f}, decl\n"
1120 "4: targets = {foo::Bar}\n"},
1121 // MemberExpr should know their using declaration.
1122 {R"cpp(
1123 struct X { void func(int); };
1124 struct Y : X {
1125 using X::func;
1126 };
1127 void foo(Y y) {
1128 $0^y.$1^func(1);
1129 }
1130 )cpp",
1131 "0: targets = {y}\n"
1132 "1: targets = {Y::func}\n"},
1133 // DeclRefExpr should know their using declaration.
1134 {R"cpp(
1135 namespace ns { void bar(int); }
1136 using ns::bar;
1137
1138 void foo() {
1139 $0^bar(10);
1140 }
1141 )cpp",
1142 "0: targets = {bar}\n"},
1143 // References from a macro.
1144 {R"cpp(
1145 #define FOO a
1146 #define BAR b
1147
1148 void foo(int a, int b) {
1149 $0^FOO+$1^BAR;
1150 }
1151 )cpp",
1152 "0: targets = {a}\n"
1153 "1: targets = {b}\n"},
1154 // No references from implicit nodes.
1155 {R"cpp(
1156 struct vector {
1157 int *begin();
1158 int *end();
1159 };
1160
1161 void foo() {
1162 for (int $0^x : $1^vector()) {
1163 $2^x = 10;
1164 }
1165 }
1166 )cpp",
1167 "0: targets = {x}, decl\n"
1168 "1: targets = {vector}\n"
1169 "2: targets = {x}\n"},
1170 // Handle UnresolvedLookupExpr.
1171 // FIXME
1172 // This case fails when expensive checks are enabled.
1173 // Seems like the order of ns1::func and ns2::func isn't defined.
1174 #ifndef EXPENSIVE_CHECKS
1175 {R"cpp(
1176 namespace ns1 { void func(char*); }
1177 namespace ns2 { void func(int*); }
1178 using namespace ns1;
1179 using namespace ns2;
1180
1181 template <class T>
1182 void foo(T t) {
1183 $0^func($1^t);
1184 }
1185 )cpp",
1186 "0: targets = {ns1::func, ns2::func}\n"
1187 "1: targets = {t}\n"},
1188 #endif
1189 // Handle UnresolvedMemberExpr.
1190 {R"cpp(
1191 struct X {
1192 void func(char*);
1193 void func(int*);
1194 };
1195
1196 template <class T>
1197 void foo(X x, T t) {
1198 $0^x.$1^func($2^t);
1199 }
1200 )cpp",
1201 "0: targets = {x}\n"
1202 "1: targets = {X::func, X::func}\n"
1203 "2: targets = {t}\n"},
1204 // Handle DependentScopeDeclRefExpr.
1205 {R"cpp(
1206 template <class T>
1207 struct S {
1208 static int value;
1209 };
1210
1211 template <class T>
1212 void foo() {
1213 $0^S<$1^T>::$2^value;
1214 }
1215 )cpp",
1216 "0: targets = {S}\n"
1217 "1: targets = {T}\n"
1218 "2: targets = {S::value}, qualifier = 'S<T>::'\n"},
1219 // Handle CXXDependentScopeMemberExpr.
1220 {R"cpp(
1221 template <class T>
1222 struct S {
1223 int value;
1224 };
1225
1226 template <class T>
1227 void foo(S<T> t) {
1228 $0^t.$1^value;
1229 }
1230 )cpp",
1231 "0: targets = {t}\n"
1232 "1: targets = {S::value}\n"},
1233 // Type template parameters.
1234 {R"cpp(
1235 template <class T>
1236 void foo() {
1237 static_cast<$0^T>(0);
1238 $1^T();
1239 $2^T $3^t;
1240 }
1241 )cpp",
1242 "0: targets = {T}\n"
1243 "1: targets = {T}\n"
1244 "2: targets = {T}\n"
1245 "3: targets = {t}, decl\n"},
1246 // Non-type template parameters.
1247 {R"cpp(
1248 template <int I>
1249 void foo() {
1250 int $0^x = $1^I;
1251 }
1252 )cpp",
1253 "0: targets = {x}, decl\n"
1254 "1: targets = {I}\n"},
1255 // Template template parameters.
1256 {R"cpp(
1257 template <class T> struct vector {};
1258
1259 template <template<class> class TT, template<class> class ...TP>
1260 void foo() {
1261 $0^TT<int> $1^x;
1262 $2^foo<$3^TT>();
1263 $4^foo<$5^vector>();
1264 $6^foo<$7^TP...>();
1265 }
1266 )cpp",
1267 "0: targets = {TT}\n"
1268 "1: targets = {x}, decl\n"
1269 "2: targets = {foo}\n"
1270 "3: targets = {TT}\n"
1271 "4: targets = {foo}\n"
1272 "5: targets = {vector}\n"
1273 "6: targets = {foo}\n"
1274 "7: targets = {TP}\n"},
1275 // Non-type template parameters with declarations.
1276 {R"cpp(
1277 int func();
1278 template <int(*)()> struct wrapper {};
1279
1280 template <int(*FuncParam)()>
1281 void foo() {
1282 $0^wrapper<$1^func> $2^w;
1283 $3^FuncParam();
1284 }
1285 )cpp",
1286 "0: targets = {wrapper<&func>}\n"
1287 "1: targets = {func}\n"
1288 "2: targets = {w}, decl\n"
1289 "3: targets = {FuncParam}\n"},
1290 // declaration references.
1291 {R"cpp(
1292 namespace ns {}
1293 class S {};
1294 void foo() {
1295 class $0^Foo { $1^Foo(); ~$2^Foo(); int $3^field; };
1296 int $4^Var;
1297 enum $5^E { $6^ABC };
1298 typedef int $7^INT;
1299 using $8^INT2 = int;
1300 namespace $9^NS = $10^ns;
1301 }
1302 )cpp",
1303 "0: targets = {Foo}, decl\n"
1304 "1: targets = {foo()::Foo::Foo}, decl\n"
1305 "2: targets = {Foo}\n"
1306 "3: targets = {foo()::Foo::field}, decl\n"
1307 "4: targets = {Var}, decl\n"
1308 "5: targets = {E}, decl\n"
1309 "6: targets = {foo()::ABC}, decl\n"
1310 "7: targets = {INT}, decl\n"
1311 "8: targets = {INT2}, decl\n"
1312 "9: targets = {NS}, decl\n"
1313 "10: targets = {ns}\n"},
1314 // User-defined conversion operator.
1315 {R"cpp(
1316 void foo() {
1317 class $0^Bar {};
1318 class $1^Foo {
1319 public:
1320 // FIXME: This should have only one reference to Bar.
1321 $2^operator $3^$4^Bar();
1322 };
1323
1324 $5^Foo $6^f;
1325 $7^f.$8^operator $9^Bar();
1326 }
1327 )cpp",
1328 "0: targets = {Bar}, decl\n"
1329 "1: targets = {Foo}, decl\n"
1330 "2: targets = {foo()::Foo::operator Bar}, decl\n"
1331 "3: targets = {Bar}\n"
1332 "4: targets = {Bar}\n"
1333 "5: targets = {Foo}\n"
1334 "6: targets = {f}, decl\n"
1335 "7: targets = {f}\n"
1336 "8: targets = {foo()::Foo::operator Bar}\n"
1337 "9: targets = {Bar}\n"},
1338 // Destructor.
1339 {R"cpp(
1340 void foo() {
1341 class $0^Foo {
1342 public:
1343 ~$1^Foo() {}
1344
1345 void $2^destructMe() {
1346 this->~$3^Foo();
1347 }
1348 };
1349
1350 $4^Foo $5^f;
1351 $6^f.~ /*...*/ $7^Foo();
1352 }
1353 )cpp",
1354 "0: targets = {Foo}, decl\n"
1355 // FIXME: It's better to target destructor's FunctionDecl instead of
1356 // the type itself (similar to constructor).
1357 "1: targets = {Foo}\n"
1358 "2: targets = {foo()::Foo::destructMe}, decl\n"
1359 "3: targets = {Foo}\n"
1360 "4: targets = {Foo}\n"
1361 "5: targets = {f}, decl\n"
1362 "6: targets = {f}\n"
1363 "7: targets = {Foo}\n"},
1364 // cxx constructor initializer.
1365 {R"cpp(
1366 class Base {};
1367 void foo() {
1368 // member initializer
1369 class $0^X {
1370 int $1^abc;
1371 $2^X(): $3^abc() {}
1372 };
1373 // base initializer
1374 class $4^Derived : public $5^Base {
1375 $6^Base $7^B;
1376 $8^Derived() : $9^Base() {}
1377 };
1378 // delegating initializer
1379 class $10^Foo {
1380 $11^Foo(int);
1381 $12^Foo(): $13^Foo(111) {}
1382 };
1383 }
1384 )cpp",
1385 "0: targets = {X}, decl\n"
1386 "1: targets = {foo()::X::abc}, decl\n"
1387 "2: targets = {foo()::X::X}, decl\n"
1388 "3: targets = {foo()::X::abc}\n"
1389 "4: targets = {Derived}, decl\n"
1390 "5: targets = {Base}\n"
1391 "6: targets = {Base}\n"
1392 "7: targets = {foo()::Derived::B}, decl\n"
1393 "8: targets = {foo()::Derived::Derived}, decl\n"
1394 "9: targets = {Base}\n"
1395 "10: targets = {Foo}, decl\n"
1396 "11: targets = {foo()::Foo::Foo}, decl\n"
1397 "12: targets = {foo()::Foo::Foo}, decl\n"
1398 "13: targets = {Foo}\n"},
1399 // Anonymous entities should not be reported.
1400 {
1401 R"cpp(
1402 void foo() {
1403 class {} $0^x;
1404 int (*$1^fptr)(int $2^a, int) = nullptr;
1405 }
1406 )cpp",
1407 "0: targets = {x}, decl\n"
1408 "1: targets = {fptr}, decl\n"
1409 "2: targets = {a}, decl\n"},
1410 // Namespace aliases should be handled properly.
1411 {
1412 R"cpp(
1413 namespace ns { struct Type {}; }
1414 namespace alias = ns;
1415 namespace rec_alias = alias;
1416
1417 void foo() {
1418 $0^ns::$1^Type $2^a;
1419 $3^alias::$4^Type $5^b;
1420 $6^rec_alias::$7^Type $8^c;
1421 }
1422 )cpp",
1423 "0: targets = {ns}\n"
1424 "1: targets = {ns::Type}, qualifier = 'ns::'\n"
1425 "2: targets = {a}, decl\n"
1426 "3: targets = {alias}\n"
1427 "4: targets = {ns::Type}, qualifier = 'alias::'\n"
1428 "5: targets = {b}, decl\n"
1429 "6: targets = {rec_alias}\n"
1430 "7: targets = {ns::Type}, qualifier = 'rec_alias::'\n"
1431 "8: targets = {c}, decl\n"},
1432 // Handle SizeOfPackExpr.
1433 {
1434 R"cpp(
1435 template <typename... E>
1436 void foo() {
1437 constexpr int $0^size = sizeof...($1^E);
1438 };
1439 )cpp",
1440 "0: targets = {size}, decl\n"
1441 "1: targets = {E}\n"},
1442 // Class template argument deduction
1443 {
1444 R"cpp(
1445 template <typename T>
1446 struct Test {
1447 Test(T);
1448 };
1449 void foo() {
1450 $0^Test $1^a(5);
1451 }
1452 )cpp",
1453 "0: targets = {Test}\n"
1454 "1: targets = {a}, decl\n"},
1455 // Templates
1456 {R"cpp(
1457 namespace foo {
1458 template <typename $0^T>
1459 class $1^Bar {};
1460 }
1461 )cpp",
1462 "0: targets = {foo::Bar::T}, decl\n"
1463 "1: targets = {foo::Bar}, decl\n"},
1464 // Templates
1465 {R"cpp(
1466 namespace foo {
1467 template <typename $0^T>
1468 void $1^func();
1469 }
1470 )cpp",
1471 "0: targets = {T}, decl\n"
1472 "1: targets = {foo::func}, decl\n"},
1473 // Templates
1474 {R"cpp(
1475 namespace foo {
1476 template <typename $0^T>
1477 $1^T $2^x;
1478 }
1479 )cpp",
1480 "0: targets = {foo::T}, decl\n"
1481 "1: targets = {foo::T}\n"
1482 "2: targets = {foo::x}, decl\n"},
1483 // Templates
1484 {R"cpp(
1485 template<typename T> class vector {};
1486 namespace foo {
1487 template <typename $0^T>
1488 using $1^V = $2^vector<$3^T>;
1489 }
1490 )cpp",
1491 "0: targets = {foo::T}, decl\n"
1492 "1: targets = {foo::V}, decl\n"
1493 "2: targets = {vector}\n"
1494 "3: targets = {foo::T}\n"},
1495 // Concept
1496 {
1497 R"cpp(
1498 template <typename T>
1499 concept Drawable = requires (T t) { t.draw(); };
1500
1501 namespace foo {
1502 template <typename $0^T> requires $1^Drawable<$2^T>
1503 void $3^bar($4^T $5^t) {
1504 $6^t.$7^draw();
1505 }
1506 }
1507 )cpp",
1508 "0: targets = {T}, decl\n"
1509 "1: targets = {Drawable}\n"
1510 "2: targets = {T}\n"
1511 "3: targets = {foo::bar}, decl\n"
1512 "4: targets = {T}\n"
1513 "5: targets = {t}, decl\n"
1514 "6: targets = {t}\n"
1515 "7: targets = {}\n"},
1516 // Objective-C: properties
1517 {
1518 R"cpp(
1519 @interface I {}
1520 @property(retain) I* x;
1521 @property(retain) I* y;
1522 @end
1523 I *f;
1524 void foo() {
1525 $0^f.$1^x.$2^y = 0;
1526 }
1527 )cpp",
1528 "0: targets = {f}\n"
1529 "1: targets = {I::x}\n"
1530 "2: targets = {I::y}\n"},
1531 // Objective-C: implicit properties
1532 {
1533 R"cpp(
1534 @interface I {}
1535 -(I*)x;
1536 -(void)setY:(I*)y;
1537 @end
1538 I *f;
1539 void foo() {
1540 $0^f.$1^x.$2^y = 0;
1541 }
1542 )cpp",
1543 "0: targets = {f}\n"
1544 "1: targets = {I::x}\n"
1545 "2: targets = {I::setY:}\n"},
1546 // Designated initializers.
1547 {R"cpp(
1548 void foo() {
1549 struct $0^Foo {
1550 int $1^Bar;
1551 };
1552 $2^Foo $3^f { .$4^Bar = 42 };
1553 }
1554 )cpp",
1555 "0: targets = {Foo}, decl\n"
1556 "1: targets = {foo()::Foo::Bar}, decl\n"
1557 "2: targets = {Foo}\n"
1558 "3: targets = {f}, decl\n"
1559 "4: targets = {foo()::Foo::Bar}\n"},
1560 {R"cpp(
1561 void foo() {
1562 struct $0^Baz {
1563 int $1^Field;
1564 };
1565 struct $2^Bar {
1566 $3^Baz $4^Foo;
1567 };
1568 $5^Bar $6^bar { .$7^Foo.$8^Field = 42 };
1569 }
1570 )cpp",
1571 "0: targets = {Baz}, decl\n"
1572 "1: targets = {foo()::Baz::Field}, decl\n"
1573 "2: targets = {Bar}, decl\n"
1574 "3: targets = {Baz}\n"
1575 "4: targets = {foo()::Bar::Foo}, decl\n"
1576 "5: targets = {Bar}\n"
1577 "6: targets = {bar}, decl\n"
1578 "7: targets = {foo()::Bar::Foo}\n"
1579 "8: targets = {foo()::Baz::Field}\n"},
1580 {R"cpp(
1581 template<typename T>
1582 void crash(T);
1583 template<typename T>
1584 void foo() {
1585 $0^crash({.$1^x = $2^T()});
1586 }
1587 )cpp",
1588 "0: targets = {crash}\n"
1589 "1: targets = {}\n"
1590 "2: targets = {T}\n"},
1591 // unknown template name should not crash.
1592 {R"cpp(
1593 template <template <typename> typename T>
1594 struct Base {};
1595 namespace foo {
1596 template <typename $0^T>
1597 struct $1^Derive : $2^Base<$3^T::template $4^Unknown> {};
1598 }
1599 )cpp",
1600 "0: targets = {foo::Derive::T}, decl\n"
1601 "1: targets = {foo::Derive}, decl\n"
1602 "2: targets = {Base}\n"
1603 "3: targets = {foo::Derive::T}\n"
1604 "4: targets = {}, qualifier = 'T::'\n"},
1605 // deduction guide
1606 {R"cpp(
1607 namespace foo {
1608 template <typename $0^T>
1609 struct $1^Test {
1610 template <typename $2^I>
1611 $3^Test($4^I);
1612 };
1613 template <typename $5^I>
1614 $6^Test($7^I) -> $8^Test<typename $9^I::$10^type>;
1615 }
1616 )cpp",
1617 "0: targets = {T}, decl\n"
1618 "1: targets = {foo::Test}, decl\n"
1619 "2: targets = {I}, decl\n"
1620 "3: targets = {foo::Test::Test<T>}, decl\n"
1621 "4: targets = {I}\n"
1622 "5: targets = {I}, decl\n"
1623 "6: targets = {foo::Test}\n"
1624 "7: targets = {I}\n"
1625 "8: targets = {foo::Test}\n"
1626 "9: targets = {I}\n"
1627 "10: targets = {}, qualifier = 'I::'\n"}};
1628
1629 for (const auto &C : Cases) {
1630 llvm::StringRef ExpectedCode = C.first;
1631 llvm::StringRef ExpectedRefs = C.second;
1632
1633 auto Actual =
1634 annotateReferencesInFoo(llvm::Annotations(ExpectedCode).code());
1635 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
1636 EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
1637 }
1638 }
1639
1640 } // namespace
1641 } // namespace clangd
1642 } // namespace clang
1643