clang-tools 24.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/DeclObjC.h"
14#include "clang/AST/DeclTemplate.h"
15#include "clang/Basic/SourceLocation.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/Casting.h"
18#include "llvm/Support/raw_ostream.h"
19#include "llvm/Testing/Annotations/Annotations.h"
20#include "gmock/gmock.h"
21#include "gtest/gtest.h"
22#include <initializer_list>
23
24namespace clang {
25namespace clangd {
26namespace {
27
28// A referenced Decl together with its DeclRelationSet, for assertions.
29//
30// There's no great way to assert on the "content" of a Decl in the general case
31// that's both expressive and unambiguous (e.g. clearly distinguishes between
32// templated decls and their specializations).
33//
34// We use the result of pretty-printing the decl, with the {body} truncated.
35struct PrintedDecl {
36 PrintedDecl(const char *Name, DeclRelationSet Relations = {})
37 : Name(Name), Relations(Relations) {}
38 PrintedDecl(const NamedDecl *D, DeclRelationSet Relations = {})
39 : Relations(Relations) {
40 std::string S;
41 llvm::raw_string_ostream OS(S);
42 D->print(OS);
43 llvm::StringRef FirstLine =
44 llvm::StringRef(OS.str()).take_until([](char C) { return C == '\n'; });
45 FirstLine = FirstLine.rtrim(" {");
46 Name = std::string(FirstLine.rtrim(" {"));
47 }
48
49 std::string Name;
50 DeclRelationSet Relations;
51};
52bool operator==(const PrintedDecl &L, const PrintedDecl &R) {
53 return std::tie(L.Name, L.Relations) == std::tie(R.Name, R.Relations);
54}
55llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const PrintedDecl &D) {
56 return OS << D.Name << " Rel=" << D.Relations;
57}
58
59// The test cases in for targetDecl() take the form
60// - a piece of code (Code = "...")
61// - Code should have a single AST node marked as a [[range]]
62// - an EXPECT_DECLS() assertion that verify the type of node selected, and
63// all the decls that targetDecl() considers it to reference
64// Despite the name, these cases actually test allTargetDecls() for brevity.
65class TargetDeclTest : public ::testing::Test {
66protected:
67 using Rel = DeclRelation;
68 std::string Code;
69 std::vector<std::string> Flags;
70
71 // Asserts that `Code` has a marked selection of a node `NodeType`,
72 // and returns allTargetDecls() as PrintedDecl structs.
73 // Use via EXPECT_DECLS().
74 std::vector<PrintedDecl> assertNodeAndPrintDecls(const char *NodeType) {
75 llvm::Annotations A(Code);
76 auto TU = TestTU::withCode(A.code());
77 TU.ExtraArgs = Flags;
78 auto AST = TU.build();
79 llvm::Annotations::Range R = A.range();
80 auto Selection = SelectionTree::createRight(
81 AST.getASTContext(), AST.getTokens(), R.Begin, R.End);
82 const SelectionTree::Node *N = Selection.commonAncestor();
83 if (!N) {
84 ADD_FAILURE() << "No node selected!\n" << Code;
85 return {};
86 }
87 EXPECT_EQ(N->kind(), NodeType) << Selection;
88
89 std::vector<PrintedDecl> ActualDecls;
90 for (const auto &Entry :
91 allTargetDecls(N->ASTNode, AST.getHeuristicResolver()))
92 ActualDecls.emplace_back(Entry.first, Entry.second);
93 return ActualDecls;
94 }
95};
96
97// This is a macro to preserve line numbers in assertion failures.
98// It takes the expected decls as varargs to work around comma-in-macro issues.
99#define EXPECT_DECLS(NodeType, ...) \
100 EXPECT_THAT(assertNodeAndPrintDecls(NodeType), \
101 ::testing::UnorderedElementsAreArray( \
102 std::vector<PrintedDecl>({__VA_ARGS__}))) \
103 << Code
104using ExpectedDecls = std::vector<PrintedDecl>;
105
106TEST_F(TargetDeclTest, Exprs) {
107 Code = R"cpp(
108 int f();
109 int x = [[f]]();
110 )cpp";
111 EXPECT_DECLS("DeclRefExpr", "int f()");
112
113 Code = R"cpp(
114 struct S { S operator+(S) const; };
115 auto X = S() [[+]] S();
116 )cpp";
117 EXPECT_DECLS("DeclRefExpr", "S operator+(S) const");
118
119 Code = R"cpp(
120 int foo();
121 int s = foo[[()]];
122 )cpp";
123 EXPECT_DECLS("CallExpr", "int foo()");
124
125 Code = R"cpp(
126 struct X {
127 void operator()(int n);
128 };
129 void test() {
130 X x;
131 x[[(123)]];
132 }
133 )cpp";
134 EXPECT_DECLS("CXXOperatorCallExpr", "void operator()(int n)");
135
136 Code = R"cpp(
137 void test() {
138 goto [[label]];
139 label:
140 return;
141 }
142 )cpp";
143 EXPECT_DECLS("GotoStmt", "label:");
144 Code = R"cpp(
145 void test() {
146 [[label]]:
147 return;
148 }
149 )cpp";
150 EXPECT_DECLS("LabelStmt", "label:");
151}
152
153TEST_F(TargetDeclTest, RecoveryForC) {
154 Flags = {"-xc", "-Xclang", "-frecovery-ast"};
155 Code = R"cpp(
156 // error-ok: testing behavior on broken code
157 // int f();
158 int f(int);
159 int x = [[f]]();
160 )cpp";
161 EXPECT_DECLS("DeclRefExpr", "int f(int)");
162}
163
164TEST_F(TargetDeclTest, Recovery) {
165 Code = R"cpp(
166 // error-ok: testing behavior on broken code
167 int f();
168 int f(int, int);
169 int x = [[f]](42);
170 )cpp";
171 EXPECT_DECLS("UnresolvedLookupExpr", "int f()", "int f(int, int)");
172}
173
174TEST_F(TargetDeclTest, RecoveryType) {
175 Code = R"cpp(
176 // error-ok: testing behavior on broken code
177 struct S { int member; };
178 S overloaded(int);
179 void foo() {
180 // No overload matches, but we have recovery-expr with the correct type.
181 overloaded().[[member]];
182 }
183 )cpp";
184 EXPECT_DECLS("MemberExpr", "int member");
185}
186
187TEST_F(TargetDeclTest, UsingDecl) {
188 Code = R"cpp(
189 namespace foo {
190 int f(int);
191 int f(char);
192 }
193 using foo::f;
194 int x = [[f]](42);
195 )cpp";
196 // f(char) is not referenced!
197 EXPECT_DECLS("DeclRefExpr", {"using foo::f", Rel::Alias}, {"int f(int)"});
198
199 Code = R"cpp(
200 namespace foo {
201 int f(int);
202 int f(char);
203 }
204 [[using foo::f]];
205 )cpp";
206 // All overloads are referenced.
207 EXPECT_DECLS("UsingDecl", {"using foo::f", Rel::Alias}, {"int f(int)"},
208 {"int f(char)"});
209
210 Code = R"cpp(
211 struct X {
212 int foo();
213 };
214 struct Y : X {
215 using X::foo;
216 };
217 int x = Y().[[foo]]();
218 )cpp";
219 EXPECT_DECLS("MemberExpr", {"using X::foo", Rel::Alias}, {"int foo()"});
220
221 Code = R"cpp(
222 template <typename T>
223 struct Base {
224 void waldo() {}
225 };
226 template <typename T>
227 struct Derived : Base<T> {
228 using Base<T>::[[waldo]];
229 };
230 )cpp";
231 EXPECT_DECLS("UnresolvedUsingValueDecl", {"using Base<T>::waldo", Rel::Alias},
232 {"void waldo()"});
233
234 Code = R"cpp(
235 namespace ns {
236 template<typename T> class S {};
237 }
238
239 using ns::S;
240
241 template<typename T>
242 using A = [[S]]<T>;
243 )cpp";
244 EXPECT_DECLS("TemplateSpecializationTypeLoc", {"using ns::S", Rel::Alias},
245 {"template <typename T> class S"},
246 {"class S", Rel::TemplatePattern});
247
248 Code = R"cpp(
249 namespace ns {
250 template<typename T> class S {};
251 }
252
253 using ns::S;
254 template <template <typename> class T> class X {};
255 using B = X<[[S]]>;
256 )cpp";
257 EXPECT_DECLS("TemplateArgumentLoc", {"using ns::S", Rel::Alias},
258 {"template <typename T> class S"});
259
260 Code = R"cpp(
261 namespace ns {
262 template<typename T> class S { public: S(T); };
263 }
264
265 using ns::S;
266 [[S]] s(123);
267 )cpp";
268 Flags.push_back("-std=c++17"); // For CTAD feature.
269 EXPECT_DECLS("DeducedTemplateSpecializationTypeLoc",
270 {"using ns::S", Rel::Alias}, {"template <typename T> class S"},
271 {"class S", Rel::TemplatePattern});
272
273 Code = R"cpp(
274 template<typename T>
275 class Foo { public: class foo {}; };
276 template <class T> class A : public Foo<T> {
277 using typename Foo<T>::foo;
278 [[foo]] abc;
279 };
280 )cpp";
281 EXPECT_DECLS("UnresolvedUsingTypeLoc",
282 {"using typename Foo<T>::foo", Rel::Alias});
283
284 // Using enum.
285 Flags.push_back("-std=c++20");
286 Code = R"cpp(
287 namespace ns { enum class A { X }; }
288 [[using enum ns::A]];
289 )cpp";
290 EXPECT_DECLS("UsingEnumDecl", "enum class A : int");
291
292 Code = R"cpp(
293 namespace ns { enum class A { X }; }
294 using enum ns::A;
295 auto m = [[X]];
296 )cpp";
297 EXPECT_DECLS("DeclRefExpr", "X");
298}
299
300TEST_F(TargetDeclTest, BaseSpecifier) {
301 Code = R"cpp(
302 struct X {};
303 struct Y : [[private]] X {};
304 )cpp";
305 EXPECT_DECLS("CXXBaseSpecifier", "struct X");
306 Code = R"cpp(
307 struct X {};
308 struct Y : [[private X]] {};
309 )cpp";
310 EXPECT_DECLS("CXXBaseSpecifier", "struct X");
311 Code = R"cpp(
312 struct X {};
313 struct Y : private [[X]] {};
314 )cpp";
315 EXPECT_DECLS("RecordTypeLoc", "struct X");
316}
317
318TEST_F(TargetDeclTest, ConstructorInitList) {
319 Code = R"cpp(
320 struct X {
321 int a;
322 X() : [[a]](42) {}
323 };
324 )cpp";
325 EXPECT_DECLS("CXXCtorInitializer", "int a");
326
327 Code = R"cpp(
328 struct X {
329 X() : [[X]](1) {}
330 X(int);
331 };
332 )cpp";
333 EXPECT_DECLS("RecordTypeLoc", "struct X");
334}
335
336TEST_F(TargetDeclTest, DesignatedInit) {
337 Flags = {"-xc"}; // array designators are a C99 extension.
338 Code = R"c(
339 struct X { int a; };
340 struct Y { int b; struct X c[2]; };
341 struct Y y = { .c[0].[[a]] = 1 };
342 )c";
343 EXPECT_DECLS("DesignatedInitExpr", "int a");
344}
345
346TEST_F(TargetDeclTest, OffsetOf) {
347 Code = R"cpp(
348 struct Foo { int bar; };
349 int x = __builtin_offsetof(Foo, [[bar]]);
350 )cpp";
351 EXPECT_DECLS("OffsetOfNode", "int bar");
352
353 Code = R"cpp(
354 struct Inner { int c; };
355 struct Outer { Inner b; };
356 int x = __builtin_offsetof(Outer, [[b]].c);
357 )cpp";
358 EXPECT_DECLS("OffsetOfNode", "Inner b");
359
360 Code = R"cpp(
361 struct Inner { int c; };
362 struct Outer { Inner b; };
363 int x = __builtin_offsetof(Outer, b.[[c]]);
364 )cpp";
365 EXPECT_DECLS("OffsetOfNode", "int c");
366
367 // Selection that spans multiple components doesn't match any single
368 // OffsetOfNode.
369 Code = R"cpp(
370 struct Inner { int c; };
371 struct Outer { Inner b; };
372 int x = __builtin_offsetof(Outer, [[b.c]]);
373 )cpp";
374 EXPECT_THAT(assertNodeAndPrintDecls("OffsetOfExpr"), ::testing::IsEmpty());
375
376 Code = R"cpp(
377 struct Inner { int c; };
378 struct Outer { Inner b; };
379 int x = __builtin_offsetof(Outer, [[b.]]c);
380 )cpp";
381 EXPECT_THAT(assertNodeAndPrintDecls("OffsetOfExpr"), ::testing::IsEmpty());
382
383 Code = R"cpp(
384 struct Inner { int c; };
385 struct Outer { Inner b; };
386 int x = __builtin_offsetof(Outer, b[[.c]]);
387 )cpp";
388 EXPECT_DECLS("OffsetOfNode", "int c");
389
390 Code = R"cpp(
391 struct Foo { int bar; };
392 int x = __builtin_[[offsetof]](Foo, bar);
393 )cpp";
394 EXPECT_THAT(assertNodeAndPrintDecls("OffsetOfExpr"), ::testing::IsEmpty());
395
396 // Base-class component: the implicit Base node has no source range, so
397 // the cursor on `x` resolves precisely to the inherited field.
398 Code = R"cpp(
399 struct B { int x; };
400 struct D : B {};
401 int o = __builtin_offsetof(D, [[x]]);
402 )cpp";
403 EXPECT_DECLS("OffsetOfNode", "int x");
404
405 // Dependent-type identifier component: no resolvable decl, but must not
406 // crash and must not produce spurious targets.
407 Code = R"cpp(
408 template <typename T> int f() { return __builtin_offsetof(T, [[x]]); }
409 )cpp";
410 EXPECT_THAT(assertNodeAndPrintDecls("OffsetOfNode"), ::testing::IsEmpty());
411
412 // C-style offsetof macro form resolves the same as __builtin_offsetof.
413 Code = R"cpp(
414 #define offsetof(t, m) __builtin_offsetof(t, m)
415 struct Foo { int bar; };
416 int x = offsetof(Foo, [[bar]]);
417 )cpp";
418 EXPECT_DECLS("OffsetOfNode", "int bar");
419
420 // Array-index expression in an offsetof designator should still resolve as
421 // the expression, not as the enclosing OffsetOfNode.
422 Code = R"cpp(
423 struct Foo { int bar[4]; };
424 int i;
425 int x = __builtin_offsetof(Foo, bar[ [[i]] ]);
426 )cpp";
427 EXPECT_DECLS("DeclRefExpr", "int i");
428}
429
430TEST_F(TargetDeclTest, NestedNameSpecifier) {
431 Code = R"cpp(
432 namespace a { namespace b { int c; } }
433 int x = a::[[b::]]c;
434 )cpp";
435 EXPECT_DECLS("NestedNameSpecifierLoc", "namespace b");
436
437 Code = R"cpp(
438 namespace a { struct X { enum { y }; }; }
439 int x = a::[[X::]]y;
440 )cpp";
441 EXPECT_DECLS("NestedNameSpecifierLoc", "struct X");
442
443 Code = R"cpp(
444 template <typename T>
445 int x = [[T::]]y;
446 )cpp";
447 EXPECT_DECLS("NestedNameSpecifierLoc", "typename T");
448
449 Code = R"cpp(
450 namespace a { int x; }
451 namespace b = a;
452 int y = [[b]]::x;
453 )cpp";
454 EXPECT_DECLS("NestedNameSpecifierLoc", {"namespace b = a", Rel::Alias},
455 {"namespace a", Rel::Underlying});
456}
457
458TEST_F(TargetDeclTest, Types) {
459 Code = R"cpp(
460 struct X{};
461 [[X]] x;
462 )cpp";
463 EXPECT_DECLS("RecordTypeLoc", "struct X");
464
465 Code = R"cpp(
466 struct S{};
467 typedef S X;
468 [[X]] x;
469 )cpp";
470 EXPECT_DECLS("TypedefTypeLoc", {"typedef S X", Rel::Alias},
471 {"struct S", Rel::Underlying});
472 Code = R"cpp(
473 namespace ns { struct S{}; }
474 typedef ns::S X;
475 [[X]] x;
476 )cpp";
477 EXPECT_DECLS("TypedefTypeLoc", {"typedef ns::S X", Rel::Alias},
478 {"struct S", Rel::Underlying});
479
480 Code = R"cpp(
481 template<class T>
482 void foo() { [[T]] x; }
483 )cpp";
484 EXPECT_DECLS("TemplateTypeParmTypeLoc", "class T");
485 Flags.clear();
486
487 Code = R"cpp(
488 template<template<typename> class T>
489 void foo() { [[T<int>]] x; }
490 )cpp";
491 EXPECT_DECLS("TemplateSpecializationTypeLoc", "template <typename> class T");
492 Flags.clear();
493
494 Code = R"cpp(
495 template<template<typename> class ...T>
496 class C {
497 C<[[T...]]> foo;
498 };
499 )cpp";
500 EXPECT_DECLS("TemplateArgumentLoc", {"template <typename> class ...T"});
501 Flags.clear();
502
503 Code = R"cpp(
504 struct S{};
505 S X;
506 [[decltype]](X) Y;
507 )cpp";
508 EXPECT_DECLS("DecltypeTypeLoc", {"struct S", Rel::Underlying});
509
510 Code = R"cpp(
511 struct S{};
512 [[auto]] X = S{};
513 )cpp";
514 // FIXME: deduced type missing in AST. https://llvm.org/PR42914
515 EXPECT_DECLS("AutoTypeLoc", );
516
517 Code = R"cpp(
518 template <typename... E>
519 struct S {
520 static const int size = sizeof...([[E]]);
521 };
522 )cpp";
523 EXPECT_DECLS("SizeOfPackExpr", "typename ...E");
524
525 Code = R"cpp(
526 template <typename T>
527 class Foo {
528 void f([[Foo]] x);
529 };
530 )cpp";
531 EXPECT_DECLS("InjectedClassNameTypeLoc", "class Foo");
532}
533
534TEST_F(TargetDeclTest, ClassTemplate) {
535 Code = R"cpp(
536 // Implicit specialization.
537 template<int x> class Foo{};
538 [[Foo<42>]] B;
539 )cpp";
540 EXPECT_DECLS("TemplateSpecializationTypeLoc",
541 {"template<> class Foo<42>", Rel::TemplateInstantiation},
542 {"class Foo", Rel::TemplatePattern});
543
544 Code = R"cpp(
545 template<typename T> class Foo {};
546 // The "Foo<int>" SpecializationDecl is incomplete, there is no
547 // instantiation happening.
548 void func([[Foo<int>]] *);
549 )cpp";
550 EXPECT_DECLS("TemplateSpecializationTypeLoc",
551 {"class Foo", Rel::TemplatePattern},
552 {"template<> class Foo<int>", Rel::TemplateInstantiation});
553
554 Code = R"cpp(
555 // Explicit specialization.
556 template<int x> class Foo{};
557 template<> class Foo<42>{};
558 [[Foo<42>]] B;
559 )cpp";
560 EXPECT_DECLS("TemplateSpecializationTypeLoc", "template<> class Foo<42>");
561
562 Code = R"cpp(
563 // Partial specialization.
564 template<typename T> class Foo{};
565 template<typename T> class Foo<T*>{};
566 [[Foo<int*>]] B;
567 )cpp";
568 EXPECT_DECLS("TemplateSpecializationTypeLoc",
569 {"template<> class Foo<int *>", Rel::TemplateInstantiation},
570 {"template <typename T> class Foo<T *>", Rel::TemplatePattern});
571
572 Code = R"cpp(
573 // Template template argument.
574 template<typename T> struct Vector {};
575 template <template <typename> class Container>
576 struct A {};
577 A<[[Vector]]> a;
578 )cpp";
579 EXPECT_DECLS("TemplateArgumentLoc", {"template <typename T> struct Vector"});
580
581 Flags.push_back("-std=c++17"); // for CTAD tests
582
583 Code = R"cpp(
584 // Class template argument deduction
585 template <typename T>
586 struct Test {
587 Test(T);
588 };
589 void foo() {
590 [[Test]] a(5);
591 }
592 )cpp";
593 EXPECT_DECLS("DeducedTemplateSpecializationTypeLoc",
594 {"struct Test", Rel::TemplatePattern});
595
596 Code = R"cpp(
597 // Deduction guide
598 template <typename T>
599 struct Test {
600 template <typename I>
601 Test(I, I);
602 };
603 template <typename I>
604 [[Test]](I, I) -> Test<typename I::type>;
605 )cpp";
606 EXPECT_DECLS("CXXDeductionGuideDecl", {"template <typename T> struct Test"});
607}
608
609TEST_F(TargetDeclTest, Concept) {
610 Flags.push_back("-std=c++20");
611
612 // FIXME: Should we truncate the pretty-printed form of a concept decl
613 // somewhere?
614
615 Code = R"cpp(
616 template <typename T>
617 concept Fooable = requires (T t) { t.foo(); };
618
619 template <typename T> requires [[Fooable]]<T>
620 void bar(T t) {
621 t.foo();
622 }
623 )cpp";
625 "ConceptReference",
626 {"template <typename T> concept Fooable = requires (T t) { t.foo(); }"});
627
628 // trailing requires clause
629 Code = R"cpp(
630 template <typename T>
631 concept Fooable = true;
632
633 template <typename T>
634 void foo() requires [[Fooable]]<T>;
635 )cpp";
636 EXPECT_DECLS("ConceptReference",
637 {"template <typename T> concept Fooable = true"});
638
639 // constrained-parameter
640 Code = R"cpp(
641 template <typename T>
642 concept Fooable = true;
643
644 template <[[Fooable]] T>
645 void bar(T t);
646 )cpp";
647 EXPECT_DECLS("ConceptReference",
648 {"template <typename T> concept Fooable = true"});
649
650 // partial-concept-id
651 Code = R"cpp(
652 template <typename T, typename U>
653 concept Fooable = true;
654
655 template <[[Fooable]]<int> T>
656 void bar(T t);
657 )cpp";
658 EXPECT_DECLS("ConceptReference",
659 {"template <typename T, typename U> concept Fooable = true"});
660}
661
662TEST_F(TargetDeclTest, Coroutine) {
663 Flags.push_back("-std=c++20");
664
665 Code = R"cpp(
666 namespace std {
667 template <typename, typename...> struct coroutine_traits;
668 template <typename> struct coroutine_handle {
669 template <typename U>
670 coroutine_handle(coroutine_handle<U>&&) noexcept;
671 static coroutine_handle from_address(void* __addr) noexcept;
672 };
673 } // namespace std
674
675 struct executor {};
676 struct awaitable {};
677 struct awaitable_frame {
678 awaitable get_return_object();
679 void return_void();
680 void unhandled_exception();
681 struct result_t {
682 ~result_t();
683 bool await_ready() const noexcept;
684 void await_suspend(std::coroutine_handle<void>) noexcept;
685 void await_resume() const noexcept;
686 };
687 result_t initial_suspend() noexcept;
688 result_t final_suspend() noexcept;
689 result_t await_transform(executor) noexcept;
690 };
691
692 namespace std {
693 template <>
694 struct coroutine_traits<awaitable> {
695 typedef awaitable_frame promise_type;
696 };
697 } // namespace std
698
699 awaitable foo() {
700 co_await [[executor]]();
701 }
702 )cpp";
703 EXPECT_DECLS("RecordTypeLoc", "struct executor");
704}
705
706TEST_F(TargetDeclTest, RewrittenBinaryOperator) {
707 Flags.push_back("-std=c++20");
708
709 Code = R"cpp(
710 namespace std {
711 struct strong_ordering {
712 int n;
713 constexpr operator int() const { return n; }
714 static const strong_ordering equal, greater, less;
715 };
716 constexpr strong_ordering strong_ordering::equal = {0};
717 constexpr strong_ordering strong_ordering::greater = {1};
718 constexpr strong_ordering strong_ordering::less = {-1};
719 }
720
721 struct Foo
722 {
723 int x;
724 auto operator<=>(const Foo&) const = default;
725 };
726
727 bool x = (Foo(1) [[!=]] Foo(2));
728 )cpp";
729 EXPECT_DECLS("CXXRewrittenBinaryOperator",
730 {"bool operator==(const Foo &) const noexcept = default"});
731}
732
733TEST_F(TargetDeclTest, FunctionTemplate) {
734 Code = R"cpp(
735 // Implicit specialization.
736 template<typename T> bool foo(T) { return false; };
737 bool x = [[foo]](42);
738 )cpp";
739 EXPECT_DECLS("DeclRefExpr",
740 {"template<> bool foo<int>(int)", Rel::TemplateInstantiation},
741 {"bool foo(T)", Rel::TemplatePattern});
742
743 Code = R"cpp(
744 // Explicit specialization.
745 template<typename T> bool foo(T) { return false; };
746 template<> bool foo<int>(int) { return false; };
747 bool x = [[foo]](42);
748 )cpp";
749 EXPECT_DECLS("DeclRefExpr", "template<> bool foo<int>(int)");
750}
751
752TEST_F(TargetDeclTest, VariableTemplate) {
753 // Pretty-printer doesn't do a very good job of variable templates :-(
754 Code = R"cpp(
755 // Implicit specialization.
756 template<typename T> int foo;
757 int x = [[foo]]<char>;
758 )cpp";
759 EXPECT_DECLS("DeclRefExpr", {"int foo", Rel::TemplateInstantiation},
760 {"int foo", Rel::TemplatePattern});
761
762 Code = R"cpp(
763 // Explicit specialization.
764 template<typename T> int foo;
765 template <> bool foo<char>;
766 int x = [[foo]]<char>;
767 )cpp";
768 EXPECT_DECLS("DeclRefExpr", "bool foo");
769
770 Code = R"cpp(
771 // Partial specialization.
772 template<typename T> int foo;
773 template<typename T> bool foo<T*>;
774 bool x = [[foo]]<char*>;
775 )cpp";
776 EXPECT_DECLS("DeclRefExpr", {"bool foo", Rel::TemplateInstantiation},
777 {"bool foo", Rel::TemplatePattern});
778}
779
780TEST_F(TargetDeclTest, TypeAliasTemplate) {
781 Code = R"cpp(
782 template<typename T, int X> class SmallVector {};
783 template<typename U> using TinyVector = SmallVector<U, 1>;
784 [[TinyVector<int>]] X;
785 )cpp";
786 EXPECT_DECLS("TemplateSpecializationTypeLoc",
787 {"template<> class SmallVector<int, 1>",
788 Rel::TemplateInstantiation | Rel::Underlying},
789 {"class SmallVector", Rel::TemplatePattern | Rel::Underlying},
790 {"using TinyVector = SmallVector<U, 1>",
791 Rel::Alias | Rel::TemplatePattern});
792}
793
794TEST_F(TargetDeclTest, BuiltinTemplates) {
795 Code = R"cpp(
796 template <class T, T... Index> struct integer_sequence {};
797 [[__make_integer_seq]]<integer_sequence, int, 3> X;
798 )cpp";
800 "TemplateSpecializationTypeLoc",
801 {"struct integer_sequence", Rel::TemplatePattern | Rel::Underlying},
802 {"template<> struct integer_sequence<int, <0, 1, 2>>",
803 Rel::TemplateInstantiation | Rel::Underlying});
804
805 // Dependent context.
806 Code = R"cpp(
807 template <class T, T... Index> struct integer_sequence;
808
809 template <class T, int N>
810 using make_integer_sequence = [[__make_integer_seq]]<integer_sequence, T, N>;
811 )cpp";
812 EXPECT_DECLS("TemplateSpecializationTypeLoc", );
813
814 Code = R"cpp(
815 template <int N, class... Pack>
816 using type_pack_element = [[__type_pack_element]]<N, Pack...>;
817 )cpp";
818 EXPECT_DECLS("TemplateSpecializationTypeLoc", );
819
820 Code = R"cpp(
821 template <template <class...> class Templ, class... Types>
822 using dedup_types = Templ<[[__builtin_dedup_pack]]<Types...>...>;
823 )cpp";
824 EXPECT_DECLS("TemplateSpecializationTypeLoc", );
825}
826
827TEST_F(TargetDeclTest, MemberOfTemplate) {
828 Code = R"cpp(
829 template <typename T> struct Foo {
830 int x(T);
831 };
832 int y = Foo<int>().[[x]](42);
833 )cpp";
834 EXPECT_DECLS("MemberExpr", {"int x(int)", Rel::TemplateInstantiation},
835 {"int x(T)", Rel::TemplatePattern});
836
837 Code = R"cpp(
838 template <typename T> struct Foo {
839 template <typename U>
840 int x(T, U);
841 };
842 int y = Foo<char>().[[x]]('c', 42);
843 )cpp";
844 EXPECT_DECLS("MemberExpr",
845 {"template<> int x<int>(char, int)", Rel::TemplateInstantiation},
846 {"int x(T, U)", Rel::TemplatePattern});
847}
848
849TEST_F(TargetDeclTest, Lambda) {
850 Code = R"cpp(
851 void foo(int x = 42) {
852 auto l = [ [[x]] ]{ return x + 1; };
853 };
854 )cpp";
855 EXPECT_DECLS("DeclRefExpr", "int x = 42");
856
857 // It seems like this should refer to another var, with the outer param being
858 // an underlying decl. But it doesn't seem to exist.
859 Code = R"cpp(
860 void foo(int x = 42) {
861 auto l = [x]{ return [[x]] + 1; };
862 };
863 )cpp";
864 EXPECT_DECLS("DeclRefExpr", "int x = 42");
865
866 Code = R"cpp(
867 void foo() {
868 auto l = [x = 1]{ return [[x]] + 1; };
869 };
870 )cpp";
871 // FIXME: why both auto and int?
872 EXPECT_DECLS("DeclRefExpr", "auto int x = 1");
873}
874
875TEST_F(TargetDeclTest, OverloadExpr) {
876 Flags.push_back("--target=x86_64-pc-linux-gnu");
877
878 Code = R"cpp(
879 void func(int*);
880 void func(char*);
881
882 template <class T>
883 void foo(T t) {
884 [[func]](t);
885 };
886 )cpp";
887 EXPECT_DECLS("UnresolvedLookupExpr", "void func(int *)", "void func(char *)");
888
889 Code = R"cpp(
890 struct X {
891 void func(int*);
892 void func(char*);
893 };
894
895 template <class T>
896 void foo(X x, T t) {
897 x.[[func]](t);
898 };
899 )cpp";
900 EXPECT_DECLS("UnresolvedMemberExpr", "void func(int *)", "void func(char *)");
901
902 Code = R"cpp(
903 struct X {
904 static void *operator new(unsigned long);
905 };
906 auto* k = [[new]] X();
907 )cpp";
908 EXPECT_DECLS("CXXNewExpr", "static void *operator new(unsigned long)");
909 Code = R"cpp(
910 void *operator new(unsigned long);
911 auto* k = [[new]] int();
912 )cpp";
913 EXPECT_DECLS("CXXNewExpr", "void *operator new(unsigned long)");
914
915 Code = R"cpp(
916 struct X {
917 static void operator delete(void *) noexcept;
918 };
919 void k(X* x) {
920 [[delete]] x;
921 }
922 )cpp";
923 EXPECT_DECLS("CXXDeleteExpr", "static void operator delete(void *) noexcept");
924 Code = R"cpp(
925 void operator delete(void *) noexcept;
926 void k(int* x) {
927 [[delete]] x;
928 }
929 )cpp";
930 // Sized deallocation is enabled by default in C++14 onwards.
931 EXPECT_DECLS("CXXDeleteExpr",
932 "void operator delete(void *, __size_t) noexcept");
933}
934
935TEST_F(TargetDeclTest, DependentExprs) {
936 Flags.push_back("--std=c++20");
937
938 // Heuristic resolution of method of dependent field
939 Code = R"cpp(
940 struct A { void foo() {} };
941 template <typename T>
942 struct B {
943 A a;
944 void bar() {
945 this->a.[[foo]]();
946 }
947 };
948 )cpp";
949 EXPECT_DECLS("MemberExpr", "void foo()");
950
951 // Similar to above but base expression involves a function call.
952 Code = R"cpp(
953 struct A {
954 void foo() {}
955 };
956 struct B {
957 A getA();
958 };
959 template <typename T>
960 struct C {
961 B c;
962 void bar() {
963 this->c.getA().[[foo]]();
964 }
965 };
966 )cpp";
967 EXPECT_DECLS("MemberExpr", "void foo()");
968
969 // Similar to above but uses a function pointer.
970 Code = R"cpp(
971 struct A {
972 void foo() {}
973 };
974 struct B {
975 using FPtr = A(*)();
976 FPtr fptr;
977 };
978 template <typename T>
979 struct C {
980 B c;
981 void bar() {
982 this->c.fptr().[[foo]]();
983 }
984 };
985 )cpp";
986 EXPECT_DECLS("MemberExpr", "void foo()");
987
988 // Base expression involves a member access into this.
989 Code = R"cpp(
990 struct Bar {
991 int aaaa;
992 };
993 template <typename T> struct Foo {
994 Bar func(int);
995 void test() {
996 func(1).[[aaaa]];
997 }
998 };
999 )cpp";
1000 EXPECT_DECLS("CXXDependentScopeMemberExpr", "int aaaa");
1001
1002 Code = R"cpp(
1003 class Foo {
1004 public:
1005 static Foo k(int);
1006 template <typename T> T convert() const;
1007 };
1008 template <typename T>
1009 void test() {
1010 Foo::k(T()).template [[convert]]<T>();
1011 }
1012 )cpp";
1013 EXPECT_DECLS("CXXDependentScopeMemberExpr",
1014 "template <typename T> T convert() const");
1015
1016 Code = R"cpp(
1017 template <typename T>
1018 struct Waldo {
1019 void find();
1020 };
1021 template <typename T>
1022 using Wally = Waldo<T>;
1023 template <typename T>
1024 void foo(Wally<T> w) {
1025 w.[[find]]();
1026 }
1027 )cpp";
1028 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void find()");
1029
1030 Code = R"cpp(
1031 template <typename T>
1032 struct Waldo {
1033 void find();
1034 };
1035 template <typename T>
1036 struct MetaWaldo {
1037 using Type = Waldo<T>;
1038 };
1039 template <typename T>
1040 void foo(typename MetaWaldo<T>::Type w) {
1041 w.[[find]]();
1042 }
1043 )cpp";
1044 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void find()");
1045
1046 Code = R"cpp(
1047 struct Waldo {
1048 void find();
1049 };
1050 template <typename T>
1051 using Wally = Waldo;
1052 template <typename>
1053 struct S : Wally<int> {
1054 void Foo() { this->[[find]](); }
1055 };
1056 )cpp";
1057 EXPECT_DECLS("MemberExpr", "void find()");
1058
1059 // Base expression is the type of a non-type template parameter
1060 // which is deduced using CTAD.
1061 Code = R"cpp(
1062 template <int N>
1063 struct Waldo {
1064 const int found = N;
1065 };
1066
1067 template <Waldo W>
1068 int test() {
1069 return W.[[found]];
1070 }
1071 )cpp";
1072 EXPECT_DECLS("CXXDependentScopeMemberExpr", "const int found = N");
1073}
1074
1075TEST_F(TargetDeclTest, DependentTypes) {
1076 // Heuristic resolution of dependent type name
1077 Code = R"cpp(
1078 template <typename>
1079 struct A { struct B {}; };
1080
1081 template <typename T>
1082 void foo(typename A<T>::[[B]]);
1083 )cpp";
1084 EXPECT_DECLS("DependentNameTypeLoc", "struct B");
1085
1086 // Heuristic resolution of dependent type name within a NestedNameSpecifierLoc
1087 Code = R"cpp(
1088 template <typename>
1089 struct A { struct B { struct C {}; }; };
1090
1091 template <typename T>
1092 void foo(typename A<T>::[[B]]::C);
1093 )cpp";
1094 EXPECT_DECLS("DependentNameTypeLoc", "struct B");
1095
1096 // Heuristic resolution of dependent type name whose qualifier is also
1097 // dependent
1098 Code = R"cpp(
1099 template <typename>
1100 struct A { struct B { struct C {}; }; };
1101
1102 template <typename T>
1103 void foo(typename A<T>::B::[[C]]);
1104 )cpp";
1105 EXPECT_DECLS("DependentNameTypeLoc", "struct C");
1106
1107 // Heuristic resolution of dependent template name
1108 Code = R"cpp(
1109 template <typename>
1110 struct A {
1111 template <typename> struct B {};
1112 };
1113
1114 template <typename T>
1115 void foo(typename A<T>::template [[B]]<int>);
1116 )cpp";
1117 EXPECT_DECLS("TemplateSpecializationTypeLoc", "template <typename> struct B");
1118
1119 // Dependent name with recursive definition. We don't expect a
1120 // result, but we shouldn't get into a stack overflow either.
1121 Code = R"cpp(
1122 template <int N>
1123 struct waldo {
1124 typedef typename waldo<N - 1>::type::[[next]] type;
1125 };
1126 )cpp";
1127 EXPECT_DECLS("DependentNameTypeLoc", );
1128
1129 // Similar to above but using mutually recursive templates.
1130 Code = R"cpp(
1131 template <int N>
1132 struct odd;
1133
1134 template <int N>
1135 struct even {
1136 using type = typename odd<N - 1>::type::next;
1137 };
1138
1139 template <int N>
1140 struct odd {
1141 using type = typename even<N - 1>::type::[[next]];
1142 };
1143 )cpp";
1144 EXPECT_DECLS("DependentNameTypeLoc", );
1145}
1146
1147TEST_F(TargetDeclTest, TypedefCascade) {
1148 Code = R"cpp(
1149 struct C {
1150 using type = int;
1151 };
1152 struct B {
1153 using type = C::type;
1154 };
1155 struct A {
1156 using type = B::type;
1157 };
1158 A::[[type]] waldo;
1159 )cpp";
1160 EXPECT_DECLS("TypedefTypeLoc",
1161 {"using type = int", Rel::Alias | Rel::Underlying},
1162 {"using type = C::type", Rel::Alias | Rel::Underlying},
1163 {"using type = B::type", Rel::Alias});
1164}
1165
1166TEST_F(TargetDeclTest, RecursiveTemplate) {
1167 Flags.push_back("-std=c++20"); // the test case uses concepts
1168
1169 Code = R"cpp(
1170 template <typename T>
1171 concept Leaf = false;
1172
1173 template <typename Tree>
1174 struct descend_left {
1175 using type = typename descend_left<typename Tree::left>::[[type]];
1176 };
1177
1178 template <Leaf Tree>
1179 struct descend_left<Tree> {
1180 using type = typename Tree::value;
1181 };
1182 )cpp";
1183 EXPECT_DECLS("DependentNameTypeLoc",
1184 {"using type = typename descend_left<typename Tree::left>::type",
1185 Rel::Alias | Rel::Underlying});
1186}
1187
1188TEST_F(TargetDeclTest, ObjC) {
1189 Flags = {"-xobjective-c"};
1190 Code = R"cpp(
1191 @interface Foo {}
1192 -(void)bar;
1193 @end
1194 void test(Foo *f) {
1195 [f [[bar]] ];
1196 }
1197 )cpp";
1198 EXPECT_DECLS("ObjCMessageExpr", "- (void)bar");
1199
1200 Code = R"cpp(
1201 @interface Foo { @public int bar; }
1202 @end
1203 int test(Foo *f) {
1204 return [[f->bar]];
1205 }
1206 )cpp";
1207 EXPECT_DECLS("ObjCIvarRefExpr", "int bar");
1208
1209 Code = R"cpp(
1210 @interface Foo {}
1211 -(int) x;
1212 -(void) setX:(int)x;
1213 @end
1214 void test(Foo *f) {
1215 [[f.x]] = 42;
1216 }
1217 )cpp";
1218 EXPECT_DECLS("ObjCPropertyRefExpr", "- (void)setX:(int)x");
1219
1220 Code = R"cpp(
1221 @interface I {}
1222 @property(retain) I* x;
1223 @property(retain) I* y;
1224 @end
1225 void test(I *f) {
1226 [[f.x]].y = 0;
1227 }
1228 )cpp";
1229 EXPECT_DECLS("ObjCPropertyRefExpr",
1230 "@property(atomic, retain, readwrite) I *x");
1231
1232 Code = R"cpp(
1233 @interface MYObject
1234 @end
1235 @interface Interface
1236 @property(retain) [[MYObject]] *x;
1237 @end
1238 )cpp";
1239 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface MYObject");
1240
1241 Code = R"cpp(
1242 @interface MYObject2
1243 @end
1244 @interface Interface
1245 @property(retain, nonnull) [[MYObject2]] *x;
1246 @end
1247 )cpp";
1248 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface MYObject2");
1249
1250 Code = R"cpp(
1251 @protocol Foo
1252 @end
1253 id test() {
1254 return [[@protocol(Foo)]];
1255 }
1256 )cpp";
1257 EXPECT_DECLS("ObjCProtocolExpr", "@protocol Foo");
1258
1259 Code = R"cpp(
1260 @interface Foo
1261 @end
1262 void test([[Foo]] *p);
1263 )cpp";
1264 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface Foo");
1265
1266 Code = R"cpp(// Don't consider implicit interface as the target.
1267 @implementation [[Implicit]]
1268 @end
1269 )cpp";
1270 EXPECT_DECLS("ObjCImplementationDecl", "@implementation Implicit");
1271
1272 Code = R"cpp(
1273 @interface Foo
1274 @end
1275 @implementation [[Foo]]
1276 @end
1277 )cpp";
1278 EXPECT_DECLS("ObjCImplementationDecl", "@interface Foo");
1279
1280 Code = R"cpp(
1281 @interface Foo
1282 @end
1283 @interface Foo (Ext)
1284 @end
1285 @implementation [[Foo]] (Ext)
1286 @end
1287 )cpp";
1288 EXPECT_DECLS("ObjCCategoryImplDecl", "@interface Foo(Ext)");
1289
1290 Code = R"cpp(
1291 @interface Foo
1292 @end
1293 @interface Foo (Ext)
1294 @end
1295 @implementation Foo ([[Ext]])
1296 @end
1297 )cpp";
1298 EXPECT_DECLS("ObjCCategoryImplDecl", "@interface Foo(Ext)");
1299
1300 Code = R"cpp(
1301 void test(id</*error-ok*/[[InvalidProtocol]]> p);
1302 )cpp";
1303 EXPECT_DECLS("ParmVarDecl", "id p");
1304
1305 Code = R"cpp(
1306 @class C;
1307 @protocol Foo
1308 @end
1309 void test([[C]]<Foo> *p);
1310 )cpp";
1311 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@class C;");
1312
1313 Code = R"cpp(
1314 @class C;
1315 @protocol Foo
1316 @end
1317 void test(C<[[Foo]]> *p);
1318 )cpp";
1319 EXPECT_DECLS("ObjCProtocolLoc", "@protocol Foo");
1320
1321 Code = R"cpp(
1322 @class C;
1323 @protocol Foo
1324 @end
1325 @protocol Bar
1326 @end
1327 void test(C<[[Foo]], Bar> *p);
1328 )cpp";
1329 EXPECT_DECLS("ObjCProtocolLoc", "@protocol Foo");
1330
1331 Code = R"cpp(
1332 @class C;
1333 @protocol Foo
1334 @end
1335 @protocol Bar
1336 @end
1337 void test(C<Foo, [[Bar]]> *p);
1338 )cpp";
1339 EXPECT_DECLS("ObjCProtocolLoc", "@protocol Bar");
1340
1341 Code = R"cpp(
1342 @interface Foo
1343 + (id)sharedInstance;
1344 @end
1345 @implementation Foo
1346 + (id)sharedInstance { return 0; }
1347 @end
1348 void test() {
1349 id value = [[Foo]].sharedInstance;
1350 }
1351 )cpp";
1352 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface Foo");
1353
1354 Code = R"cpp(
1355 @interface Foo
1356 + (id)sharedInstance;
1357 @end
1358 @implementation Foo
1359 + (id)sharedInstance { return 0; }
1360 @end
1361 void test() {
1362 id value = Foo.[[sharedInstance]];
1363 }
1364 )cpp";
1365 EXPECT_DECLS("ObjCPropertyRefExpr", "+ (id)sharedInstance");
1366
1367 Code = R"cpp(
1368 @interface Foo
1369 + ([[id]])sharedInstance;
1370 @end
1371 )cpp";
1372 EXPECT_DECLS("TypedefTypeLoc", );
1373
1374 Code = R"cpp(
1375 @interface Foo
1376 + ([[instancetype]])sharedInstance;
1377 @end
1378 )cpp";
1379 EXPECT_DECLS("TypedefTypeLoc", );
1380}
1381
1382class FindExplicitReferencesTest : public ::testing::Test {
1383protected:
1384 struct AllRefs {
1385 std::string AnnotatedCode;
1386 std::string DumpedReferences;
1387 };
1388
1389 TestTU newTU(llvm::StringRef Code) {
1390 TestTU TU;
1391 TU.Code = std::string(Code);
1392
1393 // FIXME: Auto-completion in a template requires disabling delayed template
1394 // parsing.
1395 TU.ExtraArgs.push_back("-std=c++20");
1396 TU.ExtraArgs.push_back("-xobjective-c++");
1397
1398 return TU;
1399 }
1400
1401 AllRefs annotatedReferences(llvm::StringRef Code, ParsedAST &AST,
1402 std::vector<ReferenceLoc> Refs) {
1403 auto &SM = AST.getSourceManager();
1404 llvm::stable_sort(Refs, [&](const ReferenceLoc &L, const ReferenceLoc &R) {
1405 return SM.isBeforeInTranslationUnit(L.NameLoc, R.NameLoc);
1406 });
1407
1408 std::string AnnotatedCode;
1409 unsigned NextCodeChar = 0;
1410 for (unsigned I = 0; I < Refs.size(); ++I) {
1411 auto &R = Refs[I];
1412
1413 SourceLocation Pos = R.NameLoc;
1414 assert(Pos.isValid());
1415 if (Pos.isMacroID()) // FIXME: figure out how to show macro locations.
1416 Pos = SM.getExpansionLoc(Pos);
1417 assert(Pos.isFileID());
1418
1419 FileID File;
1420 unsigned Offset;
1421 std::tie(File, Offset) = SM.getDecomposedLoc(Pos);
1422 if (File == SM.getMainFileID()) {
1423 // Print the reference in a source code.
1424 assert(NextCodeChar <= Offset);
1425 AnnotatedCode += Code.substr(NextCodeChar, Offset - NextCodeChar);
1426 AnnotatedCode += "$" + std::to_string(I) + "^";
1427
1428 NextCodeChar = Offset;
1429 }
1430 }
1431 AnnotatedCode += Code.substr(NextCodeChar);
1432
1433 std::string DumpedReferences;
1434 for (unsigned I = 0; I < Refs.size(); ++I)
1435 DumpedReferences += std::string(llvm::formatv("{0}: {1}\n", I, Refs[I]));
1436
1437 return AllRefs{std::move(AnnotatedCode), std::move(DumpedReferences)};
1438 }
1439
1440 /// Parses \p Code, and annotates its body with results of
1441 /// findExplicitReferences on all top level decls.
1442 /// See actual tests for examples of annotation format.
1443 AllRefs annotateAllReferences(llvm::StringRef Code) {
1444 TestTU TU = newTU(Code);
1445 auto AST = TU.build();
1446
1447 std::vector<ReferenceLoc> Refs;
1448 for (auto *TopLevel : AST.getLocalTopLevelDecls())
1450 TopLevel, [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1451 AST.getHeuristicResolver());
1452 return annotatedReferences(Code, AST, std::move(Refs));
1453 }
1454
1455 /// Parses \p Code, finds function or namespace '::foo' and annotates its body
1456 /// with results of findExplicitReferences.
1457 /// See actual tests for examples of annotation format.
1458 AllRefs annotateReferencesInFoo(llvm::StringRef Code) {
1459 TestTU TU = newTU(Code);
1460 auto AST = TU.build();
1461 auto *TestDecl = &findDecl(AST, "foo");
1462 if (auto *T = llvm::dyn_cast<FunctionTemplateDecl>(TestDecl))
1463 TestDecl = T->getTemplatedDecl();
1464
1465 std::vector<ReferenceLoc> Refs;
1466 if (const auto *Func = llvm::dyn_cast<FunctionDecl>(TestDecl))
1468 Func->getBody(),
1469 [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1470 AST.getHeuristicResolver());
1471 else if (const auto *NS = llvm::dyn_cast<NamespaceDecl>(TestDecl))
1473 NS,
1474 [&Refs, &NS](ReferenceLoc R) {
1475 // Avoid adding the namespace foo decl to the results.
1476 if (R.Targets.size() == 1 && R.Targets.front() == NS)
1477 return;
1478 Refs.push_back(std::move(R));
1479 },
1480 AST.getHeuristicResolver());
1481 else if (const auto *OC = llvm::dyn_cast<ObjCContainerDecl>(TestDecl))
1483 OC, [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1484 AST.getHeuristicResolver());
1485 else
1486 ADD_FAILURE() << "Failed to find ::foo decl for test";
1487
1488 return annotatedReferences(Code, AST, std::move(Refs));
1489 }
1490};
1491
1492TEST_F(FindExplicitReferencesTest, AllRefsInFoo) {
1493 std::pair</*Code*/ llvm::StringRef, /*References*/ llvm::StringRef> Cases[] =
1494 {// Simple expressions.
1495 {R"cpp(
1496 int global;
1497 int func();
1498 void foo(int param) {
1499 $0^global = $1^param + $2^func();
1500 }
1501 )cpp",
1502 "0: targets = {global}\n"
1503 "1: targets = {param}\n"
1504 "2: targets = {func}\n"},
1505 {R"cpp(
1506 struct X { int a; };
1507 void foo(X x) {
1508 $0^x.$1^a = 10;
1509 }
1510 )cpp",
1511 "0: targets = {x}\n"
1512 "1: targets = {X::a}\n"},
1513 {R"cpp(
1514 // error-ok: testing with broken code
1515 int bar();
1516 int foo() {
1517 return $0^bar() + $1^bar(42);
1518 }
1519 )cpp",
1520 "0: targets = {bar}\n"
1521 "1: targets = {bar}\n"},
1522 // Namespaces and aliases.
1523 {R"cpp(
1524 namespace ns {}
1525 namespace alias = ns;
1526 void foo() {
1527 using namespace $0^ns;
1528 using namespace $1^alias;
1529 }
1530 )cpp",
1531 "0: targets = {ns}\n"
1532 "1: targets = {alias}\n"},
1533 // Using declarations.
1534 {R"cpp(
1535 namespace ns { int global; }
1536 void foo() {
1537 using $0^ns::$1^global;
1538 }
1539 )cpp",
1540 "0: targets = {ns}\n"
1541 "1: targets = {ns::global}, qualifier = 'ns::'\n"},
1542 // Using enum declarations.
1543 {R"cpp(
1544 namespace ns { enum class A {}; }
1545 void foo() {
1546 using enum $0^ns::$1^A;
1547 }
1548 )cpp",
1549 "0: targets = {ns}\n"
1550 "1: targets = {ns::A}, qualifier = 'ns::'\n"},
1551 // Simple types.
1552 {R"cpp(
1553 struct Struct { int a; };
1554 using Typedef = int;
1555 void foo() {
1556 $0^Struct $1^x;
1557 $2^Typedef $3^y;
1558 static_cast<$4^Struct*>(0);
1559 }
1560 )cpp",
1561 "0: targets = {Struct}\n"
1562 "1: targets = {x}, decl\n"
1563 "2: targets = {Typedef}\n"
1564 "3: targets = {y}, decl\n"
1565 "4: targets = {Struct}\n"},
1566 // Name qualifiers.
1567 {R"cpp(
1568 namespace a { namespace b { struct S { typedef int type; }; } }
1569 void foo() {
1570 $0^a::$1^b::$2^S $3^x;
1571 using namespace $4^a::$5^b;
1572 $6^S::$7^type $8^y;
1573 }
1574 )cpp",
1575 "0: targets = {a}\n"
1576 "1: targets = {a::b}, qualifier = 'a::'\n"
1577 "2: targets = {a::b::S}, qualifier = 'a::b::'\n"
1578 "3: targets = {x}, decl\n"
1579 "4: targets = {a}\n"
1580 "5: targets = {a::b}, qualifier = 'a::'\n"
1581 "6: targets = {a::b::S}\n"
1582 "7: targets = {a::b::S::type}, qualifier = 'S::'\n"
1583 "8: targets = {y}, decl\n"},
1584 {R"cpp(
1585 void foo() {
1586 $0^ten: // PRINT "HELLO WORLD!"
1587 goto $1^ten;
1588 }
1589 )cpp",
1590 "0: targets = {ten}, decl\n"
1591 "1: targets = {ten}\n"},
1592 // Simple templates.
1593 {R"cpp(
1594 template <class T> struct vector { using value_type = T; };
1595 template <> struct vector<bool> { using value_type = bool; };
1596 void foo() {
1597 $0^vector<int> $1^vi;
1598 $2^vector<bool> $3^vb;
1599 }
1600 )cpp",
1601 "0: targets = {vector<int>}\n"
1602 "1: targets = {vi}, decl\n"
1603 "2: targets = {vector<bool>}\n"
1604 "3: targets = {vb}, decl\n"},
1605 // Template type aliases.
1606 {R"cpp(
1607 template <class T> struct vector { using value_type = T; };
1608 template <> struct vector<bool> { using value_type = bool; };
1609 template <class T> using valias = vector<T>;
1610 void foo() {
1611 $0^valias<int> $1^vi;
1612 $2^valias<bool> $3^vb;
1613 }
1614 )cpp",
1615 "0: targets = {valias}\n"
1616 "1: targets = {vi}, decl\n"
1617 "2: targets = {valias}\n"
1618 "3: targets = {vb}, decl\n"},
1619 // Injected class name.
1620 {R"cpp(
1621 namespace foo {
1622 template <typename $0^T>
1623 class $1^Bar {
1624 ~$2^Bar();
1625 void $3^f($4^Bar);
1626 };
1627 }
1628 )cpp",
1629 "0: targets = {foo::Bar::T}, decl\n"
1630 "1: targets = {foo::Bar}, decl\n"
1631 "2: targets = {foo::Bar}\n"
1632 "3: targets = {foo::Bar::f}, decl\n"
1633 "4: targets = {foo::Bar}\n"},
1634 // MemberExpr should know their using declaration.
1635 {R"cpp(
1636 struct X { void func(int); };
1637 struct Y : X {
1638 using X::func;
1639 };
1640 void foo(Y y) {
1641 $0^y.$1^func(1);
1642 }
1643 )cpp",
1644 "0: targets = {y}\n"
1645 "1: targets = {Y::func}\n"},
1646 // DeclRefExpr should know their using declaration.
1647 {R"cpp(
1648 namespace ns { void bar(int); }
1649 using ns::bar;
1650
1651 void foo() {
1652 $0^bar(10);
1653 }
1654 )cpp",
1655 "0: targets = {bar}\n"},
1656 // References from a macro.
1657 {R"cpp(
1658 #define FOO a
1659 #define BAR b
1660
1661 void foo(int a, int b) {
1662 $0^FOO+$1^BAR;
1663 }
1664 )cpp",
1665 "0: targets = {a}\n"
1666 "1: targets = {b}\n"},
1667 // No references from implicit nodes.
1668 {R"cpp(
1669 struct vector {
1670 int *begin();
1671 int *end();
1672 };
1673
1674 void foo() {
1675 for (int $0^x : $1^vector()) {
1676 $2^x = 10;
1677 }
1678 }
1679 )cpp",
1680 "0: targets = {x}, decl\n"
1681 "1: targets = {vector}\n"
1682 "2: targets = {x}\n"},
1683 // Handle UnresolvedLookupExpr.
1684 {R"cpp(
1685 namespace ns1 { void func(char*); }
1686 namespace ns2 { void func(int*); }
1687 using namespace ns1;
1688 using namespace ns2;
1689
1690 template <class T>
1691 void foo(T t) {
1692 $0^func($1^t);
1693 }
1694 )cpp",
1695 "0: targets = {ns1::func, ns2::func}\n"
1696 "1: targets = {t}\n"},
1697 // Handle UnresolvedMemberExpr.
1698 {R"cpp(
1699 struct X {
1700 void func(char*);
1701 void func(int*);
1702 };
1703
1704 template <class T>
1705 void foo(X x, T t) {
1706 $0^x.$1^func($2^t);
1707 }
1708 )cpp",
1709 "0: targets = {x}\n"
1710 "1: targets = {X::func, X::func}\n"
1711 "2: targets = {t}\n"},
1712 // Handle DependentScopeDeclRefExpr.
1713 {R"cpp(
1714 template <class T>
1715 struct S {
1716 static int value;
1717 };
1718
1719 template <class T>
1720 void foo() {
1721 $0^S<$1^T>::$2^value;
1722 }
1723 )cpp",
1724 "0: targets = {S}\n"
1725 "1: targets = {T}\n"
1726 "2: targets = {S::value}, qualifier = 'S<T>::'\n"},
1727 // Handle CXXDependentScopeMemberExpr.
1728 {R"cpp(
1729 template <class T>
1730 struct S {
1731 int value;
1732 };
1733
1734 template <class T>
1735 void foo(S<T> t) {
1736 $0^t.$1^value;
1737 }
1738 )cpp",
1739 "0: targets = {t}\n"
1740 "1: targets = {S::value}\n"},
1741 // Type template parameters.
1742 {R"cpp(
1743 template <class T>
1744 void foo() {
1745 static_cast<$0^T>(0);
1746 $1^T();
1747 $2^T $3^t;
1748 }
1749 )cpp",
1750 "0: targets = {T}\n"
1751 "1: targets = {T}\n"
1752 "2: targets = {T}\n"
1753 "3: targets = {t}, decl\n"},
1754 // Non-type template parameters.
1755 {R"cpp(
1756 template <int I>
1757 void foo() {
1758 int $0^x = $1^I;
1759 }
1760 )cpp",
1761 "0: targets = {x}, decl\n"
1762 "1: targets = {I}\n"},
1763 // Template template parameters.
1764 {R"cpp(
1765 template <class T> struct vector {};
1766
1767 template <template<class> class TT, template<class> class ...TP>
1768 void foo() {
1769 $0^TT<int> $1^x;
1770 $2^foo<$3^TT>();
1771 $4^foo<$5^vector>();
1772 $6^foo<$7^TP...>();
1773 }
1774 )cpp",
1775 "0: targets = {TT}\n"
1776 "1: targets = {x}, decl\n"
1777 "2: targets = {foo}\n"
1778 "3: targets = {TT}\n"
1779 "4: targets = {foo}\n"
1780 "5: targets = {vector}\n"
1781 "6: targets = {foo}\n"
1782 "7: targets = {TP}\n"},
1783 // Non-type template parameters with declarations.
1784 {R"cpp(
1785 int func();
1786 template <int(*)()> struct wrapper {};
1787
1788 template <int(*FuncParam)()>
1789 void foo() {
1790 $0^wrapper<$1^func> $2^w;
1791 $3^FuncParam();
1792 }
1793 )cpp",
1794 "0: targets = {wrapper<&func>}\n"
1795 "1: targets = {func}\n"
1796 "2: targets = {w}, decl\n"
1797 "3: targets = {FuncParam}\n"},
1798 // declaration references.
1799 {R"cpp(
1800 namespace ns {}
1801 class S {};
1802 void foo() {
1803 class $0^Foo { $1^Foo(); ~$2^Foo(); int $3^field; };
1804 int $4^Var;
1805 enum $5^E { $6^ABC };
1806 typedef int $7^INT;
1807 using $8^INT2 = int;
1808 namespace $9^NS = $10^ns;
1809 }
1810 )cpp",
1811 "0: targets = {Foo}, decl\n"
1812 "1: targets = {foo()::Foo::Foo}, decl\n"
1813 "2: targets = {Foo}\n"
1814 "3: targets = {foo()::Foo::field}, decl\n"
1815 "4: targets = {Var}, decl\n"
1816 "5: targets = {E}, decl\n"
1817 "6: targets = {foo()::ABC}, decl\n"
1818 "7: targets = {INT}, decl\n"
1819 "8: targets = {INT2}, decl\n"
1820 "9: targets = {NS}, decl\n"
1821 "10: targets = {ns}\n"},
1822 // User-defined conversion operator.
1823 {R"cpp(
1824 void foo() {
1825 class $0^Bar {};
1826 class $1^Foo {
1827 public:
1828 // FIXME: This should have only one reference to Bar.
1829 $2^operator $3^$4^Bar();
1830 };
1831
1832 $5^Foo $6^f;
1833 $7^f.$8^operator $9^Bar();
1834 }
1835 )cpp",
1836 "0: targets = {Bar}, decl\n"
1837 "1: targets = {Foo}, decl\n"
1838 "2: targets = {foo()::Foo::operator Bar}, decl\n"
1839 "3: targets = {Bar}\n"
1840 "4: targets = {Bar}\n"
1841 "5: targets = {Foo}\n"
1842 "6: targets = {f}, decl\n"
1843 "7: targets = {f}\n"
1844 "8: targets = {foo()::Foo::operator Bar}\n"
1845 "9: targets = {Bar}\n"},
1846 // Destructor.
1847 {R"cpp(
1848 void foo() {
1849 class $0^Foo {
1850 public:
1851 ~$1^Foo() {}
1852
1853 void $2^destructMe() {
1854 this->~$3^Foo();
1855 }
1856 };
1857
1858 $4^Foo $5^f;
1859 $6^f.~ /*...*/ $7^Foo();
1860 }
1861 )cpp",
1862 "0: targets = {Foo}, decl\n"
1863 // FIXME: It's better to target destructor's FunctionDecl instead of
1864 // the type itself (similar to constructor).
1865 "1: targets = {Foo}\n"
1866 "2: targets = {foo()::Foo::destructMe}, decl\n"
1867 "3: targets = {Foo}\n"
1868 "4: targets = {Foo}\n"
1869 "5: targets = {f}, decl\n"
1870 "6: targets = {f}\n"
1871 "7: targets = {Foo}\n"},
1872 // cxx constructor initializer.
1873 {R"cpp(
1874 class Base {};
1875 void foo() {
1876 // member initializer
1877 class $0^X {
1878 int $1^abc;
1879 $2^X(): $3^abc() {}
1880 };
1881 // base initializer
1882 class $4^Derived : public $5^Base {
1883 $6^Base $7^B;
1884 $8^Derived() : $9^Base() {}
1885 };
1886 // delegating initializer
1887 class $10^Foo {
1888 $11^Foo(int);
1889 $12^Foo(): $13^Foo(111) {}
1890 };
1891 }
1892 )cpp",
1893 "0: targets = {X}, decl\n"
1894 "1: targets = {foo()::X::abc}, decl\n"
1895 "2: targets = {foo()::X::X}, decl\n"
1896 "3: targets = {foo()::X::abc}\n"
1897 "4: targets = {Derived}, decl\n"
1898 "5: targets = {Base}\n"
1899 "6: targets = {Base}\n"
1900 "7: targets = {foo()::Derived::B}, decl\n"
1901 "8: targets = {foo()::Derived::Derived}, decl\n"
1902 "9: targets = {Base}\n"
1903 "10: targets = {Foo}, decl\n"
1904 "11: targets = {foo()::Foo::Foo}, decl\n"
1905 "12: targets = {foo()::Foo::Foo}, decl\n"
1906 "13: targets = {Foo}\n"},
1907 // Anonymous entities should not be reported.
1908 {
1909 R"cpp(
1910 void foo() {
1911 $0^class {} $1^x;
1912 int (*$2^fptr)(int $3^a, int) = nullptr;
1913 }
1914 )cpp",
1915 "0: targets = {(unnamed class)}\n"
1916 "1: targets = {x}, decl\n"
1917 "2: targets = {fptr}, decl\n"
1918 "3: targets = {a}, decl\n"},
1919 // Namespace aliases should be handled properly.
1920 {
1921 R"cpp(
1922 namespace ns { struct Type {}; }
1923 namespace alias = ns;
1924 namespace rec_alias = alias;
1925
1926 void foo() {
1927 $0^ns::$1^Type $2^a;
1928 $3^alias::$4^Type $5^b;
1929 $6^rec_alias::$7^Type $8^c;
1930 }
1931 )cpp",
1932 "0: targets = {ns}\n"
1933 "1: targets = {ns::Type}, qualifier = 'ns::'\n"
1934 "2: targets = {a}, decl\n"
1935 "3: targets = {alias}\n"
1936 "4: targets = {ns::Type}, qualifier = 'alias::'\n"
1937 "5: targets = {b}, decl\n"
1938 "6: targets = {rec_alias}\n"
1939 "7: targets = {ns::Type}, qualifier = 'rec_alias::'\n"
1940 "8: targets = {c}, decl\n"},
1941 // Handle SizeOfPackExpr.
1942 {
1943 R"cpp(
1944 template <typename... E>
1945 void foo() {
1946 constexpr int $0^size = sizeof...($1^E);
1947 };
1948 )cpp",
1949 "0: targets = {size}, decl\n"
1950 "1: targets = {E}\n"},
1951 // Class template argument deduction
1952 {
1953 R"cpp(
1954 template <typename T>
1955 struct Test {
1956 Test(T);
1957 };
1958 void foo() {
1959 $0^Test $1^a(5);
1960 }
1961 )cpp",
1962 "0: targets = {Test}\n"
1963 "1: targets = {a}, decl\n"},
1964 // Templates
1965 {R"cpp(
1966 namespace foo {
1967 template <typename $0^T>
1968 class $1^Bar {};
1969 }
1970 )cpp",
1971 "0: targets = {foo::Bar::T}, decl\n"
1972 "1: targets = {foo::Bar}, decl\n"},
1973 // Templates
1974 {R"cpp(
1975 namespace foo {
1976 template <typename $0^T>
1977 void $1^func();
1978 }
1979 )cpp",
1980 "0: targets = {T}, decl\n"
1981 "1: targets = {foo::func}, decl\n"},
1982 // Templates
1983 {R"cpp(
1984 namespace foo {
1985 template <typename $0^T>
1986 $1^T $2^x;
1987 }
1988 )cpp",
1989 "0: targets = {foo::T}, decl\n"
1990 "1: targets = {foo::T}\n"
1991 "2: targets = {foo::x}, decl\n"},
1992 // Templates
1993 {R"cpp(
1994 template<typename T> class vector {};
1995 namespace foo {
1996 template <typename $0^T>
1997 using $1^V = $2^vector<$3^T>;
1998 }
1999 )cpp",
2000 "0: targets = {foo::T}, decl\n"
2001 "1: targets = {foo::V}, decl\n"
2002 "2: targets = {vector}\n"
2003 "3: targets = {foo::T}\n"},
2004 // Concept
2005 {
2006 R"cpp(
2007 template <typename T>
2008 concept Drawable = requires (T t) { t.draw(); };
2009
2010 namespace foo {
2011 template <typename $0^T> requires $1^Drawable<$2^T>
2012 void $3^bar($4^T $5^t) {
2013 $6^t.$7^draw();
2014 }
2015 }
2016 )cpp",
2017 "0: targets = {T}, decl\n"
2018 "1: targets = {Drawable}\n"
2019 "2: targets = {T}\n"
2020 "3: targets = {foo::bar}, decl\n"
2021 "4: targets = {T}\n"
2022 "5: targets = {t}, decl\n"
2023 "6: targets = {t}\n"
2024 "7: targets = {}\n"},
2025 // Objective-C: instance variables
2026 {
2027 R"cpp(
2028 @interface I {
2029 @public
2030 I *_z;
2031 }
2032 @end
2033 I *f;
2034 void foo() {
2035 $0^f->$1^_z = 0;
2036 }
2037 )cpp",
2038 "0: targets = {f}\n"
2039 "1: targets = {I::_z}\n"},
2040 // Objective-C: properties
2041 {
2042 R"cpp(
2043 @interface I {}
2044 @property(retain) I* x;
2045 @property(retain) I* y;
2046 @end
2047 I *f;
2048 void foo() {
2049 $0^f.$1^x.$2^y = 0;
2050 }
2051 )cpp",
2052 "0: targets = {f}\n"
2053 "1: targets = {I::x}\n"
2054 "2: targets = {I::y}\n"},
2055 // Objective-C: implicit properties
2056 {
2057 R"cpp(
2058 @interface I {}
2059 -(I*)x;
2060 -(void)setY:(I*)y;
2061 @end
2062 I *f;
2063 void foo() {
2064 $0^f.$1^x.$2^y = 0;
2065 }
2066 )cpp",
2067 "0: targets = {f}\n"
2068 "1: targets = {I::x}\n"
2069 "2: targets = {I::setY:}\n"},
2070 // Objective-C: class properties
2071 {
2072 R"cpp(
2073 @interface I {}
2074 @property(class) I *x;
2075 @end
2076 id local;
2077 void foo() {
2078 $0^I.$1^x = 0;
2079 $2^local = $3^I.$4^x;
2080 }
2081 )cpp",
2082 "0: targets = {I}\n"
2083 "1: targets = {I::setX:}\n"
2084 "2: targets = {local}\n"
2085 "3: targets = {I}\n"
2086 "4: targets = {I::x}\n"},
2087 // Objective-C: implicit class properties
2088 {
2089 R"cpp(
2090 @interface I {}
2091 +(I*)x;
2092 +(void)setX:(I*)x;
2093 @end
2094 id local;
2095 void foo() {
2096 $0^I.$1^x = 0;
2097 $2^local = $3^I.$4^x;
2098 }
2099 )cpp",
2100 "0: targets = {I}\n"
2101 "1: targets = {I::setX:}\n"
2102 "2: targets = {local}\n"
2103 "3: targets = {I}\n"
2104 "4: targets = {I::x}\n"},
2105 {// Objective-C: methods
2106 R"cpp(
2107 @interface I
2108 -(void) a:(int)x b:(int)y;
2109 @end
2110 void foo(I *i) {
2111 [$0^i $1^a:1 b:2];
2112 }
2113 )cpp",
2114 "0: targets = {i}\n"
2115 "1: targets = {I::a:b:}\n"},
2116 {// Objective-C: protocols
2117 R"cpp(
2118 @interface I
2119 @end
2120 @protocol P
2121 @end
2122 void foo() {
2123 $0^I<$1^P> *$2^x;
2124 }
2125 )cpp",
2126 "0: targets = {I}\n"
2127 "1: targets = {P}\n"
2128 "2: targets = {x}, decl\n"},
2129
2130 // Designated initializers.
2131 {R"cpp(
2132 void foo() {
2133 struct $0^Foo {
2134 int $1^Bar;
2135 };
2136 $2^Foo $3^f { .$4^Bar = 42 };
2137 }
2138 )cpp",
2139 "0: targets = {Foo}, decl\n"
2140 "1: targets = {foo()::Foo::Bar}, decl\n"
2141 "2: targets = {Foo}\n"
2142 "3: targets = {f}, decl\n"
2143 "4: targets = {foo()::Foo::Bar}\n"},
2144 {R"cpp(
2145 void foo() {
2146 struct $0^Baz {
2147 int $1^Field;
2148 };
2149 struct $2^Bar {
2150 $3^Baz $4^Foo;
2151 };
2152 $5^Bar $6^bar { .$7^Foo.$8^Field = 42 };
2153 }
2154 )cpp",
2155 "0: targets = {Baz}, decl\n"
2156 "1: targets = {foo()::Baz::Field}, decl\n"
2157 "2: targets = {Bar}, decl\n"
2158 "3: targets = {Baz}\n"
2159 "4: targets = {foo()::Bar::Foo}, decl\n"
2160 "5: targets = {Bar}\n"
2161 "6: targets = {bar}, decl\n"
2162 "7: targets = {foo()::Bar::Foo}\n"
2163 "8: targets = {foo()::Baz::Field}\n"},
2164 // offsetof
2165 {R"cpp(
2166 void foo() {
2167 struct $0^Foo { int $1^bar; };
2168 int $2^x = __builtin_offsetof($3^Foo, $4^bar);
2169 }
2170 )cpp",
2171 "0: targets = {Foo}, decl\n"
2172 "1: targets = {foo()::Foo::bar}, decl\n"
2173 "2: targets = {x}, decl\n"
2174 "3: targets = {Foo}\n"
2175 "4: targets = {foo()::Foo::bar}\n"},
2176 // offsetof with a nested field designator -- each component must
2177 // resolve to its own source position, not a shared one.
2178 {R"cpp(
2179 void foo() {
2180 struct $0^A {
2181 $1^struct { int $2^c; } $3^B;
2182 };
2183 int $4^x = __builtin_offsetof($5^A, $6^B.$7^c);
2184 }
2185 )cpp",
2186 "0: targets = {A}, decl\n"
2187 "1: targets = {foo()::A::(unnamed struct)}\n"
2188 "2: targets = {foo()::A::(unnamed struct)::c}, decl\n"
2189 "3: targets = {foo()::A::B}, decl\n"
2190 "4: targets = {x}, decl\n"
2191 "5: targets = {A}\n"
2192 "6: targets = {foo()::A::B}\n"
2193 "7: targets = {foo()::A::(unnamed struct)::c}\n"},
2194 // offsetof with an array-subscript component -- array indices are not
2195 // emitted as offsetof references (the subscript expression is still
2196 // visited independently).
2197 {R"cpp(
2198 void foo() {
2199 struct $0^A { int $1^arr[4]; };
2200 int $2^i = 0;
2201 int $3^x = __builtin_offsetof($4^A, $5^arr[$6^i]);
2202 }
2203 )cpp",
2204 "0: targets = {A}, decl\n"
2205 "1: targets = {foo()::A::arr}, decl\n"
2206 "2: targets = {i}, decl\n"
2207 "3: targets = {x}, decl\n"
2208 "4: targets = {A}\n"
2209 "5: targets = {foo()::A::arr}\n"
2210 "6: targets = {i}\n"},
2211 {R"cpp(
2212 template<typename T>
2213 void crash(T);
2214 template<typename T>
2215 void foo() {
2216 $0^crash({.$1^x = $2^T()});
2217 }
2218 )cpp",
2219 "0: targets = {crash}\n"
2220 "1: targets = {}\n"
2221 "2: targets = {T}\n"},
2222 // unknown template name should not crash.
2223 {R"cpp(
2224 template <template <typename> typename T>
2225 struct Base {};
2226 namespace foo {
2227 template <typename $0^T>
2228 struct $1^Derive : $2^Base<$3^T::template $4^Unknown> {};
2229 }
2230 )cpp",
2231 "0: targets = {foo::Derive::T}, decl\n"
2232 "1: targets = {foo::Derive}, decl\n"
2233 "2: targets = {Base}\n"
2234 "3: targets = {foo::Derive::T}\n"
2235 "4: targets = {}, qualifier = 'T::'\n"},
2236 // deduction guide
2237 {R"cpp(
2238 namespace foo {
2239 template <typename $0^T>
2240 struct $1^Test {
2241 template <typename $2^I>
2242 $3^Test($4^I);
2243 };
2244 template <typename $5^I>
2245 $6^Test($7^I) -> $8^Test<typename $9^I::$10^type>;
2246 }
2247 )cpp",
2248 "0: targets = {T}, decl\n"
2249 "1: targets = {foo::Test}, decl\n"
2250 "2: targets = {I}, decl\n"
2251 "3: targets = {foo::Test::Test<T>}, decl\n"
2252 "4: targets = {I}\n"
2253 "5: targets = {I}, decl\n"
2254 "6: targets = {foo::Test}\n"
2255 "7: targets = {I}\n"
2256 "8: targets = {foo::Test}\n"
2257 "9: targets = {I}\n"
2258 "10: targets = {}, qualifier = 'I::'\n"}};
2259
2260 for (const auto &C : Cases) {
2261 llvm::StringRef ExpectedCode = C.first;
2262 llvm::StringRef ExpectedRefs = C.second;
2263
2264 auto Actual =
2265 annotateReferencesInFoo(llvm::Annotations(ExpectedCode).code());
2266 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
2267 EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
2268 }
2269}
2270
2271TEST_F(FindExplicitReferencesTest, AllRefs) {
2272 std::pair</*Code*/ llvm::StringRef, /*References*/ llvm::StringRef> Cases[] =
2273 {{R"cpp(
2274 @interface $0^MyClass
2275 @end
2276 @implementation $1^$2^MyClass
2277 @end
2278 )cpp",
2279 "0: targets = {MyClass}, decl\n"
2280 "1: targets = {MyClass}\n"
2281 "2: targets = {MyClass}, decl\n"},
2282 {R"cpp(
2283 @interface $0^MyClass
2284 @end
2285 @interface $1^MyClass ($2^Category)
2286 @end
2287 @implementation $3^MyClass ($4^$5^Category)
2288 @end
2289 )cpp",
2290 "0: targets = {MyClass}, decl\n"
2291 "1: targets = {MyClass}\n"
2292 "2: targets = {Category}, decl\n"
2293 "3: targets = {MyClass}\n"
2294 "4: targets = {Category}\n"
2295 "5: targets = {Category}, decl\n"}};
2296
2297 for (const auto &C : Cases) {
2298 llvm::StringRef ExpectedCode = C.first;
2299 llvm::StringRef ExpectedRefs = C.second;
2300
2301 auto Actual = annotateAllReferences(llvm::Annotations(ExpectedCode).code());
2302 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
2303 EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
2304 }
2305}
2306
2307} // namespace
2308} // namespace clangd
2309} // namespace clang
#define EXPECT_DECLS(NodeType,...)
static SelectionTree createRight(ASTContext &AST, const syntax::TokenBuffer &Tokens, unsigned Begin, unsigned End)
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition TestTU.cpp:220
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 ...
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.
bool operator==(const Inclusion &LHS, const Inclusion &RHS)
Definition Headers.cpp:356
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static TestTU withCode(llvm::StringRef Code)
Definition TestTU.h:36