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