12#include "clang/AST/Decl.h"
13#include "clang/AST/DeclObjC.h"
14#include "clang/AST/DeclTemplate.h"
15#include "clang/Basic/SourceLocation.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/Casting.h"
18#include "llvm/Support/raw_ostream.h"
19#include "llvm/Testing/Annotations/Annotations.h"
20#include "gmock/gmock.h"
21#include "gtest/gtest.h"
22#include <initializer_list>
36 PrintedDecl(
const char *Name, DeclRelationSet Relations = {})
37 : Name(Name), Relations(Relations) {}
38 PrintedDecl(
const NamedDecl *D, DeclRelationSet Relations = {})
39 : Relations(Relations) {
41 llvm::raw_string_ostream OS(S);
43 llvm::StringRef FirstLine =
44 llvm::StringRef(OS.str()).take_until([](
char C) {
return C ==
'\n'; });
45 FirstLine = FirstLine.rtrim(
" {");
46 Name = std::string(FirstLine.rtrim(
" {"));
50 DeclRelationSet Relations;
52bool operator==(
const PrintedDecl &L,
const PrintedDecl &R) {
53 return std::tie(L.Name, L.Relations) == std::tie(R.Name, R.Relations);
55llvm::raw_ostream &
operator<<(llvm::raw_ostream &OS,
const PrintedDecl &D) {
56 return OS <<
D.Name <<
" Rel=" <<
D.Relations;
65class TargetDeclTest :
public ::testing::Test {
69 std::vector<std::string> Flags;
74 std::vector<PrintedDecl> assertNodeAndPrintDecls(
const char *NodeType) {
75 llvm::Annotations
A(Code);
78 auto AST = TU.build();
79 llvm::Annotations::Range R =
A.range();
81 AST.getASTContext(),
AST.getTokens(), R.Begin, R.End);
82 const SelectionTree::Node *N = Selection.commonAncestor();
84 ADD_FAILURE() <<
"No node selected!\n" << Code;
87 EXPECT_EQ(N->kind(), NodeType) << Selection;
89 std::vector<PrintedDecl> ActualDecls;
90 for (
const auto &Entry :
92 ActualDecls.emplace_back(Entry.first, Entry.second);
99#define EXPECT_DECLS(NodeType, ...) \
100 EXPECT_THAT(assertNodeAndPrintDecls(NodeType), \
101 ::testing::UnorderedElementsAreArray( \
102 std::vector<PrintedDecl>({__VA_ARGS__}))) \
104using ExpectedDecls = std::vector<PrintedDecl>;
106TEST_F(TargetDeclTest, Exprs) {
114 struct S { S operator+(S) const; };
115 auto X = S() [[+]] S();
127 void operator()(int n);
134 EXPECT_DECLS("CXXOperatorCallExpr",
"void operator()(int n)");
153TEST_F(TargetDeclTest, RecoveryForC) {
154 Flags = {
"-xc",
"-Xclang",
"-frecovery-ast"};
156 // error-ok: testing behavior on broken code
164TEST_F(TargetDeclTest, Recovery) {
166 // error-ok: testing behavior on broken code
171 EXPECT_DECLS("UnresolvedLookupExpr",
"int f()",
"int f(int, int)");
174TEST_F(TargetDeclTest, RecoveryType) {
176 // error-ok: testing behavior on broken code
177 struct S { int member; };
180 // No overload matches, but we have recovery-expr with the correct type.
181 overloaded().[[member]];
187TEST_F(TargetDeclTest, UsingDecl) {
197 EXPECT_DECLS(
"DeclRefExpr", {
"using foo::f", Rel::Alias}, {
"int f(int)"});
207 EXPECT_DECLS(
"UsingDecl", {
"using foo::f", Rel::Alias}, {
"int f(int)"},
217 int x = Y().[[foo]]();
219 EXPECT_DECLS("MemberExpr", {
"using X::foo", Rel::Alias}, {
"int foo()"});
222 template <typename T>
226 template <typename T>
227 struct Derived : Base<T> {
228 using Base<T>::[[waldo]];
231 EXPECT_DECLS("UnresolvedUsingValueDecl", {
"using Base<T>::waldo", Rel::Alias},
236 template<typename T> class S {};
244 EXPECT_DECLS("TemplateSpecializationTypeLoc", {
"using ns::S", Rel::Alias},
245 {
"template <typename T> class S"},
246 {
"class S", Rel::TemplatePattern});
250 template<typename T> class S {};
254 template <template <typename> class T> class X {};
257 EXPECT_DECLS("TemplateArgumentLoc", {
"using ns::S", Rel::Alias},
258 {
"template <typename T> class S"});
262 template<typename T> class S { public: S(T); };
268 Flags.push_back("-std=c++17");
270 {
"using ns::S", Rel::Alias}, {
"template <typename T> class S"},
271 {
"class S", Rel::TemplatePattern});
275 class Foo { public: class foo {}; };
276 template <class T> class A : public Foo<T> {
277 using typename Foo<T>::foo;
282 {
"using typename Foo<T>::foo", Rel::Alias});
285 Flags.push_back(
"-std=c++20");
287 namespace ns { enum class A { X }; }
288 [[using enum ns::A]];
293 namespace ns { enum class A { X }; }
300TEST_F(TargetDeclTest, BaseSpecifier) {
303 struct Y : [[private]] X {};
308 struct Y : [[private X]] {};
313 struct Y : private [[X]] {};
318TEST_F(TargetDeclTest, ConstructorInitList) {
336TEST_F(TargetDeclTest, DesignatedInit) {
340 struct Y { int b; struct X c[2]; };
341 struct Y y = { .c[0].[[a]] = 1 };
346TEST_F(TargetDeclTest, OffsetOf) {
348 struct Foo { int bar; };
349 int x = __builtin_offsetof(Foo, [[bar]]);
354 struct Inner { int c; };
355 struct Outer { Inner b; };
356 int x = __builtin_offsetof(Outer, [[b]].c);
361 struct Inner { int c; };
362 struct Outer { Inner b; };
363 int x = __builtin_offsetof(Outer, b.[[c]]);
370 struct Inner { int c; };
371 struct Outer { Inner b; };
372 int x = __builtin_offsetof(Outer, [[b.c]]);
374 EXPECT_THAT(assertNodeAndPrintDecls("OffsetOfExpr"), ::testing::IsEmpty());
377 struct Inner { int c; };
378 struct Outer { Inner b; };
379 int x = __builtin_offsetof(Outer, [[b.]]c);
381 EXPECT_THAT(assertNodeAndPrintDecls("OffsetOfExpr"), ::testing::IsEmpty());
384 struct Inner { int c; };
385 struct Outer { Inner b; };
386 int x = __builtin_offsetof(Outer, b[[.c]]);
391 struct Foo { int bar; };
392 int x = __builtin_[[offsetof]](Foo, bar);
394 EXPECT_THAT(assertNodeAndPrintDecls("OffsetOfExpr"), ::testing::IsEmpty());
401 int o = __builtin_offsetof(D, [[x]]);
408 template <typename T> int f() { return __builtin_offsetof(T, [[x]]); }
410 EXPECT_THAT(assertNodeAndPrintDecls("OffsetOfNode"), ::testing::IsEmpty());
414 #define offsetof(t, m) __builtin_offsetof(t, m)
415 struct Foo { int bar; };
416 int x = offsetof(Foo, [[bar]]);
423 struct Foo { int bar[4]; };
425 int x = __builtin_offsetof(Foo, bar[ [[i]] ]);
430TEST_F(TargetDeclTest, NestedNameSpecifier) {
432 namespace a { namespace b { int c; } }
438 namespace a { struct X { enum { y }; }; }
444 template <typename T>
450 namespace a { int x; }
454 EXPECT_DECLS("NestedNameSpecifierLoc", {
"namespace b = a", Rel::Alias},
455 {
"namespace a", Rel::Underlying});
458TEST_F(TargetDeclTest, Types) {
470 EXPECT_DECLS("TypedefTypeLoc", {
"typedef S X", Rel::Alias},
471 {
"struct S", Rel::Underlying});
473 namespace ns { struct S{}; }
477 EXPECT_DECLS("TypedefTypeLoc", {
"typedef ns::S X", Rel::Alias},
478 {
"struct S", Rel::Underlying});
482 void foo() { [[T]] x; }
488 template<template<typename> class T>
489 void foo() { [[T<int>]] x; }
491 EXPECT_DECLS("TemplateSpecializationTypeLoc",
"template <typename> class T");
495 template<template<typename> class ...T>
500 EXPECT_DECLS("TemplateArgumentLoc", {
"template <typename> class ...T"});
508 EXPECT_DECLS("DecltypeTypeLoc", {
"struct S", Rel::Underlying});
518 template <typename... E>
520 static const int size = sizeof...([[E]]);
526 template <typename T>
534TEST_F(TargetDeclTest, ClassTemplate) {
536 // Implicit specialization.
537 template<int x> class Foo{};
541 {
"template<> class Foo<42>", Rel::TemplateInstantiation},
542 {
"class Foo", Rel::TemplatePattern});
545 template<typename T> class Foo {};
546 // The "Foo<int>" SpecializationDecl is incomplete, there is no
547 // instantiation happening.
548 void func([[Foo<int>]] *);
551 {
"class Foo", Rel::TemplatePattern},
552 {
"template<> class Foo<int>", Rel::TemplateInstantiation});
555 // Explicit specialization.
556 template<int x> class Foo{};
557 template<> class Foo<42>{};
560 EXPECT_DECLS("TemplateSpecializationTypeLoc",
"template<> class Foo<42>");
563 // Partial specialization.
564 template<typename T> class Foo{};
565 template<typename T> class Foo<T*>{};
569 {
"template<> class Foo<int *>", Rel::TemplateInstantiation},
570 {
"template <typename T> class Foo<T *>", Rel::TemplatePattern});
573 // Template template argument.
574 template<typename T> struct Vector {};
575 template <template <typename> class Container>
579 EXPECT_DECLS("TemplateArgumentLoc", {
"template <typename T> struct Vector"});
581 Flags.push_back(
"-std=c++17");
584 // Class template argument deduction
585 template <typename T>
594 {
"struct Test", Rel::TemplatePattern});
598 template <typename T>
600 template <typename I>
603 template <typename I>
604 [[Test]](I, I) -> Test<typename I::type>;
606 EXPECT_DECLS("CXXDeductionGuideDecl", {
"template <typename T> struct Test"});
610 Flags.push_back(
"-std=c++20");
616 template <typename T>
617 concept Fooable = requires (T t) { t.foo(); };
619 template <typename T> requires [[Fooable]]<T>
626 {
"template <typename T> concept Fooable = requires (T t) { t.foo(); }"});
630 template <typename T>
631 concept Fooable = true;
633 template <typename T>
634 void foo() requires [[Fooable]]<T>;
637 {
"template <typename T> concept Fooable = true"});
641 template <typename T>
642 concept Fooable = true;
644 template <[[Fooable]] T>
648 {
"template <typename T> concept Fooable = true"});
652 template <typename T, typename U>
653 concept Fooable = true;
655 template <[[Fooable]]<int> T>
659 {
"template <typename T, typename U> concept Fooable = true"});
662TEST_F(TargetDeclTest, Coroutine) {
663 Flags.push_back(
"-std=c++20");
667 template <typename, typename...> struct coroutine_traits;
668 template <typename> struct coroutine_handle {
669 template <typename U>
670 coroutine_handle(coroutine_handle<U>&&) noexcept;
671 static coroutine_handle from_address(void* __addr) noexcept;
677 struct awaitable_frame {
678 awaitable get_return_object();
680 void unhandled_exception();
683 bool await_ready() const noexcept;
684 void await_suspend(std::coroutine_handle<void>) noexcept;
685 void await_resume() const noexcept;
687 result_t initial_suspend() noexcept;
688 result_t final_suspend() noexcept;
689 result_t await_transform(executor) noexcept;
694 struct coroutine_traits<awaitable> {
695 typedef awaitable_frame promise_type;
700 co_await [[executor]]();
706TEST_F(TargetDeclTest, RewrittenBinaryOperator) {
707 Flags.push_back(
"-std=c++20");
711 struct strong_ordering {
713 constexpr operator int() const { return n; }
714 static const strong_ordering equal, greater, less;
716 constexpr strong_ordering strong_ordering::equal = {0};
717 constexpr strong_ordering strong_ordering::greater = {1};
718 constexpr strong_ordering strong_ordering::less = {-1};
724 auto operator<=>(const Foo&) const = default;
727 bool x = (Foo(1) [[!=]] Foo(2));
730 {
"bool operator==(const Foo &) const noexcept = default"});
733TEST_F(TargetDeclTest, FunctionTemplate) {
735 // Implicit specialization.
736 template<typename T> bool foo(T) { return false; };
737 bool x = [[foo]](42);
740 {
"template<> bool foo<int>(int)", Rel::TemplateInstantiation},
741 {
"bool foo(T)", Rel::TemplatePattern});
744 // Explicit specialization.
745 template<typename T> bool foo(T) { return false; };
746 template<> bool foo<int>(int) { return false; };
747 bool x = [[foo]](42);
749 EXPECT_DECLS("DeclRefExpr",
"template<> bool foo<int>(int)");
752TEST_F(TargetDeclTest, VariableTemplate) {
755 // Implicit specialization.
756 template<typename T> int foo;
757 int x = [[foo]]<char>;
759 EXPECT_DECLS("DeclRefExpr", {
"int foo", Rel::TemplateInstantiation},
760 {
"int foo", Rel::TemplatePattern});
763 // Explicit specialization.
764 template<typename T> int foo;
765 template <> bool foo<char>;
766 int x = [[foo]]<char>;
771 // Partial specialization.
772 template<typename T> int foo;
773 template<typename T> bool foo<T*>;
774 bool x = [[foo]]<char*>;
776 EXPECT_DECLS("DeclRefExpr", {
"bool foo", Rel::TemplateInstantiation},
777 {
"bool foo", Rel::TemplatePattern});
780TEST_F(TargetDeclTest, TypeAliasTemplate) {
782 template<typename T, int X> class SmallVector {};
783 template<typename U> using TinyVector = SmallVector<U, 1>;
784 [[TinyVector<int>]] X;
787 {
"template<> class SmallVector<int, 1>",
788 Rel::TemplateInstantiation | Rel::Underlying},
789 {
"class SmallVector", Rel::TemplatePattern | Rel::Underlying},
790 {
"using TinyVector = SmallVector<U, 1>",
791 Rel::Alias | Rel::TemplatePattern});
794TEST_F(TargetDeclTest, BuiltinTemplates) {
796 template <class T, T... Index> struct integer_sequence {};
797 [[__make_integer_seq]]<integer_sequence, int, 3> X;
800 "TemplateSpecializationTypeLoc",
801 {
"struct integer_sequence", Rel::TemplatePattern | Rel::Underlying},
802 {
"template<> struct integer_sequence<int, <0, 1, 2>>",
803 Rel::TemplateInstantiation | Rel::Underlying});
807 template <class T, T... Index> struct integer_sequence;
809 template <class T, int N>
810 using make_integer_sequence = [[__make_integer_seq]]<integer_sequence, T, N>;
815 template <int N, class... Pack>
816 using type_pack_element = [[__type_pack_element]]<N, Pack...>;
821 template <template <class...> class Templ, class... Types>
822 using dedup_types = Templ<[[__builtin_dedup_pack]]<Types...>...>;
827TEST_F(TargetDeclTest, MemberOfTemplate) {
829 template <typename T> struct Foo {
832 int y = Foo<int>().[[x]](42);
834 EXPECT_DECLS("MemberExpr", {
"int x(int)", Rel::TemplateInstantiation},
835 {
"int x(T)", Rel::TemplatePattern});
838 template <typename T> struct Foo {
839 template <typename U>
842 int y = Foo<char>().[[x]]('c', 42);
845 {
"template<> int x<int>(char, int)", Rel::TemplateInstantiation},
846 {
"int x(T, U)", Rel::TemplatePattern});
849TEST_F(TargetDeclTest, Lambda) {
851 void foo(int x = 42) {
852 auto l = [ [[x]] ]{ return x + 1; };
860 void foo(int x = 42) {
861 auto l = [x]{ return [[x]] + 1; };
868 auto l = [x = 1]{ return [[x]] + 1; };
875TEST_F(TargetDeclTest, OverloadExpr) {
876 Flags.push_back(
"--target=x86_64-pc-linux-gnu");
887 EXPECT_DECLS("UnresolvedLookupExpr",
"void func(int *)",
"void func(char *)");
900 EXPECT_DECLS("UnresolvedMemberExpr",
"void func(int *)",
"void func(char *)");
904 static void *operator new(unsigned long);
906 auto* k = [[new]] X();
908 EXPECT_DECLS("CXXNewExpr",
"static void *operator new(unsigned long)");
910 void *operator new(unsigned long);
911 auto* k = [[new]] int();
913 EXPECT_DECLS("CXXNewExpr",
"void *operator new(unsigned long)");
917 static void operator delete(void *) noexcept;
923 EXPECT_DECLS("CXXDeleteExpr",
"static void operator delete(void *) noexcept");
925 void operator delete(void *) noexcept;
932 "void operator delete(void *, __size_t) noexcept");
935TEST_F(TargetDeclTest, DependentExprs) {
936 Flags.push_back(
"--std=c++20");
940 struct A { void foo() {} };
941 template <typename T>
959 template <typename T>
963 this->c.getA().[[foo]]();
978 template <typename T>
982 this->c.fptr().[[foo]]();
993 template <typename T> struct Foo {
1000 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"int aaaa");
1006 template <typename T> T convert() const;
1008 template <typename T>
1010 Foo::k(T()).template [[convert]]<T>();
1014 "template <typename T> T convert() const");
1017 template <typename T>
1021 template <typename T>
1022 using Wally = Waldo<T>;
1023 template <typename T>
1024 void foo(Wally<T> w) {
1028 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"void find()");
1031 template <typename T>
1035 template <typename T>
1037 using Type = Waldo<T>;
1039 template <typename T>
1040 void foo(typename MetaWaldo<T>::Type w) {
1044 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"void find()");
1050 template <typename T>
1051 using Wally = Waldo;
1053 struct S : Wally<int> {
1054 void Foo() { this->[[find]](); }
1064 const int found = N;
1072 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"const int found = N");
1075TEST_F(TargetDeclTest, DependentTypes) {
1079 struct A { struct B {}; };
1081 template <typename T>
1082 void foo(typename A<T>::[[B]]);
1089 struct A { struct B { struct C {}; }; };
1091 template <typename T>
1092 void foo(typename A<T>::[[B]]::C);
1100 struct A { struct B { struct C {}; }; };
1102 template <typename T>
1103 void foo(typename A<T>::B::[[C]]);
1111 template <typename> struct B {};
1114 template <typename T>
1115 void foo(typename A<T>::template [[B]]<int>);
1117 EXPECT_DECLS("TemplateSpecializationTypeLoc",
"template <typename> struct B");
1124 typedef typename waldo<N - 1>::type::[[next]] type;
1136 using type = typename odd<N - 1>::type::next;
1141 using type = typename even<N - 1>::type::[[next]];
1147TEST_F(TargetDeclTest, TypedefCascade) {
1153 using type = C::type;
1156 using type = B::type;
1161 {
"using type = int", Rel::Alias | Rel::Underlying},
1162 {
"using type = C::type", Rel::Alias | Rel::Underlying},
1163 {
"using type = B::type", Rel::Alias});
1166TEST_F(TargetDeclTest, RecursiveTemplate) {
1167 Flags.push_back(
"-std=c++20");
1170 template <typename T>
1171 concept Leaf = false;
1173 template <typename Tree>
1174 struct descend_left {
1175 using type = typename descend_left<typename Tree::left>::[[type]];
1178 template <Leaf Tree>
1179 struct descend_left<Tree> {
1180 using type = typename Tree::value;
1184 {
"using type = typename descend_left<typename Tree::left>::type",
1185 Rel::Alias | Rel::Underlying});
1188TEST_F(TargetDeclTest, ObjC) {
1189 Flags = {
"-xobjective-c"};
1201 @interface Foo { @public int bar; }
1212 -(void) setX:(int)x;
1218 EXPECT_DECLS("ObjCPropertyRefExpr",
"- (void)setX:(int)x");
1222 @property(retain) I* x;
1223 @property(retain) I* y;
1230 "@property(atomic, retain, readwrite) I *x");
1235 @interface Interface
1236 @property(retain) [[MYObject]] *x;
1239 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface MYObject");
1242 @interface MYObject2
1244 @interface Interface
1245 @property(retain, nonnull) [[MYObject2]] *x;
1248 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface MYObject2");
1254 return [[@protocol(Foo)]];
1262 void test([[Foo]] *p);
1264 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface Foo");
1266 Code = R
"cpp(// Don't consider implicit interface as the target.
1267 @implementation [[Implicit]]
1270 EXPECT_DECLS("ObjCImplementationDecl",
"@implementation Implicit");
1275 @implementation [[Foo]]
1278 EXPECT_DECLS("ObjCImplementationDecl",
"@interface Foo");
1283 @interface Foo (Ext)
1285 @implementation [[Foo]] (Ext)
1288 EXPECT_DECLS("ObjCCategoryImplDecl",
"@interface Foo(Ext)");
1293 @interface Foo (Ext)
1295 @implementation Foo ([[Ext]])
1298 EXPECT_DECLS("ObjCCategoryImplDecl",
"@interface Foo(Ext)");
1301 void test(id</*error-ok*/[[InvalidProtocol]]> p);
1309 void test([[C]]<Foo> *p);
1317 void test(C<[[Foo]]> *p);
1327 void test(C<[[Foo]], Bar> *p);
1337 void test(C<Foo, [[Bar]]> *p);
1343 + (id)sharedInstance;
1346 + (id)sharedInstance { return 0; }
1349 id value = [[Foo]].sharedInstance;
1352 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface Foo");
1356 + (id)sharedInstance;
1359 + (id)sharedInstance { return 0; }
1362 id value = Foo.[[sharedInstance]];
1365 EXPECT_DECLS("ObjCPropertyRefExpr",
"+ (id)sharedInstance");
1369 + ([[id]])sharedInstance;
1376 + ([[instancetype]])sharedInstance;
1382class FindExplicitReferencesTest :
public ::testing::Test {
1385 std::string AnnotatedCode;
1386 std::string DumpedReferences;
1389 TestTU newTU(llvm::StringRef Code) {
1391 TU.Code = std::string(Code);
1395 TU.ExtraArgs.push_back(
"-std=c++20");
1396 TU.ExtraArgs.push_back(
"-xobjective-c++");
1401 AllRefs annotatedReferences(llvm::StringRef Code, ParsedAST &
AST,
1402 std::vector<ReferenceLoc> Refs) {
1403 auto &SM =
AST.getSourceManager();
1404 llvm::stable_sort(Refs, [&](
const ReferenceLoc &L,
const ReferenceLoc &R) {
1405 return SM.isBeforeInTranslationUnit(L.NameLoc, R.NameLoc);
1408 std::string AnnotatedCode;
1409 unsigned NextCodeChar = 0;
1410 for (
unsigned I = 0; I < Refs.size(); ++I) {
1413 SourceLocation Pos = R.NameLoc;
1414 assert(Pos.isValid());
1415 if (Pos.isMacroID())
1416 Pos = SM.getExpansionLoc(Pos);
1417 assert(Pos.isFileID());
1421 std::tie(
File, Offset) = SM.getDecomposedLoc(Pos);
1422 if (
File == SM.getMainFileID()) {
1424 assert(NextCodeChar <= Offset);
1425 AnnotatedCode += Code.substr(NextCodeChar, Offset - NextCodeChar);
1426 AnnotatedCode +=
"$" + std::to_string(I) +
"^";
1428 NextCodeChar = Offset;
1431 AnnotatedCode += Code.substr(NextCodeChar);
1433 std::string DumpedReferences;
1434 for (
unsigned I = 0; I < Refs.size(); ++I)
1435 DumpedReferences += std::string(llvm::formatv(
"{0}: {1}\n", I, Refs[I]));
1437 return AllRefs{std::move(AnnotatedCode), std::move(DumpedReferences)};
1443 AllRefs annotateAllReferences(llvm::StringRef Code) {
1444 TestTU TU = newTU(Code);
1445 auto AST = TU.build();
1447 std::vector<ReferenceLoc> Refs;
1448 for (
auto *TopLevel :
AST.getLocalTopLevelDecls())
1450 TopLevel, [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1451 AST.getHeuristicResolver());
1452 return annotatedReferences(Code,
AST, std::move(Refs));
1458 AllRefs annotateReferencesInFoo(llvm::StringRef Code) {
1459 TestTU TU = newTU(Code);
1460 auto AST = TU.build();
1462 if (
auto *T = llvm::dyn_cast<FunctionTemplateDecl>(TestDecl))
1463 TestDecl =
T->getTemplatedDecl();
1465 std::vector<ReferenceLoc> Refs;
1466 if (
const auto *Func = llvm::dyn_cast<FunctionDecl>(TestDecl))
1469 [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1470 AST.getHeuristicResolver());
1471 else if (
const auto *NS = llvm::dyn_cast<NamespaceDecl>(TestDecl))
1474 [&Refs, &NS](ReferenceLoc R) {
1476 if (R.Targets.size() == 1 && R.Targets.front() == NS)
1478 Refs.push_back(std::move(R));
1480 AST.getHeuristicResolver());
1481 else if (
const auto *OC = llvm::dyn_cast<ObjCContainerDecl>(TestDecl))
1483 OC, [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1484 AST.getHeuristicResolver());
1486 ADD_FAILURE() <<
"Failed to find ::foo decl for test";
1488 return annotatedReferences(Code,
AST, std::move(Refs));
1492TEST_F(FindExplicitReferencesTest, AllRefsInFoo) {
1493 std::pair< llvm::StringRef, llvm::StringRef> Cases[] =
1498 void foo(int param) {
1499 $0^global = $1^param + $2^func();
1502 "0: targets = {global}\n"
1503 "1: targets = {param}\n"
1504 "2: targets = {func}\n"},
1506 struct X { int a; };
1511 "0: targets = {x}\n"
1512 "1: targets = {X::a}\n"},
1514 // error-ok: testing with broken code
1517 return $0^bar() + $1^bar(42);
1520 "0: targets = {bar}\n"
1521 "1: targets = {bar}\n"},
1525 namespace alias = ns;
1527 using namespace $0^ns;
1528 using namespace $1^alias;
1531 "0: targets = {ns}\n"
1532 "1: targets = {alias}\n"},
1535 namespace ns { int global; }
1537 using $0^ns::$1^global;
1540 "0: targets = {ns}\n"
1541 "1: targets = {ns::global}, qualifier = 'ns::'\n"},
1544 namespace ns { enum class A {}; }
1546 using enum $0^ns::$1^A;
1549 "0: targets = {ns}\n"
1550 "1: targets = {ns::A}, qualifier = 'ns::'\n"},
1553 struct Struct { int a; };
1554 using Typedef = int;
1558 static_cast<$4^Struct*>(0);
1561 "0: targets = {Struct}\n"
1562 "1: targets = {x}, decl\n"
1563 "2: targets = {Typedef}\n"
1564 "3: targets = {y}, decl\n"
1565 "4: targets = {Struct}\n"},
1568 namespace a { namespace b { struct S { typedef int type; }; } }
1570 $0^a::$1^b::$2^S $3^x;
1571 using namespace $4^a::$5^b;
1575 "0: targets = {a}\n"
1576 "1: targets = {a::b}, qualifier = 'a::'\n"
1577 "2: targets = {a::b::S}, qualifier = 'a::b::'\n"
1578 "3: targets = {x}, decl\n"
1579 "4: targets = {a}\n"
1580 "5: targets = {a::b}, qualifier = 'a::'\n"
1581 "6: targets = {a::b::S}\n"
1582 "7: targets = {a::b::S::type}, qualifier = 'S::'\n"
1583 "8: targets = {y}, decl\n"},
1586 $0^ten: // PRINT "HELLO WORLD!"
1590 "0: targets = {ten}, decl\n"
1591 "1: targets = {ten}\n"},
1594 template <class T> struct vector { using value_type = T; };
1595 template <> struct vector<bool> { using value_type = bool; };
1597 $0^vector<int> $1^vi;
1598 $2^vector<bool> $3^vb;
1601 "0: targets = {vector<int>}\n"
1602 "1: targets = {vi}, decl\n"
1603 "2: targets = {vector<bool>}\n"
1604 "3: targets = {vb}, decl\n"},
1607 template <class T> struct vector { using value_type = T; };
1608 template <> struct vector<bool> { using value_type = bool; };
1609 template <class T> using valias = vector<T>;
1611 $0^valias<int> $1^vi;
1612 $2^valias<bool> $3^vb;
1615 "0: targets = {valias}\n"
1616 "1: targets = {vi}, decl\n"
1617 "2: targets = {valias}\n"
1618 "3: targets = {vb}, decl\n"},
1622 template <typename $0^T>
1629 "0: targets = {foo::Bar::T}, decl\n"
1630 "1: targets = {foo::Bar}, decl\n"
1631 "2: targets = {foo::Bar}\n"
1632 "3: targets = {foo::Bar::f}, decl\n"
1633 "4: targets = {foo::Bar}\n"},
1636 struct X { void func(int); };
1644 "0: targets = {y}\n"
1645 "1: targets = {Y::func}\n"},
1648 namespace ns { void bar(int); }
1655 "0: targets = {bar}\n"},
1661 void foo(int a, int b) {
1665 "0: targets = {a}\n"
1666 "1: targets = {b}\n"},
1675 for (int $0^x : $1^vector()) {
1680 "0: targets = {x}, decl\n"
1681 "1: targets = {vector}\n"
1682 "2: targets = {x}\n"},
1685 namespace ns1 { void func(char*); }
1686 namespace ns2 { void func(int*); }
1687 using namespace ns1;
1688 using namespace ns2;
1695 "0: targets = {ns1::func, ns2::func}\n"
1696 "1: targets = {t}\n"},
1705 void foo(X x, T t) {
1709 "0: targets = {x}\n"
1710 "1: targets = {X::func, X::func}\n"
1711 "2: targets = {t}\n"},
1721 $0^S<$1^T>::$2^value;
1724 "0: targets = {S}\n"
1725 "1: targets = {T}\n"
1726 "2: targets = {S::value}, qualifier = 'S<T>::'\n"},
1739 "0: targets = {t}\n"
1740 "1: targets = {S::value}\n"},
1745 static_cast<$0^T>(0);
1750 "0: targets = {T}\n"
1751 "1: targets = {T}\n"
1752 "2: targets = {T}\n"
1753 "3: targets = {t}, decl\n"},
1761 "0: targets = {x}, decl\n"
1762 "1: targets = {I}\n"},
1765 template <class T> struct vector {};
1767 template <template<class> class TT, template<class> class ...TP>
1771 $4^foo<$5^vector>();
1775 "0: targets = {TT}\n"
1776 "1: targets = {x}, decl\n"
1777 "2: targets = {foo}\n"
1778 "3: targets = {TT}\n"
1779 "4: targets = {foo}\n"
1780 "5: targets = {vector}\n"
1781 "6: targets = {foo}\n"
1782 "7: targets = {TP}\n"},
1786 template <int(*)()> struct wrapper {};
1788 template <int(*FuncParam)()>
1790 $0^wrapper<$1^func> $2^w;
1794 "0: targets = {wrapper<&func>}\n"
1795 "1: targets = {func}\n"
1796 "2: targets = {w}, decl\n"
1797 "3: targets = {FuncParam}\n"},
1803 class $0^Foo { $1^Foo(); ~$2^Foo(); int $3^field; };
1805 enum $5^E { $6^ABC };
1807 using $8^INT2 = int;
1808 namespace $9^NS = $10^ns;
1811 "0: targets = {Foo}, decl\n"
1812 "1: targets = {foo()::Foo::Foo}, decl\n"
1813 "2: targets = {Foo}\n"
1814 "3: targets = {foo()::Foo::field}, decl\n"
1815 "4: targets = {Var}, decl\n"
1816 "5: targets = {E}, decl\n"
1817 "6: targets = {foo()::ABC}, decl\n"
1818 "7: targets = {INT}, decl\n"
1819 "8: targets = {INT2}, decl\n"
1820 "9: targets = {NS}, decl\n"
1821 "10: targets = {ns}\n"},
1828 // FIXME: This should have only one reference to Bar.
1829 $2^operator $3^$4^Bar();
1833 $7^f.$8^operator $9^Bar();
1836 "0: targets = {Bar}, decl\n"
1837 "1: targets = {Foo}, decl\n"
1838 "2: targets = {foo()::Foo::operator Bar}, decl\n"
1839 "3: targets = {Bar}\n"
1840 "4: targets = {Bar}\n"
1841 "5: targets = {Foo}\n"
1842 "6: targets = {f}, decl\n"
1843 "7: targets = {f}\n"
1844 "8: targets = {foo()::Foo::operator Bar}\n"
1845 "9: targets = {Bar}\n"},
1853 void $2^destructMe() {
1859 $6^f.~ /*...*/ $7^Foo();
1862 "0: targets = {Foo}, decl\n"
1865 "1: targets = {Foo}\n"
1866 "2: targets = {foo()::Foo::destructMe}, decl\n"
1867 "3: targets = {Foo}\n"
1868 "4: targets = {Foo}\n"
1869 "5: targets = {f}, decl\n"
1870 "6: targets = {f}\n"
1871 "7: targets = {Foo}\n"},
1876 // member initializer
1882 class $4^Derived : public $5^Base {
1884 $8^Derived() : $9^Base() {}
1886 // delegating initializer
1889 $12^Foo(): $13^Foo(111) {}
1893 "0: targets = {X}, decl\n"
1894 "1: targets = {foo()::X::abc}, decl\n"
1895 "2: targets = {foo()::X::X}, decl\n"
1896 "3: targets = {foo()::X::abc}\n"
1897 "4: targets = {Derived}, decl\n"
1898 "5: targets = {Base}\n"
1899 "6: targets = {Base}\n"
1900 "7: targets = {foo()::Derived::B}, decl\n"
1901 "8: targets = {foo()::Derived::Derived}, decl\n"
1902 "9: targets = {Base}\n"
1903 "10: targets = {Foo}, decl\n"
1904 "11: targets = {foo()::Foo::Foo}, decl\n"
1905 "12: targets = {foo()::Foo::Foo}, decl\n"
1906 "13: targets = {Foo}\n"},
1912 int (*$2^fptr)(int $3^a, int) = nullptr;
1915 "0: targets = {(unnamed class)}\n"
1916 "1: targets = {x}, decl\n"
1917 "2: targets = {fptr}, decl\n"
1918 "3: targets = {a}, decl\n"},
1922 namespace ns { struct Type {}; }
1923 namespace alias = ns;
1924 namespace rec_alias = alias;
1927 $0^ns::$1^Type $2^a;
1928 $3^alias::$4^Type $5^b;
1929 $6^rec_alias::$7^Type $8^c;
1932 "0: targets = {ns}\n"
1933 "1: targets = {ns::Type}, qualifier = 'ns::'\n"
1934 "2: targets = {a}, decl\n"
1935 "3: targets = {alias}\n"
1936 "4: targets = {ns::Type}, qualifier = 'alias::'\n"
1937 "5: targets = {b}, decl\n"
1938 "6: targets = {rec_alias}\n"
1939 "7: targets = {ns::Type}, qualifier = 'rec_alias::'\n"
1940 "8: targets = {c}, decl\n"},
1944 template <typename... E>
1946 constexpr int $0^size = sizeof...($1^E);
1949 "0: targets = {size}, decl\n"
1950 "1: targets = {E}\n"},
1954 template <typename T>
1962 "0: targets = {Test}\n"
1963 "1: targets = {a}, decl\n"},
1967 template <typename $0^T>
1971 "0: targets = {foo::Bar::T}, decl\n"
1972 "1: targets = {foo::Bar}, decl\n"},
1976 template <typename $0^T>
1980 "0: targets = {T}, decl\n"
1981 "1: targets = {foo::func}, decl\n"},
1985 template <typename $0^T>
1989 "0: targets = {foo::T}, decl\n"
1990 "1: targets = {foo::T}\n"
1991 "2: targets = {foo::x}, decl\n"},
1994 template<typename T> class vector {};
1996 template <typename $0^T>
1997 using $1^V = $2^vector<$3^T>;
2000 "0: targets = {foo::T}, decl\n"
2001 "1: targets = {foo::V}, decl\n"
2002 "2: targets = {vector}\n"
2003 "3: targets = {foo::T}\n"},
2007 template <typename T>
2008 concept Drawable = requires (T t) { t.draw(); };
2011 template <typename $0^T> requires $1^Drawable<$2^T>
2012 void $3^bar($4^T $5^t) {
2017 "0: targets = {T}, decl\n"
2018 "1: targets = {Drawable}\n"
2019 "2: targets = {T}\n"
2020 "3: targets = {foo::bar}, decl\n"
2021 "4: targets = {T}\n"
2022 "5: targets = {t}, decl\n"
2023 "6: targets = {t}\n"
2024 "7: targets = {}\n"},
2038 "0: targets = {f}\n"
2039 "1: targets = {I::_z}\n"},
2044 @property(retain) I* x;
2045 @property(retain) I* y;
2052 "0: targets = {f}\n"
2053 "1: targets = {I::x}\n"
2054 "2: targets = {I::y}\n"},
2067 "0: targets = {f}\n"
2068 "1: targets = {I::x}\n"
2069 "2: targets = {I::setY:}\n"},
2074 @property(class) I *x;
2079 $2^local = $3^I.$4^x;
2082 "0: targets = {I}\n"
2083 "1: targets = {I::setX:}\n"
2084 "2: targets = {local}\n"
2085 "3: targets = {I}\n"
2086 "4: targets = {I::x}\n"},
2097 $2^local = $3^I.$4^x;
2100 "0: targets = {I}\n"
2101 "1: targets = {I::setX:}\n"
2102 "2: targets = {local}\n"
2103 "3: targets = {I}\n"
2104 "4: targets = {I::x}\n"},
2108 -(void) a:(int)x b:(int)y;
2114 "0: targets = {i}\n"
2115 "1: targets = {I::a:b:}\n"},
2126 "0: targets = {I}\n"
2127 "1: targets = {P}\n"
2128 "2: targets = {x}, decl\n"},
2136 $2^Foo $3^f { .$4^Bar = 42 };
2139 "0: targets = {Foo}, decl\n"
2140 "1: targets = {foo()::Foo::Bar}, decl\n"
2141 "2: targets = {Foo}\n"
2142 "3: targets = {f}, decl\n"
2143 "4: targets = {foo()::Foo::Bar}\n"},
2152 $5^Bar $6^bar { .$7^Foo.$8^Field = 42 };
2155 "0: targets = {Baz}, decl\n"
2156 "1: targets = {foo()::Baz::Field}, decl\n"
2157 "2: targets = {Bar}, decl\n"
2158 "3: targets = {Baz}\n"
2159 "4: targets = {foo()::Bar::Foo}, decl\n"
2160 "5: targets = {Bar}\n"
2161 "6: targets = {bar}, decl\n"
2162 "7: targets = {foo()::Bar::Foo}\n"
2163 "8: targets = {foo()::Baz::Field}\n"},
2167 struct $0^Foo { int $1^bar; };
2168 int $2^x = __builtin_offsetof($3^Foo, $4^bar);
2171 "0: targets = {Foo}, decl\n"
2172 "1: targets = {foo()::Foo::bar}, decl\n"
2173 "2: targets = {x}, decl\n"
2174 "3: targets = {Foo}\n"
2175 "4: targets = {foo()::Foo::bar}\n"},
2181 $1^struct { int $2^c; } $3^B;
2183 int $4^x = __builtin_offsetof($5^A, $6^B.$7^c);
2186 "0: targets = {A}, decl\n"
2187 "1: targets = {foo()::A::(unnamed struct)}\n"
2188 "2: targets = {foo()::A::(unnamed struct)::c}, decl\n"
2189 "3: targets = {foo()::A::B}, decl\n"
2190 "4: targets = {x}, decl\n"
2191 "5: targets = {A}\n"
2192 "6: targets = {foo()::A::B}\n"
2193 "7: targets = {foo()::A::(unnamed struct)::c}\n"},
2199 struct $0^A { int $1^arr[4]; };
2201 int $3^x = __builtin_offsetof($4^A, $5^arr[$6^i]);
2204 "0: targets = {A}, decl\n"
2205 "1: targets = {foo()::A::arr}, decl\n"
2206 "2: targets = {i}, decl\n"
2207 "3: targets = {x}, decl\n"
2208 "4: targets = {A}\n"
2209 "5: targets = {foo()::A::arr}\n"
2210 "6: targets = {i}\n"},
2212 template<typename T>
2214 template<typename T>
2216 $0^crash({.$1^x = $2^T()});
2219 "0: targets = {crash}\n"
2221 "2: targets = {T}\n"},
2224 template <template <typename> typename T>
2227 template <typename $0^T>
2228 struct $1^Derive : $2^Base<$3^T::template $4^Unknown> {};
2231 "0: targets = {foo::Derive::T}, decl\n"
2232 "1: targets = {foo::Derive}, decl\n"
2233 "2: targets = {Base}\n"
2234 "3: targets = {foo::Derive::T}\n"
2235 "4: targets = {}, qualifier = 'T::'\n"},
2239 template <typename $0^T>
2241 template <typename $2^I>
2244 template <typename $5^I>
2245 $6^Test($7^I) -> $8^Test<typename $9^I::$10^type>;
2248 "0: targets = {T}, decl\n"
2249 "1: targets = {foo::Test}, decl\n"
2250 "2: targets = {I}, decl\n"
2251 "3: targets = {foo::Test::Test<T>}, decl\n"
2252 "4: targets = {I}\n"
2253 "5: targets = {I}, decl\n"
2254 "6: targets = {foo::Test}\n"
2255 "7: targets = {I}\n"
2256 "8: targets = {foo::Test}\n"
2257 "9: targets = {I}\n"
2258 "10: targets = {}, qualifier = 'I::'\n"}};
2260 for (
const auto &C : Cases) {
2261 llvm::StringRef ExpectedCode =
C.first;
2262 llvm::StringRef ExpectedRefs =
C.second;
2265 annotateReferencesInFoo(llvm::Annotations(ExpectedCode).code());
2266 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
2267 EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
2271TEST_F(FindExplicitReferencesTest, AllRefs) {
2272 std::pair< llvm::StringRef, llvm::StringRef> Cases[] =
2274 @interface $0^MyClass
2276 @implementation $1^$2^MyClass
2279 "0: targets = {MyClass}, decl\n"
2280 "1: targets = {MyClass}\n"
2281 "2: targets = {MyClass}, decl\n"},
2283 @interface $0^MyClass
2285 @interface $1^MyClass ($2^Category)
2287 @implementation $3^MyClass ($4^$5^Category)
2290 "0: targets = {MyClass}, decl\n"
2291 "1: targets = {MyClass}\n"
2292 "2: targets = {Category}, decl\n"
2293 "3: targets = {MyClass}\n"
2294 "4: targets = {Category}\n"
2295 "5: targets = {Category}, decl\n"}};
2297 for (
const auto &C : Cases) {
2298 llvm::StringRef ExpectedCode =
C.first;
2299 llvm::StringRef ExpectedRefs =
C.second;
2301 auto Actual = annotateAllReferences(llvm::Annotations(ExpectedCode).code());
2302 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
2303 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)