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>
35 PrintedDecl(
const char *Name, DeclRelationSet Relations = {})
36 :
Name(Name), Relations(Relations) {}
37 PrintedDecl(
const NamedDecl *D, DeclRelationSet Relations = {})
38 : Relations(Relations) {
40 llvm::raw_string_ostream
OS(S);
42 llvm::StringRef FirstLine =
43 llvm::StringRef(
OS.str()).take_until([](
char C) {
return C ==
'\n'; });
44 FirstLine = FirstLine.rtrim(
" {");
45 Name = std::string(FirstLine.rtrim(
" {"));
49 DeclRelationSet Relations;
51bool operator==(
const PrintedDecl &L,
const PrintedDecl &R) {
52 return std::tie(L.Name, L.Relations) == std::tie(R.Name, R.Relations);
54llvm::raw_ostream &
operator<<(llvm::raw_ostream &
OS,
const PrintedDecl &D) {
55 return OS <<
D.Name <<
" Rel=" <<
D.Relations;
64class TargetDeclTest :
public ::testing::Test {
73 std::vector<PrintedDecl> assertNodeAndPrintDecls(
const char *NodeType) {
74 llvm::Annotations A(
Code);
77 auto AST = TU.build();
78 llvm::Annotations::Range R = A.range();
80 AST.getASTContext(),
AST.getTokens(), R.Begin, R.End);
81 const SelectionTree::Node *N = Selection.commonAncestor();
83 ADD_FAILURE() <<
"No node selected!\n" <<
Code;
86 EXPECT_EQ(N->kind(), NodeType) << Selection;
88 std::vector<PrintedDecl> ActualDecls;
89 for (
const auto &
Entry :
91 ActualDecls.emplace_back(
Entry.first,
Entry.second);
98#define EXPECT_DECLS(NodeType, ...) \
99 EXPECT_THAT(assertNodeAndPrintDecls(NodeType), \
100 ::testing::UnorderedElementsAreArray( \
101 std::vector<PrintedDecl>({__VA_ARGS__}))) \
103using ExpectedDecls = std::vector<PrintedDecl>;
105TEST_F(TargetDeclTest, Exprs) {
113 struct S { S operator+(S) const; };
114 auto X = S() [[+]] S();
126 void operator()(int n);
133 EXPECT_DECLS("CXXOperatorCallExpr",
"void operator()(int n)");
152TEST_F(TargetDeclTest, RecoveryForC) {
153 Flags = {
"-xc",
"-Xclang",
"-frecovery-ast"};
155 // error-ok: testing behavior on broken code
163TEST_F(TargetDeclTest, Recovery) {
165 // error-ok: testing behavior on broken code
170 EXPECT_DECLS("UnresolvedLookupExpr",
"int f()",
"int f(int, int)");
173TEST_F(TargetDeclTest, RecoveryType) {
175 // error-ok: testing behavior on broken code
176 struct S { int member; };
179 // No overload matches, but we have recovery-expr with the correct type.
180 overloaded().[[member]];
186TEST_F(TargetDeclTest, UsingDecl) {
196 EXPECT_DECLS(
"DeclRefExpr", {
"using foo::f", Rel::Alias}, {
"int f(int)"});
206 EXPECT_DECLS(
"UsingDecl", {
"using foo::f", Rel::Alias}, {
"int f(int)"},
216 int x = Y().[[foo]]();
218 EXPECT_DECLS("MemberExpr", {
"using X::foo", Rel::Alias}, {
"int foo()"});
221 template <typename T>
225 template <typename T>
226 struct Derived : Base<T> {
227 using Base<T>::[[waldo]];
230 EXPECT_DECLS("UnresolvedUsingValueDecl", {
"using Base<T>::waldo", Rel::Alias},
235 template<typename T> class S {};
243 EXPECT_DECLS("TemplateSpecializationTypeLoc", {
"using ns::S", Rel::Alias},
244 {
"template <typename T> class S"},
245 {
"class S", Rel::TemplatePattern});
249 template<typename T> class S {};
253 template <template <typename> class T> class X {};
256 EXPECT_DECLS("TemplateArgumentLoc", {
"using ns::S", Rel::Alias},
257 {
"template <typename T> class S"});
261 template<typename T> class S { public: S(T); };
267 Flags.push_back("-std=c++17");
269 {
"using ns::S", Rel::Alias}, {
"template <typename T> class S"},
270 {
"class S", Rel::TemplatePattern});
274 class Foo { public: class foo {}; };
275 template <class T> class A : public Foo<T> {
276 using typename Foo<T>::foo;
281 {
"using typename Foo<T>::foo", Rel::Alias});
284 Flags.push_back(
"-std=c++20");
286 namespace ns { enum class A { X }; }
287 [[using enum ns::A]];
292 namespace ns { enum class A { X }; }
299TEST_F(TargetDeclTest, BaseSpecifier) {
302 struct Y : [[private]] X {};
307 struct Y : [[private X]] {};
312 struct Y : private [[X]] {};
317TEST_F(TargetDeclTest, ConstructorInitList) {
335TEST_F(TargetDeclTest, DesignatedInit) {
339 struct Y { int b; struct X c[2]; };
340 struct Y y = { .c[0].[[a]] = 1 };
345TEST_F(TargetDeclTest, NestedNameSpecifier) {
347 namespace a { namespace b { int c; } }
353 namespace a { struct X { enum { y }; }; }
359 template <typename T>
365 namespace a { int x; }
369 EXPECT_DECLS("NestedNameSpecifierLoc", {
"namespace b = a", Rel::Alias},
370 {
"namespace a", Rel::Underlying});
373TEST_F(TargetDeclTest, Types) {
385 EXPECT_DECLS("TypedefTypeLoc", {
"typedef S X", Rel::Alias},
386 {
"struct S", Rel::Underlying});
388 namespace ns { struct S{}; }
392 EXPECT_DECLS("TypedefTypeLoc", {
"typedef ns::S X", Rel::Alias},
393 {
"struct S", Rel::Underlying});
397 void foo() { [[T]] x; }
403 template<template<typename> class T>
404 void foo() { [[T<int>]] x; }
406 EXPECT_DECLS("TemplateSpecializationTypeLoc",
"template <typename> class T");
410 template<template<typename> class ...T>
415 EXPECT_DECLS("TemplateArgumentLoc", {
"template <typename> class ...T"});
423 EXPECT_DECLS("DecltypeTypeLoc", {
"struct S", Rel::Underlying});
433 template <typename... E>
435 static const int size = sizeof...([[E]]);
441 template <typename T>
449TEST_F(TargetDeclTest, ClassTemplate) {
451 // Implicit specialization.
452 template<int x> class Foo{};
456 {
"template<> class Foo<42>", Rel::TemplateInstantiation},
457 {
"class Foo", Rel::TemplatePattern});
460 template<typename T> class Foo {};
461 // The "Foo<int>" SpecializationDecl is incomplete, there is no
462 // instantiation happening.
463 void func([[Foo<int>]] *);
466 {
"class Foo", Rel::TemplatePattern},
467 {
"template<> class Foo<int>", Rel::TemplateInstantiation});
470 // Explicit specialization.
471 template<int x> class Foo{};
472 template<> class Foo<42>{};
475 EXPECT_DECLS("TemplateSpecializationTypeLoc",
"template<> class Foo<42>");
478 // Partial specialization.
479 template<typename T> class Foo{};
480 template<typename T> class Foo<T*>{};
484 {
"template<> class Foo<int *>", Rel::TemplateInstantiation},
485 {
"template <typename T> class Foo<T *>", Rel::TemplatePattern});
488 // Template template argument.
489 template<typename T> struct Vector {};
490 template <template <typename> class Container>
494 EXPECT_DECLS("TemplateArgumentLoc", {
"template <typename T> struct Vector"});
496 Flags.push_back(
"-std=c++17");
499 // Class template argument deduction
500 template <typename T>
509 {
"struct Test", Rel::TemplatePattern});
513 template <typename T>
515 template <typename I>
518 template <typename I>
519 [[Test]](I, I) -> Test<typename I::type>;
521 EXPECT_DECLS("CXXDeductionGuideDecl", {
"template <typename T> struct Test"});
525 Flags.push_back(
"-std=c++20");
531 template <typename T>
532 concept Fooable = requires (T t) { t.foo(); };
534 template <typename T> requires [[Fooable]]<T>
540 "ConceptSpecializationExpr",
541 {
"template <typename T> concept Fooable = requires (T t) { t.foo(); }"});
545 template <typename T>
546 concept Fooable = true;
548 template <typename T>
549 void foo() requires [[Fooable]]<T>;
552 {
"template <typename T> concept Fooable = true"});
556 template <typename T>
557 concept Fooable = true;
559 template <[[Fooable]] T>
563 {
"template <typename T> concept Fooable = true"});
567 template <typename T, typename U>
568 concept Fooable = true;
570 template <[[Fooable]]<int> T>
574 {
"template <typename T, typename U> concept Fooable = true"});
577TEST_F(TargetDeclTest, Coroutine) {
578 Flags.push_back(
"-std=c++20");
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;
592 struct awaitable_frame {
593 awaitable get_return_object();
595 void unhandled_exception();
598 bool await_ready() const noexcept;
599 void await_suspend(std::coroutine_handle<void>) noexcept;
600 void await_resume() const noexcept;
602 result_t initial_suspend() noexcept;
603 result_t final_suspend() noexcept;
604 result_t await_transform(executor) noexcept;
609 struct coroutine_traits<awaitable> {
610 typedef awaitable_frame promise_type;
615 co_await [[executor]]();
621TEST_F(TargetDeclTest, FunctionTemplate) {
623 // Implicit specialization.
624 template<typename T> bool foo(T) { return false; };
625 bool x = [[foo]](42);
628 {
"template<> bool foo<int>(int)", Rel::TemplateInstantiation},
629 {
"bool foo(T)", Rel::TemplatePattern});
632 // Explicit specialization.
633 template<typename T> bool foo(T) { return false; };
634 template<> bool foo<int>(int) { return false; };
635 bool x = [[foo]](42);
637 EXPECT_DECLS("DeclRefExpr",
"template<> bool foo<int>(int)");
640TEST_F(TargetDeclTest, VariableTemplate) {
643 // Implicit specialization.
644 template<typename T> int foo;
645 int x = [[foo]]<char>;
647 EXPECT_DECLS("DeclRefExpr", {
"int foo", Rel::TemplateInstantiation},
648 {
"int foo", Rel::TemplatePattern});
651 // Explicit specialization.
652 template<typename T> int foo;
653 template <> bool foo<char>;
654 int x = [[foo]]<char>;
659 // Partial specialization.
660 template<typename T> int foo;
661 template<typename T> bool foo<T*>;
662 bool x = [[foo]]<char*>;
664 EXPECT_DECLS("DeclRefExpr", {
"bool foo", Rel::TemplateInstantiation},
665 {
"bool foo", Rel::TemplatePattern});
668TEST_F(TargetDeclTest, TypeAliasTemplate) {
670 template<typename T, int X> class SmallVector {};
671 template<typename U> using TinyVector = SmallVector<U, 1>;
672 [[TinyVector<int>]] X;
675 {
"template<> class SmallVector<int, 1>",
676 Rel::TemplateInstantiation | Rel::Underlying},
677 {
"class SmallVector", Rel::TemplatePattern | Rel::Underlying},
678 {
"using TinyVector = SmallVector<U, 1>",
679 Rel::Alias | Rel::TemplatePattern});
682TEST_F(TargetDeclTest, MemberOfTemplate) {
684 template <typename T> struct Foo {
687 int y = Foo<int>().[[x]](42);
689 EXPECT_DECLS("MemberExpr", {
"int x(int)", Rel::TemplateInstantiation},
690 {
"int x(T)", Rel::TemplatePattern});
693 template <typename T> struct Foo {
694 template <typename U>
697 int y = Foo<char>().[[x]]('c', 42);
700 {
"template<> int x<int>(char, int)", Rel::TemplateInstantiation},
701 {
"int x(T, U)", Rel::TemplatePattern});
704TEST_F(TargetDeclTest, Lambda) {
706 void foo(int x = 42) {
707 auto l = [ [[x]] ]{ return x + 1; };
715 void foo(int x = 42) {
716 auto l = [x]{ return [[x]] + 1; };
723 auto l = [x = 1]{ return [[x]] + 1; };
730TEST_F(TargetDeclTest, OverloadExpr) {
731 Flags.push_back(
"--target=x86_64-pc-linux-gnu");
742 EXPECT_DECLS("UnresolvedLookupExpr",
"void func(int *)",
"void func(char *)");
755 EXPECT_DECLS("UnresolvedMemberExpr",
"void func(int *)",
"void func(char *)");
759 static void *operator new(unsigned long);
761 auto* k = [[new]] X();
763 EXPECT_DECLS("CXXNewExpr",
"static void *operator new(unsigned long)");
765 void *operator new(unsigned long);
766 auto* k = [[new]] int();
768 EXPECT_DECLS("CXXNewExpr",
"void *operator new(unsigned long)");
772 static void operator delete(void *) noexcept;
778 EXPECT_DECLS("CXXDeleteExpr",
"static void operator delete(void *) noexcept");
780 void operator delete(void *) noexcept;
785 EXPECT_DECLS("CXXDeleteExpr",
"void operator delete(void *) noexcept");
788TEST_F(TargetDeclTest, DependentExprs) {
791 struct A { void foo() {} };
792 template <typename T>
800 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"void foo()");
810 template <typename T>
814 this->c.getA().[[foo]]();
818 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"void foo()");
829 template <typename T>
833 this->c.fptr().[[foo]]();
837 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"void foo()");
844 template <typename T> struct Foo {
851 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"int aaaa");
857 template <typename T> T convert() const;
859 template <typename T>
861 Foo::k(T()).template [[convert]]<T>();
865 "template <typename T> T convert() const");
868TEST_F(TargetDeclTest, DependentTypes) {
872 struct A { struct B {}; };
874 template <typename T>
875 void foo(typename A<T>::[[B]]);
882 struct A { struct B { struct C {}; }; };
884 template <typename T>
885 void foo(typename A<T>::[[B]]::C);
893 struct A { struct B { struct C {}; }; };
895 template <typename T>
896 void foo(typename A<T>::B::[[C]]);
904 template <typename> struct B {};
907 template <typename T>
908 void foo(typename A<T>::template [[B]]<int>);
911 "template <typename> struct B");
914TEST_F(TargetDeclTest, TypedefCascade) {
920 using type = C::type;
923 using type = B::type;
928 {
"using type = int", Rel::Alias | Rel::Underlying},
929 {
"using type = C::type", Rel::Alias | Rel::Underlying},
930 {
"using type = B::type", Rel::Alias});
933TEST_F(TargetDeclTest, RecursiveTemplate) {
934 Flags.push_back(
"-std=c++20");
937 template <typename T>
938 concept Leaf = false;
940 template <typename Tree>
941 struct descend_left {
942 using type = typename descend_left<typename Tree::left>::[[type]];
946 struct descend_left<Tree> {
947 using type = typename Tree::value;
951 {
"using type = typename descend_left<typename Tree::left>::type",
952 Rel::Alias | Rel::Underlying});
955TEST_F(TargetDeclTest, ObjC) {
956 Flags = {
"-xobjective-c"};
968 @interface Foo { @public int bar; }
985 EXPECT_DECLS("ObjCPropertyRefExpr",
"- (void)setX:(int)x");
989 @property(retain) I* x;
990 @property(retain) I* y;
997 "@property(atomic, retain, readwrite) I *x");
1002 @interface Interface
1003 @property(retain) [[MYObject]] *x;
1006 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface MYObject");
1009 @interface MYObject2
1011 @interface Interface
1012 @property(retain, nonnull) [[MYObject2]] *x;
1015 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface MYObject2");
1021 return [[@protocol(Foo)]];
1029 void test([[Foo]] *p);
1031 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface Foo");
1033 Code = R
"cpp(// Don't consider implicit interface as the target.
1034 @implementation [[Implicit]]
1037 EXPECT_DECLS("ObjCImplementationDecl",
"@implementation Implicit");
1042 @implementation [[Foo]]
1045 EXPECT_DECLS("ObjCImplementationDecl",
"@interface Foo");
1050 @interface Foo (Ext)
1052 @implementation [[Foo]] (Ext)
1055 EXPECT_DECLS("ObjCCategoryImplDecl",
"@interface Foo(Ext)");
1058 void test(id</*error-ok*/[[InvalidProtocol]]> p);
1066 void test([[C]]<Foo> *p);
1074 void test(C<[[Foo]]> *p);
1084 void test(C<[[Foo]], Bar> *p);
1094 void test(C<Foo, [[Bar]]> *p);
1100 + (id)sharedInstance;
1103 + (id)sharedInstance { return 0; }
1106 id value = [[Foo]].sharedInstance;
1109 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface Foo");
1113 + (id)sharedInstance;
1116 + (id)sharedInstance { return 0; }
1119 id value = Foo.[[sharedInstance]];
1122 EXPECT_DECLS("ObjCPropertyRefExpr",
"+ (id)sharedInstance");
1126 + ([[id]])sharedInstance;
1133 + ([[instancetype]])sharedInstance;
1139class FindExplicitReferencesTest :
public ::testing::Test {
1149 AllRefs annotateReferencesInFoo(llvm::StringRef
Code) {
1151 TU.Code = std::string(
Code);
1155 TU.ExtraArgs.push_back(
"-std=c++20");
1156 TU.ExtraArgs.push_back(
"-xobjective-c++");
1158 auto AST = TU.build();
1160 if (
auto *T = llvm::dyn_cast<FunctionTemplateDecl>(TestDecl))
1161 TestDecl = T->getTemplatedDecl();
1163 std::vector<ReferenceLoc> Refs;
1164 if (
const auto *Func = llvm::dyn_cast<FunctionDecl>(TestDecl))
1167 [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1168 AST.getHeuristicResolver());
1169 else if (
const auto *NS = llvm::dyn_cast<NamespaceDecl>(TestDecl))
1172 [&Refs, &NS](ReferenceLoc R) {
1174 if (R.Targets.size() == 1 && R.Targets.front() == NS)
1176 Refs.push_back(std::move(R));
1178 AST.getHeuristicResolver());
1180 ADD_FAILURE() <<
"Failed to find ::foo decl for test";
1182 auto &SM =
AST.getSourceManager();
1183 llvm::sort(Refs, [&](
const ReferenceLoc &L,
const ReferenceLoc &R) {
1184 return SM.isBeforeInTranslationUnit(L.NameLoc, R.NameLoc);
1188 unsigned NextCodeChar = 0;
1189 for (
unsigned I = 0; I < Refs.size(); ++I) {
1192 SourceLocation
Pos = R.NameLoc;
1193 assert(
Pos.isValid());
1194 if (
Pos.isMacroID())
1195 Pos = SM.getExpansionLoc(
Pos);
1196 assert(
Pos.isFileID());
1201 if (
File == SM.getMainFileID()) {
1203 assert(NextCodeChar <=
Offset);
1213 for (
unsigned I = 0; I < Refs.size(); ++I)
1220TEST_F(FindExplicitReferencesTest,
All) {
1221 std::pair< llvm::StringRef, llvm::StringRef> Cases[] =
1226 void foo(int param) {
1227 $0^global = $1^param + $2^func();
1230 "0: targets = {global}\n"
1231 "1: targets = {param}\n"
1232 "2: targets = {func}\n"},
1234 struct X { int a; };
1239 "0: targets = {x}\n"
1240 "1: targets = {X::a}\n"},
1242 // error-ok: testing with broken code
1245 return $0^bar() + $1^bar(42);
1248 "0: targets = {bar}\n"
1249 "1: targets = {bar}\n"},
1253 namespace alias = ns;
1255 using namespace $0^ns;
1256 using namespace $1^alias;
1259 "0: targets = {ns}\n"
1260 "1: targets = {alias}\n"},
1263 namespace ns { int global; }
1265 using $0^ns::$1^global;
1268 "0: targets = {ns}\n"
1269 "1: targets = {ns::global}, qualifier = 'ns::'\n"},
1272 namespace ns { enum class A {}; }
1274 using enum $0^ns::$1^A;
1277 "0: targets = {ns}\n"
1278 "1: targets = {ns::A}, qualifier = 'ns::'\n"},
1281 struct Struct { int a; };
1282 using Typedef = int;
1286 static_cast<$4^Struct*>(0);
1289 "0: targets = {Struct}\n"
1290 "1: targets = {x}, decl\n"
1291 "2: targets = {Typedef}\n"
1292 "3: targets = {y}, decl\n"
1293 "4: targets = {Struct}\n"},
1296 namespace a { namespace b { struct S { typedef int type; }; } }
1298 $0^a::$1^b::$2^S $3^x;
1299 using namespace $4^a::$5^b;
1303 "0: targets = {a}\n"
1304 "1: targets = {a::b}, qualifier = 'a::'\n"
1305 "2: targets = {a::b::S}, qualifier = 'a::b::'\n"
1306 "3: targets = {x}, decl\n"
1307 "4: targets = {a}\n"
1308 "5: targets = {a::b}, qualifier = 'a::'\n"
1309 "6: targets = {a::b::S}\n"
1310 "7: targets = {a::b::S::type}, qualifier = 'struct S::'\n"
1311 "8: targets = {y}, decl\n"},
1314 $0^ten: // PRINT "HELLO WORLD!"
1318 "0: targets = {ten}, decl\n"
1319 "1: targets = {ten}\n"},
1322 template <class T> struct vector { using value_type = T; };
1323 template <> struct vector<bool> { using value_type = bool; };
1325 $0^vector<int> $1^vi;
1326 $2^vector<bool> $3^vb;
1329 "0: targets = {vector<int>}\n"
1330 "1: targets = {vi}, decl\n"
1331 "2: targets = {vector<bool>}\n"
1332 "3: targets = {vb}, decl\n"},
1335 template <class T> struct vector { using value_type = T; };
1336 template <> struct vector<bool> { using value_type = bool; };
1337 template <class T> using valias = vector<T>;
1339 $0^valias<int> $1^vi;
1340 $2^valias<bool> $3^vb;
1343 "0: targets = {valias}\n"
1344 "1: targets = {vi}, decl\n"
1345 "2: targets = {valias}\n"
1346 "3: targets = {vb}, decl\n"},
1350 template <typename $0^T>
1357 "0: targets = {foo::Bar::T}, decl\n"
1358 "1: targets = {foo::Bar}, decl\n"
1359 "2: targets = {foo::Bar}\n"
1360 "3: targets = {foo::Bar::f}, decl\n"
1361 "4: targets = {foo::Bar}\n"},
1364 struct X { void func(int); };
1372 "0: targets = {y}\n"
1373 "1: targets = {Y::func}\n"},
1376 namespace ns { void bar(int); }
1383 "0: targets = {bar}\n"},
1389 void foo(int a, int b) {
1393 "0: targets = {a}\n"
1394 "1: targets = {b}\n"},
1403 for (int $0^x : $1^vector()) {
1408 "0: targets = {x}, decl\n"
1409 "1: targets = {vector}\n"
1410 "2: targets = {x}\n"},
1413 namespace ns1 { void func(char*); }
1414 namespace ns2 { void func(int*); }
1415 using namespace ns1;
1416 using namespace ns2;
1423 "0: targets = {ns1::func, ns2::func}\n"
1424 "1: targets = {t}\n"},
1433 void foo(X x, T t) {
1437 "0: targets = {x}\n"
1438 "1: targets = {X::func, X::func}\n"
1439 "2: targets = {t}\n"},
1449 $0^S<$1^T>::$2^value;
1452 "0: targets = {S}\n"
1453 "1: targets = {T}\n"
1454 "2: targets = {S::value}, qualifier = 'S<T>::'\n"},
1467 "0: targets = {t}\n"
1468 "1: targets = {S::value}\n"},
1473 static_cast<$0^T>(0);
1478 "0: targets = {T}\n"
1479 "1: targets = {T}\n"
1480 "2: targets = {T}\n"
1481 "3: targets = {t}, decl\n"},
1489 "0: targets = {x}, decl\n"
1490 "1: targets = {I}\n"},
1493 template <class T> struct vector {};
1495 template <template<class> class TT, template<class> class ...TP>
1499 $4^foo<$5^vector>();
1503 "0: targets = {TT}\n"
1504 "1: targets = {x}, decl\n"
1505 "2: targets = {foo}\n"
1506 "3: targets = {TT}\n"
1507 "4: targets = {foo}\n"
1508 "5: targets = {vector}\n"
1509 "6: targets = {foo}\n"
1510 "7: targets = {TP}\n"},
1514 template <int(*)()> struct wrapper {};
1516 template <int(*FuncParam)()>
1518 $0^wrapper<$1^func> $2^w;
1522 "0: targets = {wrapper<&func>}\n"
1523 "1: targets = {func}\n"
1524 "2: targets = {w}, decl\n"
1525 "3: targets = {FuncParam}\n"},
1531 class $0^Foo { $1^Foo(); ~$2^Foo(); int $3^field; };
1533 enum $5^E { $6^ABC };
1535 using $8^INT2 = int;
1536 namespace $9^NS = $10^ns;
1539 "0: targets = {Foo}, decl\n"
1540 "1: targets = {foo()::Foo::Foo}, decl\n"
1541 "2: targets = {Foo}\n"
1542 "3: targets = {foo()::Foo::field}, decl\n"
1543 "4: targets = {Var}, decl\n"
1544 "5: targets = {E}, decl\n"
1545 "6: targets = {foo()::ABC}, decl\n"
1546 "7: targets = {INT}, decl\n"
1547 "8: targets = {INT2}, decl\n"
1548 "9: targets = {NS}, decl\n"
1549 "10: targets = {ns}\n"},
1556 // FIXME: This should have only one reference to Bar.
1557 $2^operator $3^$4^Bar();
1561 $7^f.$8^operator $9^Bar();
1564 "0: targets = {Bar}, decl\n"
1565 "1: targets = {Foo}, decl\n"
1566 "2: targets = {foo()::Foo::operator Bar}, decl\n"
1567 "3: targets = {Bar}\n"
1568 "4: targets = {Bar}\n"
1569 "5: targets = {Foo}\n"
1570 "6: targets = {f}, decl\n"
1571 "7: targets = {f}\n"
1572 "8: targets = {foo()::Foo::operator Bar}\n"
1573 "9: targets = {Bar}\n"},
1581 void $2^destructMe() {
1587 $6^f.~ /*...*/ $7^Foo();
1590 "0: targets = {Foo}, decl\n"
1593 "1: targets = {Foo}\n"
1594 "2: targets = {foo()::Foo::destructMe}, decl\n"
1595 "3: targets = {Foo}\n"
1596 "4: targets = {Foo}\n"
1597 "5: targets = {f}, decl\n"
1598 "6: targets = {f}\n"
1599 "7: targets = {Foo}\n"},
1604 // member initializer
1610 class $4^Derived : public $5^Base {
1612 $8^Derived() : $9^Base() {}
1614 // delegating initializer
1617 $12^Foo(): $13^Foo(111) {}
1621 "0: targets = {X}, decl\n"
1622 "1: targets = {foo()::X::abc}, decl\n"
1623 "2: targets = {foo()::X::X}, decl\n"
1624 "3: targets = {foo()::X::abc}\n"
1625 "4: targets = {Derived}, decl\n"
1626 "5: targets = {Base}\n"
1627 "6: targets = {Base}\n"
1628 "7: targets = {foo()::Derived::B}, decl\n"
1629 "8: targets = {foo()::Derived::Derived}, decl\n"
1630 "9: targets = {Base}\n"
1631 "10: targets = {Foo}, decl\n"
1632 "11: targets = {foo()::Foo::Foo}, decl\n"
1633 "12: targets = {foo()::Foo::Foo}, decl\n"
1634 "13: targets = {Foo}\n"},
1640 int (*$2^fptr)(int $3^a, int) = nullptr;
1643 "0: targets = {(unnamed)}\n"
1644 "1: targets = {x}, decl\n"
1645 "2: targets = {fptr}, decl\n"
1646 "3: targets = {a}, decl\n"},
1650 namespace ns { struct Type {}; }
1651 namespace alias = ns;
1652 namespace rec_alias = alias;
1655 $0^ns::$1^Type $2^a;
1656 $3^alias::$4^Type $5^b;
1657 $6^rec_alias::$7^Type $8^c;
1660 "0: targets = {ns}\n"
1661 "1: targets = {ns::Type}, qualifier = 'ns::'\n"
1662 "2: targets = {a}, decl\n"
1663 "3: targets = {alias}\n"
1664 "4: targets = {ns::Type}, qualifier = 'alias::'\n"
1665 "5: targets = {b}, decl\n"
1666 "6: targets = {rec_alias}\n"
1667 "7: targets = {ns::Type}, qualifier = 'rec_alias::'\n"
1668 "8: targets = {c}, decl\n"},
1672 template <typename... E>
1674 constexpr int $0^size = sizeof...($1^E);
1677 "0: targets = {size}, decl\n"
1678 "1: targets = {E}\n"},
1682 template <typename T>
1690 "0: targets = {Test}\n"
1691 "1: targets = {a}, decl\n"},
1695 template <typename $0^T>
1699 "0: targets = {foo::Bar::T}, decl\n"
1700 "1: targets = {foo::Bar}, decl\n"},
1704 template <typename $0^T>
1708 "0: targets = {T}, decl\n"
1709 "1: targets = {foo::func}, decl\n"},
1713 template <typename $0^T>
1717 "0: targets = {foo::T}, decl\n"
1718 "1: targets = {foo::T}\n"
1719 "2: targets = {foo::x}, decl\n"},
1722 template<typename T> class vector {};
1724 template <typename $0^T>
1725 using $1^V = $2^vector<$3^T>;
1728 "0: targets = {foo::T}, decl\n"
1729 "1: targets = {foo::V}, decl\n"
1730 "2: targets = {vector}\n"
1731 "3: targets = {foo::T}\n"},
1735 template <typename T>
1736 concept Drawable = requires (T t) { t.draw(); };
1739 template <typename $0^T> requires $1^Drawable<$2^T>
1740 void $3^bar($4^T $5^t) {
1745 "0: targets = {T}, decl\n"
1746 "1: targets = {Drawable}\n"
1747 "2: targets = {T}\n"
1748 "3: targets = {foo::bar}, decl\n"
1749 "4: targets = {T}\n"
1750 "5: targets = {t}, decl\n"
1751 "6: targets = {t}\n"
1752 "7: targets = {}\n"},
1766 "0: targets = {f}\n"
1767 "1: targets = {I::_z}\n"},
1772 @property(retain) I* x;
1773 @property(retain) I* y;
1780 "0: targets = {f}\n"
1781 "1: targets = {I::x}\n"
1782 "2: targets = {I::y}\n"},
1795 "0: targets = {f}\n"
1796 "1: targets = {I::x}\n"
1797 "2: targets = {I::setY:}\n"},
1802 @property(class) I *x;
1807 $2^local = $3^I.$4^x;
1810 "0: targets = {I}\n"
1811 "1: targets = {I::setX:}\n"
1812 "2: targets = {local}\n"
1813 "3: targets = {I}\n"
1814 "4: targets = {I::x}\n"},
1825 $2^local = $3^I.$4^x;
1828 "0: targets = {I}\n"
1829 "1: targets = {I::setX:}\n"
1830 "2: targets = {local}\n"
1831 "3: targets = {I}\n"
1832 "4: targets = {I::x}\n"},
1836 -(void) a:(int)x b:(int)y;
1842 "0: targets = {i}\n"
1843 "1: targets = {I::a:b:}\n"},
1854 "0: targets = {I}\n"
1855 "1: targets = {P}\n"
1856 "2: targets = {x}, decl\n"},
1864 $2^Foo $3^f { .$4^Bar = 42 };
1867 "0: targets = {Foo}, decl\n"
1868 "1: targets = {foo()::Foo::Bar}, decl\n"
1869 "2: targets = {Foo}\n"
1870 "3: targets = {f}, decl\n"
1871 "4: targets = {foo()::Foo::Bar}\n"},
1880 $5^Bar $6^bar { .$7^Foo.$8^Field = 42 };
1883 "0: targets = {Baz}, decl\n"
1884 "1: targets = {foo()::Baz::Field}, decl\n"
1885 "2: targets = {Bar}, decl\n"
1886 "3: targets = {Baz}\n"
1887 "4: targets = {foo()::Bar::Foo}, decl\n"
1888 "5: targets = {Bar}\n"
1889 "6: targets = {bar}, decl\n"
1890 "7: targets = {foo()::Bar::Foo}\n"
1891 "8: targets = {foo()::Baz::Field}\n"},
1893 template<typename T>
1895 template<typename T>
1897 $0^crash({.$1^x = $2^T()});
1900 "0: targets = {crash}\n"
1902 "2: targets = {T}\n"},
1905 template <template <typename> typename T>
1908 template <typename $0^T>
1909 struct $1^Derive : $2^Base<$3^T::template $4^Unknown> {};
1912 "0: targets = {foo::Derive::T}, decl\n"
1913 "1: targets = {foo::Derive}, decl\n"
1914 "2: targets = {Base}\n"
1915 "3: targets = {foo::Derive::T}\n"
1916 "4: targets = {}, qualifier = 'T::'\n"},
1920 template <typename $0^T>
1922 template <typename $2^I>
1925 template <typename $5^I>
1926 $6^Test($7^I) -> $8^Test<typename $9^I::$10^type>;
1929 "0: targets = {T}, decl\n"
1930 "1: targets = {foo::Test}, decl\n"
1931 "2: targets = {I}, decl\n"
1932 "3: targets = {foo::Test::Test<T>}, decl\n"
1933 "4: targets = {I}\n"
1934 "5: targets = {I}, decl\n"
1935 "6: targets = {foo::Test}\n"
1936 "7: targets = {I}\n"
1937 "8: targets = {foo::Test}\n"
1938 "9: targets = {I}\n"
1939 "10: targets = {}, qualifier = 'I::'\n"}};
1941 for (
const auto &
C : Cases) {
1942 llvm::StringRef ExpectedCode =
C.first;
1943 llvm::StringRef ExpectedRefs =
C.second;
1946 annotateReferencesInFoo(llvm::Annotations(ExpectedCode).code());
1947 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
1948 EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
#define EXPECT_DECLS(NodeType,...)
std::string AnnotatedCode
std::string DumpedReferences
llvm::raw_string_ostream OS
static SelectionTree createRight(ASTContext &AST, const syntax::TokenBuffer &Tokens, unsigned Begin, unsigned End)
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
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)
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static TestTU withCode(llvm::StringRef Code)