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 // Deduced specialization of a template template parameter
598 template <template<typename> class X>
599 void foo() {
600 [[X]] a;
601 }
602 )cpp";
603 EXPECT_DECLS("DeducedTemplateSpecializationTypeLoc",
604 "template <typename> class X");
605
606 Code = R"cpp(
607 // Deduction guide
608 template <typename T>
609 struct Test {
610 template <typename I>
611 Test(I, I);
612 };
613 template <typename I>
614 [[Test]](I, I) -> Test<typename I::type>;
615 )cpp";
616 EXPECT_DECLS("CXXDeductionGuideDecl", {"template <typename T> struct Test"});
617}
618
619TEST_F(TargetDeclTest, Concept) {
620 Flags.push_back("-std=c++20");
621
622 // FIXME: Should we truncate the pretty-printed form of a concept decl
623 // somewhere?
624
625 Code = R"cpp(
626 template <typename T>
627 concept Fooable = requires (T t) { t.foo(); };
628
629 template <typename T> requires [[Fooable]]<T>
630 void bar(T t) {
631 t.foo();
632 }
633 )cpp";
635 "ConceptReference",
636 {"template <typename T> concept Fooable = requires (T t) { t.foo(); }"});
637
638 // trailing requires clause
639 Code = R"cpp(
640 template <typename T>
641 concept Fooable = true;
642
643 template <typename T>
644 void foo() requires [[Fooable]]<T>;
645 )cpp";
646 EXPECT_DECLS("ConceptReference",
647 {"template <typename T> concept Fooable = true"});
648
649 // constrained-parameter
650 Code = R"cpp(
651 template <typename T>
652 concept Fooable = true;
653
654 template <[[Fooable]] T>
655 void bar(T t);
656 )cpp";
657 EXPECT_DECLS("ConceptReference",
658 {"template <typename T> concept Fooable = true"});
659
660 // partial-concept-id
661 Code = R"cpp(
662 template <typename T, typename U>
663 concept Fooable = true;
664
665 template <[[Fooable]]<int> T>
666 void bar(T t);
667 )cpp";
668 EXPECT_DECLS("ConceptReference",
669 {"template <typename T, typename U> concept Fooable = true"});
670}
671
672TEST_F(TargetDeclTest, PackIndexing) {
673 Flags.push_back("-std=c++2d");
674
675 Code = R"cpp(
676 // Deduced specialization of an indexed template template parameter pack
677 template <template <typename> class... X>
678 void foo() {
679 [[X]]...[0] a(1);
680 }
681 )cpp";
682 EXPECT_DECLS("DeducedTemplateSpecializationTypeLoc",
683 "template <typename> class ...X");
684
685 Code = R"cpp(
686 // Specialization of an indexed template template parameter pack
687 template <template <typename> class... X>
688 void foo() {
689 [[X]]...[0]<int> x;
690 }
691 )cpp";
692 EXPECT_DECLS("TemplateSpecializationTypeLoc",
693 "template <typename> class ...X");
694}
695
696TEST_F(TargetDeclTest, PackIndexedConcept) {
697 Flags.push_back("-std=c++2d");
698
699 // constrained-parameter
700 Code = R"cpp(
701 template <template <class> concept... CC>
702 struct S {
703 template <[[CC]]...[0] T>
704 void bar(T t);
705 };
706 )cpp";
707 EXPECT_DECLS("ConceptReference", {"template <class> concept ...CC"});
708
709 // constrained placeholder type
710 Code = R"cpp(
711 template <template <class> concept... CC>
712 void bar([[CC]]...[0] auto t);
713 )cpp";
714 EXPECT_DECLS("ConceptReference", {"template <class> concept ...CC"});
715}
716
717TEST_F(TargetDeclTest, Coroutine) {
718 Flags.push_back("-std=c++20");
719
720 Code = R"cpp(
721 namespace std {
722 template <typename, typename...> struct coroutine_traits;
723 template <typename> struct coroutine_handle {
724 template <typename U>
725 coroutine_handle(coroutine_handle<U>&&) noexcept;
726 static coroutine_handle from_address(void* __addr) noexcept;
727 };
728 } // namespace std
729
730 struct executor {};
731 struct awaitable {};
732 struct awaitable_frame {
733 awaitable get_return_object();
734 void return_void();
735 void unhandled_exception();
736 struct result_t {
737 ~result_t();
738 bool await_ready() const noexcept;
739 void await_suspend(std::coroutine_handle<void>) noexcept;
740 void await_resume() const noexcept;
741 };
742 result_t initial_suspend() noexcept;
743 result_t final_suspend() noexcept;
744 result_t await_transform(executor) noexcept;
745 };
746
747 namespace std {
748 template <>
749 struct coroutine_traits<awaitable> {
750 typedef awaitable_frame promise_type;
751 };
752 } // namespace std
753
754 awaitable foo() {
755 co_await [[executor]]();
756 }
757 )cpp";
758 EXPECT_DECLS("RecordTypeLoc", "struct executor");
759}
760
761TEST_F(TargetDeclTest, RewrittenBinaryOperator) {
762 Flags.push_back("-std=c++20");
763
764 Code = R"cpp(
765 namespace std {
766 struct strong_ordering {
767 int n;
768 constexpr operator int() const { return n; }
769 static const strong_ordering equal, greater, less;
770 };
771 constexpr strong_ordering strong_ordering::equal = {0};
772 constexpr strong_ordering strong_ordering::greater = {1};
773 constexpr strong_ordering strong_ordering::less = {-1};
774 }
775
776 struct Foo
777 {
778 int x;
779 auto operator<=>(const Foo&) const = default;
780 };
781
782 bool x = (Foo(1) [[!=]] Foo(2));
783 )cpp";
784 EXPECT_DECLS("CXXRewrittenBinaryOperator",
785 {"std::strong_ordering operator<=>(const Foo &) const = default",
786 Rel::TemplatePattern},
787 {"bool operator==(const Foo &) const noexcept = default",
788 Rel::TemplateInstantiation});
789}
790
791TEST_F(TargetDeclTest, FunctionTemplate) {
792 Code = R"cpp(
793 // Implicit specialization.
794 template<typename T> bool foo(T) { return false; };
795 bool x = [[foo]](42);
796 )cpp";
797 EXPECT_DECLS("DeclRefExpr",
798 {"template<> bool foo<int>(int)", Rel::TemplateInstantiation},
799 {"bool foo(T)", Rel::TemplatePattern});
800
801 Code = R"cpp(
802 // Explicit specialization.
803 template<typename T> bool foo(T) { return false; };
804 template<> bool foo<int>(int) { return false; };
805 bool x = [[foo]](42);
806 )cpp";
807 EXPECT_DECLS("DeclRefExpr", "template<> bool foo<int>(int)");
808}
809
810TEST_F(TargetDeclTest, VariableTemplate) {
811 // Pretty-printer doesn't do a very good job of variable templates :-(
812 Code = R"cpp(
813 // Implicit specialization.
814 template<typename T> int foo;
815 int x = [[foo]]<char>;
816 )cpp";
817 EXPECT_DECLS("DeclRefExpr", {"int foo", Rel::TemplateInstantiation},
818 {"int foo", Rel::TemplatePattern});
819
820 Code = R"cpp(
821 // Explicit specialization.
822 template<typename T> int foo;
823 template <> bool foo<char>;
824 int x = [[foo]]<char>;
825 )cpp";
826 EXPECT_DECLS("DeclRefExpr", "bool foo");
827
828 Code = R"cpp(
829 // Partial specialization.
830 template<typename T> int foo;
831 template<typename T> bool foo<T*>;
832 bool x = [[foo]]<char*>;
833 )cpp";
834 EXPECT_DECLS("DeclRefExpr", {"bool foo", Rel::TemplateInstantiation},
835 {"bool foo", Rel::TemplatePattern});
836}
837
838TEST_F(TargetDeclTest, TypeAliasTemplate) {
839 Code = R"cpp(
840 template<typename T, int X> class SmallVector {};
841 template<typename U> using TinyVector = SmallVector<U, 1>;
842 [[TinyVector<int>]] X;
843 )cpp";
844 EXPECT_DECLS("TemplateSpecializationTypeLoc",
845 {"template<> class SmallVector<int, 1>",
846 Rel::TemplateInstantiation | Rel::Underlying},
847 {"class SmallVector", Rel::TemplatePattern | Rel::Underlying},
848 {"using TinyVector = SmallVector<U, 1>",
849 Rel::Alias | Rel::TemplatePattern});
850}
851
852TEST_F(TargetDeclTest, BuiltinTemplates) {
853 Code = R"cpp(
854 template <class T, T... Index> struct integer_sequence {};
855 [[__make_integer_seq]]<integer_sequence, int, 3> X;
856 )cpp";
858 "TemplateSpecializationTypeLoc",
859 {"struct integer_sequence", Rel::TemplatePattern | Rel::Underlying},
860 {"template<> struct integer_sequence<int, <0, 1, 2>>",
861 Rel::TemplateInstantiation | Rel::Underlying});
862
863 // Dependent context.
864 Code = R"cpp(
865 template <class T, T... Index> struct integer_sequence;
866
867 template <class T, int N>
868 using make_integer_sequence = [[__make_integer_seq]]<integer_sequence, T, N>;
869 )cpp";
870 EXPECT_DECLS("TemplateSpecializationTypeLoc", );
871
872 Code = R"cpp(
873 template <int N, class... Pack>
874 using type_pack_element = [[__type_pack_element]]<N, Pack...>;
875 )cpp";
876 EXPECT_DECLS("TemplateSpecializationTypeLoc", );
877
878 Code = R"cpp(
879 template <template <class...> class Templ, class... Types>
880 using dedup_types = Templ<[[__builtin_dedup_pack]]<Types...>...>;
881 )cpp";
882 EXPECT_DECLS("TemplateSpecializationTypeLoc", );
883
884 Code = R"cpp(
885 template <template <class...> class Templ, class... Types>
886 using sort_types = Templ<[[__builtin_sort_pack]]<Types...>...>;
887 )cpp";
888 EXPECT_DECLS("TemplateSpecializationTypeLoc", );
889}
890
891TEST_F(TargetDeclTest, MemberOfTemplate) {
892 Code = R"cpp(
893 template <typename T> struct Foo {
894 int x(T);
895 };
896 int y = Foo<int>().[[x]](42);
897 )cpp";
898 EXPECT_DECLS("MemberExpr", {"int x(int)", Rel::TemplateInstantiation},
899 {"int x(T)", Rel::TemplatePattern});
900
901 Code = R"cpp(
902 template <typename T> struct Foo {
903 template <typename U>
904 int x(T, U);
905 };
906 int y = Foo<char>().[[x]]('c', 42);
907 )cpp";
908 EXPECT_DECLS("MemberExpr",
909 {"template<> int x<int>(char, int)", Rel::TemplateInstantiation},
910 {"int x(T, U)", Rel::TemplatePattern});
911}
912
913TEST_F(TargetDeclTest, Lambda) {
914 Code = R"cpp(
915 void foo(int x = 42) {
916 auto l = [ [[x]] ]{ return x + 1; };
917 };
918 )cpp";
919 EXPECT_DECLS("DeclRefExpr", "int x = 42");
920
921 // It seems like this should refer to another var, with the outer param being
922 // an underlying decl. But it doesn't seem to exist.
923 Code = R"cpp(
924 void foo(int x = 42) {
925 auto l = [x]{ return [[x]] + 1; };
926 };
927 )cpp";
928 EXPECT_DECLS("DeclRefExpr", "int x = 42");
929
930 Code = R"cpp(
931 void foo() {
932 auto l = [x = 1]{ return [[x]] + 1; };
933 };
934 )cpp";
935 // FIXME: why both auto and int?
936 EXPECT_DECLS("DeclRefExpr", "auto int x = 1");
937}
938
939TEST_F(TargetDeclTest, OverloadExpr) {
940 Flags.push_back("--target=x86_64-pc-linux-gnu");
941
942 Code = R"cpp(
943 void func(int*);
944 void func(char*);
945
946 template <class T>
947 void foo(T t) {
948 [[func]](t);
949 };
950 )cpp";
951 EXPECT_DECLS("UnresolvedLookupExpr", "void func(int *)", "void func(char *)");
952
953 Code = R"cpp(
954 struct X {
955 void func(int*);
956 void func(char*);
957 };
958
959 template <class T>
960 void foo(X x, T t) {
961 x.[[func]](t);
962 };
963 )cpp";
964 EXPECT_DECLS("UnresolvedMemberExpr", "void func(int *)", "void func(char *)");
965
966 Code = R"cpp(
967 struct X {
968 static void *operator new(unsigned long);
969 };
970 auto* k = [[new]] X();
971 )cpp";
972 EXPECT_DECLS("CXXNewExpr", "static void *operator new(unsigned long)");
973 Code = R"cpp(
974 void *operator new(unsigned long);
975 auto* k = [[new]] int();
976 )cpp";
977 EXPECT_DECLS("CXXNewExpr", "void *operator new(unsigned long)");
978
979 Code = R"cpp(
980 struct X {
981 static void operator delete(void *) noexcept;
982 };
983 void k(X* x) {
984 [[delete]] x;
985 }
986 )cpp";
987 EXPECT_DECLS("CXXDeleteExpr", "static void operator delete(void *) noexcept");
988 Code = R"cpp(
989 void operator delete(void *) noexcept;
990 void k(int* x) {
991 [[delete]] x;
992 }
993 )cpp";
994 // Sized deallocation is enabled by default in C++14 onwards.
995 EXPECT_DECLS("CXXDeleteExpr",
996 "void operator delete(void *, __size_t) noexcept");
997}
998
999TEST_F(TargetDeclTest, DependentExprs) {
1000 Flags.push_back("--std=c++20");
1001
1002 // Heuristic resolution of method of dependent field
1003 Code = R"cpp(
1004 struct A { void foo() {} };
1005 template <typename T>
1006 struct B {
1007 A a;
1008 void bar() {
1009 this->a.[[foo]]();
1010 }
1011 };
1012 )cpp";
1013 EXPECT_DECLS("MemberExpr", "void foo()");
1014
1015 // Similar to above but base expression involves a function call.
1016 Code = R"cpp(
1017 struct A {
1018 void foo() {}
1019 };
1020 struct B {
1021 A getA();
1022 };
1023 template <typename T>
1024 struct C {
1025 B c;
1026 void bar() {
1027 this->c.getA().[[foo]]();
1028 }
1029 };
1030 )cpp";
1031 EXPECT_DECLS("MemberExpr", "void foo()");
1032
1033 // Similar to above but uses a function pointer.
1034 Code = R"cpp(
1035 struct A {
1036 void foo() {}
1037 };
1038 struct B {
1039 using FPtr = A(*)();
1040 FPtr fptr;
1041 };
1042 template <typename T>
1043 struct C {
1044 B c;
1045 void bar() {
1046 this->c.fptr().[[foo]]();
1047 }
1048 };
1049 )cpp";
1050 EXPECT_DECLS("MemberExpr", "void foo()");
1051
1052 // Base expression involves a member access into this.
1053 Code = R"cpp(
1054 struct Bar {
1055 int aaaa;
1056 };
1057 template <typename T> struct Foo {
1058 Bar func(int);
1059 void test() {
1060 func(1).[[aaaa]];
1061 }
1062 };
1063 )cpp";
1064 EXPECT_DECLS("CXXDependentScopeMemberExpr", "int aaaa");
1065
1066 Code = R"cpp(
1067 class Foo {
1068 public:
1069 static Foo k(int);
1070 template <typename T> T convert() const;
1071 };
1072 template <typename T>
1073 void test() {
1074 Foo::k(T()).template [[convert]]<T>();
1075 }
1076 )cpp";
1077 EXPECT_DECLS("CXXDependentScopeMemberExpr",
1078 "template <typename T> T convert() const");
1079
1080 Code = R"cpp(
1081 template <typename T>
1082 struct Waldo {
1083 void find();
1084 };
1085 template <typename T>
1086 using Wally = Waldo<T>;
1087 template <typename T>
1088 void foo(Wally<T> w) {
1089 w.[[find]]();
1090 }
1091 )cpp";
1092 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void find()");
1093
1094 Code = R"cpp(
1095 template <typename T>
1096 struct Waldo {
1097 void find();
1098 };
1099 template <typename T>
1100 struct MetaWaldo {
1101 using Type = Waldo<T>;
1102 };
1103 template <typename T>
1104 void foo(typename MetaWaldo<T>::Type w) {
1105 w.[[find]]();
1106 }
1107 )cpp";
1108 EXPECT_DECLS("CXXDependentScopeMemberExpr", "void find()");
1109
1110 Code = R"cpp(
1111 struct Waldo {
1112 void find();
1113 };
1114 template <typename T>
1115 using Wally = Waldo;
1116 template <typename>
1117 struct S : Wally<int> {
1118 void Foo() { this->[[find]](); }
1119 };
1120 )cpp";
1121 EXPECT_DECLS("MemberExpr", "void find()");
1122
1123 // Base expression is the type of a non-type template parameter
1124 // which is deduced using CTAD.
1125 Code = R"cpp(
1126 template <int N>
1127 struct Waldo {
1128 const int found = N;
1129 };
1130
1131 template <Waldo W>
1132 int test() {
1133 return W.[[found]];
1134 }
1135 )cpp";
1136 EXPECT_DECLS("CXXDependentScopeMemberExpr", "const int found = N");
1137}
1138
1139TEST_F(TargetDeclTest, DependentTypes) {
1140 // Heuristic resolution of dependent type name
1141 Code = R"cpp(
1142 template <typename>
1143 struct A { struct B {}; };
1144
1145 template <typename T>
1146 void foo(typename A<T>::[[B]]);
1147 )cpp";
1148 EXPECT_DECLS("DependentNameTypeLoc", "struct B");
1149
1150 // Heuristic resolution of dependent type name within a NestedNameSpecifierLoc
1151 Code = R"cpp(
1152 template <typename>
1153 struct A { struct B { struct C {}; }; };
1154
1155 template <typename T>
1156 void foo(typename A<T>::[[B]]::C);
1157 )cpp";
1158 EXPECT_DECLS("DependentNameTypeLoc", "struct B");
1159
1160 // Heuristic resolution of dependent type name whose qualifier is also
1161 // dependent
1162 Code = R"cpp(
1163 template <typename>
1164 struct A { struct B { struct C {}; }; };
1165
1166 template <typename T>
1167 void foo(typename A<T>::B::[[C]]);
1168 )cpp";
1169 EXPECT_DECLS("DependentNameTypeLoc", "struct C");
1170
1171 // Heuristic resolution of dependent template name
1172 Code = R"cpp(
1173 template <typename>
1174 struct A {
1175 template <typename> struct B {};
1176 };
1177
1178 template <typename T>
1179 void foo(typename A<T>::template [[B]]<int>);
1180 )cpp";
1181 EXPECT_DECLS("TemplateSpecializationTypeLoc", "template <typename> struct B");
1182
1183 // Dependent name with recursive definition. We don't expect a
1184 // result, but we shouldn't get into a stack overflow either.
1185 Code = R"cpp(
1186 template <int N>
1187 struct waldo {
1188 typedef typename waldo<N - 1>::type::[[next]] type;
1189 };
1190 )cpp";
1191 EXPECT_DECLS("DependentNameTypeLoc", );
1192
1193 // Similar to above but using mutually recursive templates.
1194 Code = R"cpp(
1195 template <int N>
1196 struct odd;
1197
1198 template <int N>
1199 struct even {
1200 using type = typename odd<N - 1>::type::next;
1201 };
1202
1203 template <int N>
1204 struct odd {
1205 using type = typename even<N - 1>::type::[[next]];
1206 };
1207 )cpp";
1208 EXPECT_DECLS("DependentNameTypeLoc", );
1209}
1210
1211TEST_F(TargetDeclTest, TypedefCascade) {
1212 Code = R"cpp(
1213 struct C {
1214 using type = int;
1215 };
1216 struct B {
1217 using type = C::type;
1218 };
1219 struct A {
1220 using type = B::type;
1221 };
1222 A::[[type]] waldo;
1223 )cpp";
1224 EXPECT_DECLS("TypedefTypeLoc",
1225 {"using type = int", Rel::Alias | Rel::Underlying},
1226 {"using type = C::type", Rel::Alias | Rel::Underlying},
1227 {"using type = B::type", Rel::Alias});
1228}
1229
1230TEST_F(TargetDeclTest, RecursiveTemplate) {
1231 Flags.push_back("-std=c++20"); // the test case uses concepts
1232
1233 Code = R"cpp(
1234 template <typename T>
1235 concept Leaf = false;
1236
1237 template <typename Tree>
1238 struct descend_left {
1239 using type = typename descend_left<typename Tree::left>::[[type]];
1240 };
1241
1242 template <Leaf Tree>
1243 struct descend_left<Tree> {
1244 using type = typename Tree::value;
1245 };
1246 )cpp";
1247 EXPECT_DECLS("DependentNameTypeLoc",
1248 {"using type = typename descend_left<typename Tree::left>::type",
1249 Rel::Alias | Rel::Underlying});
1250}
1251
1252TEST_F(TargetDeclTest, ObjC) {
1253 Flags = {"-xobjective-c"};
1254 Code = R"cpp(
1255 @interface Foo {}
1256 -(void)bar;
1257 @end
1258 void test(Foo *f) {
1259 [f [[bar]] ];
1260 }
1261 )cpp";
1262 EXPECT_DECLS("ObjCMessageExpr", "- (void)bar");
1263
1264 Code = R"cpp(
1265 @interface Foo { @public int bar; }
1266 @end
1267 int test(Foo *f) {
1268 return [[f->bar]];
1269 }
1270 )cpp";
1271 EXPECT_DECLS("ObjCIvarRefExpr", "int bar");
1272
1273 Code = R"cpp(
1274 @interface Foo {}
1275 -(int) x;
1276 -(void) setX:(int)x;
1277 @end
1278 void test(Foo *f) {
1279 [[f.x]] = 42;
1280 }
1281 )cpp";
1282 EXPECT_DECLS("ObjCPropertyRefExpr", "- (void)setX:(int)x");
1283
1284 Code = R"cpp(
1285 @interface I {}
1286 @property(retain) I* x;
1287 @property(retain) I* y;
1288 @end
1289 void test(I *f) {
1290 [[f.x]].y = 0;
1291 }
1292 )cpp";
1293 EXPECT_DECLS("ObjCPropertyRefExpr",
1294 "@property(atomic, retain, readwrite) I *x");
1295
1296 Code = R"cpp(
1297 @interface MYObject
1298 @end
1299 @interface Interface
1300 @property(retain) [[MYObject]] *x;
1301 @end
1302 )cpp";
1303 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface MYObject");
1304
1305 Code = R"cpp(
1306 @interface MYObject2
1307 @end
1308 @interface Interface
1309 @property(retain, nonnull) [[MYObject2]] *x;
1310 @end
1311 )cpp";
1312 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface MYObject2");
1313
1314 Code = R"cpp(
1315 @protocol Foo
1316 @end
1317 id test() {
1318 return [[@protocol(Foo)]];
1319 }
1320 )cpp";
1321 EXPECT_DECLS("ObjCProtocolExpr", "@protocol Foo");
1322
1323 Code = R"cpp(
1324 @interface Foo
1325 @end
1326 void test([[Foo]] *p);
1327 )cpp";
1328 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface Foo");
1329
1330 Code = R"cpp(// Don't consider implicit interface as the target.
1331 @implementation [[Implicit]]
1332 @end
1333 )cpp";
1334 EXPECT_DECLS("ObjCImplementationDecl", "@implementation Implicit");
1335
1336 Code = R"cpp(
1337 @interface Foo
1338 @end
1339 @implementation [[Foo]]
1340 @end
1341 )cpp";
1342 EXPECT_DECLS("ObjCImplementationDecl", "@interface Foo");
1343
1344 Code = R"cpp(
1345 @interface Foo
1346 @end
1347 @interface Foo (Ext)
1348 @end
1349 @implementation [[Foo]] (Ext)
1350 @end
1351 )cpp";
1352 EXPECT_DECLS("ObjCCategoryImplDecl", "@interface Foo(Ext)");
1353
1354 Code = R"cpp(
1355 @interface Foo
1356 @end
1357 @interface Foo (Ext)
1358 @end
1359 @implementation Foo ([[Ext]])
1360 @end
1361 )cpp";
1362 EXPECT_DECLS("ObjCCategoryImplDecl", "@interface Foo(Ext)");
1363
1364 Code = R"cpp(
1365 void test(id</*error-ok*/[[InvalidProtocol]]> p);
1366 )cpp";
1367 EXPECT_DECLS("ParmVarDecl", "id p");
1368
1369 Code = R"cpp(
1370 @class C;
1371 @protocol Foo
1372 @end
1373 void test([[C]]<Foo> *p);
1374 )cpp";
1375 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@class C;");
1376
1377 Code = R"cpp(
1378 @class C;
1379 @protocol Foo
1380 @end
1381 void test(C<[[Foo]]> *p);
1382 )cpp";
1383 EXPECT_DECLS("ObjCProtocolLoc", "@protocol Foo");
1384
1385 Code = R"cpp(
1386 @class C;
1387 @protocol Foo
1388 @end
1389 @protocol Bar
1390 @end
1391 void test(C<[[Foo]], Bar> *p);
1392 )cpp";
1393 EXPECT_DECLS("ObjCProtocolLoc", "@protocol Foo");
1394
1395 Code = R"cpp(
1396 @class C;
1397 @protocol Foo
1398 @end
1399 @protocol Bar
1400 @end
1401 void test(C<Foo, [[Bar]]> *p);
1402 )cpp";
1403 EXPECT_DECLS("ObjCProtocolLoc", "@protocol Bar");
1404
1405 Code = R"cpp(
1406 @interface Foo
1407 + (id)sharedInstance;
1408 @end
1409 @implementation Foo
1410 + (id)sharedInstance { return 0; }
1411 @end
1412 void test() {
1413 id value = [[Foo]].sharedInstance;
1414 }
1415 )cpp";
1416 EXPECT_DECLS("ObjCInterfaceTypeLoc", "@interface Foo");
1417
1418 Code = R"cpp(
1419 @interface Foo
1420 + (id)sharedInstance;
1421 @end
1422 @implementation Foo
1423 + (id)sharedInstance { return 0; }
1424 @end
1425 void test() {
1426 id value = Foo.[[sharedInstance]];
1427 }
1428 )cpp";
1429 EXPECT_DECLS("ObjCPropertyRefExpr", "+ (id)sharedInstance");
1430
1431 Code = R"cpp(
1432 @interface Foo
1433 + ([[id]])sharedInstance;
1434 @end
1435 )cpp";
1436 EXPECT_DECLS("TypedefTypeLoc", );
1437
1438 Code = R"cpp(
1439 @interface Foo
1440 + ([[instancetype]])sharedInstance;
1441 @end
1442 )cpp";
1443 EXPECT_DECLS("TypedefTypeLoc", );
1444}
1445
1446class FindExplicitReferencesTest : public ::testing::Test {
1447protected:
1448 struct AllRefs {
1449 std::string AnnotatedCode;
1450 std::string DumpedReferences;
1451 };
1452
1453 TestTU newTU(llvm::StringRef Code) {
1454 TestTU TU;
1455 TU.Code = std::string(Code);
1456
1457 // FIXME: Auto-completion in a template requires disabling delayed template
1458 // parsing.
1459 TU.ExtraArgs.push_back("-std=c++20");
1460 TU.ExtraArgs.push_back("-xobjective-c++");
1461
1462 return TU;
1463 }
1464
1465 AllRefs annotatedReferences(llvm::StringRef Code, ParsedAST &AST,
1466 std::vector<ReferenceLoc> Refs) {
1467 auto &SM = AST.getSourceManager();
1468 llvm::stable_sort(Refs, [&](const ReferenceLoc &L, const ReferenceLoc &R) {
1469 return SM.isBeforeInTranslationUnit(L.NameLoc, R.NameLoc);
1470 });
1471
1472 std::string AnnotatedCode;
1473 unsigned NextCodeChar = 0;
1474 for (unsigned I = 0; I < Refs.size(); ++I) {
1475 auto &R = Refs[I];
1476
1477 SourceLocation Pos = R.NameLoc;
1478 assert(Pos.isValid());
1479 if (Pos.isMacroID()) // FIXME: figure out how to show macro locations.
1480 Pos = SM.getExpansionLoc(Pos);
1481 assert(Pos.isFileID());
1482
1483 FileID File;
1484 unsigned Offset;
1485 std::tie(File, Offset) = SM.getDecomposedLoc(Pos);
1486 if (File == SM.getMainFileID()) {
1487 // Print the reference in a source code.
1488 assert(NextCodeChar <= Offset);
1489 AnnotatedCode += Code.substr(NextCodeChar, Offset - NextCodeChar);
1490 AnnotatedCode += "$" + std::to_string(I) + "^";
1491
1492 NextCodeChar = Offset;
1493 }
1494 }
1495 AnnotatedCode += Code.substr(NextCodeChar);
1496
1497 std::string DumpedReferences;
1498 for (unsigned I = 0; I < Refs.size(); ++I)
1499 DumpedReferences += std::string(llvm::formatv("{0}: {1}\n", I, Refs[I]));
1500
1501 return AllRefs{std::move(AnnotatedCode), std::move(DumpedReferences)};
1502 }
1503
1504 /// Parses \p Code, and annotates its body with results of
1505 /// findExplicitReferences on all top level decls.
1506 /// See actual tests for examples of annotation format.
1507 AllRefs annotateAllReferences(llvm::StringRef Code) {
1508 TestTU TU = newTU(Code);
1509 auto AST = TU.build();
1510
1511 std::vector<ReferenceLoc> Refs;
1512 for (auto *TopLevel : AST.getLocalTopLevelDecls())
1514 TopLevel, [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1515 AST.getHeuristicResolver());
1516 return annotatedReferences(Code, AST, std::move(Refs));
1517 }
1518
1519 /// Parses \p Code, finds function or namespace '::foo' and annotates its body
1520 /// with results of findExplicitReferences.
1521 /// See actual tests for examples of annotation format.
1522 AllRefs annotateReferencesInFoo(llvm::StringRef Code) {
1523 TestTU TU = newTU(Code);
1524 auto AST = TU.build();
1525 auto *TestDecl = &findDecl(AST, "foo");
1526 if (auto *T = llvm::dyn_cast<FunctionTemplateDecl>(TestDecl))
1527 TestDecl = T->getTemplatedDecl();
1528
1529 std::vector<ReferenceLoc> Refs;
1530 if (const auto *Func = llvm::dyn_cast<FunctionDecl>(TestDecl))
1532 Func->getBody(),
1533 [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1534 AST.getHeuristicResolver());
1535 else if (const auto *NS = llvm::dyn_cast<NamespaceDecl>(TestDecl))
1537 NS,
1538 [&Refs, &NS](ReferenceLoc R) {
1539 // Avoid adding the namespace foo decl to the results.
1540 if (R.Targets.size() == 1 && R.Targets.front() == NS)
1541 return;
1542 Refs.push_back(std::move(R));
1543 },
1544 AST.getHeuristicResolver());
1545 else if (const auto *OC = llvm::dyn_cast<ObjCContainerDecl>(TestDecl))
1547 OC, [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1548 AST.getHeuristicResolver());
1549 else
1550 ADD_FAILURE() << "Failed to find ::foo decl for test";
1551
1552 return annotatedReferences(Code, AST, std::move(Refs));
1553 }
1554};
1555
1556TEST_F(FindExplicitReferencesTest, AllRefsInFoo) {
1557 std::pair</*Code*/ llvm::StringRef, /*References*/ llvm::StringRef> Cases[] =
1558 {// Simple expressions.
1559 {R"cpp(
1560 int global;
1561 int func();
1562 void foo(int param) {
1563 $0^global = $1^param + $2^func();
1564 }
1565 )cpp",
1566 "0: targets = {global}\n"
1567 "1: targets = {param}\n"
1568 "2: targets = {func}\n"},
1569 {R"cpp(
1570 struct X { int a; };
1571 void foo(X x) {
1572 $0^x.$1^a = 10;
1573 }
1574 )cpp",
1575 "0: targets = {x}\n"
1576 "1: targets = {X::a}\n"},
1577 {R"cpp(
1578 // error-ok: testing with broken code
1579 int bar();
1580 int foo() {
1581 return $0^bar() + $1^bar(42);
1582 }
1583 )cpp",
1584 "0: targets = {bar}\n"
1585 "1: targets = {bar}\n"},
1586 // Namespaces and aliases.
1587 {R"cpp(
1588 namespace ns {}
1589 namespace alias = ns;
1590 void foo() {
1591 using namespace $0^ns;
1592 using namespace $1^alias;
1593 }
1594 )cpp",
1595 "0: targets = {ns}\n"
1596 "1: targets = {alias}\n"},
1597 // Using declarations.
1598 {R"cpp(
1599 namespace ns { int global; }
1600 void foo() {
1601 using $0^ns::$1^global;
1602 }
1603 )cpp",
1604 "0: targets = {ns}\n"
1605 "1: targets = {ns::global}, qualifier = 'ns::'\n"},
1606 // Using enum declarations.
1607 {R"cpp(
1608 namespace ns { enum class A {}; }
1609 void foo() {
1610 using enum $0^ns::$1^A;
1611 }
1612 )cpp",
1613 "0: targets = {ns}\n"
1614 "1: targets = {ns::A}, qualifier = 'ns::'\n"},
1615 // Simple types.
1616 {R"cpp(
1617 struct Struct { int a; };
1618 using Typedef = int;
1619 void foo() {
1620 $0^Struct $1^x;
1621 $2^Typedef $3^y;
1622 static_cast<$4^Struct*>(0);
1623 }
1624 )cpp",
1625 "0: targets = {Struct}\n"
1626 "1: targets = {x}, decl\n"
1627 "2: targets = {Typedef}\n"
1628 "3: targets = {y}, decl\n"
1629 "4: targets = {Struct}\n"},
1630 // Name qualifiers.
1631 {R"cpp(
1632 namespace a { namespace b { struct S { typedef int type; }; } }
1633 void foo() {
1634 $0^a::$1^b::$2^S $3^x;
1635 using namespace $4^a::$5^b;
1636 $6^S::$7^type $8^y;
1637 }
1638 )cpp",
1639 "0: targets = {a}\n"
1640 "1: targets = {a::b}, qualifier = 'a::'\n"
1641 "2: targets = {a::b::S}, qualifier = 'a::b::'\n"
1642 "3: targets = {x}, decl\n"
1643 "4: targets = {a}\n"
1644 "5: targets = {a::b}, qualifier = 'a::'\n"
1645 "6: targets = {a::b::S}\n"
1646 "7: targets = {a::b::S::type}, qualifier = 'S::'\n"
1647 "8: targets = {y}, decl\n"},
1648 {R"cpp(
1649 void foo() {
1650 $0^ten: // PRINT "HELLO WORLD!"
1651 goto $1^ten;
1652 }
1653 )cpp",
1654 "0: targets = {ten}, decl\n"
1655 "1: targets = {ten}\n"},
1656 // Simple templates.
1657 {R"cpp(
1658 template <class T> struct vector { using value_type = T; };
1659 template <> struct vector<bool> { using value_type = bool; };
1660 void foo() {
1661 $0^vector<int> $1^vi;
1662 $2^vector<bool> $3^vb;
1663 }
1664 )cpp",
1665 "0: targets = {vector<int>}\n"
1666 "1: targets = {vi}, decl\n"
1667 "2: targets = {vector<bool>}\n"
1668 "3: targets = {vb}, decl\n"},
1669 // Template type aliases.
1670 {R"cpp(
1671 template <class T> struct vector { using value_type = T; };
1672 template <> struct vector<bool> { using value_type = bool; };
1673 template <class T> using valias = vector<T>;
1674 void foo() {
1675 $0^valias<int> $1^vi;
1676 $2^valias<bool> $3^vb;
1677 }
1678 )cpp",
1679 "0: targets = {valias}\n"
1680 "1: targets = {vi}, decl\n"
1681 "2: targets = {valias}\n"
1682 "3: targets = {vb}, decl\n"},
1683 // Injected class name.
1684 {R"cpp(
1685 namespace foo {
1686 template <typename $0^T>
1687 class $1^Bar {
1688 ~$2^Bar();
1689 void $3^f($4^Bar);
1690 };
1691 }
1692 )cpp",
1693 "0: targets = {foo::Bar::T}, decl\n"
1694 "1: targets = {foo::Bar}, decl\n"
1695 "2: targets = {foo::Bar}\n"
1696 "3: targets = {foo::Bar::f}, decl\n"
1697 "4: targets = {foo::Bar}\n"},
1698 // MemberExpr should know their using declaration.
1699 {R"cpp(
1700 struct X { void func(int); };
1701 struct Y : X {
1702 using X::func;
1703 };
1704 void foo(Y y) {
1705 $0^y.$1^func(1);
1706 }
1707 )cpp",
1708 "0: targets = {y}\n"
1709 "1: targets = {Y::func}\n"},
1710 // DeclRefExpr should know their using declaration.
1711 {R"cpp(
1712 namespace ns { void bar(int); }
1713 using ns::bar;
1714
1715 void foo() {
1716 $0^bar(10);
1717 }
1718 )cpp",
1719 "0: targets = {bar}\n"},
1720 // References from a macro.
1721 {R"cpp(
1722 #define FOO a
1723 #define BAR b
1724
1725 void foo(int a, int b) {
1726 $0^FOO+$1^BAR;
1727 }
1728 )cpp",
1729 "0: targets = {a}\n"
1730 "1: targets = {b}\n"},
1731 // No references from implicit nodes.
1732 {R"cpp(
1733 struct vector {
1734 int *begin();
1735 int *end();
1736 };
1737
1738 void foo() {
1739 for (int $0^x : $1^vector()) {
1740 $2^x = 10;
1741 }
1742 }
1743 )cpp",
1744 "0: targets = {x}, decl\n"
1745 "1: targets = {vector}\n"
1746 "2: targets = {x}\n"},
1747 // Handle UnresolvedLookupExpr.
1748 {R"cpp(
1749 namespace ns1 { void func(char*); }
1750 namespace ns2 { void func(int*); }
1751 using namespace ns1;
1752 using namespace ns2;
1753
1754 template <class T>
1755 void foo(T t) {
1756 $0^func($1^t);
1757 }
1758 )cpp",
1759 "0: targets = {ns1::func, ns2::func}\n"
1760 "1: targets = {t}\n"},
1761 // Handle UnresolvedMemberExpr.
1762 {R"cpp(
1763 struct X {
1764 void func(char*);
1765 void func(int*);
1766 };
1767
1768 template <class T>
1769 void foo(X x, T t) {
1770 $0^x.$1^func($2^t);
1771 }
1772 )cpp",
1773 "0: targets = {x}\n"
1774 "1: targets = {X::func, X::func}\n"
1775 "2: targets = {t}\n"},
1776 // Handle DependentScopeDeclRefExpr.
1777 {R"cpp(
1778 template <class T>
1779 struct S {
1780 static int value;
1781 };
1782
1783 template <class T>
1784 void foo() {
1785 $0^S<$1^T>::$2^value;
1786 }
1787 )cpp",
1788 "0: targets = {S}\n"
1789 "1: targets = {T}\n"
1790 "2: targets = {S::value}, qualifier = 'S<T>::'\n"},
1791 // Handle CXXDependentScopeMemberExpr.
1792 {R"cpp(
1793 template <class T>
1794 struct S {
1795 int value;
1796 };
1797
1798 template <class T>
1799 void foo(S<T> t) {
1800 $0^t.$1^value;
1801 }
1802 )cpp",
1803 "0: targets = {t}\n"
1804 "1: targets = {S::value}\n"},
1805 // Type template parameters.
1806 {R"cpp(
1807 template <class T>
1808 void foo() {
1809 static_cast<$0^T>(0);
1810 $1^T();
1811 $2^T $3^t;
1812 }
1813 )cpp",
1814 "0: targets = {T}\n"
1815 "1: targets = {T}\n"
1816 "2: targets = {T}\n"
1817 "3: targets = {t}, decl\n"},
1818 // Non-type template parameters.
1819 {R"cpp(
1820 template <int I>
1821 void foo() {
1822 int $0^x = $1^I;
1823 }
1824 )cpp",
1825 "0: targets = {x}, decl\n"
1826 "1: targets = {I}\n"},
1827 // Template template parameters.
1828 {R"cpp(
1829 template <class T> struct vector {};
1830
1831 template <template<class> class TT, template<class> class ...TP>
1832 void foo() {
1833 $0^TT<int> $1^x;
1834 $2^foo<$3^TT>();
1835 $4^foo<$5^vector>();
1836 $6^foo<$7^TP...>();
1837 }
1838 )cpp",
1839 "0: targets = {TT}\n"
1840 "1: targets = {x}, decl\n"
1841 "2: targets = {foo}\n"
1842 "3: targets = {TT}\n"
1843 "4: targets = {foo}\n"
1844 "5: targets = {vector}\n"
1845 "6: targets = {foo}\n"
1846 "7: targets = {TP}\n"},
1847 // Non-type template parameters with declarations.
1848 {R"cpp(
1849 int func();
1850 template <int(*)()> struct wrapper {};
1851
1852 template <int(*FuncParam)()>
1853 void foo() {
1854 $0^wrapper<$1^func> $2^w;
1855 $3^FuncParam();
1856 }
1857 )cpp",
1858 "0: targets = {wrapper<&func>}\n"
1859 "1: targets = {func}\n"
1860 "2: targets = {w}, decl\n"
1861 "3: targets = {FuncParam}\n"},
1862 // declaration references.
1863 {R"cpp(
1864 namespace ns {}
1865 class S {};
1866 void foo() {
1867 class $0^Foo { $1^Foo(); ~$2^Foo(); int $3^field; };
1868 int $4^Var;
1869 enum $5^E { $6^ABC };
1870 typedef int $7^INT;
1871 using $8^INT2 = int;
1872 namespace $9^NS = $10^ns;
1873 }
1874 )cpp",
1875 "0: targets = {Foo}, decl\n"
1876 "1: targets = {foo()::Foo::Foo}, decl\n"
1877 "2: targets = {Foo}\n"
1878 "3: targets = {foo()::Foo::field}, decl\n"
1879 "4: targets = {Var}, decl\n"
1880 "5: targets = {E}, decl\n"
1881 "6: targets = {foo()::ABC}, decl\n"
1882 "7: targets = {INT}, decl\n"
1883 "8: targets = {INT2}, decl\n"
1884 "9: targets = {NS}, decl\n"
1885 "10: targets = {ns}\n"},
1886 // User-defined conversion operator.
1887 {R"cpp(
1888 void foo() {
1889 class $0^Bar {};
1890 class $1^Foo {
1891 public:
1892 // FIXME: This should have only one reference to Bar.
1893 $2^operator $3^$4^Bar();
1894 };
1895
1896 $5^Foo $6^f;
1897 $7^f.$8^operator $9^Bar();
1898 }
1899 )cpp",
1900 "0: targets = {Bar}, decl\n"
1901 "1: targets = {Foo}, decl\n"
1902 "2: targets = {foo()::Foo::operator Bar}, decl\n"
1903 "3: targets = {Bar}\n"
1904 "4: targets = {Bar}\n"
1905 "5: targets = {Foo}\n"
1906 "6: targets = {f}, decl\n"
1907 "7: targets = {f}\n"
1908 "8: targets = {foo()::Foo::operator Bar}\n"
1909 "9: targets = {Bar}\n"},
1910 // Destructor.
1911 {R"cpp(
1912 void foo() {
1913 class $0^Foo {
1914 public:
1915 ~$1^Foo() {}
1916
1917 void $2^destructMe() {
1918 this->~$3^Foo();
1919 }
1920 };
1921
1922 $4^Foo $5^f;
1923 $6^f.~ /*...*/ $7^Foo();
1924 }
1925 )cpp",
1926 "0: targets = {Foo}, decl\n"
1927 // FIXME: It's better to target destructor's FunctionDecl instead of
1928 // the type itself (similar to constructor).
1929 "1: targets = {Foo}\n"
1930 "2: targets = {foo()::Foo::destructMe}, decl\n"
1931 "3: targets = {Foo}\n"
1932 "4: targets = {Foo}\n"
1933 "5: targets = {f}, decl\n"
1934 "6: targets = {f}\n"
1935 "7: targets = {Foo}\n"},
1936 // cxx constructor initializer.
1937 {R"cpp(
1938 class Base {};
1939 void foo() {
1940 // member initializer
1941 class $0^X {
1942 int $1^abc;
1943 $2^X(): $3^abc() {}
1944 };
1945 // base initializer
1946 class $4^Derived : public $5^Base {
1947 $6^Base $7^B;
1948 $8^Derived() : $9^Base() {}
1949 };
1950 // delegating initializer
1951 class $10^Foo {
1952 $11^Foo(int);
1953 $12^Foo(): $13^Foo(111) {}
1954 };
1955 }
1956 )cpp",
1957 "0: targets = {X}, decl\n"
1958 "1: targets = {foo()::X::abc}, decl\n"
1959 "2: targets = {foo()::X::X}, decl\n"
1960 "3: targets = {foo()::X::abc}\n"
1961 "4: targets = {Derived}, decl\n"
1962 "5: targets = {Base}\n"
1963 "6: targets = {Base}\n"
1964 "7: targets = {foo()::Derived::B}, decl\n"
1965 "8: targets = {foo()::Derived::Derived}, decl\n"
1966 "9: targets = {Base}\n"
1967 "10: targets = {Foo}, decl\n"
1968 "11: targets = {foo()::Foo::Foo}, decl\n"
1969 "12: targets = {foo()::Foo::Foo}, decl\n"
1970 "13: targets = {Foo}\n"},
1971 // Anonymous entities should not be reported.
1972 {
1973 R"cpp(
1974 void foo() {
1975 $0^class {} $1^x;
1976 int (*$2^fptr)(int $3^a, int) = nullptr;
1977 }
1978 )cpp",
1979 "0: targets = {(unnamed class)}\n"
1980 "1: targets = {x}, decl\n"
1981 "2: targets = {fptr}, decl\n"
1982 "3: targets = {a}, decl\n"},
1983 // Namespace aliases should be handled properly.
1984 {
1985 R"cpp(
1986 namespace ns { struct Type {}; }
1987 namespace alias = ns;
1988 namespace rec_alias = alias;
1989
1990 void foo() {
1991 $0^ns::$1^Type $2^a;
1992 $3^alias::$4^Type $5^b;
1993 $6^rec_alias::$7^Type $8^c;
1994 }
1995 )cpp",
1996 "0: targets = {ns}\n"
1997 "1: targets = {ns::Type}, qualifier = 'ns::'\n"
1998 "2: targets = {a}, decl\n"
1999 "3: targets = {alias}\n"
2000 "4: targets = {ns::Type}, qualifier = 'alias::'\n"
2001 "5: targets = {b}, decl\n"
2002 "6: targets = {rec_alias}\n"
2003 "7: targets = {ns::Type}, qualifier = 'rec_alias::'\n"
2004 "8: targets = {c}, decl\n"},
2005 // Handle SizeOfPackExpr.
2006 {
2007 R"cpp(
2008 template <typename... E>
2009 void foo() {
2010 constexpr int $0^size = sizeof...($1^E);
2011 };
2012 )cpp",
2013 "0: targets = {size}, decl\n"
2014 "1: targets = {E}\n"},
2015 // Class template argument deduction
2016 {
2017 R"cpp(
2018 template <typename T>
2019 struct Test {
2020 Test(T);
2021 };
2022 void foo() {
2023 $0^Test $1^a(5);
2024 }
2025 )cpp",
2026 "0: targets = {Test}\n"
2027 "1: targets = {a}, decl\n"},
2028 // Templates
2029 {R"cpp(
2030 namespace foo {
2031 template <typename $0^T>
2032 class $1^Bar {};
2033 }
2034 )cpp",
2035 "0: targets = {foo::Bar::T}, decl\n"
2036 "1: targets = {foo::Bar}, decl\n"},
2037 // Templates
2038 {R"cpp(
2039 namespace foo {
2040 template <typename $0^T>
2041 void $1^func();
2042 }
2043 )cpp",
2044 "0: targets = {T}, decl\n"
2045 "1: targets = {foo::func}, decl\n"},
2046 // Templates
2047 {R"cpp(
2048 namespace foo {
2049 template <typename $0^T>
2050 $1^T $2^x;
2051 }
2052 )cpp",
2053 "0: targets = {foo::T}, decl\n"
2054 "1: targets = {foo::T}\n"
2055 "2: targets = {foo::x}, decl\n"},
2056 // Templates
2057 {R"cpp(
2058 template<typename T> class vector {};
2059 namespace foo {
2060 template <typename $0^T>
2061 using $1^V = $2^vector<$3^T>;
2062 }
2063 )cpp",
2064 "0: targets = {foo::T}, decl\n"
2065 "1: targets = {foo::V}, decl\n"
2066 "2: targets = {vector}\n"
2067 "3: targets = {foo::T}\n"},
2068 // Concept
2069 {
2070 R"cpp(
2071 template <typename T>
2072 concept Drawable = requires (T t) { t.draw(); };
2073
2074 namespace foo {
2075 template <typename $0^T> requires $1^Drawable<$2^T>
2076 void $3^bar($4^T $5^t) {
2077 $6^t.$7^draw();
2078 }
2079 }
2080 )cpp",
2081 "0: targets = {T}, decl\n"
2082 "1: targets = {Drawable}\n"
2083 "2: targets = {T}\n"
2084 "3: targets = {foo::bar}, decl\n"
2085 "4: targets = {T}\n"
2086 "5: targets = {t}, decl\n"
2087 "6: targets = {t}\n"
2088 "7: targets = {}\n"},
2089 // Objective-C: instance variables
2090 {
2091 R"cpp(
2092 @interface I {
2093 @public
2094 I *_z;
2095 }
2096 @end
2097 I *f;
2098 void foo() {
2099 $0^f->$1^_z = 0;
2100 }
2101 )cpp",
2102 "0: targets = {f}\n"
2103 "1: targets = {I::_z}\n"},
2104 // Objective-C: properties
2105 {
2106 R"cpp(
2107 @interface I {}
2108 @property(retain) I* x;
2109 @property(retain) I* y;
2110 @end
2111 I *f;
2112 void foo() {
2113 $0^f.$1^x.$2^y = 0;
2114 }
2115 )cpp",
2116 "0: targets = {f}\n"
2117 "1: targets = {I::x}\n"
2118 "2: targets = {I::y}\n"},
2119 // Objective-C: implicit properties
2120 {
2121 R"cpp(
2122 @interface I {}
2123 -(I*)x;
2124 -(void)setY:(I*)y;
2125 @end
2126 I *f;
2127 void foo() {
2128 $0^f.$1^x.$2^y = 0;
2129 }
2130 )cpp",
2131 "0: targets = {f}\n"
2132 "1: targets = {I::x}\n"
2133 "2: targets = {I::setY:}\n"},
2134 // Objective-C: class properties
2135 {
2136 R"cpp(
2137 @interface I {}
2138 @property(class) I *x;
2139 @end
2140 id local;
2141 void foo() {
2142 $0^I.$1^x = 0;
2143 $2^local = $3^I.$4^x;
2144 }
2145 )cpp",
2146 "0: targets = {I}\n"
2147 "1: targets = {I::setX:}\n"
2148 "2: targets = {local}\n"
2149 "3: targets = {I}\n"
2150 "4: targets = {I::x}\n"},
2151 // Objective-C: implicit class properties
2152 {
2153 R"cpp(
2154 @interface I {}
2155 +(I*)x;
2156 +(void)setX:(I*)x;
2157 @end
2158 id local;
2159 void foo() {
2160 $0^I.$1^x = 0;
2161 $2^local = $3^I.$4^x;
2162 }
2163 )cpp",
2164 "0: targets = {I}\n"
2165 "1: targets = {I::setX:}\n"
2166 "2: targets = {local}\n"
2167 "3: targets = {I}\n"
2168 "4: targets = {I::x}\n"},
2169 {// Objective-C: methods
2170 R"cpp(
2171 @interface I
2172 -(void) a:(int)x b:(int)y;
2173 @end
2174 void foo(I *i) {
2175 [$0^i $1^a:1 b:2];
2176 }
2177 )cpp",
2178 "0: targets = {i}\n"
2179 "1: targets = {I::a:b:}\n"},
2180 {// Objective-C: protocols
2181 R"cpp(
2182 @interface I
2183 @end
2184 @protocol P
2185 @end
2186 void foo() {
2187 $0^I<$1^P> *$2^x;
2188 }
2189 )cpp",
2190 "0: targets = {I}\n"
2191 "1: targets = {P}\n"
2192 "2: targets = {x}, decl\n"},
2193
2194 // Designated initializers.
2195 {R"cpp(
2196 void foo() {
2197 struct $0^Foo {
2198 int $1^Bar;
2199 };
2200 $2^Foo $3^f { .$4^Bar = 42 };
2201 }
2202 )cpp",
2203 "0: targets = {Foo}, decl\n"
2204 "1: targets = {foo()::Foo::Bar}, decl\n"
2205 "2: targets = {Foo}\n"
2206 "3: targets = {f}, decl\n"
2207 "4: targets = {foo()::Foo::Bar}\n"},
2208 {R"cpp(
2209 void foo() {
2210 struct $0^Baz {
2211 int $1^Field;
2212 };
2213 struct $2^Bar {
2214 $3^Baz $4^Foo;
2215 };
2216 $5^Bar $6^bar { .$7^Foo.$8^Field = 42 };
2217 }
2218 )cpp",
2219 "0: targets = {Baz}, decl\n"
2220 "1: targets = {foo()::Baz::Field}, decl\n"
2221 "2: targets = {Bar}, decl\n"
2222 "3: targets = {Baz}\n"
2223 "4: targets = {foo()::Bar::Foo}, decl\n"
2224 "5: targets = {Bar}\n"
2225 "6: targets = {bar}, decl\n"
2226 "7: targets = {foo()::Bar::Foo}\n"
2227 "8: targets = {foo()::Baz::Field}\n"},
2228 // offsetof
2229 {R"cpp(
2230 void foo() {
2231 struct $0^Foo { int $1^bar; };
2232 int $2^x = __builtin_offsetof($3^Foo, $4^bar);
2233 }
2234 )cpp",
2235 "0: targets = {Foo}, decl\n"
2236 "1: targets = {foo()::Foo::bar}, decl\n"
2237 "2: targets = {x}, decl\n"
2238 "3: targets = {Foo}\n"
2239 "4: targets = {foo()::Foo::bar}\n"},
2240 // offsetof with a nested field designator -- each component must
2241 // resolve to its own source position, not a shared one.
2242 {R"cpp(
2243 void foo() {
2244 struct $0^A {
2245 $1^struct { int $2^c; } $3^B;
2246 };
2247 int $4^x = __builtin_offsetof($5^A, $6^B.$7^c);
2248 }
2249 )cpp",
2250 "0: targets = {A}, decl\n"
2251 "1: targets = {foo()::A::(unnamed struct)}\n"
2252 "2: targets = {foo()::A::(unnamed struct)::c}, decl\n"
2253 "3: targets = {foo()::A::B}, decl\n"
2254 "4: targets = {x}, decl\n"
2255 "5: targets = {A}\n"
2256 "6: targets = {foo()::A::B}\n"
2257 "7: targets = {foo()::A::(unnamed struct)::c}\n"},
2258 // offsetof with an array-subscript component -- array indices are not
2259 // emitted as offsetof references (the subscript expression is still
2260 // visited independently).
2261 {R"cpp(
2262 void foo() {
2263 struct $0^A { int $1^arr[4]; };
2264 int $2^i = 0;
2265 int $3^x = __builtin_offsetof($4^A, $5^arr[$6^i]);
2266 }
2267 )cpp",
2268 "0: targets = {A}, decl\n"
2269 "1: targets = {foo()::A::arr}, decl\n"
2270 "2: targets = {i}, decl\n"
2271 "3: targets = {x}, decl\n"
2272 "4: targets = {A}\n"
2273 "5: targets = {foo()::A::arr}\n"
2274 "6: targets = {i}\n"},
2275 {R"cpp(
2276 template<typename T>
2277 void crash(T);
2278 template<typename T>
2279 void foo() {
2280 $0^crash({.$1^x = $2^T()});
2281 }
2282 )cpp",
2283 "0: targets = {crash}\n"
2284 "1: targets = {}\n"
2285 "2: targets = {T}\n"},
2286 // unknown template name should not crash.
2287 {R"cpp(
2288 template <template <typename> typename T>
2289 struct Base {};
2290 namespace foo {
2291 template <typename $0^T>
2292 struct $1^Derive : $2^Base<$3^T::template $4^Unknown> {};
2293 }
2294 )cpp",
2295 "0: targets = {foo::Derive::T}, decl\n"
2296 "1: targets = {foo::Derive}, decl\n"
2297 "2: targets = {Base}\n"
2298 "3: targets = {foo::Derive::T}\n"
2299 "4: targets = {}, qualifier = 'T::'\n"},
2300 // deduction guide
2301 {R"cpp(
2302 namespace foo {
2303 template <typename $0^T>
2304 struct $1^Test {
2305 template <typename $2^I>
2306 $3^Test($4^I);
2307 };
2308 template <typename $5^I>
2309 $6^Test($7^I) -> $8^Test<typename $9^I::$10^type>;
2310 }
2311 )cpp",
2312 "0: targets = {T}, decl\n"
2313 "1: targets = {foo::Test}, decl\n"
2314 "2: targets = {I}, decl\n"
2315 "3: targets = {foo::Test::Test<T>}, decl\n"
2316 "4: targets = {I}\n"
2317 "5: targets = {I}, decl\n"
2318 "6: targets = {foo::Test}\n"
2319 "7: targets = {I}\n"
2320 "8: targets = {foo::Test}\n"
2321 "9: targets = {I}\n"
2322 "10: targets = {}, qualifier = 'I::'\n"}};
2323
2324 for (const auto &C : Cases) {
2325 llvm::StringRef ExpectedCode = C.first;
2326 llvm::StringRef ExpectedRefs = C.second;
2327
2328 auto Actual =
2329 annotateReferencesInFoo(llvm::Annotations(ExpectedCode).code());
2330 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
2331 EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
2332 }
2333}
2334
2335TEST_F(FindExplicitReferencesTest, AllRefs) {
2336 std::pair</*Code*/ llvm::StringRef, /*References*/ llvm::StringRef> Cases[] =
2337 {{R"cpp(
2338 @interface $0^MyClass
2339 @end
2340 @implementation $1^$2^MyClass
2341 @end
2342 )cpp",
2343 "0: targets = {MyClass}, decl\n"
2344 "1: targets = {MyClass}\n"
2345 "2: targets = {MyClass}, decl\n"},
2346 {R"cpp(
2347 @interface $0^MyClass
2348 @end
2349 @interface $1^MyClass ($2^Category)
2350 @end
2351 @implementation $3^MyClass ($4^$5^Category)
2352 @end
2353 )cpp",
2354 "0: targets = {MyClass}, decl\n"
2355 "1: targets = {MyClass}\n"
2356 "2: targets = {Category}, decl\n"
2357 "3: targets = {MyClass}\n"
2358 "4: targets = {Category}\n"
2359 "5: targets = {Category}, decl\n"}};
2360
2361 for (const auto &C : Cases) {
2362 llvm::StringRef ExpectedCode = C.first;
2363 llvm::StringRef ExpectedRefs = C.second;
2364
2365 auto Actual = annotateAllReferences(llvm::Annotations(ExpectedCode).code());
2366 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
2367 EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
2368 }
2369}
2370
2371} // namespace
2372} // namespace clangd
2373} // 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