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