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, PackIndexedConcept) {
663 Flags.push_back(
"-std=c++2d");
667 template <template <class> concept... CC>
669 template <[[CC]]...[0] T>
673 EXPECT_DECLS("ConceptReference", {
"template <class> concept ...CC"});
677 template <template <class> concept... CC>
678 void bar([[CC]]...[0] auto t);
680 EXPECT_DECLS("ConceptReference", {
"template <class> concept ...CC"});
683TEST_F(TargetDeclTest, Coroutine) {
684 Flags.push_back(
"-std=c++20");
688 template <typename, typename...> struct coroutine_traits;
689 template <typename> struct coroutine_handle {
690 template <typename U>
691 coroutine_handle(coroutine_handle<U>&&) noexcept;
692 static coroutine_handle from_address(void* __addr) noexcept;
698 struct awaitable_frame {
699 awaitable get_return_object();
701 void unhandled_exception();
704 bool await_ready() const noexcept;
705 void await_suspend(std::coroutine_handle<void>) noexcept;
706 void await_resume() const noexcept;
708 result_t initial_suspend() noexcept;
709 result_t final_suspend() noexcept;
710 result_t await_transform(executor) noexcept;
715 struct coroutine_traits<awaitable> {
716 typedef awaitable_frame promise_type;
721 co_await [[executor]]();
727TEST_F(TargetDeclTest, RewrittenBinaryOperator) {
728 Flags.push_back(
"-std=c++20");
732 struct strong_ordering {
734 constexpr operator int() const { return n; }
735 static const strong_ordering equal, greater, less;
737 constexpr strong_ordering strong_ordering::equal = {0};
738 constexpr strong_ordering strong_ordering::greater = {1};
739 constexpr strong_ordering strong_ordering::less = {-1};
745 auto operator<=>(const Foo&) const = default;
748 bool x = (Foo(1) [[!=]] Foo(2));
751 {
"bool operator==(const Foo &) const noexcept = default"});
754TEST_F(TargetDeclTest, FunctionTemplate) {
756 // Implicit specialization.
757 template<typename T> bool foo(T) { return false; };
758 bool x = [[foo]](42);
761 {
"template<> bool foo<int>(int)", Rel::TemplateInstantiation},
762 {
"bool foo(T)", Rel::TemplatePattern});
765 // Explicit specialization.
766 template<typename T> bool foo(T) { return false; };
767 template<> bool foo<int>(int) { return false; };
768 bool x = [[foo]](42);
770 EXPECT_DECLS("DeclRefExpr",
"template<> bool foo<int>(int)");
773TEST_F(TargetDeclTest, VariableTemplate) {
776 // Implicit specialization.
777 template<typename T> int foo;
778 int x = [[foo]]<char>;
780 EXPECT_DECLS("DeclRefExpr", {
"int foo", Rel::TemplateInstantiation},
781 {
"int foo", Rel::TemplatePattern});
784 // Explicit specialization.
785 template<typename T> int foo;
786 template <> bool foo<char>;
787 int x = [[foo]]<char>;
792 // Partial specialization.
793 template<typename T> int foo;
794 template<typename T> bool foo<T*>;
795 bool x = [[foo]]<char*>;
797 EXPECT_DECLS("DeclRefExpr", {
"bool foo", Rel::TemplateInstantiation},
798 {
"bool foo", Rel::TemplatePattern});
801TEST_F(TargetDeclTest, TypeAliasTemplate) {
803 template<typename T, int X> class SmallVector {};
804 template<typename U> using TinyVector = SmallVector<U, 1>;
805 [[TinyVector<int>]] X;
808 {
"template<> class SmallVector<int, 1>",
809 Rel::TemplateInstantiation | Rel::Underlying},
810 {
"class SmallVector", Rel::TemplatePattern | Rel::Underlying},
811 {
"using TinyVector = SmallVector<U, 1>",
812 Rel::Alias | Rel::TemplatePattern});
815TEST_F(TargetDeclTest, BuiltinTemplates) {
817 template <class T, T... Index> struct integer_sequence {};
818 [[__make_integer_seq]]<integer_sequence, int, 3> X;
821 "TemplateSpecializationTypeLoc",
822 {
"struct integer_sequence", Rel::TemplatePattern | Rel::Underlying},
823 {
"template<> struct integer_sequence<int, <0, 1, 2>>",
824 Rel::TemplateInstantiation | Rel::Underlying});
828 template <class T, T... Index> struct integer_sequence;
830 template <class T, int N>
831 using make_integer_sequence = [[__make_integer_seq]]<integer_sequence, T, N>;
836 template <int N, class... Pack>
837 using type_pack_element = [[__type_pack_element]]<N, Pack...>;
842 template <template <class...> class Templ, class... Types>
843 using dedup_types = Templ<[[__builtin_dedup_pack]]<Types...>...>;
848TEST_F(TargetDeclTest, MemberOfTemplate) {
850 template <typename T> struct Foo {
853 int y = Foo<int>().[[x]](42);
855 EXPECT_DECLS("MemberExpr", {
"int x(int)", Rel::TemplateInstantiation},
856 {
"int x(T)", Rel::TemplatePattern});
859 template <typename T> struct Foo {
860 template <typename U>
863 int y = Foo<char>().[[x]]('c', 42);
866 {
"template<> int x<int>(char, int)", Rel::TemplateInstantiation},
867 {
"int x(T, U)", Rel::TemplatePattern});
870TEST_F(TargetDeclTest, Lambda) {
872 void foo(int x = 42) {
873 auto l = [ [[x]] ]{ return x + 1; };
881 void foo(int x = 42) {
882 auto l = [x]{ return [[x]] + 1; };
889 auto l = [x = 1]{ return [[x]] + 1; };
896TEST_F(TargetDeclTest, OverloadExpr) {
897 Flags.push_back(
"--target=x86_64-pc-linux-gnu");
908 EXPECT_DECLS("UnresolvedLookupExpr",
"void func(int *)",
"void func(char *)");
921 EXPECT_DECLS("UnresolvedMemberExpr",
"void func(int *)",
"void func(char *)");
925 static void *operator new(unsigned long);
927 auto* k = [[new]] X();
929 EXPECT_DECLS("CXXNewExpr",
"static void *operator new(unsigned long)");
931 void *operator new(unsigned long);
932 auto* k = [[new]] int();
934 EXPECT_DECLS("CXXNewExpr",
"void *operator new(unsigned long)");
938 static void operator delete(void *) noexcept;
944 EXPECT_DECLS("CXXDeleteExpr",
"static void operator delete(void *) noexcept");
946 void operator delete(void *) noexcept;
953 "void operator delete(void *, __size_t) noexcept");
956TEST_F(TargetDeclTest, DependentExprs) {
957 Flags.push_back(
"--std=c++20");
961 struct A { void foo() {} };
962 template <typename T>
980 template <typename T>
984 this->c.getA().[[foo]]();
999 template <typename T>
1003 this->c.fptr().[[foo]]();
1014 template <typename T> struct Foo {
1021 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"int aaaa");
1027 template <typename T> T convert() const;
1029 template <typename T>
1031 Foo::k(T()).template [[convert]]<T>();
1035 "template <typename T> T convert() const");
1038 template <typename T>
1042 template <typename T>
1043 using Wally = Waldo<T>;
1044 template <typename T>
1045 void foo(Wally<T> w) {
1049 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"void find()");
1052 template <typename T>
1056 template <typename T>
1058 using Type = Waldo<T>;
1060 template <typename T>
1061 void foo(typename MetaWaldo<T>::Type w) {
1065 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"void find()");
1071 template <typename T>
1072 using Wally = Waldo;
1074 struct S : Wally<int> {
1075 void Foo() { this->[[find]](); }
1085 const int found = N;
1093 EXPECT_DECLS("CXXDependentScopeMemberExpr",
"const int found = N");
1096TEST_F(TargetDeclTest, DependentTypes) {
1100 struct A { struct B {}; };
1102 template <typename T>
1103 void foo(typename A<T>::[[B]]);
1110 struct A { struct B { struct C {}; }; };
1112 template <typename T>
1113 void foo(typename A<T>::[[B]]::C);
1121 struct A { struct B { struct C {}; }; };
1123 template <typename T>
1124 void foo(typename A<T>::B::[[C]]);
1132 template <typename> struct B {};
1135 template <typename T>
1136 void foo(typename A<T>::template [[B]]<int>);
1138 EXPECT_DECLS("TemplateSpecializationTypeLoc",
"template <typename> struct B");
1145 typedef typename waldo<N - 1>::type::[[next]] type;
1157 using type = typename odd<N - 1>::type::next;
1162 using type = typename even<N - 1>::type::[[next]];
1168TEST_F(TargetDeclTest, TypedefCascade) {
1174 using type = C::type;
1177 using type = B::type;
1182 {
"using type = int", Rel::Alias | Rel::Underlying},
1183 {
"using type = C::type", Rel::Alias | Rel::Underlying},
1184 {
"using type = B::type", Rel::Alias});
1187TEST_F(TargetDeclTest, RecursiveTemplate) {
1188 Flags.push_back(
"-std=c++20");
1191 template <typename T>
1192 concept Leaf = false;
1194 template <typename Tree>
1195 struct descend_left {
1196 using type = typename descend_left<typename Tree::left>::[[type]];
1199 template <Leaf Tree>
1200 struct descend_left<Tree> {
1201 using type = typename Tree::value;
1205 {
"using type = typename descend_left<typename Tree::left>::type",
1206 Rel::Alias | Rel::Underlying});
1209TEST_F(TargetDeclTest, ObjC) {
1210 Flags = {
"-xobjective-c"};
1222 @interface Foo { @public int bar; }
1233 -(void) setX:(int)x;
1239 EXPECT_DECLS("ObjCPropertyRefExpr",
"- (void)setX:(int)x");
1243 @property(retain) I* x;
1244 @property(retain) I* y;
1251 "@property(atomic, retain, readwrite) I *x");
1256 @interface Interface
1257 @property(retain) [[MYObject]] *x;
1260 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface MYObject");
1263 @interface MYObject2
1265 @interface Interface
1266 @property(retain, nonnull) [[MYObject2]] *x;
1269 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface MYObject2");
1275 return [[@protocol(Foo)]];
1283 void test([[Foo]] *p);
1285 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface Foo");
1287 Code = R
"cpp(// Don't consider implicit interface as the target.
1288 @implementation [[Implicit]]
1291 EXPECT_DECLS("ObjCImplementationDecl",
"@implementation Implicit");
1296 @implementation [[Foo]]
1299 EXPECT_DECLS("ObjCImplementationDecl",
"@interface Foo");
1304 @interface Foo (Ext)
1306 @implementation [[Foo]] (Ext)
1309 EXPECT_DECLS("ObjCCategoryImplDecl",
"@interface Foo(Ext)");
1314 @interface Foo (Ext)
1316 @implementation Foo ([[Ext]])
1319 EXPECT_DECLS("ObjCCategoryImplDecl",
"@interface Foo(Ext)");
1322 void test(id</*error-ok*/[[InvalidProtocol]]> p);
1330 void test([[C]]<Foo> *p);
1338 void test(C<[[Foo]]> *p);
1348 void test(C<[[Foo]], Bar> *p);
1358 void test(C<Foo, [[Bar]]> *p);
1364 + (id)sharedInstance;
1367 + (id)sharedInstance { return 0; }
1370 id value = [[Foo]].sharedInstance;
1373 EXPECT_DECLS("ObjCInterfaceTypeLoc",
"@interface Foo");
1377 + (id)sharedInstance;
1380 + (id)sharedInstance { return 0; }
1383 id value = Foo.[[sharedInstance]];
1386 EXPECT_DECLS("ObjCPropertyRefExpr",
"+ (id)sharedInstance");
1390 + ([[id]])sharedInstance;
1397 + ([[instancetype]])sharedInstance;
1403class FindExplicitReferencesTest :
public ::testing::Test {
1406 std::string AnnotatedCode;
1407 std::string DumpedReferences;
1410 TestTU newTU(llvm::StringRef Code) {
1412 TU.Code = std::string(Code);
1416 TU.ExtraArgs.push_back(
"-std=c++20");
1417 TU.ExtraArgs.push_back(
"-xobjective-c++");
1422 AllRefs annotatedReferences(llvm::StringRef Code, ParsedAST &
AST,
1423 std::vector<ReferenceLoc> Refs) {
1424 auto &SM =
AST.getSourceManager();
1425 llvm::stable_sort(Refs, [&](
const ReferenceLoc &L,
const ReferenceLoc &R) {
1426 return SM.isBeforeInTranslationUnit(L.NameLoc, R.NameLoc);
1429 std::string AnnotatedCode;
1430 unsigned NextCodeChar = 0;
1431 for (
unsigned I = 0; I < Refs.size(); ++I) {
1434 SourceLocation Pos = R.NameLoc;
1435 assert(Pos.isValid());
1436 if (Pos.isMacroID())
1437 Pos = SM.getExpansionLoc(Pos);
1438 assert(Pos.isFileID());
1442 std::tie(
File, Offset) = SM.getDecomposedLoc(Pos);
1443 if (
File == SM.getMainFileID()) {
1445 assert(NextCodeChar <= Offset);
1446 AnnotatedCode += Code.substr(NextCodeChar, Offset - NextCodeChar);
1447 AnnotatedCode +=
"$" + std::to_string(I) +
"^";
1449 NextCodeChar = Offset;
1452 AnnotatedCode += Code.substr(NextCodeChar);
1454 std::string DumpedReferences;
1455 for (
unsigned I = 0; I < Refs.size(); ++I)
1456 DumpedReferences += std::string(llvm::formatv(
"{0}: {1}\n", I, Refs[I]));
1458 return AllRefs{std::move(AnnotatedCode), std::move(DumpedReferences)};
1464 AllRefs annotateAllReferences(llvm::StringRef Code) {
1465 TestTU TU = newTU(Code);
1466 auto AST = TU.build();
1468 std::vector<ReferenceLoc> Refs;
1469 for (
auto *TopLevel :
AST.getLocalTopLevelDecls())
1471 TopLevel, [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1472 AST.getHeuristicResolver());
1473 return annotatedReferences(Code,
AST, std::move(Refs));
1479 AllRefs annotateReferencesInFoo(llvm::StringRef Code) {
1480 TestTU TU = newTU(Code);
1481 auto AST = TU.build();
1483 if (
auto *T = llvm::dyn_cast<FunctionTemplateDecl>(TestDecl))
1484 TestDecl =
T->getTemplatedDecl();
1486 std::vector<ReferenceLoc> Refs;
1487 if (
const auto *Func = llvm::dyn_cast<FunctionDecl>(TestDecl))
1490 [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1491 AST.getHeuristicResolver());
1492 else if (
const auto *NS = llvm::dyn_cast<NamespaceDecl>(TestDecl))
1495 [&Refs, &NS](ReferenceLoc R) {
1497 if (R.Targets.size() == 1 && R.Targets.front() == NS)
1499 Refs.push_back(std::move(R));
1501 AST.getHeuristicResolver());
1502 else if (
const auto *OC = llvm::dyn_cast<ObjCContainerDecl>(TestDecl))
1504 OC, [&Refs](ReferenceLoc R) { Refs.push_back(std::move(R)); },
1505 AST.getHeuristicResolver());
1507 ADD_FAILURE() <<
"Failed to find ::foo decl for test";
1509 return annotatedReferences(Code,
AST, std::move(Refs));
1513TEST_F(FindExplicitReferencesTest, AllRefsInFoo) {
1514 std::pair< llvm::StringRef, llvm::StringRef> Cases[] =
1519 void foo(int param) {
1520 $0^global = $1^param + $2^func();
1523 "0: targets = {global}\n"
1524 "1: targets = {param}\n"
1525 "2: targets = {func}\n"},
1527 struct X { int a; };
1532 "0: targets = {x}\n"
1533 "1: targets = {X::a}\n"},
1535 // error-ok: testing with broken code
1538 return $0^bar() + $1^bar(42);
1541 "0: targets = {bar}\n"
1542 "1: targets = {bar}\n"},
1546 namespace alias = ns;
1548 using namespace $0^ns;
1549 using namespace $1^alias;
1552 "0: targets = {ns}\n"
1553 "1: targets = {alias}\n"},
1556 namespace ns { int global; }
1558 using $0^ns::$1^global;
1561 "0: targets = {ns}\n"
1562 "1: targets = {ns::global}, qualifier = 'ns::'\n"},
1565 namespace ns { enum class A {}; }
1567 using enum $0^ns::$1^A;
1570 "0: targets = {ns}\n"
1571 "1: targets = {ns::A}, qualifier = 'ns::'\n"},
1574 struct Struct { int a; };
1575 using Typedef = int;
1579 static_cast<$4^Struct*>(0);
1582 "0: targets = {Struct}\n"
1583 "1: targets = {x}, decl\n"
1584 "2: targets = {Typedef}\n"
1585 "3: targets = {y}, decl\n"
1586 "4: targets = {Struct}\n"},
1589 namespace a { namespace b { struct S { typedef int type; }; } }
1591 $0^a::$1^b::$2^S $3^x;
1592 using namespace $4^a::$5^b;
1596 "0: targets = {a}\n"
1597 "1: targets = {a::b}, qualifier = 'a::'\n"
1598 "2: targets = {a::b::S}, qualifier = 'a::b::'\n"
1599 "3: targets = {x}, decl\n"
1600 "4: targets = {a}\n"
1601 "5: targets = {a::b}, qualifier = 'a::'\n"
1602 "6: targets = {a::b::S}\n"
1603 "7: targets = {a::b::S::type}, qualifier = 'S::'\n"
1604 "8: targets = {y}, decl\n"},
1607 $0^ten: // PRINT "HELLO WORLD!"
1611 "0: targets = {ten}, decl\n"
1612 "1: targets = {ten}\n"},
1615 template <class T> struct vector { using value_type = T; };
1616 template <> struct vector<bool> { using value_type = bool; };
1618 $0^vector<int> $1^vi;
1619 $2^vector<bool> $3^vb;
1622 "0: targets = {vector<int>}\n"
1623 "1: targets = {vi}, decl\n"
1624 "2: targets = {vector<bool>}\n"
1625 "3: targets = {vb}, decl\n"},
1628 template <class T> struct vector { using value_type = T; };
1629 template <> struct vector<bool> { using value_type = bool; };
1630 template <class T> using valias = vector<T>;
1632 $0^valias<int> $1^vi;
1633 $2^valias<bool> $3^vb;
1636 "0: targets = {valias}\n"
1637 "1: targets = {vi}, decl\n"
1638 "2: targets = {valias}\n"
1639 "3: targets = {vb}, decl\n"},
1643 template <typename $0^T>
1650 "0: targets = {foo::Bar::T}, decl\n"
1651 "1: targets = {foo::Bar}, decl\n"
1652 "2: targets = {foo::Bar}\n"
1653 "3: targets = {foo::Bar::f}, decl\n"
1654 "4: targets = {foo::Bar}\n"},
1657 struct X { void func(int); };
1665 "0: targets = {y}\n"
1666 "1: targets = {Y::func}\n"},
1669 namespace ns { void bar(int); }
1676 "0: targets = {bar}\n"},
1682 void foo(int a, int b) {
1686 "0: targets = {a}\n"
1687 "1: targets = {b}\n"},
1696 for (int $0^x : $1^vector()) {
1701 "0: targets = {x}, decl\n"
1702 "1: targets = {vector}\n"
1703 "2: targets = {x}\n"},
1706 namespace ns1 { void func(char*); }
1707 namespace ns2 { void func(int*); }
1708 using namespace ns1;
1709 using namespace ns2;
1716 "0: targets = {ns1::func, ns2::func}\n"
1717 "1: targets = {t}\n"},
1726 void foo(X x, T t) {
1730 "0: targets = {x}\n"
1731 "1: targets = {X::func, X::func}\n"
1732 "2: targets = {t}\n"},
1742 $0^S<$1^T>::$2^value;
1745 "0: targets = {S}\n"
1746 "1: targets = {T}\n"
1747 "2: targets = {S::value}, qualifier = 'S<T>::'\n"},
1760 "0: targets = {t}\n"
1761 "1: targets = {S::value}\n"},
1766 static_cast<$0^T>(0);
1771 "0: targets = {T}\n"
1772 "1: targets = {T}\n"
1773 "2: targets = {T}\n"
1774 "3: targets = {t}, decl\n"},
1782 "0: targets = {x}, decl\n"
1783 "1: targets = {I}\n"},
1786 template <class T> struct vector {};
1788 template <template<class> class TT, template<class> class ...TP>
1792 $4^foo<$5^vector>();
1796 "0: targets = {TT}\n"
1797 "1: targets = {x}, decl\n"
1798 "2: targets = {foo}\n"
1799 "3: targets = {TT}\n"
1800 "4: targets = {foo}\n"
1801 "5: targets = {vector}\n"
1802 "6: targets = {foo}\n"
1803 "7: targets = {TP}\n"},
1807 template <int(*)()> struct wrapper {};
1809 template <int(*FuncParam)()>
1811 $0^wrapper<$1^func> $2^w;
1815 "0: targets = {wrapper<&func>}\n"
1816 "1: targets = {func}\n"
1817 "2: targets = {w}, decl\n"
1818 "3: targets = {FuncParam}\n"},
1824 class $0^Foo { $1^Foo(); ~$2^Foo(); int $3^field; };
1826 enum $5^E { $6^ABC };
1828 using $8^INT2 = int;
1829 namespace $9^NS = $10^ns;
1832 "0: targets = {Foo}, decl\n"
1833 "1: targets = {foo()::Foo::Foo}, decl\n"
1834 "2: targets = {Foo}\n"
1835 "3: targets = {foo()::Foo::field}, decl\n"
1836 "4: targets = {Var}, decl\n"
1837 "5: targets = {E}, decl\n"
1838 "6: targets = {foo()::ABC}, decl\n"
1839 "7: targets = {INT}, decl\n"
1840 "8: targets = {INT2}, decl\n"
1841 "9: targets = {NS}, decl\n"
1842 "10: targets = {ns}\n"},
1849 // FIXME: This should have only one reference to Bar.
1850 $2^operator $3^$4^Bar();
1854 $7^f.$8^operator $9^Bar();
1857 "0: targets = {Bar}, decl\n"
1858 "1: targets = {Foo}, decl\n"
1859 "2: targets = {foo()::Foo::operator Bar}, decl\n"
1860 "3: targets = {Bar}\n"
1861 "4: targets = {Bar}\n"
1862 "5: targets = {Foo}\n"
1863 "6: targets = {f}, decl\n"
1864 "7: targets = {f}\n"
1865 "8: targets = {foo()::Foo::operator Bar}\n"
1866 "9: targets = {Bar}\n"},
1874 void $2^destructMe() {
1880 $6^f.~ /*...*/ $7^Foo();
1883 "0: targets = {Foo}, decl\n"
1886 "1: targets = {Foo}\n"
1887 "2: targets = {foo()::Foo::destructMe}, decl\n"
1888 "3: targets = {Foo}\n"
1889 "4: targets = {Foo}\n"
1890 "5: targets = {f}, decl\n"
1891 "6: targets = {f}\n"
1892 "7: targets = {Foo}\n"},
1897 // member initializer
1903 class $4^Derived : public $5^Base {
1905 $8^Derived() : $9^Base() {}
1907 // delegating initializer
1910 $12^Foo(): $13^Foo(111) {}
1914 "0: targets = {X}, decl\n"
1915 "1: targets = {foo()::X::abc}, decl\n"
1916 "2: targets = {foo()::X::X}, decl\n"
1917 "3: targets = {foo()::X::abc}\n"
1918 "4: targets = {Derived}, decl\n"
1919 "5: targets = {Base}\n"
1920 "6: targets = {Base}\n"
1921 "7: targets = {foo()::Derived::B}, decl\n"
1922 "8: targets = {foo()::Derived::Derived}, decl\n"
1923 "9: targets = {Base}\n"
1924 "10: targets = {Foo}, decl\n"
1925 "11: targets = {foo()::Foo::Foo}, decl\n"
1926 "12: targets = {foo()::Foo::Foo}, decl\n"
1927 "13: targets = {Foo}\n"},
1933 int (*$2^fptr)(int $3^a, int) = nullptr;
1936 "0: targets = {(unnamed class)}\n"
1937 "1: targets = {x}, decl\n"
1938 "2: targets = {fptr}, decl\n"
1939 "3: targets = {a}, decl\n"},
1943 namespace ns { struct Type {}; }
1944 namespace alias = ns;
1945 namespace rec_alias = alias;
1948 $0^ns::$1^Type $2^a;
1949 $3^alias::$4^Type $5^b;
1950 $6^rec_alias::$7^Type $8^c;
1953 "0: targets = {ns}\n"
1954 "1: targets = {ns::Type}, qualifier = 'ns::'\n"
1955 "2: targets = {a}, decl\n"
1956 "3: targets = {alias}\n"
1957 "4: targets = {ns::Type}, qualifier = 'alias::'\n"
1958 "5: targets = {b}, decl\n"
1959 "6: targets = {rec_alias}\n"
1960 "7: targets = {ns::Type}, qualifier = 'rec_alias::'\n"
1961 "8: targets = {c}, decl\n"},
1965 template <typename... E>
1967 constexpr int $0^size = sizeof...($1^E);
1970 "0: targets = {size}, decl\n"
1971 "1: targets = {E}\n"},
1975 template <typename T>
1983 "0: targets = {Test}\n"
1984 "1: targets = {a}, decl\n"},
1988 template <typename $0^T>
1992 "0: targets = {foo::Bar::T}, decl\n"
1993 "1: targets = {foo::Bar}, decl\n"},
1997 template <typename $0^T>
2001 "0: targets = {T}, decl\n"
2002 "1: targets = {foo::func}, decl\n"},
2006 template <typename $0^T>
2010 "0: targets = {foo::T}, decl\n"
2011 "1: targets = {foo::T}\n"
2012 "2: targets = {foo::x}, decl\n"},
2015 template<typename T> class vector {};
2017 template <typename $0^T>
2018 using $1^V = $2^vector<$3^T>;
2021 "0: targets = {foo::T}, decl\n"
2022 "1: targets = {foo::V}, decl\n"
2023 "2: targets = {vector}\n"
2024 "3: targets = {foo::T}\n"},
2028 template <typename T>
2029 concept Drawable = requires (T t) { t.draw(); };
2032 template <typename $0^T> requires $1^Drawable<$2^T>
2033 void $3^bar($4^T $5^t) {
2038 "0: targets = {T}, decl\n"
2039 "1: targets = {Drawable}\n"
2040 "2: targets = {T}\n"
2041 "3: targets = {foo::bar}, decl\n"
2042 "4: targets = {T}\n"
2043 "5: targets = {t}, decl\n"
2044 "6: targets = {t}\n"
2045 "7: targets = {}\n"},
2059 "0: targets = {f}\n"
2060 "1: targets = {I::_z}\n"},
2065 @property(retain) I* x;
2066 @property(retain) I* y;
2073 "0: targets = {f}\n"
2074 "1: targets = {I::x}\n"
2075 "2: targets = {I::y}\n"},
2088 "0: targets = {f}\n"
2089 "1: targets = {I::x}\n"
2090 "2: targets = {I::setY:}\n"},
2095 @property(class) I *x;
2100 $2^local = $3^I.$4^x;
2103 "0: targets = {I}\n"
2104 "1: targets = {I::setX:}\n"
2105 "2: targets = {local}\n"
2106 "3: targets = {I}\n"
2107 "4: targets = {I::x}\n"},
2118 $2^local = $3^I.$4^x;
2121 "0: targets = {I}\n"
2122 "1: targets = {I::setX:}\n"
2123 "2: targets = {local}\n"
2124 "3: targets = {I}\n"
2125 "4: targets = {I::x}\n"},
2129 -(void) a:(int)x b:(int)y;
2135 "0: targets = {i}\n"
2136 "1: targets = {I::a:b:}\n"},
2147 "0: targets = {I}\n"
2148 "1: targets = {P}\n"
2149 "2: targets = {x}, decl\n"},
2157 $2^Foo $3^f { .$4^Bar = 42 };
2160 "0: targets = {Foo}, decl\n"
2161 "1: targets = {foo()::Foo::Bar}, decl\n"
2162 "2: targets = {Foo}\n"
2163 "3: targets = {f}, decl\n"
2164 "4: targets = {foo()::Foo::Bar}\n"},
2173 $5^Bar $6^bar { .$7^Foo.$8^Field = 42 };
2176 "0: targets = {Baz}, decl\n"
2177 "1: targets = {foo()::Baz::Field}, decl\n"
2178 "2: targets = {Bar}, decl\n"
2179 "3: targets = {Baz}\n"
2180 "4: targets = {foo()::Bar::Foo}, decl\n"
2181 "5: targets = {Bar}\n"
2182 "6: targets = {bar}, decl\n"
2183 "7: targets = {foo()::Bar::Foo}\n"
2184 "8: targets = {foo()::Baz::Field}\n"},
2188 struct $0^Foo { int $1^bar; };
2189 int $2^x = __builtin_offsetof($3^Foo, $4^bar);
2192 "0: targets = {Foo}, decl\n"
2193 "1: targets = {foo()::Foo::bar}, decl\n"
2194 "2: targets = {x}, decl\n"
2195 "3: targets = {Foo}\n"
2196 "4: targets = {foo()::Foo::bar}\n"},
2202 $1^struct { int $2^c; } $3^B;
2204 int $4^x = __builtin_offsetof($5^A, $6^B.$7^c);
2207 "0: targets = {A}, decl\n"
2208 "1: targets = {foo()::A::(unnamed struct)}\n"
2209 "2: targets = {foo()::A::(unnamed struct)::c}, decl\n"
2210 "3: targets = {foo()::A::B}, decl\n"
2211 "4: targets = {x}, decl\n"
2212 "5: targets = {A}\n"
2213 "6: targets = {foo()::A::B}\n"
2214 "7: targets = {foo()::A::(unnamed struct)::c}\n"},
2220 struct $0^A { int $1^arr[4]; };
2222 int $3^x = __builtin_offsetof($4^A, $5^arr[$6^i]);
2225 "0: targets = {A}, decl\n"
2226 "1: targets = {foo()::A::arr}, decl\n"
2227 "2: targets = {i}, decl\n"
2228 "3: targets = {x}, decl\n"
2229 "4: targets = {A}\n"
2230 "5: targets = {foo()::A::arr}\n"
2231 "6: targets = {i}\n"},
2233 template<typename T>
2235 template<typename T>
2237 $0^crash({.$1^x = $2^T()});
2240 "0: targets = {crash}\n"
2242 "2: targets = {T}\n"},
2245 template <template <typename> typename T>
2248 template <typename $0^T>
2249 struct $1^Derive : $2^Base<$3^T::template $4^Unknown> {};
2252 "0: targets = {foo::Derive::T}, decl\n"
2253 "1: targets = {foo::Derive}, decl\n"
2254 "2: targets = {Base}\n"
2255 "3: targets = {foo::Derive::T}\n"
2256 "4: targets = {}, qualifier = 'T::'\n"},
2260 template <typename $0^T>
2262 template <typename $2^I>
2265 template <typename $5^I>
2266 $6^Test($7^I) -> $8^Test<typename $9^I::$10^type>;
2269 "0: targets = {T}, decl\n"
2270 "1: targets = {foo::Test}, decl\n"
2271 "2: targets = {I}, decl\n"
2272 "3: targets = {foo::Test::Test<T>}, decl\n"
2273 "4: targets = {I}\n"
2274 "5: targets = {I}, decl\n"
2275 "6: targets = {foo::Test}\n"
2276 "7: targets = {I}\n"
2277 "8: targets = {foo::Test}\n"
2278 "9: targets = {I}\n"
2279 "10: targets = {}, qualifier = 'I::'\n"}};
2281 for (
const auto &C : Cases) {
2282 llvm::StringRef ExpectedCode =
C.first;
2283 llvm::StringRef ExpectedRefs =
C.second;
2286 annotateReferencesInFoo(llvm::Annotations(ExpectedCode).code());
2287 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
2288 EXPECT_EQ(ExpectedRefs, Actual.DumpedReferences) << ExpectedCode;
2292TEST_F(FindExplicitReferencesTest, AllRefs) {
2293 std::pair< llvm::StringRef, llvm::StringRef> Cases[] =
2295 @interface $0^MyClass
2297 @implementation $1^$2^MyClass
2300 "0: targets = {MyClass}, decl\n"
2301 "1: targets = {MyClass}\n"
2302 "2: targets = {MyClass}, decl\n"},
2304 @interface $0^MyClass
2306 @interface $1^MyClass ($2^Category)
2308 @implementation $3^MyClass ($4^$5^Category)
2311 "0: targets = {MyClass}, decl\n"
2312 "1: targets = {MyClass}\n"
2313 "2: targets = {Category}, decl\n"
2314 "3: targets = {MyClass}\n"
2315 "4: targets = {Category}\n"
2316 "5: targets = {Category}, decl\n"}};
2318 for (
const auto &C : Cases) {
2319 llvm::StringRef ExpectedCode =
C.first;
2320 llvm::StringRef ExpectedRefs =
C.second;
2322 auto Actual = annotateAllReferences(llvm::Annotations(ExpectedCode).code());
2323 EXPECT_EQ(ExpectedCode, Actual.AnnotatedCode);
2324 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)