[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / clangd / unittests / FindTargetTests.cpp
blob19e80658de063ce86a2d5cb7e2f5ceda9fcc5fe1
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"
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/Annotations/Annotations.h"
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include <initializer_list>
23 namespace clang {
24 namespace clangd {
25 namespace {
27 // A referenced Decl together with its DeclRelationSet, for assertions.
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).
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(" {"));
48 std::string Name;
49 DeclRelationSet Relations;
51 bool operator==(const PrintedDecl &L, const PrintedDecl &R) {
52 return std::tie(L.Name, L.Relations) == std::tie(R.Name, R.Relations);
54 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const PrintedDecl &D) {
55 return OS << D.Name << " Rel=" << D.Relations;
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;
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 {};
86 EXPECT_EQ(N->kind(), NodeType) << Selection;
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;
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>;
105 TEST_F(TargetDeclTest, Exprs) {
106 Code = R"cpp(
107 int f();
108 int x = [[f]]();
109 )cpp";
110 EXPECT_DECLS("DeclRefExpr", "int f()");
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");
118 Code = R"cpp(
119 int foo();
120 int s = foo[[()]];
121 )cpp";
122 EXPECT_DECLS("CallExpr", "int foo()");
124 Code = R"cpp(
125 struct X {
126 void operator()(int n);
128 void test() {
129 X x;
130 x[[(123)]];
132 )cpp";
133 EXPECT_DECLS("CXXOperatorCallExpr", "void operator()(int n)");
135 Code = R"cpp(
136 void test() {
137 goto [[label]];
138 label:
139 return;
141 )cpp";
142 EXPECT_DECLS("GotoStmt", "label:");
143 Code = R"cpp(
144 void test() {
145 [[label]]:
146 return;
148 )cpp";
149 EXPECT_DECLS("LabelStmt", "label:");
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)");
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)");
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]];
182 )cpp";
183 EXPECT_DECLS("MemberExpr", "int member");
186 TEST_F(TargetDeclTest, UsingDecl) {
187 Code = R"cpp(
188 namespace foo {
189 int f(int);
190 int f(char);
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)"});
198 Code = R"cpp(
199 namespace foo {
200 int f(int);
201 int f(char);
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)"});
209 Code = R"cpp(
210 struct X {
211 int foo();
213 struct Y : X {
214 using X::foo;
216 int x = Y().[[foo]]();
217 )cpp";
218 EXPECT_DECLS("MemberExpr", {"using X::foo", Rel::Alias}, {"int foo()"});
220 Code = R"cpp(
221 template <typename T>
222 struct Base {
223 void waldo() {}
225 template <typename T>
226 struct Derived : Base<T> {
227 using Base<T>::[[waldo]];
229 )cpp";
230 EXPECT_DECLS("UnresolvedUsingValueDecl", {"using Base<T>::waldo", Rel::Alias},
231 {"void waldo()"});
233 Code = R"cpp(
234 namespace ns {
235 template<typename T> class S {};
238 using ns::S;
240 template<typename T>
241 using A = [[S]]<T>;
242 )cpp";
243 EXPECT_DECLS("TemplateSpecializationTypeLoc", {"using ns::S", Rel::Alias},
244 {"template <typename T> class S"},
245 {"class S", Rel::TemplatePattern});
247 Code = R"cpp(
248 namespace ns {
249 template<typename T> class S {};
252 using ns::S;
253 template <template <typename> class T> class X {};
254 using B = X<[[S]]>;
255 )cpp";
256 EXPECT_DECLS("TemplateArgumentLoc", {"using ns::S", Rel::Alias},
257 {"template <typename T> class S"});
259 Code = R"cpp(
260 namespace ns {
261 template<typename T> class S { public: S(T); };
264 using ns::S;
265 [[S]] s(123);
266 )cpp";
267 Flags.push_back("-std=c++17"); // For CTAD feature.
268 EXPECT_DECLS("DeducedTemplateSpecializationTypeLoc",
269 {"using ns::S", Rel::Alias}, {"template <typename T> class S"},
270 {"class S", Rel::TemplatePattern});
272 Code = R"cpp(
273 template<typename T>
274 class Foo { public: class foo {}; };
275 template <class T> class A : public Foo<T> {
276 using typename Foo<T>::foo;
277 [[foo]] abc;
279 )cpp";
280 EXPECT_DECLS("UnresolvedUsingTypeLoc",
281 {"using typename Foo<T>::foo", Rel::Alias});
283 // Using enum.
284 Flags.push_back("-std=c++20");
285 Code = R"cpp(
286 namespace ns { enum class A { X }; }
287 [[using enum ns::A]];
288 )cpp";
289 EXPECT_DECLS("UsingEnumDecl", "enum class A : int");
291 Code = R"cpp(
292 namespace ns { enum class A { X }; }
293 using enum ns::A;
294 auto m = [[X]];
295 )cpp";
296 EXPECT_DECLS("DeclRefExpr", "X");
299 TEST_F(TargetDeclTest, BaseSpecifier) {
300 Code = R"cpp(
301 struct X {};
302 struct Y : [[private]] X {};
303 )cpp";
304 EXPECT_DECLS("CXXBaseSpecifier", "struct X");
305 Code = R"cpp(
306 struct X {};
307 struct Y : [[private X]] {};
308 )cpp";
309 EXPECT_DECLS("CXXBaseSpecifier", "struct X");
310 Code = R"cpp(
311 struct X {};
312 struct Y : private [[X]] {};
313 )cpp";
314 EXPECT_DECLS("RecordTypeLoc", "struct X");
317 TEST_F(TargetDeclTest, ConstructorInitList) {
318 Code = R"cpp(
319 struct X {
320 int a;
321 X() : [[a]](42) {}
323 )cpp";
324 EXPECT_DECLS("CXXCtorInitializer", "int a");
326 Code = R"cpp(
327 struct X {
328 X() : [[X]](1) {}
329 X(int);
331 )cpp";
332 EXPECT_DECLS("RecordTypeLoc", "struct X");
335 TEST_F(TargetDeclTest, DesignatedInit) {
336 Flags = {"-xc"}; // array designators are a C99 extension.
337 Code = R"c(
338 struct X { int a; };
339 struct Y { int b; struct X c[2]; };
340 struct Y y = { .c[0].[[a]] = 1 };
341 )c";
342 EXPECT_DECLS("DesignatedInitExpr", "int a");
345 TEST_F(TargetDeclTest, NestedNameSpecifier) {
346 Code = R"cpp(
347 namespace a { namespace b { int c; } }
348 int x = a::[[b::]]c;
349 )cpp";
350 EXPECT_DECLS("NestedNameSpecifierLoc", "namespace b");
352 Code = R"cpp(
353 namespace a { struct X { enum { y }; }; }
354 int x = a::[[X::]]y;
355 )cpp";
356 EXPECT_DECLS("NestedNameSpecifierLoc", "struct X");
358 Code = R"cpp(
359 template <typename T>
360 int x = [[T::]]y;
361 )cpp";
362 EXPECT_DECLS("NestedNameSpecifierLoc", "typename T");
364 Code = R"cpp(
365 namespace a { int x; }
366 namespace b = a;
367 int y = [[b]]::x;
368 )cpp";
369 EXPECT_DECLS("NestedNameSpecifierLoc", {"namespace b = a", Rel::Alias},
370 {"namespace a", Rel::Underlying});
373 TEST_F(TargetDeclTest, Types) {
374 Code = R"cpp(
375 struct X{};
376 [[X]] x;
377 )cpp";
378 EXPECT_DECLS("RecordTypeLoc", "struct X");
380 Code = R"cpp(
381 struct S{};
382 typedef S X;
383 [[X]] x;
384 )cpp";
385 EXPECT_DECLS("TypedefTypeLoc", {"typedef S X", Rel::Alias},
386 {"struct S", Rel::Underlying});
387 Code = R"cpp(
388 namespace ns { struct S{}; }
389 typedef ns::S X;
390 [[X]] x;
391 )cpp";
392 EXPECT_DECLS("TypedefTypeLoc", {"typedef ns::S X", Rel::Alias},
393 {"struct S", Rel::Underlying});
395 Code = R"cpp(
396 template<class T>
397 void foo() { [[T]] x; }
398 )cpp";
399 EXPECT_DECLS("TemplateTypeParmTypeLoc", "class T");
400 Flags.clear();
402 Code = R"cpp(
403 template<template<typename> class T>
404 void foo() { [[T<int>]] x; }
405 )cpp";
406 EXPECT_DECLS("TemplateSpecializationTypeLoc", "template <typename> class T");
407 Flags.clear();
409 Code = R"cpp(
410 template<template<typename> class ...T>
411 class C {
412 C<[[T...]]> foo;
414 )cpp";
415 EXPECT_DECLS("TemplateArgumentLoc", {"template <typename> class ...T"});
416 Flags.clear();
418 Code = R"cpp(
419 struct S{};
420 S X;
421 [[decltype]](X) Y;
422 )cpp";
423 EXPECT_DECLS("DecltypeTypeLoc", {"struct S", Rel::Underlying});
425 Code = R"cpp(
426 struct S{};
427 [[auto]] X = S{};
428 )cpp";
429 // FIXME: deduced type missing in AST. https://llvm.org/PR42914
430 EXPECT_DECLS("AutoTypeLoc");
432 Code = R"cpp(
433 template <typename... E>
434 struct S {
435 static const int size = sizeof...([[E]]);
437 )cpp";
438 EXPECT_DECLS("SizeOfPackExpr", "typename ...E");
440 Code = R"cpp(
441 template <typename T>
442 class Foo {
443 void f([[Foo]] x);
445 )cpp";
446 EXPECT_DECLS("InjectedClassNameTypeLoc", "class Foo");
449 TEST_F(TargetDeclTest, ClassTemplate) {
450 Code = R"cpp(
451 // Implicit specialization.
452 template<int x> class Foo{};
453 [[Foo<42>]] B;
454 )cpp";
455 EXPECT_DECLS("TemplateSpecializationTypeLoc",
456 {"template<> class Foo<42>", Rel::TemplateInstantiation},
457 {"class Foo", Rel::TemplatePattern});
459 Code = R"cpp(
460 template<typename T> class Foo {};
461 // The "Foo<int>" SpecializationDecl is incomplete, there is no
462 // instantiation happening.
463 void func([[Foo<int>]] *);
464 )cpp";
465 EXPECT_DECLS("TemplateSpecializationTypeLoc",
466 {"class Foo", Rel::TemplatePattern},
467 {"template<> class Foo<int>", Rel::TemplateInstantiation});
469 Code = R"cpp(
470 // Explicit specialization.
471 template<int x> class Foo{};
472 template<> class Foo<42>{};
473 [[Foo<42>]] B;
474 )cpp";
475 EXPECT_DECLS("TemplateSpecializationTypeLoc", "template<> class Foo<42>");
477 Code = R"cpp(
478 // Partial specialization.
479 template<typename T> class Foo{};
480 template<typename T> class Foo<T*>{};
481 [[Foo<int*>]] B;
482 )cpp";
483 EXPECT_DECLS("TemplateSpecializationTypeLoc",
484 {"template<> class Foo<int *>", Rel::TemplateInstantiation},
485 {"template <typename T> class Foo<T *>", Rel::TemplatePattern});
487 Code = R"cpp(
488 // Template template argument.
489 template<typename T> struct Vector {};
490 template <template <typename> class Container>
491 struct A {};
492 A<[[Vector]]> a;
493 )cpp";
494 EXPECT_DECLS("TemplateArgumentLoc", {"template <typename T> struct Vector"});
496 Flags.push_back("-std=c++17"); // for CTAD tests
498 Code = R"cpp(
499 // Class template argument deduction
500 template <typename T>
501 struct Test {
502 Test(T);
504 void foo() {
505 [[Test]] a(5);
507 )cpp";
508 EXPECT_DECLS("DeducedTemplateSpecializationTypeLoc",
509 {"struct Test", Rel::TemplatePattern});
511 Code = R"cpp(
512 // Deduction guide
513 template <typename T>
514 struct Test {
515 template <typename I>
516 Test(I, I);
518 template <typename I>
519 [[Test]](I, I) -> Test<typename I::type>;
520 )cpp";
521 EXPECT_DECLS("CXXDeductionGuideDecl", {"template <typename T> struct Test"});
524 TEST_F(TargetDeclTest, Concept) {
525 Flags.push_back("-std=c++20");
527 // FIXME: Should we truncate the pretty-printed form of a concept decl
528 // somewhere?
530 Code = R"cpp(
531 template <typename T>
532 concept Fooable = requires (T t) { t.foo(); };
534 template <typename T> requires [[Fooable]]<T>
535 void bar(T t) {
536 t.foo();
538 )cpp";
539 EXPECT_DECLS(
540 "ConceptSpecializationExpr",
541 {"template <typename T> concept Fooable = requires (T t) { t.foo(); }"});
543 // trailing requires clause
544 Code = R"cpp(
545 template <typename T>
546 concept Fooable = true;
548 template <typename T>
549 void foo() requires [[Fooable]]<T>;
550 )cpp";
551 EXPECT_DECLS("ConceptSpecializationExpr",
552 {"template <typename T> concept Fooable = true"});
554 // constrained-parameter
555 Code = R"cpp(
556 template <typename T>
557 concept Fooable = true;
559 template <[[Fooable]] T>
560 void bar(T t);
561 )cpp";
562 EXPECT_DECLS("ConceptSpecializationExpr",
563 {"template <typename T> concept Fooable = true"});
565 // partial-concept-id
566 Code = R"cpp(
567 template <typename T, typename U>
568 concept Fooable = true;
570 template <[[Fooable]]<int> T>
571 void bar(T t);
572 )cpp";
573 EXPECT_DECLS("ConceptSpecializationExpr",
574 {"template <typename T, typename U> concept Fooable = true"});
577 TEST_F(TargetDeclTest, Coroutine) {
578 Flags.push_back("-std=c++20");
580 Code = R"cpp(
581 namespace std {
582 template <typename, typename...> struct coroutine_traits;
583 template <typename> struct coroutine_handle {
584 template <typename U>
585 coroutine_handle(coroutine_handle<U>&&) noexcept;
586 static coroutine_handle from_address(void* __addr) noexcept;
588 } // namespace std
590 struct executor {};
591 struct awaitable {};
592 struct awaitable_frame {
593 awaitable get_return_object();
594 void return_void();
595 void unhandled_exception();
596 struct result_t {
597 ~result_t();
598 bool await_ready() const noexcept;
599 void await_suspend(std::coroutine_handle<void>) noexcept;
600 void await_resume() const noexcept;
602 result_t initial_suspend() noexcept;
603 result_t final_suspend() noexcept;
604 result_t await_transform(executor) noexcept;
607 namespace std {
608 template <>
609 struct coroutine_traits<awaitable> {
610 typedef awaitable_frame promise_type;
612 } // namespace std
614 awaitable foo() {
615 co_await [[executor]]();
617 )cpp";
618 EXPECT_DECLS("RecordTypeLoc", "struct executor");
621 TEST_F(TargetDeclTest, RewrittenBinaryOperator) {
622 Flags.push_back("-std=c++20");
624 Code = R"cpp(
625 namespace std {
626 struct strong_ordering {
627 int n;
628 constexpr operator int() const { return n; }
629 static const strong_ordering equal, greater, less;
631 constexpr strong_ordering strong_ordering::equal = {0};
632 constexpr strong_ordering strong_ordering::greater = {1};
633 constexpr strong_ordering strong_ordering::less = {-1};
636 struct Foo
638 int x;
639 auto operator<=>(const Foo&) const = default;
642 bool x = (Foo(1) [[!=]] Foo(2));
643 )cpp";
644 EXPECT_DECLS("CXXRewrittenBinaryOperator",
645 {"std::strong_ordering operator<=>(const Foo &) const = default",
646 Rel::TemplatePattern},
647 {"bool operator==(const Foo &) const noexcept = default",
648 Rel::TemplateInstantiation});
651 TEST_F(TargetDeclTest, FunctionTemplate) {
652 Code = R"cpp(
653 // Implicit specialization.
654 template<typename T> bool foo(T) { return false; };
655 bool x = [[foo]](42);
656 )cpp";
657 EXPECT_DECLS("DeclRefExpr",
658 {"template<> bool foo<int>(int)", Rel::TemplateInstantiation},
659 {"bool foo(T)", Rel::TemplatePattern});
661 Code = R"cpp(
662 // Explicit specialization.
663 template<typename T> bool foo(T) { return false; };
664 template<> bool foo<int>(int) { return false; };
665 bool x = [[foo]](42);
666 )cpp";
667 EXPECT_DECLS("DeclRefExpr", "template<> bool foo<int>(int)");
670 TEST_F(TargetDeclTest, VariableTemplate) {
671 // Pretty-printer doesn't do a very good job of variable templates :-(
672 Code = R"cpp(
673 // Implicit specialization.
674 template<typename T> int foo;
675 int x = [[foo]]<char>;
676 )cpp";
677 EXPECT_DECLS("DeclRefExpr", {"int foo", Rel::TemplateInstantiation},
678 {"int foo", Rel::TemplatePattern});
680 Code = R"cpp(
681 // Explicit specialization.
682 template<typename T> int foo;
683 template <> bool foo<char>;
684 int x = [[foo]]<char>;
685 )cpp";
686 EXPECT_DECLS("DeclRefExpr", "bool foo");
688 Code = R"cpp(
689 // Partial specialization.
690 template<typename T> int foo;
691 template<typename T> bool foo<T*>;
692 bool x = [[foo]]<char*>;
693 )cpp";
694 EXPECT_DECLS("DeclRefExpr", {"bool foo", Rel::TemplateInstantiation},
695 {"bool foo", Rel::TemplatePattern});
698 TEST_F(TargetDeclTest, TypeAliasTemplate) {
699 Code = R"cpp(
700 template<typename T, int X> class SmallVector {};
701 template<typename U> using TinyVector = SmallVector<U, 1>;
702 [[TinyVector<int>]] X;
703 )cpp";
704 EXPECT_DECLS("TemplateSpecializationTypeLoc",
705 {"template<> class SmallVector<int, 1>",
706 Rel::TemplateInstantiation | Rel::Underlying},
707 {"class SmallVector", Rel::TemplatePattern | Rel::Underlying},
708 {"using TinyVector = SmallVector<U, 1>",
709 Rel::Alias | Rel::TemplatePattern});
712 TEST_F(TargetDeclTest, MemberOfTemplate) {
713 Code = R"cpp(
714 template <typename T> struct Foo {
715 int x(T);
717 int y = Foo<int>().[[x]](42);
718 )cpp";
719 EXPECT_DECLS("MemberExpr", {"int x(int)", Rel::TemplateInstantiation},
720 {"int x(T)", Rel::TemplatePattern});
722 Code = R"cpp(
723 template <typename T> struct Foo {
724 template <typename U>
725 int x(T, U);
727 int y = Foo<char>().[[x]]('c', 42);
728 )cpp";
729 EXPECT_DECLS("MemberExpr",
730 {"template<> int x<int>(char, int)", Rel::TemplateInstantiation},
731 {"int x(T, U)", Rel::TemplatePattern});
734 TEST_F(TargetDeclTest, Lambda) {
735 Code = R"cpp(
736 void foo(int x = 42) {
737 auto l = [ [[x]] ]{ return x + 1; };
739 )cpp";
740 EXPECT_DECLS("DeclRefExpr", "int x = 42");
742 // It seems like this should refer to another var, with the outer param being
743 // an underlying decl. But it doesn't seem to exist.
744 Code = R"cpp(
745 void foo(int x = 42) {
746 auto l = [x]{ return [[x]] + 1; };
748 )cpp";
749 EXPECT_DECLS("DeclRefExpr", "int x = 42");
751 Code = R"cpp(
752 void foo() {
753 auto l = [x = 1]{ return [[x]] + 1; };
755 )cpp";
756 // FIXME: why both auto and int?
757 EXPECT_DECLS("DeclRefExpr", "auto int x = 1");
760 TEST_F(TargetDeclTest, OverloadExpr) {
761 Flags.push_back("--target=x86_64-pc-linux-gnu");
763 Code = R"cpp(
764 void func(int*);
765 void func(char*);
767 template <class T>
768 void foo(T t) {
769 [[func]](t);
771 )cpp";
772 EXPECT_DECLS("UnresolvedLookupExpr", "void func(int *)", "void func(char *)");
774 Code = R"cpp(
775 struct X {
776 void func(int*);
777 void func(char*);
780 template <class T>
781 void foo(X x, T t) {
782 x.[[func]](t);
784 )cpp";
785 EXPECT_DECLS("UnresolvedMemberExpr", "void func(int *)", "void func(char *)");
787 Code = R"cpp(
788 struct X {
789 static void *operator new(unsigned long);
791 auto* k = [[new]] X();
792 )cpp";
793 EXPECT_DECLS("CXXNewExpr", "static void *operator new(unsigned long)");
794 Code = R"cpp(
795 void *operator new(unsigned long);
796 auto* k = [[new]] int();
797 )cpp";
798 EXPECT_DECLS("CXXNewExpr", "void *operator new(unsigned long)");
800 Code = R"cpp(
801 struct X {
802 static void operator delete(void *) noexcept;
804 void k(X* x) {
805 [[delete]] x;
807 )cpp";
808 EXPECT_DECLS("CXXDeleteExpr", "static void operator delete(void *) noexcept");
809 Code = R"cpp(
810 void operator delete(void *) noexcept;
811 void k(int* x) {
812 [[delete]] x;
814 )cpp";
815 EXPECT_DECLS("CXXDeleteExpr", "void operator delete(void *) noexcept");
818 TEST_F(TargetDeclTest, DependentExprs) {
819 // Heuristic resolution of method of dependent field
820 Code = R"cpp(
821 struct A { void foo() {} };
822 template <typename T>
823 struct B {
824 A a;
825 void bar() {
826 this->a.[[foo]]();
829 )cpp";
830 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void foo()");
832 // Similar to above but base expression involves a function call.
833 Code = R"cpp(
834 struct A {
835 void foo() {}
837 struct B {
838 A getA();
840 template <typename T>
841 struct C {
842 B c;
843 void bar() {
844 this->c.getA().[[foo]]();
847 )cpp";
848 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void foo()");
850 // Similar to above but uses a function pointer.
851 Code = R"cpp(
852 struct A {
853 void foo() {}
855 struct B {
856 using FPtr = A(*)();
857 FPtr fptr;
859 template <typename T>
860 struct C {
861 B c;
862 void bar() {
863 this->c.fptr().[[foo]]();
866 )cpp";
867 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void foo()");
869 // Base expression involves a member access into this.
870 Code = R"cpp(
871 struct Bar {
872 int aaaa;
874 template <typename T> struct Foo {
875 Bar func(int);
876 void test() {
877 func(1).[[aaaa]];
880 )cpp";
881 EXPECT_DECLS("CXXDependentScopeMemberExpr", "int aaaa");
883 Code = R"cpp(
884 class Foo {
885 public:
886 static Foo k(int);
887 template <typename T> T convert() const;
889 template <typename T>
890 void test() {
891 Foo::k(T()).template [[convert]]<T>();
893 )cpp";
894 EXPECT_DECLS("CXXDependentScopeMemberExpr",
895 "template <typename T> T convert() const");
897 Code = R"cpp(
898 template <typename T>
899 struct Waldo {
900 void find();
902 template <typename T>
903 using Wally = Waldo<T>;
904 template <typename T>
905 void foo(Wally<T> w) {
906 w.[[find]]();
908 )cpp";
909 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void find()");
911 Code = R"cpp(
912 template <typename T>
913 struct Waldo {
914 void find();
916 template <typename T>
917 struct MetaWaldo {
918 using Type = Waldo<T>;
920 template <typename T>
921 void foo(typename MetaWaldo<T>::Type w) {
922 w.[[find]]();
924 )cpp";
925 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void find()");
927 Code = R"cpp(
928 struct Waldo {
929 void find();
931 template <typename T>
932 using Wally = Waldo;
933 template <typename>
934 struct S : Wally<int> {
935 void Foo() { this->[[find]](); }
937 )cpp";
938 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void find()");
941 TEST_F(TargetDeclTest, DependentTypes) {
942 // Heuristic resolution of dependent type name
943 Code = R"cpp(
944 template <typename>
945 struct A { struct B {}; };
947 template <typename T>
948 void foo(typename A<T>::[[B]]);
949 )cpp";
950 EXPECT_DECLS("DependentNameTypeLoc", "struct B");
952 // Heuristic resolution of dependent type name which doesn't get a TypeLoc
953 Code = R"cpp(
954 template <typename>
955 struct A { struct B { struct C {}; }; };
957 template <typename T>
958 void foo(typename A<T>::[[B]]::C);
959 )cpp";
960 EXPECT_DECLS("NestedNameSpecifierLoc", "struct B");
962 // Heuristic resolution of dependent type name whose qualifier is also
963 // dependent
964 Code = R"cpp(
965 template <typename>
966 struct A { struct B { struct C {}; }; };
968 template <typename T>
969 void foo(typename A<T>::B::[[C]]);
970 )cpp";
971 EXPECT_DECLS("DependentNameTypeLoc", "struct C");
973 // Heuristic resolution of dependent template name
974 Code = R"cpp(
975 template <typename>
976 struct A {
977 template <typename> struct B {};
980 template <typename T>
981 void foo(typename A<T>::template [[B]]<int>);
982 )cpp";
983 EXPECT_DECLS("DependentTemplateSpecializationTypeLoc",
984 "template <typename> struct B");
987 TEST_F(TargetDeclTest, TypedefCascade) {
988 Code = R"cpp(
989 struct C {
990 using type = int;
992 struct B {
993 using type = C::type;
995 struct A {
996 using type = B::type;
998 A::[[type]] waldo;
999 )cpp";
1000 EXPECT_DECLS("TypedefTypeLoc",
1001 {"using type = int", Rel::Alias | Rel::Underlying},
1002 {"using type = C::type", Rel::Alias | Rel::Underlying},
1003 {"using type = B::type", Rel::Alias});
1006 TEST_F(TargetDeclTest, RecursiveTemplate) {
1007 Flags.push_back("-std=c++20"); // the test case uses concepts
1009 Code = R"cpp(
1010 template <typename T>
1011 concept Leaf = false;
1013 template <typename Tree>
1014 struct descend_left {
1015 using type = typename descend_left<typename Tree::left>::[[type]];
1018 template <Leaf Tree>
1019 struct descend_left<Tree> {
1020 using type = typename Tree::value;
1022 )cpp";
1023 EXPECT_DECLS("DependentNameTypeLoc",
1024 {"using type = typename descend_left<typename Tree::left>::type",
1025 Rel::Alias | Rel::Underlying});
1028 TEST_F(TargetDeclTest, ObjC) {
1029 Flags = {"-xobjective-c"};
1030 Code = R"cpp(
1031 @interface Foo {}
1032 -(void)bar;
1033 @end
1034 void test(Foo *f) {
1035 [f [[bar]] ];
1037 )cpp";
1038 EXPECT_DECLS("ObjCMessageExpr", "- (void)bar");
1040 Code = R"cpp(
1041 @interface Foo { @public int bar; }
1042 @end
1043 int test(Foo *f) {
1044 return [[f->bar]];
1046 )cpp";
1047 EXPECT_DECLS("ObjCIvarRefExpr", "int bar");
1049 Code = R"cpp(
1050 @interface Foo {}
1051 -(int) x;
1052 -(void) setX:(int)x;
1053 @end
1054 void test(Foo *f) {
1055 [[f.x]] = 42;
1057 )cpp";
1058 EXPECT_DECLS("ObjCPropertyRefExpr", "- (void)setX:(int)x");
1060 Code = R"cpp(
1061 @interface I {}
1062 @property(retain) I* x;
1063 @property(retain) I* y;
1064 @end
1065 void test(I *f) {
1066 [[f.x]].y = 0;
1068 )cpp";
1069 EXPECT_DECLS("ObjCPropertyRefExpr",
1070 "@property(atomic, retain, readwrite) I *x");
1072 Code = R"cpp(
1073 @interface MYObject
1074 @end
1075 @interface Interface
1076 @property(retain) [[MYObject]] *x;
1077 @end
1078 )cpp";
1079 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface MYObject");
1081 Code = R"cpp(
1082 @interface MYObject2
1083 @end
1084 @interface Interface
1085 @property(retain, nonnull) [[MYObject2]] *x;
1086 @end
1087 )cpp";
1088 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface MYObject2");
1090 Code = R"cpp(
1091 @protocol Foo
1092 @end
1093 id test() {
1094 return [[@protocol(Foo)]];
1096 )cpp";
1097 EXPECT_DECLS("ObjCProtocolExpr", "@protocol Foo");
1099 Code = R"cpp(
1100 @interface Foo
1101 @end
1102 void test([[Foo]] *p);
1103 )cpp";
1104 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface Foo");
1106 Code = R"cpp(// Don't consider implicit interface as the target.
1107 @implementation [[Implicit]]
1108 @end
1109 )cpp";
1110 EXPECT_DECLS("ObjCImplementationDecl", "@implementation Implicit");
1112 Code = R"cpp(
1113 @interface Foo
1114 @end
1115 @implementation [[Foo]]
1116 @end
1117 )cpp";
1118 EXPECT_DECLS("ObjCImplementationDecl", "@interface Foo");
1120 Code = R"cpp(
1121 @interface Foo
1122 @end
1123 @interface Foo (Ext)
1124 @end
1125 @implementation [[Foo]] (Ext)
1126 @end
1127 )cpp";
1128 EXPECT_DECLS("ObjCCategoryImplDecl", "@interface Foo(Ext)");
1130 Code = R"cpp(
1131 @interface Foo
1132 @end
1133 @interface Foo (Ext)
1134 @end
1135 @implementation Foo ([[Ext]])
1136 @end
1137 )cpp";
1138 EXPECT_DECLS("ObjCCategoryImplDecl", "@interface Foo(Ext)");
1140 Code = R"cpp(
1141 void test(id</*error-ok*/[[InvalidProtocol]]> p);
1142 )cpp";
1143 EXPECT_DECLS("ParmVarDecl", "id p");
1145 Code = R"cpp(
1146 @class C;
1147 @protocol Foo
1148 @end
1149 void test([[C]]<Foo> *p);
1150 )cpp";
1151 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@class C;");
1153 Code = R"cpp(
1154 @class C;
1155 @protocol Foo
1156 @end
1157 void test(C<[[Foo]]> *p);
1158 )cpp";
1159 EXPECT_DECLS("ObjCProtocolLoc", "@protocol Foo");
1161 Code = R"cpp(
1162 @class C;
1163 @protocol Foo
1164 @end
1165 @protocol Bar
1166 @end
1167 void test(C<[[Foo]], Bar> *p);
1168 )cpp";
1169 EXPECT_DECLS("ObjCProtocolLoc", "@protocol Foo");
1171 Code = R"cpp(
1172 @class C;
1173 @protocol Foo
1174 @end
1175 @protocol Bar
1176 @end
1177 void test(C<Foo, [[Bar]]> *p);
1178 )cpp";
1179 EXPECT_DECLS("ObjCProtocolLoc", "@protocol Bar");
1181 Code = R"cpp(
1182 @interface Foo
1183 + (id)sharedInstance;
1184 @end
1185 @implementation Foo
1186 + (id)sharedInstance { return 0; }
1187 @end
1188 void test() {
1189 id value = [[Foo]].sharedInstance;
1191 )cpp";
1192 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface Foo");
1194 Code = R"cpp(
1195 @interface Foo
1196 + (id)sharedInstance;
1197 @end
1198 @implementation Foo
1199 + (id)sharedInstance { return 0; }
1200 @end
1201 void test() {
1202 id value = Foo.[[sharedInstance]];
1204 )cpp";
1205 EXPECT_DECLS("ObjCPropertyRefExpr", "+ (id)sharedInstance");
1207 Code = R"cpp(
1208 @interface Foo
1209 + ([[id]])sharedInstance;
1210 @end
1211 )cpp";
1212 EXPECT_DECLS("TypedefTypeLoc");
1214 Code = R"cpp(
1215 @interface Foo
1216 + ([[instancetype]])sharedInstance;
1217 @end
1218 )cpp";
1219 EXPECT_DECLS("TypedefTypeLoc");
1222 class FindExplicitReferencesTest : public ::testing::Test {
1223 protected:
1224 struct AllRefs {
1225 std::string AnnotatedCode;
1226 std::string DumpedReferences;
1229 TestTU newTU(llvm::StringRef Code) {
1230 TestTU TU;
1231 TU.Code = std::string(Code);
1233 // FIXME: Auto-completion in a template requires disabling delayed template
1234 // parsing.
1235 TU.ExtraArgs.push_back("-std=c++20");
1236 TU.ExtraArgs.push_back("-xobjective-c++");
1238 return TU;
1241 AllRefs annotatedReferences(llvm::StringRef Code, ParsedAST &AST,
1242 std::vector<ReferenceLoc> Refs) {
1243 auto &SM = AST.getSourceManager();
1244 llvm::stable_sort(Refs, [&](const ReferenceLoc &L, const ReferenceLoc &R) {
1245 return SM.isBeforeInTranslationUnit(L.NameLoc, R.NameLoc);
1248 std::string AnnotatedCode;
1249 unsigned NextCodeChar = 0;
1250 for (unsigned I = 0; I < Refs.size(); ++I) {
1251 auto &R = Refs[I];
1253 SourceLocation Pos = R.NameLoc;
1254 assert(Pos.isValid());
1255 if (Pos.isMacroID()) // FIXME: figure out how to show macro locations.
1256 Pos = SM.getExpansionLoc(Pos);
1257 assert(Pos.isFileID());
1259 FileID File;
1260 unsigned Offset;
1261 std::tie(File, Offset) = SM.getDecomposedLoc(Pos);
1262 if (File == SM.getMainFileID()) {
1263 // Print the reference in a source code.
1264 assert(NextCodeChar <= Offset);
1265 AnnotatedCode += Code.substr(NextCodeChar, Offset - NextCodeChar);
1266 AnnotatedCode += "$" + std::to_string(I) + "^";
1268 NextCodeChar = Offset;
1271 AnnotatedCode += Code.substr(NextCodeChar);
1273 std::string DumpedReferences;
1274 for (unsigned I = 0; I < Refs.size(); ++I)
1275 DumpedReferences += std::string(llvm::formatv("{0}: {1}\n", I, Refs[I]));
1277 return AllRefs{std::move(AnnotatedCode), std::move(DumpedReferences)};
1280 /// Parses \p Code, and annotates its body with results of
1281 /// findExplicitReferences on all top level decls.
1282 /// See actual tests for examples of annotation format.
1283 AllRefs annotateAllReferences(llvm::StringRef Code) {
1284 TestTU TU = newTU(Code);
1285 auto AST = TU.build();
1287 std::vector<ReferenceLoc> Refs;
1288 for (auto *TopLevel : AST.getLocalTopLevelDecls())
1289 findExplicitReferences(
1290 TopLevel, [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1291 AST.getHeuristicResolver());
1292 return annotatedReferences(Code, AST, std::move(Refs));
1295 /// Parses \p Code, finds function or namespace '::foo' and annotates its body
1296 /// with results of findExplicitReferences.
1297 /// See actual tests for examples of annotation format.
1298 AllRefs annotateReferencesInFoo(llvm::StringRef Code) {
1299 TestTU TU = newTU(Code);
1300 auto AST = TU.build();
1301 auto *TestDecl = &findDecl(AST, "foo");
1302 if (auto *T = llvm::dyn_cast<FunctionTemplateDecl>(TestDecl))
1303 TestDecl = T->getTemplatedDecl();
1305 std::vector<ReferenceLoc> Refs;
1306 if (const auto *Func = llvm::dyn_cast<FunctionDecl>(TestDecl))
1307 findExplicitReferences(
1308 Func->getBody(),
1309 [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1310 AST.getHeuristicResolver());
1311 else if (const auto *NS = llvm::dyn_cast<NamespaceDecl>(TestDecl))
1312 findExplicitReferences(
1314 [&Refs, &NS](ReferenceLoc R) {
1315 // Avoid adding the namespace foo decl to the results.
1316 if (R.Targets.size() == 1 && R.Targets.front() == NS)
1317 return;
1318 Refs.push_back(std::move(R));
1320 AST.getHeuristicResolver());
1321 else if (const auto *OC = llvm::dyn_cast<ObjCContainerDecl>(TestDecl))
1322 findExplicitReferences(
1323 OC, [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1324 AST.getHeuristicResolver());
1325 else
1326 ADD_FAILURE() << "Failed to find ::foo decl for test";
1328 return annotatedReferences(Code, AST, std::move(Refs));
1332 TEST_F(FindExplicitReferencesTest, AllRefsInFoo) {
1333 std::pair</*Code*/ llvm::StringRef, /*References*/ llvm::StringRef> Cases[] =
1334 {// Simple expressions.
1335 {R"cpp(
1336 int global;
1337 int func();
1338 void foo(int param) {
1339 $0^global = $1^param + $2^func();
1341 )cpp",
1342 "0: targets = {global}\n"
1343 "1: targets = {param}\n"
1344 "2: targets = {func}\n"},
1345 {R"cpp(
1346 struct X { int a; };
1347 void foo(X x) {
1348 $0^x.$1^a = 10;
1350 )cpp",
1351 "0: targets = {x}\n"
1352 "1: targets = {X::a}\n"},
1353 {R"cpp(
1354 // error-ok: testing with broken code
1355 int bar();
1356 int foo() {
1357 return $0^bar() + $1^bar(42);
1359 )cpp",
1360 "0: targets = {bar}\n"
1361 "1: targets = {bar}\n"},
1362 // Namespaces and aliases.
1363 {R"cpp(
1364 namespace ns {}
1365 namespace alias = ns;
1366 void foo() {
1367 using namespace $0^ns;
1368 using namespace $1^alias;
1370 )cpp",
1371 "0: targets = {ns}\n"
1372 "1: targets = {alias}\n"},
1373 // Using declarations.
1374 {R"cpp(
1375 namespace ns { int global; }
1376 void foo() {
1377 using $0^ns::$1^global;
1379 )cpp",
1380 "0: targets = {ns}\n"
1381 "1: targets = {ns::global}, qualifier = 'ns::'\n"},
1382 // Using enum declarations.
1383 {R"cpp(
1384 namespace ns { enum class A {}; }
1385 void foo() {
1386 using enum $0^ns::$1^A;
1388 )cpp",
1389 "0: targets = {ns}\n"
1390 "1: targets = {ns::A}, qualifier = 'ns::'\n"},
1391 // Simple types.
1392 {R"cpp(
1393 struct Struct { int a; };
1394 using Typedef = int;
1395 void foo() {
1396 $0^Struct $1^x;
1397 $2^Typedef $3^y;
1398 static_cast<$4^Struct*>(0);
1400 )cpp",
1401 "0: targets = {Struct}\n"
1402 "1: targets = {x}, decl\n"
1403 "2: targets = {Typedef}\n"
1404 "3: targets = {y}, decl\n"
1405 "4: targets = {Struct}\n"},
1406 // Name qualifiers.
1407 {R"cpp(
1408 namespace a { namespace b { struct S { typedef int type; }; } }
1409 void foo() {
1410 $0^a::$1^b::$2^S $3^x;
1411 using namespace $4^a::$5^b;
1412 $6^S::$7^type $8^y;
1414 )cpp",
1415 "0: targets = {a}\n"
1416 "1: targets = {a::b}, qualifier = 'a::'\n"
1417 "2: targets = {a::b::S}, qualifier = 'a::b::'\n"
1418 "3: targets = {x}, decl\n"
1419 "4: targets = {a}\n"
1420 "5: targets = {a::b}, qualifier = 'a::'\n"
1421 "6: targets = {a::b::S}\n"
1422 "7: targets = {a::b::S::type}, qualifier = 'struct S::'\n"
1423 "8: targets = {y}, decl\n"},
1424 {R"cpp(
1425 void foo() {
1426 $0^ten: // PRINT "HELLO WORLD!"
1427 goto $1^ten;
1429 )cpp",
1430 "0: targets = {ten}, decl\n"
1431 "1: targets = {ten}\n"},
1432 // Simple templates.
1433 {R"cpp(
1434 template <class T> struct vector { using value_type = T; };
1435 template <> struct vector<bool> { using value_type = bool; };
1436 void foo() {
1437 $0^vector<int> $1^vi;
1438 $2^vector<bool> $3^vb;
1440 )cpp",
1441 "0: targets = {vector<int>}\n"
1442 "1: targets = {vi}, decl\n"
1443 "2: targets = {vector<bool>}\n"
1444 "3: targets = {vb}, decl\n"},
1445 // Template type aliases.
1446 {R"cpp(
1447 template <class T> struct vector { using value_type = T; };
1448 template <> struct vector<bool> { using value_type = bool; };
1449 template <class T> using valias = vector<T>;
1450 void foo() {
1451 $0^valias<int> $1^vi;
1452 $2^valias<bool> $3^vb;
1454 )cpp",
1455 "0: targets = {valias}\n"
1456 "1: targets = {vi}, decl\n"
1457 "2: targets = {valias}\n"
1458 "3: targets = {vb}, decl\n"},
1459 // Injected class name.
1460 {R"cpp(
1461 namespace foo {
1462 template <typename $0^T>
1463 class $1^Bar {
1464 ~$2^Bar();
1465 void $3^f($4^Bar);
1468 )cpp",
1469 "0: targets = {foo::Bar::T}, decl\n"
1470 "1: targets = {foo::Bar}, decl\n"
1471 "2: targets = {foo::Bar}\n"
1472 "3: targets = {foo::Bar::f}, decl\n"
1473 "4: targets = {foo::Bar}\n"},
1474 // MemberExpr should know their using declaration.
1475 {R"cpp(
1476 struct X { void func(int); };
1477 struct Y : X {
1478 using X::func;
1480 void foo(Y y) {
1481 $0^y.$1^func(1);
1483 )cpp",
1484 "0: targets = {y}\n"
1485 "1: targets = {Y::func}\n"},
1486 // DeclRefExpr should know their using declaration.
1487 {R"cpp(
1488 namespace ns { void bar(int); }
1489 using ns::bar;
1491 void foo() {
1492 $0^bar(10);
1494 )cpp",
1495 "0: targets = {bar}\n"},
1496 // References from a macro.
1497 {R"cpp(
1498 #define FOO a
1499 #define BAR b
1501 void foo(int a, int b) {
1502 $0^FOO+$1^BAR;
1504 )cpp",
1505 "0: targets = {a}\n"
1506 "1: targets = {b}\n"},
1507 // No references from implicit nodes.
1508 {R"cpp(
1509 struct vector {
1510 int *begin();
1511 int *end();
1514 void foo() {
1515 for (int $0^x : $1^vector()) {
1516 $2^x = 10;
1519 )cpp",
1520 "0: targets = {x}, decl\n"
1521 "1: targets = {vector}\n"
1522 "2: targets = {x}\n"},
1523 // Handle UnresolvedLookupExpr.
1524 {R"cpp(
1525 namespace ns1 { void func(char*); }
1526 namespace ns2 { void func(int*); }
1527 using namespace ns1;
1528 using namespace ns2;
1530 template <class T>
1531 void foo(T t) {
1532 $0^func($1^t);
1534 )cpp",
1535 "0: targets = {ns1::func, ns2::func}\n"
1536 "1: targets = {t}\n"},
1537 // Handle UnresolvedMemberExpr.
1538 {R"cpp(
1539 struct X {
1540 void func(char*);
1541 void func(int*);
1544 template <class T>
1545 void foo(X x, T t) {
1546 $0^x.$1^func($2^t);
1548 )cpp",
1549 "0: targets = {x}\n"
1550 "1: targets = {X::func, X::func}\n"
1551 "2: targets = {t}\n"},
1552 // Handle DependentScopeDeclRefExpr.
1553 {R"cpp(
1554 template <class T>
1555 struct S {
1556 static int value;
1559 template <class T>
1560 void foo() {
1561 $0^S<$1^T>::$2^value;
1563 )cpp",
1564 "0: targets = {S}\n"
1565 "1: targets = {T}\n"
1566 "2: targets = {S::value}, qualifier = 'S<T>::'\n"},
1567 // Handle CXXDependentScopeMemberExpr.
1568 {R"cpp(
1569 template <class T>
1570 struct S {
1571 int value;
1574 template <class T>
1575 void foo(S<T> t) {
1576 $0^t.$1^value;
1578 )cpp",
1579 "0: targets = {t}\n"
1580 "1: targets = {S::value}\n"},
1581 // Type template parameters.
1582 {R"cpp(
1583 template <class T>
1584 void foo() {
1585 static_cast<$0^T>(0);
1586 $1^T();
1587 $2^T $3^t;
1589 )cpp",
1590 "0: targets = {T}\n"
1591 "1: targets = {T}\n"
1592 "2: targets = {T}\n"
1593 "3: targets = {t}, decl\n"},
1594 // Non-type template parameters.
1595 {R"cpp(
1596 template <int I>
1597 void foo() {
1598 int $0^x = $1^I;
1600 )cpp",
1601 "0: targets = {x}, decl\n"
1602 "1: targets = {I}\n"},
1603 // Template template parameters.
1604 {R"cpp(
1605 template <class T> struct vector {};
1607 template <template<class> class TT, template<class> class ...TP>
1608 void foo() {
1609 $0^TT<int> $1^x;
1610 $2^foo<$3^TT>();
1611 $4^foo<$5^vector>();
1612 $6^foo<$7^TP...>();
1614 )cpp",
1615 "0: targets = {TT}\n"
1616 "1: targets = {x}, decl\n"
1617 "2: targets = {foo}\n"
1618 "3: targets = {TT}\n"
1619 "4: targets = {foo}\n"
1620 "5: targets = {vector}\n"
1621 "6: targets = {foo}\n"
1622 "7: targets = {TP}\n"},
1623 // Non-type template parameters with declarations.
1624 {R"cpp(
1625 int func();
1626 template <int(*)()> struct wrapper {};
1628 template <int(*FuncParam)()>
1629 void foo() {
1630 $0^wrapper<$1^func> $2^w;
1631 $3^FuncParam();
1633 )cpp",
1634 "0: targets = {wrapper<&func>}\n"
1635 "1: targets = {func}\n"
1636 "2: targets = {w}, decl\n"
1637 "3: targets = {FuncParam}\n"},
1638 // declaration references.
1639 {R"cpp(
1640 namespace ns {}
1641 class S {};
1642 void foo() {
1643 class $0^Foo { $1^Foo(); ~$2^Foo(); int $3^field; };
1644 int $4^Var;
1645 enum $5^E { $6^ABC };
1646 typedef int $7^INT;
1647 using $8^INT2 = int;
1648 namespace $9^NS = $10^ns;
1650 )cpp",
1651 "0: targets = {Foo}, decl\n"
1652 "1: targets = {foo()::Foo::Foo}, decl\n"
1653 "2: targets = {Foo}\n"
1654 "3: targets = {foo()::Foo::field}, decl\n"
1655 "4: targets = {Var}, decl\n"
1656 "5: targets = {E}, decl\n"
1657 "6: targets = {foo()::ABC}, decl\n"
1658 "7: targets = {INT}, decl\n"
1659 "8: targets = {INT2}, decl\n"
1660 "9: targets = {NS}, decl\n"
1661 "10: targets = {ns}\n"},
1662 // User-defined conversion operator.
1663 {R"cpp(
1664 void foo() {
1665 class $0^Bar {};
1666 class $1^Foo {
1667 public:
1668 // FIXME: This should have only one reference to Bar.
1669 $2^operator $3^$4^Bar();
1672 $5^Foo $6^f;
1673 $7^f.$8^operator $9^Bar();
1675 )cpp",
1676 "0: targets = {Bar}, decl\n"
1677 "1: targets = {Foo}, decl\n"
1678 "2: targets = {foo()::Foo::operator Bar}, decl\n"
1679 "3: targets = {Bar}\n"
1680 "4: targets = {Bar}\n"
1681 "5: targets = {Foo}\n"
1682 "6: targets = {f}, decl\n"
1683 "7: targets = {f}\n"
1684 "8: targets = {foo()::Foo::operator Bar}\n"
1685 "9: targets = {Bar}\n"},
1686 // Destructor.
1687 {R"cpp(
1688 void foo() {
1689 class $0^Foo {
1690 public:
1691 ~$1^Foo() {}
1693 void $2^destructMe() {
1694 this->~$3^Foo();
1698 $4^Foo $5^f;
1699 $6^f.~ /*...*/ $7^Foo();
1701 )cpp",
1702 "0: targets = {Foo}, decl\n"
1703 // FIXME: It's better to target destructor's FunctionDecl instead of
1704 // the type itself (similar to constructor).
1705 "1: targets = {Foo}\n"
1706 "2: targets = {foo()::Foo::destructMe}, decl\n"
1707 "3: targets = {Foo}\n"
1708 "4: targets = {Foo}\n"
1709 "5: targets = {f}, decl\n"
1710 "6: targets = {f}\n"
1711 "7: targets = {Foo}\n"},
1712 // cxx constructor initializer.
1713 {R"cpp(
1714 class Base {};
1715 void foo() {
1716 // member initializer
1717 class $0^X {
1718 int $1^abc;
1719 $2^X(): $3^abc() {}
1721 // base initializer
1722 class $4^Derived : public $5^Base {
1723 $6^Base $7^B;
1724 $8^Derived() : $9^Base() {}
1726 // delegating initializer
1727 class $10^Foo {
1728 $11^Foo(int);
1729 $12^Foo(): $13^Foo(111) {}
1732 )cpp",
1733 "0: targets = {X}, decl\n"
1734 "1: targets = {foo()::X::abc}, decl\n"
1735 "2: targets = {foo()::X::X}, decl\n"
1736 "3: targets = {foo()::X::abc}\n"
1737 "4: targets = {Derived}, decl\n"
1738 "5: targets = {Base}\n"
1739 "6: targets = {Base}\n"
1740 "7: targets = {foo()::Derived::B}, decl\n"
1741 "8: targets = {foo()::Derived::Derived}, decl\n"
1742 "9: targets = {Base}\n"
1743 "10: targets = {Foo}, decl\n"
1744 "11: targets = {foo()::Foo::Foo}, decl\n"
1745 "12: targets = {foo()::Foo::Foo}, decl\n"
1746 "13: targets = {Foo}\n"},
1747 // Anonymous entities should not be reported.
1749 R"cpp(
1750 void foo() {
1751 $0^class {} $1^x;
1752 int (*$2^fptr)(int $3^a, int) = nullptr;
1754 )cpp",
1755 "0: targets = {(unnamed)}\n"
1756 "1: targets = {x}, decl\n"
1757 "2: targets = {fptr}, decl\n"
1758 "3: targets = {a}, decl\n"},
1759 // Namespace aliases should be handled properly.
1761 R"cpp(
1762 namespace ns { struct Type {}; }
1763 namespace alias = ns;
1764 namespace rec_alias = alias;
1766 void foo() {
1767 $0^ns::$1^Type $2^a;
1768 $3^alias::$4^Type $5^b;
1769 $6^rec_alias::$7^Type $8^c;
1771 )cpp",
1772 "0: targets = {ns}\n"
1773 "1: targets = {ns::Type}, qualifier = 'ns::'\n"
1774 "2: targets = {a}, decl\n"
1775 "3: targets = {alias}\n"
1776 "4: targets = {ns::Type}, qualifier = 'alias::'\n"
1777 "5: targets = {b}, decl\n"
1778 "6: targets = {rec_alias}\n"
1779 "7: targets = {ns::Type}, qualifier = 'rec_alias::'\n"
1780 "8: targets = {c}, decl\n"},
1781 // Handle SizeOfPackExpr.
1783 R"cpp(
1784 template <typename... E>
1785 void foo() {
1786 constexpr int $0^size = sizeof...($1^E);
1788 )cpp",
1789 "0: targets = {size}, decl\n"
1790 "1: targets = {E}\n"},
1791 // Class template argument deduction
1793 R"cpp(
1794 template <typename T>
1795 struct Test {
1796 Test(T);
1798 void foo() {
1799 $0^Test $1^a(5);
1801 )cpp",
1802 "0: targets = {Test}\n"
1803 "1: targets = {a}, decl\n"},
1804 // Templates
1805 {R"cpp(
1806 namespace foo {
1807 template <typename $0^T>
1808 class $1^Bar {};
1810 )cpp",
1811 "0: targets = {foo::Bar::T}, decl\n"
1812 "1: targets = {foo::Bar}, decl\n"},
1813 // Templates
1814 {R"cpp(
1815 namespace foo {
1816 template <typename $0^T>
1817 void $1^func();
1819 )cpp",
1820 "0: targets = {T}, decl\n"
1821 "1: targets = {foo::func}, decl\n"},
1822 // Templates
1823 {R"cpp(
1824 namespace foo {
1825 template <typename $0^T>
1826 $1^T $2^x;
1828 )cpp",
1829 "0: targets = {foo::T}, decl\n"
1830 "1: targets = {foo::T}\n"
1831 "2: targets = {foo::x}, decl\n"},
1832 // Templates
1833 {R"cpp(
1834 template<typename T> class vector {};
1835 namespace foo {
1836 template <typename $0^T>
1837 using $1^V = $2^vector<$3^T>;
1839 )cpp",
1840 "0: targets = {foo::T}, decl\n"
1841 "1: targets = {foo::V}, decl\n"
1842 "2: targets = {vector}\n"
1843 "3: targets = {foo::T}\n"},
1844 // Concept
1846 R"cpp(
1847 template <typename T>
1848 concept Drawable = requires (T t) { t.draw(); };
1850 namespace foo {
1851 template <typename $0^T> requires $1^Drawable<$2^T>
1852 void $3^bar($4^T $5^t) {
1853 $6^t.$7^draw();
1856 )cpp",
1857 "0: targets = {T}, decl\n"
1858 "1: targets = {Drawable}\n"
1859 "2: targets = {T}\n"
1860 "3: targets = {foo::bar}, decl\n"
1861 "4: targets = {T}\n"
1862 "5: targets = {t}, decl\n"
1863 "6: targets = {t}\n"
1864 "7: targets = {}\n"},
1865 // Objective-C: instance variables
1867 R"cpp(
1868 @interface I {
1869 @public
1870 I *_z;
1872 @end
1873 I *f;
1874 void foo() {
1875 $0^f->$1^_z = 0;
1877 )cpp",
1878 "0: targets = {f}\n"
1879 "1: targets = {I::_z}\n"},
1880 // Objective-C: properties
1882 R"cpp(
1883 @interface I {}
1884 @property(retain) I* x;
1885 @property(retain) I* y;
1886 @end
1887 I *f;
1888 void foo() {
1889 $0^f.$1^x.$2^y = 0;
1891 )cpp",
1892 "0: targets = {f}\n"
1893 "1: targets = {I::x}\n"
1894 "2: targets = {I::y}\n"},
1895 // Objective-C: implicit properties
1897 R"cpp(
1898 @interface I {}
1899 -(I*)x;
1900 -(void)setY:(I*)y;
1901 @end
1902 I *f;
1903 void foo() {
1904 $0^f.$1^x.$2^y = 0;
1906 )cpp",
1907 "0: targets = {f}\n"
1908 "1: targets = {I::x}\n"
1909 "2: targets = {I::setY:}\n"},
1910 // Objective-C: class properties
1912 R"cpp(
1913 @interface I {}
1914 @property(class) I *x;
1915 @end
1916 id local;
1917 void foo() {
1918 $0^I.$1^x = 0;
1919 $2^local = $3^I.$4^x;
1921 )cpp",
1922 "0: targets = {I}\n"
1923 "1: targets = {I::setX:}\n"
1924 "2: targets = {local}\n"
1925 "3: targets = {I}\n"
1926 "4: targets = {I::x}\n"},
1927 // Objective-C: implicit class properties
1929 R"cpp(
1930 @interface I {}
1931 +(I*)x;
1932 +(void)setX:(I*)x;
1933 @end
1934 id local;
1935 void foo() {
1936 $0^I.$1^x = 0;
1937 $2^local = $3^I.$4^x;
1939 )cpp",
1940 "0: targets = {I}\n"
1941 "1: targets = {I::setX:}\n"
1942 "2: targets = {local}\n"
1943 "3: targets = {I}\n"
1944 "4: targets = {I::x}\n"},
1945 {// Objective-C: methods
1946 R"cpp(
1947 @interface I
1948 -(void) a:(int)x b:(int)y;
1949 @end
1950 void foo(I *i) {
1951 [$0^i $1^a:1 b:2];
1953 )cpp",
1954 "0: targets = {i}\n"
1955 "1: targets = {I::a:b:}\n"},
1956 {// Objective-C: protocols
1957 R"cpp(
1958 @interface I
1959 @end
1960 @protocol P
1961 @end
1962 void foo() {
1963 $0^I<$1^P> *$2^x;
1965 )cpp",
1966 "0: targets = {I}\n"
1967 "1: targets = {P}\n"
1968 "2: targets = {x}, decl\n"},
1970 // Designated initializers.
1971 {R"cpp(
1972 void foo() {
1973 struct $0^Foo {
1974 int $1^Bar;
1976 $2^Foo $3^f { .$4^Bar = 42 };
1978 )cpp",
1979 "0: targets = {Foo}, decl\n"
1980 "1: targets = {foo()::Foo::Bar}, decl\n"
1981 "2: targets = {Foo}\n"
1982 "3: targets = {f}, decl\n"
1983 "4: targets = {foo()::Foo::Bar}\n"},
1984 {R"cpp(
1985 void foo() {
1986 struct $0^Baz {
1987 int $1^Field;
1989 struct $2^Bar {
1990 $3^Baz $4^Foo;
1992 $5^Bar $6^bar { .$7^Foo.$8^Field = 42 };
1994 )cpp",
1995 "0: targets = {Baz}, decl\n"
1996 "1: targets = {foo()::Baz::Field}, decl\n"
1997 "2: targets = {Bar}, decl\n"
1998 "3: targets = {Baz}\n"
1999 "4: targets = {foo()::Bar::Foo}, decl\n"
2000 "5: targets = {Bar}\n"
2001 "6: targets = {bar}, decl\n"
2002 "7: targets = {foo()::Bar::Foo}\n"
2003 "8: targets = {foo()::Baz::Field}\n"},
2004 {R"cpp(
2005 template<typename T>
2006 void crash(T);
2007 template<typename T>
2008 void foo() {
2009 $0^crash({.$1^x = $2^T()});
2011 )cpp",
2012 "0: targets = {crash}\n"
2013 "1: targets = {}\n"
2014 "2: targets = {T}\n"},
2015 // unknown template name should not crash.
2016 {R"cpp(
2017 template <template <typename> typename T>
2018 struct Base {};
2019 namespace foo {
2020 template <typename $0^T>
2021 struct $1^Derive : $2^Base<$3^T::template $4^Unknown> {};
2023 )cpp",
2024 "0: targets = {foo::Derive::T}, decl\n"
2025 "1: targets = {foo::Derive}, decl\n"
2026 "2: targets = {Base}\n"
2027 "3: targets = {foo::Derive::T}\n"
2028 "4: targets = {}, qualifier = 'T::'\n"},
2029 // deduction guide
2030 {R"cpp(
2031 namespace foo {
2032 template <typename $0^T>
2033 struct $1^Test {
2034 template <typename $2^I>
2035 $3^Test($4^I);
2037 template <typename $5^I>
2038 $6^Test($7^I) -> $8^Test<typename $9^I::$10^type>;
2040 )cpp",
2041 "0: targets = {T}, decl\n"
2042 "1: targets = {foo::Test}, decl\n"
2043 "2: targets = {I}, decl\n"
2044 "3: targets = {foo::Test::Test<T>}, decl\n"
2045 "4: targets = {I}\n"
2046 "5: targets = {I}, decl\n"
2047 "6: targets = {foo::Test}\n"
2048 "7: targets = {I}\n"
2049 "8: targets = {foo::Test}\n"
2050 "9: targets = {I}\n"
2051 "10: targets = {}, qualifier = 'I::'\n"}};
2053 for (const auto &C : Cases) {
2054 llvm::StringRef ExpectedCode = C.first;
2055 llvm::StringRef ExpectedRefs = C.second;
2057 auto Actual =
2058 annotateReferencesInFoo(llvm::Annotations(ExpectedCode).code());
2059 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
2060 EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
2064 TEST_F(FindExplicitReferencesTest, AllRefs) {
2065 std::pair</*Code*/ llvm::StringRef, /*References*/ llvm::StringRef> Cases[] =
2066 {{R"cpp(
2067 @interface $0^MyClass
2068 @end
2069 @implementation $1^$2^MyClass
2070 @end
2071 )cpp",
2072 "0: targets = {MyClass}, decl\n"
2073 "1: targets = {MyClass}\n"
2074 "2: targets = {MyClass}, decl\n"},
2075 {R"cpp(
2076 @interface $0^MyClass
2077 @end
2078 @interface $1^MyClass ($2^Category)
2079 @end
2080 @implementation $3^MyClass ($4^$5^Category)
2081 @end
2082 )cpp",
2083 "0: targets = {MyClass}, decl\n"
2084 "1: targets = {MyClass}\n"
2085 "2: targets = {Category}, decl\n"
2086 "3: targets = {MyClass}\n"
2087 "4: targets = {Category}\n"
2088 "5: targets = {Category}, decl\n"}};
2090 for (const auto &C : Cases) {
2091 llvm::StringRef ExpectedCode = C.first;
2092 llvm::StringRef ExpectedRefs = C.second;
2094 auto Actual = annotateAllReferences(llvm::Annotations(ExpectedCode).code());
2095 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
2096 EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
2100 } // namespace
2101 } // namespace clangd
2102 } // namespace clang