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 {
68 std::vector<std::string> Flags;
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>
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, RewrittenBinaryOperator) {
622 Flags.push_back(
"-std=c++20");
626 struct strong_ordering {
628 constexpr operator int() const { return n; }
629 static const strong_ordering equal, greater, less;
631 constexpr strong_ordering strong_ordering::equal = {0};
632 constexpr strong_ordering strong_ordering::greater = {1};
633 constexpr strong_ordering strong_ordering::less = {-1};
639 auto operator<=>(const Foo&) const = default;
642 bool x = (Foo(1) [[!=]] Foo(2));
645 {
"bool operator==(const Foo &) const noexcept = default"});
648TEST_F(TargetDeclTest, FunctionTemplate) {
650 // Implicit specialization.
651 template<typename T> bool foo(T) { return false; };
652 bool x = [[foo]](42);
655 {
"template<> bool foo<int>(int)", Rel::TemplateInstantiation},
656 {
"bool foo(T)", Rel::TemplatePattern});
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);
664 EXPECT_DECLS("DeclRefExpr",
"template<> bool foo<int>(int)");
667TEST_F(TargetDeclTest, VariableTemplate) {
670 // Implicit specialization.
671 template<typename T> int foo;
672 int x = [[foo]]<char>;
674 EXPECT_DECLS("DeclRefExpr", {
"int foo", Rel::TemplateInstantiation},
675 {
"int foo", Rel::TemplatePattern});
678 // Explicit specialization.
679 template<typename T> int foo;
680 template <> bool foo<char>;
681 int x = [[foo]]<char>;
686 // Partial specialization.
687 template<typename T> int foo;
688 template<typename T> bool foo<T*>;
689 bool x = [[foo]]<char*>;
691 EXPECT_DECLS("DeclRefExpr", {
"bool foo", Rel::TemplateInstantiation},
692 {
"bool foo", Rel::TemplatePattern});
695TEST_F(TargetDeclTest, TypeAliasTemplate) {
697 template<typename T, int X> class SmallVector {};
698 template<typename U> using TinyVector = SmallVector<U, 1>;
699 [[TinyVector<int>]] X;
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});
709TEST_F(TargetDeclTest, BuiltinTemplates) {
711 template <class T, T... Index> struct integer_sequence {};
712 [[__make_integer_seq]]<integer_sequence, int, 3> X;
715 "TemplateSpecializationTypeLoc",
716 {
"struct integer_sequence", Rel::TemplatePattern | Rel::Underlying},
717 {
"template<> struct integer_sequence<int, <0, 1, 2>>",
718 Rel::TemplateInstantiation | Rel::Underlying});
722 template <class T, T... Index> struct integer_sequence;
724 template <class T, int N>
725 using make_integer_sequence = [[__make_integer_seq]]<integer_sequence, T, N>;
730 template <int N, class... Pack>
731 using type_pack_element = [[__type_pack_element]]<N, Pack...>;
736 template <template <class...> class Templ, class... Types>
737 using dedup_types = Templ<[[__builtin_dedup_pack]]<Types...>...>;
742TEST_F(TargetDeclTest, MemberOfTemplate) {
744 template <typename T> struct Foo {
747 int y = Foo<int>().[[x]](42);
749 EXPECT_DECLS("MemberExpr", {
"int x(int)", Rel::TemplateInstantiation},
750 {
"int x(T)", Rel::TemplatePattern});
753 template <typename T> struct Foo {
754 template <typename U>
757 int y = Foo<char>().[[x]]('c', 42);
760 {
"template<> int x<int>(char, int)", Rel::TemplateInstantiation},
761 {
"int x(T, U)", Rel::TemplatePattern});
764TEST_F(TargetDeclTest, Lambda) {
766 void foo(int x = 42) {
767 auto l = [ [[x]] ]{ return x + 1; };
775 void foo(int x = 42) {
776 auto l = [x]{ return [[x]] + 1; };
783 auto l = [x = 1]{ return [[x]] + 1; };
790TEST_F(TargetDeclTest, OverloadExpr) {
791 Flags.push_back(
"--target=x86_64-pc-linux-gnu");
802 EXPECT_DECLS("UnresolvedLookupExpr",
"void func(int *)",
"void func(char *)");
815 EXPECT_DECLS("UnresolvedMemberExpr",
"void func(int *)",
"void func(char *)");
819 static void *operator new(unsigned long);
821 auto* k = [[new]] X();
823 EXPECT_DECLS("CXXNewExpr",
"static void *operator new(unsigned long)");
825 void *operator new(unsigned long);
826 auto* k = [[new]] int();
828 EXPECT_DECLS("CXXNewExpr",
"void *operator new(unsigned long)");
832 static void operator delete(void *) noexcept;
838 EXPECT_DECLS("CXXDeleteExpr",
"static void operator delete(void *) noexcept");
840 void operator delete(void *) noexcept;
847 "void operator delete(void *, __size_t) noexcept");
850TEST_F(TargetDeclTest, DependentExprs) {
851 Flags.push_back(
"--std=c++20");
855 struct A { void foo() {} };
856 template <typename T>
874 template <typename T>
878 this->c.getA().[[foo]]();
893 template <typename T>
897 this->c.fptr().[[foo]]();
908 template <typename T> struct Foo {
915 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"int aaaa");
921 template <typename T> T convert() const;
923 template <typename T>
925 Foo::k(T()).template [[convert]]<T>();
929 "template <typename T> T convert() const");
932 template <typename T>
936 template <typename T>
937 using Wally = Waldo<T>;
938 template <typename T>
939 void foo(Wally<T> w) {
943 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"void find()");
946 template <typename T>
950 template <typename T>
952 using Type = Waldo<T>;
954 template <typename T>
955 void foo(typename MetaWaldo<T>::Type w) {
959 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"void find()");
965 template <typename T>
968 struct S : Wally<int> {
969 void Foo() { this->[[find]](); }
987 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"const int found = N");
990TEST_F(TargetDeclTest, DependentTypes) {
994 struct A { struct B {}; };
996 template <typename T>
997 void foo(typename A<T>::[[B]]);
1004 struct A { struct B { struct C {}; }; };
1006 template <typename T>
1007 void foo(typename A<T>::[[B]]::C);
1015 struct A { struct B { struct C {}; }; };
1017 template <typename T>
1018 void foo(typename A<T>::B::[[C]]);
1026 template <typename> struct B {};
1029 template <typename T>
1030 void foo(typename A<T>::template [[B]]<int>);
1032 EXPECT_DECLS("TemplateSpecializationTypeLoc",
"template <typename> struct B");
1039 typedef typename waldo<N - 1>::type::[[next]] type;
1051 using type = typename odd<N - 1>::type::next;
1056 using type = typename even<N - 1>::type::[[next]];
1062TEST_F(TargetDeclTest, TypedefCascade) {
1068 using type = C::type;
1071 using type = B::type;
1076 {
"using type = int", Rel::Alias | Rel::Underlying},
1077 {
"using type = C::type", Rel::Alias | Rel::Underlying},
1078 {
"using type = B::type", Rel::Alias});
1081TEST_F(TargetDeclTest, RecursiveTemplate) {
1082 Flags.push_back(
"-std=c++20");
1085 template <typename T>
1086 concept Leaf = false;
1088 template <typename Tree>
1089 struct descend_left {
1090 using type = typename descend_left<typename Tree::left>::[[type]];
1093 template <Leaf Tree>
1094 struct descend_left<Tree> {
1095 using type = typename Tree::value;
1099 {
"using type = typename descend_left<typename Tree::left>::type",
1100 Rel::Alias | Rel::Underlying});
1103TEST_F(TargetDeclTest, ObjC) {
1104 Flags = {
"-xobjective-c"};
1116 @interface Foo { @public int bar; }
1127 -(void) setX:(int)x;
1133 EXPECT_DECLS("ObjCPropertyRefExpr",
"- (void)setX:(int)x");
1137 @property(retain) I* x;
1138 @property(retain) I* y;
1145 "@property(atomic, retain, readwrite) I *x");
1150 @interface Interface
1151 @property(retain) [[MYObject]] *x;
1154 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface MYObject");
1157 @interface MYObject2
1159 @interface Interface
1160 @property(retain, nonnull) [[MYObject2]] *x;
1163 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface MYObject2");
1169 return [[@protocol(Foo)]];
1177 void test([[Foo]] *p);
1179 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface Foo");
1181 Code = R
"cpp(// Don't consider implicit interface as the target.
1182 @implementation [[Implicit]]
1185 EXPECT_DECLS("ObjCImplementationDecl",
"@implementation Implicit");
1190 @implementation [[Foo]]
1193 EXPECT_DECLS("ObjCImplementationDecl",
"@interface Foo");
1198 @interface Foo (Ext)
1200 @implementation [[Foo]] (Ext)
1203 EXPECT_DECLS("ObjCCategoryImplDecl",
"@interface Foo(Ext)");
1208 @interface Foo (Ext)
1210 @implementation Foo ([[Ext]])
1213 EXPECT_DECLS("ObjCCategoryImplDecl",
"@interface Foo(Ext)");
1216 void test(id</*error-ok*/[[InvalidProtocol]]> p);
1224 void test([[C]]<Foo> *p);
1232 void test(C<[[Foo]]> *p);
1242 void test(C<[[Foo]], Bar> *p);
1252 void test(C<Foo, [[Bar]]> *p);
1258 + (id)sharedInstance;
1261 + (id)sharedInstance { return 0; }
1264 id value = [[Foo]].sharedInstance;
1267 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface Foo");
1271 + (id)sharedInstance;
1274 + (id)sharedInstance { return 0; }
1277 id value = Foo.[[sharedInstance]];
1280 EXPECT_DECLS("ObjCPropertyRefExpr",
"+ (id)sharedInstance");
1284 + ([[id]])sharedInstance;
1291 + ([[instancetype]])sharedInstance;
1297class FindExplicitReferencesTest :
public ::testing::Test {
1300 std::string AnnotatedCode;
1301 std::string DumpedReferences;
1304 TestTU newTU(llvm::StringRef Code) {
1306 TU.Code = std::string(Code);
1310 TU.ExtraArgs.push_back(
"-std=c++20");
1311 TU.ExtraArgs.push_back(
"-xobjective-c++");
1316 AllRefs annotatedReferences(llvm::StringRef Code, ParsedAST &
AST,
1317 std::vector<ReferenceLoc> Refs) {
1318 auto &SM =
AST.getSourceManager();
1319 llvm::stable_sort(Refs, [&](
const ReferenceLoc &L,
const ReferenceLoc &R) {
1320 return SM.isBeforeInTranslationUnit(L.NameLoc, R.NameLoc);
1323 std::string AnnotatedCode;
1324 unsigned NextCodeChar = 0;
1325 for (
unsigned I = 0; I < Refs.size(); ++I) {
1328 SourceLocation Pos = R.NameLoc;
1329 assert(Pos.isValid());
1330 if (Pos.isMacroID())
1331 Pos = SM.getExpansionLoc(Pos);
1332 assert(Pos.isFileID());
1336 std::tie(
File, Offset) = SM.getDecomposedLoc(Pos);
1337 if (
File == SM.getMainFileID()) {
1339 assert(NextCodeChar <= Offset);
1340 AnnotatedCode += Code.substr(NextCodeChar, Offset - NextCodeChar);
1341 AnnotatedCode +=
"$" + std::to_string(I) +
"^";
1343 NextCodeChar = Offset;
1346 AnnotatedCode += Code.substr(NextCodeChar);
1348 std::string DumpedReferences;
1349 for (
unsigned I = 0; I < Refs.size(); ++I)
1350 DumpedReferences += std::string(llvm::formatv(
"{0}: {1}\n", I, Refs[I]));
1352 return AllRefs{std::move(AnnotatedCode), std::move(DumpedReferences)};
1358 AllRefs annotateAllReferences(llvm::StringRef Code) {
1359 TestTU TU = newTU(Code);
1360 auto AST = TU.build();
1362 std::vector<ReferenceLoc> Refs;
1363 for (
auto *TopLevel :
AST.getLocalTopLevelDecls())
1365 TopLevel, [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1366 AST.getHeuristicResolver());
1367 return annotatedReferences(Code,
AST, std::move(Refs));
1373 AllRefs annotateReferencesInFoo(llvm::StringRef Code) {
1374 TestTU TU = newTU(Code);
1375 auto AST = TU.build();
1377 if (
auto *T = llvm::dyn_cast<FunctionTemplateDecl>(TestDecl))
1378 TestDecl =
T->getTemplatedDecl();
1380 std::vector<ReferenceLoc> Refs;
1381 if (
const auto *Func = llvm::dyn_cast<FunctionDecl>(TestDecl))
1384 [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1385 AST.getHeuristicResolver());
1386 else if (
const auto *NS = llvm::dyn_cast<NamespaceDecl>(TestDecl))
1389 [&Refs, &NS](ReferenceLoc R) {
1391 if (R.Targets.size() == 1 && R.Targets.front() == NS)
1393 Refs.push_back(std::move(R));
1395 AST.getHeuristicResolver());
1396 else if (
const auto *OC = llvm::dyn_cast<ObjCContainerDecl>(TestDecl))
1398 OC, [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1399 AST.getHeuristicResolver());
1401 ADD_FAILURE() <<
"Failed to find ::foo decl for test";
1403 return annotatedReferences(Code,
AST, std::move(Refs));
1407TEST_F(FindExplicitReferencesTest, AllRefsInFoo) {
1408 std::pair< llvm::StringRef, llvm::StringRef> Cases[] =
1413 void foo(int param) {
1414 $0^global = $1^param + $2^func();
1417 "0: targets = {global}\n"
1418 "1: targets = {param}\n"
1419 "2: targets = {func}\n"},
1421 struct X { int a; };
1426 "0: targets = {x}\n"
1427 "1: targets = {X::a}\n"},
1429 // error-ok: testing with broken code
1432 return $0^bar() + $1^bar(42);
1435 "0: targets = {bar}\n"
1436 "1: targets = {bar}\n"},
1440 namespace alias = ns;
1442 using namespace $0^ns;
1443 using namespace $1^alias;
1446 "0: targets = {ns}\n"
1447 "1: targets = {alias}\n"},
1450 namespace ns { int global; }
1452 using $0^ns::$1^global;
1455 "0: targets = {ns}\n"
1456 "1: targets = {ns::global}, qualifier = 'ns::'\n"},
1459 namespace ns { enum class A {}; }
1461 using enum $0^ns::$1^A;
1464 "0: targets = {ns}\n"
1465 "1: targets = {ns::A}, qualifier = 'ns::'\n"},
1468 struct Struct { int a; };
1469 using Typedef = int;
1473 static_cast<$4^Struct*>(0);
1476 "0: targets = {Struct}\n"
1477 "1: targets = {x}, decl\n"
1478 "2: targets = {Typedef}\n"
1479 "3: targets = {y}, decl\n"
1480 "4: targets = {Struct}\n"},
1483 namespace a { namespace b { struct S { typedef int type; }; } }
1485 $0^a::$1^b::$2^S $3^x;
1486 using namespace $4^a::$5^b;
1490 "0: targets = {a}\n"
1491 "1: targets = {a::b}, qualifier = 'a::'\n"
1492 "2: targets = {a::b::S}, qualifier = 'a::b::'\n"
1493 "3: targets = {x}, decl\n"
1494 "4: targets = {a}\n"
1495 "5: targets = {a::b}, qualifier = 'a::'\n"
1496 "6: targets = {a::b::S}\n"
1497 "7: targets = {a::b::S::type}, qualifier = 'S::'\n"
1498 "8: targets = {y}, decl\n"},
1501 $0^ten: // PRINT "HELLO WORLD!"
1505 "0: targets = {ten}, decl\n"
1506 "1: targets = {ten}\n"},
1509 template <class T> struct vector { using value_type = T; };
1510 template <> struct vector<bool> { using value_type = bool; };
1512 $0^vector<int> $1^vi;
1513 $2^vector<bool> $3^vb;
1516 "0: targets = {vector<int>}\n"
1517 "1: targets = {vi}, decl\n"
1518 "2: targets = {vector<bool>}\n"
1519 "3: targets = {vb}, decl\n"},
1522 template <class T> struct vector { using value_type = T; };
1523 template <> struct vector<bool> { using value_type = bool; };
1524 template <class T> using valias = vector<T>;
1526 $0^valias<int> $1^vi;
1527 $2^valias<bool> $3^vb;
1530 "0: targets = {valias}\n"
1531 "1: targets = {vi}, decl\n"
1532 "2: targets = {valias}\n"
1533 "3: targets = {vb}, decl\n"},
1537 template <typename $0^T>
1544 "0: targets = {foo::Bar::T}, decl\n"
1545 "1: targets = {foo::Bar}, decl\n"
1546 "2: targets = {foo::Bar}\n"
1547 "3: targets = {foo::Bar::f}, decl\n"
1548 "4: targets = {foo::Bar}\n"},
1551 struct X { void func(int); };
1559 "0: targets = {y}\n"
1560 "1: targets = {Y::func}\n"},
1563 namespace ns { void bar(int); }
1570 "0: targets = {bar}\n"},
1576 void foo(int a, int b) {
1580 "0: targets = {a}\n"
1581 "1: targets = {b}\n"},
1590 for (int $0^x : $1^vector()) {
1595 "0: targets = {x}, decl\n"
1596 "1: targets = {vector}\n"
1597 "2: targets = {x}\n"},
1600 namespace ns1 { void func(char*); }
1601 namespace ns2 { void func(int*); }
1602 using namespace ns1;
1603 using namespace ns2;
1610 "0: targets = {ns1::func, ns2::func}\n"
1611 "1: targets = {t}\n"},
1620 void foo(X x, T t) {
1624 "0: targets = {x}\n"
1625 "1: targets = {X::func, X::func}\n"
1626 "2: targets = {t}\n"},
1636 $0^S<$1^T>::$2^value;
1639 "0: targets = {S}\n"
1640 "1: targets = {T}\n"
1641 "2: targets = {S::value}, qualifier = 'S<T>::'\n"},
1654 "0: targets = {t}\n"
1655 "1: targets = {S::value}\n"},
1660 static_cast<$0^T>(0);
1665 "0: targets = {T}\n"
1666 "1: targets = {T}\n"
1667 "2: targets = {T}\n"
1668 "3: targets = {t}, decl\n"},
1676 "0: targets = {x}, decl\n"
1677 "1: targets = {I}\n"},
1680 template <class T> struct vector {};
1682 template <template<class> class TT, template<class> class ...TP>
1686 $4^foo<$5^vector>();
1690 "0: targets = {TT}\n"
1691 "1: targets = {x}, decl\n"
1692 "2: targets = {foo}\n"
1693 "3: targets = {TT}\n"
1694 "4: targets = {foo}\n"
1695 "5: targets = {vector}\n"
1696 "6: targets = {foo}\n"
1697 "7: targets = {TP}\n"},
1701 template <int(*)()> struct wrapper {};
1703 template <int(*FuncParam)()>
1705 $0^wrapper<$1^func> $2^w;
1709 "0: targets = {wrapper<&func>}\n"
1710 "1: targets = {func}\n"
1711 "2: targets = {w}, decl\n"
1712 "3: targets = {FuncParam}\n"},
1718 class $0^Foo { $1^Foo(); ~$2^Foo(); int $3^field; };
1720 enum $5^E { $6^ABC };
1722 using $8^INT2 = int;
1723 namespace $9^NS = $10^ns;
1726 "0: targets = {Foo}, decl\n"
1727 "1: targets = {foo()::Foo::Foo}, decl\n"
1728 "2: targets = {Foo}\n"
1729 "3: targets = {foo()::Foo::field}, decl\n"
1730 "4: targets = {Var}, decl\n"
1731 "5: targets = {E}, decl\n"
1732 "6: targets = {foo()::ABC}, decl\n"
1733 "7: targets = {INT}, decl\n"
1734 "8: targets = {INT2}, decl\n"
1735 "9: targets = {NS}, decl\n"
1736 "10: targets = {ns}\n"},
1743 // FIXME: This should have only one reference to Bar.
1744 $2^operator $3^$4^Bar();
1748 $7^f.$8^operator $9^Bar();
1751 "0: targets = {Bar}, decl\n"
1752 "1: targets = {Foo}, decl\n"
1753 "2: targets = {foo()::Foo::operator Bar}, decl\n"
1754 "3: targets = {Bar}\n"
1755 "4: targets = {Bar}\n"
1756 "5: targets = {Foo}\n"
1757 "6: targets = {f}, decl\n"
1758 "7: targets = {f}\n"
1759 "8: targets = {foo()::Foo::operator Bar}\n"
1760 "9: targets = {Bar}\n"},
1768 void $2^destructMe() {
1774 $6^f.~ /*...*/ $7^Foo();
1777 "0: targets = {Foo}, decl\n"
1780 "1: targets = {Foo}\n"
1781 "2: targets = {foo()::Foo::destructMe}, decl\n"
1782 "3: targets = {Foo}\n"
1783 "4: targets = {Foo}\n"
1784 "5: targets = {f}, decl\n"
1785 "6: targets = {f}\n"
1786 "7: targets = {Foo}\n"},
1791 // member initializer
1797 class $4^Derived : public $5^Base {
1799 $8^Derived() : $9^Base() {}
1801 // delegating initializer
1804 $12^Foo(): $13^Foo(111) {}
1808 "0: targets = {X}, decl\n"
1809 "1: targets = {foo()::X::abc}, decl\n"
1810 "2: targets = {foo()::X::X}, decl\n"
1811 "3: targets = {foo()::X::abc}\n"
1812 "4: targets = {Derived}, decl\n"
1813 "5: targets = {Base}\n"
1814 "6: targets = {Base}\n"
1815 "7: targets = {foo()::Derived::B}, decl\n"
1816 "8: targets = {foo()::Derived::Derived}, decl\n"
1817 "9: targets = {Base}\n"
1818 "10: targets = {Foo}, decl\n"
1819 "11: targets = {foo()::Foo::Foo}, decl\n"
1820 "12: targets = {foo()::Foo::Foo}, decl\n"
1821 "13: targets = {Foo}\n"},
1827 int (*$2^fptr)(int $3^a, int) = nullptr;
1830 "0: targets = {(unnamed)}\n"
1831 "1: targets = {x}, decl\n"
1832 "2: targets = {fptr}, decl\n"
1833 "3: targets = {a}, decl\n"},
1837 namespace ns { struct Type {}; }
1838 namespace alias = ns;
1839 namespace rec_alias = alias;
1842 $0^ns::$1^Type $2^a;
1843 $3^alias::$4^Type $5^b;
1844 $6^rec_alias::$7^Type $8^c;
1847 "0: targets = {ns}\n"
1848 "1: targets = {ns::Type}, qualifier = 'ns::'\n"
1849 "2: targets = {a}, decl\n"
1850 "3: targets = {alias}\n"
1851 "4: targets = {ns::Type}, qualifier = 'alias::'\n"
1852 "5: targets = {b}, decl\n"
1853 "6: targets = {rec_alias}\n"
1854 "7: targets = {ns::Type}, qualifier = 'rec_alias::'\n"
1855 "8: targets = {c}, decl\n"},
1859 template <typename... E>
1861 constexpr int $0^size = sizeof...($1^E);
1864 "0: targets = {size}, decl\n"
1865 "1: targets = {E}\n"},
1869 template <typename T>
1877 "0: targets = {Test}\n"
1878 "1: targets = {a}, decl\n"},
1882 template <typename $0^T>
1886 "0: targets = {foo::Bar::T}, decl\n"
1887 "1: targets = {foo::Bar}, decl\n"},
1891 template <typename $0^T>
1895 "0: targets = {T}, decl\n"
1896 "1: targets = {foo::func}, decl\n"},
1900 template <typename $0^T>
1904 "0: targets = {foo::T}, decl\n"
1905 "1: targets = {foo::T}\n"
1906 "2: targets = {foo::x}, decl\n"},
1909 template<typename T> class vector {};
1911 template <typename $0^T>
1912 using $1^V = $2^vector<$3^T>;
1915 "0: targets = {foo::T}, decl\n"
1916 "1: targets = {foo::V}, decl\n"
1917 "2: targets = {vector}\n"
1918 "3: targets = {foo::T}\n"},
1922 template <typename T>
1923 concept Drawable = requires (T t) { t.draw(); };
1926 template <typename $0^T> requires $1^Drawable<$2^T>
1927 void $3^bar($4^T $5^t) {
1932 "0: targets = {T}, decl\n"
1933 "1: targets = {Drawable}\n"
1934 "2: targets = {T}\n"
1935 "3: targets = {foo::bar}, decl\n"
1936 "4: targets = {T}\n"
1937 "5: targets = {t}, decl\n"
1938 "6: targets = {t}\n"
1939 "7: targets = {}\n"},
1953 "0: targets = {f}\n"
1954 "1: targets = {I::_z}\n"},
1959 @property(retain) I* x;
1960 @property(retain) I* y;
1967 "0: targets = {f}\n"
1968 "1: targets = {I::x}\n"
1969 "2: targets = {I::y}\n"},
1982 "0: targets = {f}\n"
1983 "1: targets = {I::x}\n"
1984 "2: targets = {I::setY:}\n"},
1989 @property(class) I *x;
1994 $2^local = $3^I.$4^x;
1997 "0: targets = {I}\n"
1998 "1: targets = {I::setX:}\n"
1999 "2: targets = {local}\n"
2000 "3: targets = {I}\n"
2001 "4: targets = {I::x}\n"},
2012 $2^local = $3^I.$4^x;
2015 "0: targets = {I}\n"
2016 "1: targets = {I::setX:}\n"
2017 "2: targets = {local}\n"
2018 "3: targets = {I}\n"
2019 "4: targets = {I::x}\n"},
2023 -(void) a:(int)x b:(int)y;
2029 "0: targets = {i}\n"
2030 "1: targets = {I::a:b:}\n"},
2041 "0: targets = {I}\n"
2042 "1: targets = {P}\n"
2043 "2: targets = {x}, decl\n"},
2051 $2^Foo $3^f { .$4^Bar = 42 };
2054 "0: targets = {Foo}, decl\n"
2055 "1: targets = {foo()::Foo::Bar}, decl\n"
2056 "2: targets = {Foo}\n"
2057 "3: targets = {f}, decl\n"
2058 "4: targets = {foo()::Foo::Bar}\n"},
2067 $5^Bar $6^bar { .$7^Foo.$8^Field = 42 };
2070 "0: targets = {Baz}, decl\n"
2071 "1: targets = {foo()::Baz::Field}, decl\n"
2072 "2: targets = {Bar}, decl\n"
2073 "3: targets = {Baz}\n"
2074 "4: targets = {foo()::Bar::Foo}, decl\n"
2075 "5: targets = {Bar}\n"
2076 "6: targets = {bar}, decl\n"
2077 "7: targets = {foo()::Bar::Foo}\n"
2078 "8: targets = {foo()::Baz::Field}\n"},
2080 template<typename T>
2082 template<typename T>
2084 $0^crash({.$1^x = $2^T()});
2087 "0: targets = {crash}\n"
2089 "2: targets = {T}\n"},
2092 template <template <typename> typename T>
2095 template <typename $0^T>
2096 struct $1^Derive : $2^Base<$3^T::template $4^Unknown> {};
2099 "0: targets = {foo::Derive::T}, decl\n"
2100 "1: targets = {foo::Derive}, decl\n"
2101 "2: targets = {Base}\n"
2102 "3: targets = {foo::Derive::T}\n"
2103 "4: targets = {}, qualifier = 'T::'\n"},
2107 template <typename $0^T>
2109 template <typename $2^I>
2112 template <typename $5^I>
2113 $6^Test($7^I) -> $8^Test<typename $9^I::$10^type>;
2116 "0: targets = {T}, decl\n"
2117 "1: targets = {foo::Test}, decl\n"
2118 "2: targets = {I}, decl\n"
2119 "3: targets = {foo::Test::Test<T>}, decl\n"
2120 "4: targets = {I}\n"
2121 "5: targets = {I}, decl\n"
2122 "6: targets = {foo::Test}\n"
2123 "7: targets = {I}\n"
2124 "8: targets = {foo::Test}\n"
2125 "9: targets = {I}\n"
2126 "10: targets = {}, qualifier = 'I::'\n"}};
2128 for (
const auto &C : Cases) {
2129 llvm::StringRef ExpectedCode =
C.first;
2130 llvm::StringRef ExpectedRefs =
C.second;
2133 annotateReferencesInFoo(llvm::Annotations(ExpectedCode).code());
2134 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
2135 EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
2139TEST_F(FindExplicitReferencesTest, AllRefs) {
2140 std::pair< llvm::StringRef, llvm::StringRef> Cases[] =
2142 @interface $0^MyClass
2144 @implementation $1^$2^MyClass
2147 "0: targets = {MyClass}, decl\n"
2148 "1: targets = {MyClass}\n"
2149 "2: targets = {MyClass}, decl\n"},
2151 @interface $0^MyClass
2153 @interface $1^MyClass ($2^Category)
2155 @implementation $3^MyClass ($4^$5^Category)
2158 "0: targets = {MyClass}, decl\n"
2159 "1: targets = {MyClass}\n"
2160 "2: targets = {Category}, decl\n"
2161 "3: targets = {MyClass}\n"
2162 "4: targets = {Category}\n"
2163 "5: targets = {Category}, decl\n"}};
2165 for (
const auto &C : Cases) {
2166 llvm::StringRef ExpectedCode =
C.first;
2167 llvm::StringRef ExpectedRefs =
C.second;
2169 auto Actual = annotateAllReferences(llvm::Annotations(ExpectedCode).code());
2170 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
2171 EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
#define EXPECT_DECLS(NodeType,...)
static SelectionTree createRight(ASTContext &AST, const syntax::TokenBuffer &Tokens, unsigned Begin, unsigned End)
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
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)