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