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  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  Code = R"cpp(
1000  @interface Foo
1001  + ([[id]])sharedInstance;
1002  @end
1003  )cpp";
1004  EXPECT_DECLS("TypedefTypeLoc");
1005 
1006  Code = R"cpp(
1007  @interface Foo
1008  + ([[instancetype]])sharedInstance;
1009  @end
1010  )cpp";
1011  EXPECT_DECLS("TypedefTypeLoc");
1012 }
1013 
1014 class FindExplicitReferencesTest : public ::testing::Test {
1015 protected:
1016  struct AllRefs {
1017  std::string AnnotatedCode;
1018  std::string DumpedReferences;
1019  };
1020 
1021  /// Parses \p Code, finds function or namespace '::foo' and annotates its body
1022  /// with results of findExplicitReferences.
1023  /// See actual tests for examples of annotation format.
1024  AllRefs annotateReferencesInFoo(llvm::StringRef Code) {
1025  TestTU TU;
1026  TU.Code = std::string(Code);
1027 
1028  // FIXME: Auto-completion in a template requires disabling delayed template
1029  // parsing.
1030  TU.ExtraArgs.push_back("-std=c++20");
1031  TU.ExtraArgs.push_back("-xobjective-c++");
1032 
1033  auto AST = TU.build();
1034  auto *TestDecl = &findDecl(AST, "foo");
1035  if (auto *T = llvm::dyn_cast<FunctionTemplateDecl>(TestDecl))
1036  TestDecl = T->getTemplatedDecl();
1037 
1038  std::vector<ReferenceLoc> Refs;
1039  if (const auto *Func = llvm::dyn_cast<FunctionDecl>(TestDecl))
1041  Func->getBody(),
1042  [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1043  AST.getHeuristicResolver());
1044  else if (const auto *NS = llvm::dyn_cast<NamespaceDecl>(TestDecl))
1046  NS,
1047  [&Refs, &NS](ReferenceLoc R) {
1048  // Avoid adding the namespace foo decl to the results.
1049  if (R.Targets.size() == 1 && R.Targets.front() == NS)
1050  return;
1051  Refs.push_back(std::move(R));
1052  },
1053  AST.getHeuristicResolver());
1054  else
1055  ADD_FAILURE() << "Failed to find ::foo decl for test";
1056 
1057  auto &SM = AST.getSourceManager();
1058  llvm::sort(Refs, [&](const ReferenceLoc &L, const ReferenceLoc &R) {
1059  return SM.isBeforeInTranslationUnit(L.NameLoc, R.NameLoc);
1060  });
1061 
1062  std::string AnnotatedCode;
1063  unsigned NextCodeChar = 0;
1064  for (unsigned I = 0; I < Refs.size(); ++I) {
1065  auto &R = Refs[I];
1066 
1067  SourceLocation Pos = R.NameLoc;
1068  assert(Pos.isValid());
1069  if (Pos.isMacroID()) // FIXME: figure out how to show macro locations.
1070  Pos = SM.getExpansionLoc(Pos);
1071  assert(Pos.isFileID());
1072 
1073  FileID File;
1074  unsigned Offset;
1075  std::tie(File, Offset) = SM.getDecomposedLoc(Pos);
1076  if (File == SM.getMainFileID()) {
1077  // Print the reference in a source code.
1078  assert(NextCodeChar <= Offset);
1079  AnnotatedCode += Code.substr(NextCodeChar, Offset - NextCodeChar);
1080  AnnotatedCode += "$" + std::to_string(I) + "^";
1081 
1082  NextCodeChar = Offset;
1083  }
1084  }
1085  AnnotatedCode += Code.substr(NextCodeChar);
1086 
1087  std::string DumpedReferences;
1088  for (unsigned I = 0; I < Refs.size(); ++I)
1089  DumpedReferences += std::string(llvm::formatv("{0}: {1}\n", I, Refs[I]));
1090 
1091  return AllRefs{std::move(AnnotatedCode), std::move(DumpedReferences)};
1092  }
1093 };
1094 
1095 TEST_F(FindExplicitReferencesTest, All) {
1096  std::pair</*Code*/ llvm::StringRef, /*References*/ llvm::StringRef> Cases[] =
1097  {// Simple expressions.
1098  {R"cpp(
1099  int global;
1100  int func();
1101  void foo(int param) {
1102  $0^global = $1^param + $2^func();
1103  }
1104  )cpp",
1105  "0: targets = {global}\n"
1106  "1: targets = {param}\n"
1107  "2: targets = {func}\n"},
1108  {R"cpp(
1109  struct X { int a; };
1110  void foo(X x) {
1111  $0^x.$1^a = 10;
1112  }
1113  )cpp",
1114  "0: targets = {x}\n"
1115  "1: targets = {X::a}\n"},
1116  {R"cpp(
1117  // error-ok: testing with broken code
1118  int bar();
1119  int foo() {
1120  return $0^bar() + $1^bar(42);
1121  }
1122  )cpp",
1123  "0: targets = {bar}\n"
1124  "1: targets = {bar}\n"},
1125  // Namespaces and aliases.
1126  {R"cpp(
1127  namespace ns {}
1128  namespace alias = ns;
1129  void foo() {
1130  using namespace $0^ns;
1131  using namespace $1^alias;
1132  }
1133  )cpp",
1134  "0: targets = {ns}\n"
1135  "1: targets = {alias}\n"},
1136  // Using declarations.
1137  {R"cpp(
1138  namespace ns { int global; }
1139  void foo() {
1140  using $0^ns::$1^global;
1141  }
1142  )cpp",
1143  "0: targets = {ns}\n"
1144  "1: targets = {ns::global}, qualifier = 'ns::'\n"},
1145  // Simple types.
1146  {R"cpp(
1147  struct Struct { int a; };
1148  using Typedef = int;
1149  void foo() {
1150  $0^Struct $1^x;
1151  $2^Typedef $3^y;
1152  static_cast<$4^Struct*>(0);
1153  }
1154  )cpp",
1155  "0: targets = {Struct}\n"
1156  "1: targets = {x}, decl\n"
1157  "2: targets = {Typedef}\n"
1158  "3: targets = {y}, decl\n"
1159  "4: targets = {Struct}\n"},
1160  // Name qualifiers.
1161  {R"cpp(
1162  namespace a { namespace b { struct S { typedef int type; }; } }
1163  void foo() {
1164  $0^a::$1^b::$2^S $3^x;
1165  using namespace $4^a::$5^b;
1166  $6^S::$7^type $8^y;
1167  }
1168  )cpp",
1169  "0: targets = {a}\n"
1170  "1: targets = {a::b}, qualifier = 'a::'\n"
1171  "2: targets = {a::b::S}, qualifier = 'a::b::'\n"
1172  "3: targets = {x}, decl\n"
1173  "4: targets = {a}\n"
1174  "5: targets = {a::b}, qualifier = 'a::'\n"
1175  "6: targets = {a::b::S}\n"
1176  "7: targets = {a::b::S::type}, qualifier = 'struct S::'\n"
1177  "8: targets = {y}, decl\n"},
1178  {R"cpp(
1179  void foo() {
1180  $0^ten: // PRINT "HELLO WORLD!"
1181  goto $1^ten;
1182  }
1183  )cpp",
1184  "0: targets = {ten}, decl\n"
1185  "1: targets = {ten}\n"},
1186  // Simple templates.
1187  {R"cpp(
1188  template <class T> struct vector { using value_type = T; };
1189  template <> struct vector<bool> { using value_type = bool; };
1190  void foo() {
1191  $0^vector<int> $1^vi;
1192  $2^vector<bool> $3^vb;
1193  }
1194  )cpp",
1195  "0: targets = {vector<int>}\n"
1196  "1: targets = {vi}, decl\n"
1197  "2: targets = {vector<bool>}\n"
1198  "3: targets = {vb}, decl\n"},
1199  // Template type aliases.
1200  {R"cpp(
1201  template <class T> struct vector { using value_type = T; };
1202  template <> struct vector<bool> { using value_type = bool; };
1203  template <class T> using valias = vector<T>;
1204  void foo() {
1205  $0^valias<int> $1^vi;
1206  $2^valias<bool> $3^vb;
1207  }
1208  )cpp",
1209  "0: targets = {valias}\n"
1210  "1: targets = {vi}, decl\n"
1211  "2: targets = {valias}\n"
1212  "3: targets = {vb}, decl\n"},
1213  // Injected class name.
1214  {R"cpp(
1215  namespace foo {
1216  template <typename $0^T>
1217  class $1^Bar {
1218  ~$2^Bar();
1219  void $3^f($4^Bar);
1220  };
1221  }
1222  )cpp",
1223  "0: targets = {foo::Bar::T}, decl\n"
1224  "1: targets = {foo::Bar}, decl\n"
1225  "2: targets = {foo::Bar}\n"
1226  "3: targets = {foo::Bar::f}, decl\n"
1227  "4: targets = {foo::Bar}\n"},
1228  // MemberExpr should know their using declaration.
1229  {R"cpp(
1230  struct X { void func(int); };
1231  struct Y : X {
1232  using X::func;
1233  };
1234  void foo(Y y) {
1235  $0^y.$1^func(1);
1236  }
1237  )cpp",
1238  "0: targets = {y}\n"
1239  "1: targets = {Y::func}\n"},
1240  // DeclRefExpr should know their using declaration.
1241  {R"cpp(
1242  namespace ns { void bar(int); }
1243  using ns::bar;
1244 
1245  void foo() {
1246  $0^bar(10);
1247  }
1248  )cpp",
1249  "0: targets = {bar}\n"},
1250  // References from a macro.
1251  {R"cpp(
1252  #define FOO a
1253  #define BAR b
1254 
1255  void foo(int a, int b) {
1256  $0^FOO+$1^BAR;
1257  }
1258  )cpp",
1259  "0: targets = {a}\n"
1260  "1: targets = {b}\n"},
1261  // No references from implicit nodes.
1262  {R"cpp(
1263  struct vector {
1264  int *begin();
1265  int *end();
1266  };
1267 
1268  void foo() {
1269  for (int $0^x : $1^vector()) {
1270  $2^x = 10;
1271  }
1272  }
1273  )cpp",
1274  "0: targets = {x}, decl\n"
1275  "1: targets = {vector}\n"
1276  "2: targets = {x}\n"},
1277 // Handle UnresolvedLookupExpr.
1278 // FIXME
1279 // This case fails when expensive checks are enabled.
1280 // Seems like the order of ns1::func and ns2::func isn't defined.
1281 #ifndef EXPENSIVE_CHECKS
1282  {R"cpp(
1283  namespace ns1 { void func(char*); }
1284  namespace ns2 { void func(int*); }
1285  using namespace ns1;
1286  using namespace ns2;
1287 
1288  template <class T>
1289  void foo(T t) {
1290  $0^func($1^t);
1291  }
1292  )cpp",
1293  "0: targets = {ns1::func, ns2::func}\n"
1294  "1: targets = {t}\n"},
1295 #endif
1296  // Handle UnresolvedMemberExpr.
1297  {R"cpp(
1298  struct X {
1299  void func(char*);
1300  void func(int*);
1301  };
1302 
1303  template <class T>
1304  void foo(X x, T t) {
1305  $0^x.$1^func($2^t);
1306  }
1307  )cpp",
1308  "0: targets = {x}\n"
1309  "1: targets = {X::func, X::func}\n"
1310  "2: targets = {t}\n"},
1311  // Handle DependentScopeDeclRefExpr.
1312  {R"cpp(
1313  template <class T>
1314  struct S {
1315  static int value;
1316  };
1317 
1318  template <class T>
1319  void foo() {
1320  $0^S<$1^T>::$2^value;
1321  }
1322  )cpp",
1323  "0: targets = {S}\n"
1324  "1: targets = {T}\n"
1325  "2: targets = {S::value}, qualifier = 'S<T>::'\n"},
1326  // Handle CXXDependentScopeMemberExpr.
1327  {R"cpp(
1328  template <class T>
1329  struct S {
1330  int value;
1331  };
1332 
1333  template <class T>
1334  void foo(S<T> t) {
1335  $0^t.$1^value;
1336  }
1337  )cpp",
1338  "0: targets = {t}\n"
1339  "1: targets = {S::value}\n"},
1340  // Type template parameters.
1341  {R"cpp(
1342  template <class T>
1343  void foo() {
1344  static_cast<$0^T>(0);
1345  $1^T();
1346  $2^T $3^t;
1347  }
1348  )cpp",
1349  "0: targets = {T}\n"
1350  "1: targets = {T}\n"
1351  "2: targets = {T}\n"
1352  "3: targets = {t}, decl\n"},
1353  // Non-type template parameters.
1354  {R"cpp(
1355  template <int I>
1356  void foo() {
1357  int $0^x = $1^I;
1358  }
1359  )cpp",
1360  "0: targets = {x}, decl\n"
1361  "1: targets = {I}\n"},
1362  // Template template parameters.
1363  {R"cpp(
1364  template <class T> struct vector {};
1365 
1366  template <template<class> class TT, template<class> class ...TP>
1367  void foo() {
1368  $0^TT<int> $1^x;
1369  $2^foo<$3^TT>();
1370  $4^foo<$5^vector>();
1371  $6^foo<$7^TP...>();
1372  }
1373  )cpp",
1374  "0: targets = {TT}\n"
1375  "1: targets = {x}, decl\n"
1376  "2: targets = {foo}\n"
1377  "3: targets = {TT}\n"
1378  "4: targets = {foo}\n"
1379  "5: targets = {vector}\n"
1380  "6: targets = {foo}\n"
1381  "7: targets = {TP}\n"},
1382  // Non-type template parameters with declarations.
1383  {R"cpp(
1384  int func();
1385  template <int(*)()> struct wrapper {};
1386 
1387  template <int(*FuncParam)()>
1388  void foo() {
1389  $0^wrapper<$1^func> $2^w;
1390  $3^FuncParam();
1391  }
1392  )cpp",
1393  "0: targets = {wrapper<&func>}\n"
1394  "1: targets = {func}\n"
1395  "2: targets = {w}, decl\n"
1396  "3: targets = {FuncParam}\n"},
1397  // declaration references.
1398  {R"cpp(
1399  namespace ns {}
1400  class S {};
1401  void foo() {
1402  class $0^Foo { $1^Foo(); ~$2^Foo(); int $3^field; };
1403  int $4^Var;
1404  enum $5^E { $6^ABC };
1405  typedef int $7^INT;
1406  using $8^INT2 = int;
1407  namespace $9^NS = $10^ns;
1408  }
1409  )cpp",
1410  "0: targets = {Foo}, decl\n"
1411  "1: targets = {foo()::Foo::Foo}, decl\n"
1412  "2: targets = {Foo}\n"
1413  "3: targets = {foo()::Foo::field}, decl\n"
1414  "4: targets = {Var}, decl\n"
1415  "5: targets = {E}, decl\n"
1416  "6: targets = {foo()::ABC}, decl\n"
1417  "7: targets = {INT}, decl\n"
1418  "8: targets = {INT2}, decl\n"
1419  "9: targets = {NS}, decl\n"
1420  "10: targets = {ns}\n"},
1421  // User-defined conversion operator.
1422  {R"cpp(
1423  void foo() {
1424  class $0^Bar {};
1425  class $1^Foo {
1426  public:
1427  // FIXME: This should have only one reference to Bar.
1428  $2^operator $3^$4^Bar();
1429  };
1430 
1431  $5^Foo $6^f;
1432  $7^f.$8^operator $9^Bar();
1433  }
1434  )cpp",
1435  "0: targets = {Bar}, decl\n"
1436  "1: targets = {Foo}, decl\n"
1437  "2: targets = {foo()::Foo::operator Bar}, decl\n"
1438  "3: targets = {Bar}\n"
1439  "4: targets = {Bar}\n"
1440  "5: targets = {Foo}\n"
1441  "6: targets = {f}, decl\n"
1442  "7: targets = {f}\n"
1443  "8: targets = {foo()::Foo::operator Bar}\n"
1444  "9: targets = {Bar}\n"},
1445  // Destructor.
1446  {R"cpp(
1447  void foo() {
1448  class $0^Foo {
1449  public:
1450  ~$1^Foo() {}
1451 
1452  void $2^destructMe() {
1453  this->~$3^Foo();
1454  }
1455  };
1456 
1457  $4^Foo $5^f;
1458  $6^f.~ /*...*/ $7^Foo();
1459  }
1460  )cpp",
1461  "0: targets = {Foo}, decl\n"
1462  // FIXME: It's better to target destructor's FunctionDecl instead of
1463  // the type itself (similar to constructor).
1464  "1: targets = {Foo}\n"
1465  "2: targets = {foo()::Foo::destructMe}, decl\n"
1466  "3: targets = {Foo}\n"
1467  "4: targets = {Foo}\n"
1468  "5: targets = {f}, decl\n"
1469  "6: targets = {f}\n"
1470  "7: targets = {Foo}\n"},
1471  // cxx constructor initializer.
1472  {R"cpp(
1473  class Base {};
1474  void foo() {
1475  // member initializer
1476  class $0^X {
1477  int $1^abc;
1478  $2^X(): $3^abc() {}
1479  };
1480  // base initializer
1481  class $4^Derived : public $5^Base {
1482  $6^Base $7^B;
1483  $8^Derived() : $9^Base() {}
1484  };
1485  // delegating initializer
1486  class $10^Foo {
1487  $11^Foo(int);
1488  $12^Foo(): $13^Foo(111) {}
1489  };
1490  }
1491  )cpp",
1492  "0: targets = {X}, decl\n"
1493  "1: targets = {foo()::X::abc}, decl\n"
1494  "2: targets = {foo()::X::X}, decl\n"
1495  "3: targets = {foo()::X::abc}\n"
1496  "4: targets = {Derived}, decl\n"
1497  "5: targets = {Base}\n"
1498  "6: targets = {Base}\n"
1499  "7: targets = {foo()::Derived::B}, decl\n"
1500  "8: targets = {foo()::Derived::Derived}, decl\n"
1501  "9: targets = {Base}\n"
1502  "10: targets = {Foo}, decl\n"
1503  "11: targets = {foo()::Foo::Foo}, decl\n"
1504  "12: targets = {foo()::Foo::Foo}, decl\n"
1505  "13: targets = {Foo}\n"},
1506  // Anonymous entities should not be reported.
1507  {
1508  R"cpp(
1509  void foo() {
1510  class {} $0^x;
1511  int (*$1^fptr)(int $2^a, int) = nullptr;
1512  }
1513  )cpp",
1514  "0: targets = {x}, decl\n"
1515  "1: targets = {fptr}, decl\n"
1516  "2: targets = {a}, decl\n"},
1517  // Namespace aliases should be handled properly.
1518  {
1519  R"cpp(
1520  namespace ns { struct Type {}; }
1521  namespace alias = ns;
1522  namespace rec_alias = alias;
1523 
1524  void foo() {
1525  $0^ns::$1^Type $2^a;
1526  $3^alias::$4^Type $5^b;
1527  $6^rec_alias::$7^Type $8^c;
1528  }
1529  )cpp",
1530  "0: targets = {ns}\n"
1531  "1: targets = {ns::Type}, qualifier = 'ns::'\n"
1532  "2: targets = {a}, decl\n"
1533  "3: targets = {alias}\n"
1534  "4: targets = {ns::Type}, qualifier = 'alias::'\n"
1535  "5: targets = {b}, decl\n"
1536  "6: targets = {rec_alias}\n"
1537  "7: targets = {ns::Type}, qualifier = 'rec_alias::'\n"
1538  "8: targets = {c}, decl\n"},
1539  // Handle SizeOfPackExpr.
1540  {
1541  R"cpp(
1542  template <typename... E>
1543  void foo() {
1544  constexpr int $0^size = sizeof...($1^E);
1545  };
1546  )cpp",
1547  "0: targets = {size}, decl\n"
1548  "1: targets = {E}\n"},
1549  // Class template argument deduction
1550  {
1551  R"cpp(
1552  template <typename T>
1553  struct Test {
1554  Test(T);
1555  };
1556  void foo() {
1557  $0^Test $1^a(5);
1558  }
1559  )cpp",
1560  "0: targets = {Test}\n"
1561  "1: targets = {a}, decl\n"},
1562  // Templates
1563  {R"cpp(
1564  namespace foo {
1565  template <typename $0^T>
1566  class $1^Bar {};
1567  }
1568  )cpp",
1569  "0: targets = {foo::Bar::T}, decl\n"
1570  "1: targets = {foo::Bar}, decl\n"},
1571  // Templates
1572  {R"cpp(
1573  namespace foo {
1574  template <typename $0^T>
1575  void $1^func();
1576  }
1577  )cpp",
1578  "0: targets = {T}, decl\n"
1579  "1: targets = {foo::func}, decl\n"},
1580  // Templates
1581  {R"cpp(
1582  namespace foo {
1583  template <typename $0^T>
1584  $1^T $2^x;
1585  }
1586  )cpp",
1587  "0: targets = {foo::T}, decl\n"
1588  "1: targets = {foo::T}\n"
1589  "2: targets = {foo::x}, decl\n"},
1590  // Templates
1591  {R"cpp(
1592  template<typename T> class vector {};
1593  namespace foo {
1594  template <typename $0^T>
1595  using $1^V = $2^vector<$3^T>;
1596  }
1597  )cpp",
1598  "0: targets = {foo::T}, decl\n"
1599  "1: targets = {foo::V}, decl\n"
1600  "2: targets = {vector}\n"
1601  "3: targets = {foo::T}\n"},
1602  // Concept
1603  {
1604  R"cpp(
1605  template <typename T>
1606  concept Drawable = requires (T t) { t.draw(); };
1607 
1608  namespace foo {
1609  template <typename $0^T> requires $1^Drawable<$2^T>
1610  void $3^bar($4^T $5^t) {
1611  $6^t.$7^draw();
1612  }
1613  }
1614  )cpp",
1615  "0: targets = {T}, decl\n"
1616  "1: targets = {Drawable}\n"
1617  "2: targets = {T}\n"
1618  "3: targets = {foo::bar}, decl\n"
1619  "4: targets = {T}\n"
1620  "5: targets = {t}, decl\n"
1621  "6: targets = {t}\n"
1622  "7: targets = {}\n"},
1623  // Objective-C: instance variables
1624  {
1625  R"cpp(
1626  @interface I {
1627  @public
1628  I *_z;
1629  }
1630  @end
1631  I *f;
1632  void foo() {
1633  $0^f->$1^_z = 0;
1634  }
1635  )cpp",
1636  "0: targets = {f}\n"
1637  "1: targets = {I::_z}\n"},
1638  // Objective-C: properties
1639  {
1640  R"cpp(
1641  @interface I {}
1642  @property(retain) I* x;
1643  @property(retain) I* y;
1644  @end
1645  I *f;
1646  void foo() {
1647  $0^f.$1^x.$2^y = 0;
1648  }
1649  )cpp",
1650  "0: targets = {f}\n"
1651  "1: targets = {I::x}\n"
1652  "2: targets = {I::y}\n"},
1653  // Objective-C: implicit properties
1654  {
1655  R"cpp(
1656  @interface I {}
1657  -(I*)x;
1658  -(void)setY:(I*)y;
1659  @end
1660  I *f;
1661  void foo() {
1662  $0^f.$1^x.$2^y = 0;
1663  }
1664  )cpp",
1665  "0: targets = {f}\n"
1666  "1: targets = {I::x}\n"
1667  "2: targets = {I::setY:}\n"},
1668  // Objective-C: class properties
1669  {
1670  R"cpp(
1671  @interface I {}
1672  @property(class) I *x;
1673  @end
1674  id local;
1675  void foo() {
1676  $0^I.$1^x = 0;
1677  $2^local = $3^I.$4^x;
1678  }
1679  )cpp",
1680  "0: targets = {I}\n"
1681  "1: targets = {I::setX:}\n"
1682  "2: targets = {local}\n"
1683  "3: targets = {I}\n"
1684  "4: targets = {I::x}\n"},
1685  // Objective-C: implicit class properties
1686  {
1687  R"cpp(
1688  @interface I {}
1689  +(I*)x;
1690  +(void)setX:(I*)x;
1691  @end
1692  id local;
1693  void foo() {
1694  $0^I.$1^x = 0;
1695  $2^local = $3^I.$4^x;
1696  }
1697  )cpp",
1698  "0: targets = {I}\n"
1699  "1: targets = {I::setX:}\n"
1700  "2: targets = {local}\n"
1701  "3: targets = {I}\n"
1702  "4: targets = {I::x}\n"},
1703  {// Objective-C: methods
1704  R"cpp(
1705  @interface I
1706  -(void) a:(int)x b:(int)y;
1707  @end
1708  void foo(I *i) {
1709  [$0^i $1^a:1 b:2];
1710  }
1711  )cpp",
1712  "0: targets = {i}\n"
1713  "1: targets = {I::a:b:}\n"},
1714  {// Objective-C: protocols
1715  R"cpp(
1716  @interface I
1717  @end
1718  @protocol P
1719  @end
1720  void foo() {
1721  $0^I<$1^P> *$2^x;
1722  }
1723  )cpp",
1724  "0: targets = {I}\n"
1725  "1: targets = {P}\n"
1726  "2: targets = {x}, decl\n"},
1727 
1728  // Designated initializers.
1729  {R"cpp(
1730  void foo() {
1731  struct $0^Foo {
1732  int $1^Bar;
1733  };
1734  $2^Foo $3^f { .$4^Bar = 42 };
1735  }
1736  )cpp",
1737  "0: targets = {Foo}, decl\n"
1738  "1: targets = {foo()::Foo::Bar}, decl\n"
1739  "2: targets = {Foo}\n"
1740  "3: targets = {f}, decl\n"
1741  "4: targets = {foo()::Foo::Bar}\n"},
1742  {R"cpp(
1743  void foo() {
1744  struct $0^Baz {
1745  int $1^Field;
1746  };
1747  struct $2^Bar {
1748  $3^Baz $4^Foo;
1749  };
1750  $5^Bar $6^bar { .$7^Foo.$8^Field = 42 };
1751  }
1752  )cpp",
1753  "0: targets = {Baz}, decl\n"
1754  "1: targets = {foo()::Baz::Field}, decl\n"
1755  "2: targets = {Bar}, decl\n"
1756  "3: targets = {Baz}\n"
1757  "4: targets = {foo()::Bar::Foo}, decl\n"
1758  "5: targets = {Bar}\n"
1759  "6: targets = {bar}, decl\n"
1760  "7: targets = {foo()::Bar::Foo}\n"
1761  "8: targets = {foo()::Baz::Field}\n"},
1762  {R"cpp(
1763  template<typename T>
1764  void crash(T);
1765  template<typename T>
1766  void foo() {
1767  $0^crash({.$1^x = $2^T()});
1768  }
1769  )cpp",
1770  "0: targets = {crash}\n"
1771  "1: targets = {}\n"
1772  "2: targets = {T}\n"},
1773  // unknown template name should not crash.
1774  {R"cpp(
1775  template <template <typename> typename T>
1776  struct Base {};
1777  namespace foo {
1778  template <typename $0^T>
1779  struct $1^Derive : $2^Base<$3^T::template $4^Unknown> {};
1780  }
1781  )cpp",
1782  "0: targets = {foo::Derive::T}, decl\n"
1783  "1: targets = {foo::Derive}, decl\n"
1784  "2: targets = {Base}\n"
1785  "3: targets = {foo::Derive::T}\n"
1786  "4: targets = {}, qualifier = 'T::'\n"},
1787  // deduction guide
1788  {R"cpp(
1789  namespace foo {
1790  template <typename $0^T>
1791  struct $1^Test {
1792  template <typename $2^I>
1793  $3^Test($4^I);
1794  };
1795  template <typename $5^I>
1796  $6^Test($7^I) -> $8^Test<typename $9^I::$10^type>;
1797  }
1798  )cpp",
1799  "0: targets = {T}, decl\n"
1800  "1: targets = {foo::Test}, decl\n"
1801  "2: targets = {I}, decl\n"
1802  "3: targets = {foo::Test::Test<T>}, decl\n"
1803  "4: targets = {I}\n"
1804  "5: targets = {I}, decl\n"
1805  "6: targets = {foo::Test}\n"
1806  "7: targets = {I}\n"
1807  "8: targets = {foo::Test}\n"
1808  "9: targets = {I}\n"
1809  "10: targets = {}, qualifier = 'I::'\n"}};
1810 
1811  for (const auto &C : Cases) {
1812  llvm::StringRef ExpectedCode = C.first;
1813  llvm::StringRef ExpectedRefs = C.second;
1814 
1815  auto Actual =
1816  annotateReferencesInFoo(llvm::Annotations(ExpectedCode).code());
1817  EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
1818  EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
1819  }
1820 }
1821 
1822 } // namespace
1823 } // namespace clangd
1824 } // 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:1017
Offset
size_t Offset
Definition: CodeComplete.cpp:1103
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:844
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:2013
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:1018
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:520
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:1114