1 //===- unittest/Introspection/IntrospectionTest.cpp ----------*- C++ -*---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Tests for AST location API introspection.
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/ASTContext.h"
14 #include "clang/ASTMatchers/ASTMatchFinder.h"
15 #include "clang/ASTMatchers/ASTMatchers.h"
16 #include "clang/Tooling/NodeIntrospection.h"
17 #include "clang/Tooling/Tooling.h"
18 #include "gmock/gmock-matchers.h"
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
22 using namespace clang
;
23 using namespace clang::ast_matchers
;
24 using namespace clang::tooling
;
26 using ::testing::Pair
;
27 using ::testing::UnorderedElementsAre
;
29 template <typename T
, typename MapType
>
30 std::vector
<std::pair
<std::string
, T
>>
31 FormatExpected(const MapType
&Accessors
) {
32 std::vector
<std::pair
<std::string
, T
>> Result
;
33 llvm::transform(llvm::make_filter_range(Accessors
,
34 [](const auto &Accessor
) {
35 return Accessor
.first
.isValid();
37 std::back_inserter(Result
), [](const auto &Accessor
) {
38 return std::make_pair(
39 LocationCallFormatterCpp::format(*Accessor
.second
),
45 #define STRING_LOCATION_PAIR(INSTANCE, LOC) Pair(#LOC, INSTANCE->LOC)
47 #define STRING_LOCATION_STDPAIR(INSTANCE, LOC) \
48 std::make_pair(std::string(#LOC), INSTANCE->LOC)
51 A test formatter for a hypothetical language which needs
52 neither casts nor '->'.
54 class LocationCallFormatterSimple
{
56 static void print(const LocationCall
&Call
, llvm::raw_ostream
&OS
) {
58 if (const LocationCall
*On
= Call
.on())
62 if (const LocationCall
*On
= Call
.on()) {
66 OS
<< Call
.name() << "()";
69 static std::string
format(const LocationCall
&Call
) {
71 llvm::raw_string_ostream
OS(Result
);
78 TEST(Introspection
, SourceLocations_CallContainer
) {
79 SourceLocationMap slm
;
80 SharedLocationCall Prefix
;
81 slm
.insert(std::make_pair(
83 llvm::makeIntrusiveRefCnt
<LocationCall
>(Prefix
, "getSourceRange")));
84 EXPECT_EQ(slm
.size(), 1u);
87 llvm::makeIntrusiveRefCnt
<LocationCall
>(Prefix
, "getTypeLoc");
88 slm
.insert(std::make_pair(
90 llvm::makeIntrusiveRefCnt
<LocationCall
>(callTypeLoc
, "getSourceRange")));
91 EXPECT_EQ(slm
.size(), 2u);
94 TEST(Introspection
, SourceLocations_CallContainer2
) {
96 SharedLocationCall Prefix
;
98 std::make_pair(SourceRange(), llvm::makeIntrusiveRefCnt
<LocationCall
>(
99 Prefix
, "getCXXOperatorNameRange")));
100 EXPECT_EQ(slm
.size(), 1u);
102 slm
.insert(std::make_pair(
104 llvm::makeIntrusiveRefCnt
<LocationCall
>(Prefix
, "getSourceRange")));
105 EXPECT_EQ(slm
.size(), 2u);
108 TEST(Introspection
, SourceLocations_CallChainFormatting
) {
109 SharedLocationCall Prefix
;
110 auto chainedCall
= llvm::makeIntrusiveRefCnt
<LocationCall
>(
111 llvm::makeIntrusiveRefCnt
<LocationCall
>(Prefix
, "getTypeLoc"),
113 EXPECT_EQ(LocationCallFormatterCpp::format(*chainedCall
),
114 "getTypeLoc().getSourceRange()");
117 TEST(Introspection
, SourceLocations_Formatter
) {
118 SharedLocationCall Prefix
;
119 auto chainedCall
= llvm::makeIntrusiveRefCnt
<LocationCall
>(
120 llvm::makeIntrusiveRefCnt
<LocationCall
>(
121 llvm::makeIntrusiveRefCnt
<LocationCall
>(
122 llvm::makeIntrusiveRefCnt
<LocationCall
>(
123 Prefix
, "getTypeSourceInfo", LocationCall::ReturnsPointer
),
125 "getAs<clang::TypeSpecTypeLoc>", LocationCall::IsCast
),
128 EXPECT_EQ("getTypeSourceInfo()->getTypeLoc().getAs<clang::TypeSpecTypeLoc>()."
130 LocationCallFormatterCpp::format(*chainedCall
));
131 EXPECT_EQ("getTypeSourceInfo().getTypeLoc().getNameLoc()",
132 LocationCallFormatterSimple::format(*chainedCall
));
135 TEST(Introspection
, SourceLocations_Stmt
) {
136 if (!NodeIntrospection::hasIntrospectionSupport())
138 auto AST
= buildASTFromCode("void foo() {} void bar() { foo(); }", "foo.cpp",
139 std::make_shared
<PCHContainerOperations
>());
140 auto &Ctx
= AST
->getASTContext();
141 auto &TU
= *Ctx
.getTranslationUnitDecl();
143 auto BoundNodes
= ast_matchers::match(
145 callExpr(callee(functionDecl(hasName("foo")))).bind("fooCall"))),
148 EXPECT_EQ(BoundNodes
.size(), 1u);
150 auto *FooCall
= BoundNodes
[0].getNodeAs
<CallExpr
>("fooCall");
152 auto Result
= NodeIntrospection::GetLocations(FooCall
);
154 auto ExpectedLocations
=
155 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
159 UnorderedElementsAre(STRING_LOCATION_PAIR(FooCall
, getBeginLoc()),
160 STRING_LOCATION_PAIR(FooCall
, getEndLoc()),
161 STRING_LOCATION_PAIR(FooCall
, getExprLoc()),
162 STRING_LOCATION_PAIR(FooCall
, getRParenLoc())));
164 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
166 EXPECT_THAT(ExpectedRanges
, UnorderedElementsAre(STRING_LOCATION_PAIR(
167 FooCall
, getSourceRange())));
170 TEST(Introspection
, SourceLocations_Decl
) {
171 if (!NodeIntrospection::hasIntrospectionSupport())
174 buildASTFromCode(R
"cpp(
177 template <typename T, typename U> struct Foo {};
178 template <typename T, typename U> struct Bar {
180 template <typename A, typename B>
181 Foo<A, B> method(int i, bool b) const noexcept(true);
187 template <typename T, typename U>
188 template <typename A, typename B>
189 ns1::ns2::Foo<A, B> ns1::ns2::Bar<T, U>::Nested::method(int i, bool b) const
192 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
193 auto &Ctx
= AST
->getASTContext();
194 auto &TU
= *Ctx
.getTranslationUnitDecl();
196 auto BoundNodes
= ast_matchers::match(
198 cxxMethodDecl(hasName("method"), isDefinition()).bind("method"))),
201 EXPECT_EQ(BoundNodes
.size(), 1u);
203 const auto *MethodDecl
= BoundNodes
[0].getNodeAs
<CXXMethodDecl
>("method");
205 auto Result
= NodeIntrospection::GetLocations(MethodDecl
);
207 auto ExpectedLocations
=
208 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
210 llvm::sort(ExpectedLocations
);
213 std::vector
<std::pair
<std::string
, SourceLocation
>> ActualLocations
{
214 STRING_LOCATION_STDPAIR(MethodDecl
, getBeginLoc()),
215 STRING_LOCATION_STDPAIR(MethodDecl
, getBodyRBrace()),
216 STRING_LOCATION_STDPAIR(MethodDecl
, getEndLoc()),
217 STRING_LOCATION_STDPAIR(MethodDecl
, getInnerLocStart()),
218 STRING_LOCATION_STDPAIR(MethodDecl
, getLocation()),
219 STRING_LOCATION_STDPAIR(MethodDecl
, getNameInfo().getBeginLoc()),
220 STRING_LOCATION_STDPAIR(MethodDecl
, getNameInfo().getEndLoc()),
221 STRING_LOCATION_STDPAIR(MethodDecl
, getNameInfo().getLoc()),
222 STRING_LOCATION_STDPAIR(MethodDecl
, getOuterLocStart()),
223 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getBeginLoc()),
224 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getEndLoc()),
225 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getLocalBeginLoc()),
226 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getLocalEndLoc()),
227 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getBeginLoc()),
228 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getEndLoc()),
229 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getLocalBeginLoc()),
230 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getLocalEndLoc()),
231 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getPrefix().getBeginLoc()),
232 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getPrefix().getEndLoc()),
233 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getPrefix().getLocalBeginLoc()),
234 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getPrefix().getLocalEndLoc()),
235 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getPrefix().getPrefix().getBeginLoc()),
236 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getPrefix().getPrefix().getEndLoc()),
237 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getPrefix().getPrefix().getLocalBeginLoc()),
238 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getPrefix().getPrefix().getLocalEndLoc()),
239 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getLAngleLoc()),
240 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getRAngleLoc()),
241 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getTemplateNameLoc()),
242 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getTypeLoc().getBeginLoc()),
243 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getTypeLoc().getEndLoc()),
244 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc()),
245 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getTypeLoc().getBeginLoc()),
246 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getTypeLoc().getEndLoc()),
247 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getLParenLoc()),
248 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getLocalRangeBegin()),
249 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getLocalRangeEnd()),
250 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getRParenLoc()),
251 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getLAngleLoc()),
252 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getRAngleLoc()),
253 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getTemplateNameLoc()),
254 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getBeginLoc()),
255 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getEndLoc()),
256 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getBeginLoc()),
257 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getEndLoc()),
258 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getLocalBeginLoc()),
259 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getLocalEndLoc()),
260 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getPrefix().getBeginLoc()),
261 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getPrefix().getEndLoc()),
262 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getPrefix().getLocalBeginLoc()),
263 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getPrefix().getLocalEndLoc()),
264 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getBeginLoc()),
265 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getEndLoc()),
266 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getLAngleLoc()),
267 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getRAngleLoc()),
268 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getTemplateNameLoc()),
269 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getNextTypeLoc().getBeginLoc()),
270 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getNextTypeLoc().getEndLoc()),
271 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getBeginLoc()),
272 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getEndLoc()),
273 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getLAngleLoc()),
274 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getRAngleLoc()),
275 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getTemplateNameLoc()),
276 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getBeginLoc()),
277 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getEndLoc()),
278 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getBeginLoc()),
279 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getEndLoc()),
280 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getLocalBeginLoc()),
281 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getLocalEndLoc()),
282 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getPrefix().getBeginLoc()),
283 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getPrefix().getEndLoc()),
284 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getPrefix().getLocalBeginLoc()),
285 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getPrefix().getLocalEndLoc()),
286 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getBeginLoc()),
287 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getEndLoc()),
288 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getLAngleLoc()),
289 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getRAngleLoc()),
290 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getTemplateNameLoc()),
291 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getNextTypeLoc().getBeginLoc()),
292 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getNextTypeLoc().getEndLoc()),
293 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSpecEndLoc()),
294 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSpecStartLoc())
298 EXPECT_EQ(ExpectedLocations
, ActualLocations
);
300 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
302 llvm::sort(ExpectedRanges
, [](const auto &LHS
, const auto &RHS
) {
303 return LHS
.first
< RHS
.first
;
308 llvm::makeArrayRef(ExpectedRanges
),
309 (ArrayRef
<std::pair
<std::string
, SourceRange
>>{
310 STRING_LOCATION_STDPAIR(MethodDecl
, getExceptionSpecSourceRange()),
311 STRING_LOCATION_STDPAIR(MethodDecl
, getNameInfo().getSourceRange()),
312 STRING_LOCATION_STDPAIR(MethodDecl
, getParametersSourceRange()),
313 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getLocalSourceRange()),
314 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getLocalSourceRange()),
315 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getPrefix().getLocalSourceRange()),
316 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getPrefix().getPrefix().getLocalSourceRange()),
317 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getPrefix().getPrefix().getSourceRange()),
318 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getPrefix().getSourceRange()),
319 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getSourceRange()),
320 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getTypeLoc().getLocalSourceRange()),
321 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getPrefix().getTypeLoc().getSourceRange()),
322 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getSourceRange()),
323 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getTypeLoc().getLocalSourceRange()),
324 STRING_LOCATION_STDPAIR(MethodDecl
, getQualifierLoc().getTypeLoc().getSourceRange()),
325 STRING_LOCATION_STDPAIR(MethodDecl
, getReturnTypeSourceRange()),
326 STRING_LOCATION_STDPAIR(MethodDecl
, getSourceRange()),
327 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getExceptionSpecRange()),
328 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getParensRange()),
329 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getLocalSourceRange()),
330 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getSourceRange()),
331 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getLocalSourceRange()),
332 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getPrefix().getLocalSourceRange()),
333 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getPrefix().getSourceRange()),
334 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getSourceRange()),
335 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getLocalSourceRange()),
336 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getNextTypeLoc().getLocalSourceRange()),
337 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getNextTypeLoc().getSourceRange()),
338 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getSourceRange()),
339 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getLocalSourceRange()),
340 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getLocalSourceRange()),
341 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getSourceRange()),
342 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getLocalSourceRange()),
343 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getPrefix().getLocalSourceRange()),
344 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getPrefix().getSourceRange()),
345 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getQualifierLoc().getSourceRange()),
346 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getLocalSourceRange()),
347 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getNextTypeLoc().getLocalSourceRange()),
348 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getNextTypeLoc().getSourceRange()),
349 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getSourceRange()),
350 STRING_LOCATION_STDPAIR(MethodDecl
, getTypeSourceInfo()->getTypeLoc().getSourceRange())
355 TEST(Introspection
, SourceLocations_NNS
) {
356 if (!NodeIntrospection::hasIntrospectionSupport())
359 buildASTFromCode(R
"cpp(
368 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
369 auto &Ctx
= AST
->getASTContext();
370 auto &TU
= *Ctx
.getTranslationUnitDecl();
372 auto BoundNodes
= ast_matchers::match(
373 decl(hasDescendant(nestedNameSpecifierLoc().bind("nns"))), TU
, Ctx
);
375 EXPECT_EQ(BoundNodes
.size(), 1u);
377 const auto *NNS
= BoundNodes
[0].getNodeAs
<NestedNameSpecifierLoc
>("nns");
379 auto Result
= NodeIntrospection::GetLocations(*NNS
);
381 auto ExpectedLocations
=
382 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
384 llvm::sort(ExpectedLocations
);
387 llvm::makeArrayRef(ExpectedLocations
),
388 (ArrayRef
<std::pair
<std::string
, SourceLocation
>>{
389 STRING_LOCATION_STDPAIR(NNS
, getBeginLoc()),
390 STRING_LOCATION_STDPAIR(NNS
, getEndLoc()),
391 STRING_LOCATION_STDPAIR(NNS
, getLocalBeginLoc()),
392 STRING_LOCATION_STDPAIR(NNS
, getLocalEndLoc()),
393 STRING_LOCATION_STDPAIR(NNS
, getPrefix().getBeginLoc()),
394 STRING_LOCATION_STDPAIR(NNS
, getPrefix().getEndLoc()),
395 STRING_LOCATION_STDPAIR(NNS
, getPrefix().getLocalBeginLoc()),
396 STRING_LOCATION_STDPAIR(NNS
, getPrefix().getLocalEndLoc()),
397 STRING_LOCATION_STDPAIR(
398 NNS
, getTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc()),
399 STRING_LOCATION_STDPAIR(NNS
, getTypeLoc().getBeginLoc()),
400 STRING_LOCATION_STDPAIR(NNS
, getTypeLoc().getEndLoc())}));
402 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
406 UnorderedElementsAre(
407 STRING_LOCATION_PAIR(NNS
, getPrefix().getLocalSourceRange()),
408 STRING_LOCATION_PAIR(NNS
, getPrefix().getSourceRange()),
409 STRING_LOCATION_PAIR(NNS
, getLocalSourceRange()),
410 STRING_LOCATION_PAIR(NNS
, getSourceRange()),
411 STRING_LOCATION_PAIR(NNS
, getTypeLoc().getSourceRange()),
412 STRING_LOCATION_PAIR(NNS
, getTypeLoc().getLocalSourceRange())));
415 TEST(Introspection
, SourceLocations_TA_Type
) {
416 if (!NodeIntrospection::hasIntrospectionSupport())
419 buildASTFromCode(R
"cpp(
430 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
431 auto &Ctx
= AST
->getASTContext();
432 auto &TU
= *Ctx
.getTranslationUnitDecl();
434 auto BoundNodes
= ast_matchers::match(
435 decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU
, Ctx
);
437 EXPECT_EQ(BoundNodes
.size(), 1u);
439 const auto *TA
= BoundNodes
[0].getNodeAs
<TemplateArgumentLoc
>("ta");
441 auto Result
= NodeIntrospection::GetLocations(*TA
);
443 auto ExpectedLocations
=
444 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
447 EXPECT_THAT(ExpectedLocations
,
448 UnorderedElementsAre(
449 STRING_LOCATION_PAIR(TA
, getLocation()),
450 STRING_LOCATION_PAIR(TA
,
451 getTypeSourceInfo()->getTypeLoc().getAs
<clang::BuiltinTypeLoc
>().getBuiltinLoc()),
452 STRING_LOCATION_PAIR(TA
,
453 getTypeSourceInfo()->getTypeLoc().getAs
<clang::BuiltinTypeLoc
>().getNameLoc()),
454 STRING_LOCATION_PAIR(
455 TA
, getTypeSourceInfo()->getTypeLoc().getBeginLoc()),
456 STRING_LOCATION_PAIR(
457 TA
, getTypeSourceInfo()->getTypeLoc().getEndLoc())
461 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
465 UnorderedElementsAre(
466 STRING_LOCATION_PAIR(TA
, getSourceRange()),
467 STRING_LOCATION_PAIR(
468 TA
, getTypeSourceInfo()->getTypeLoc().getSourceRange()),
469 STRING_LOCATION_PAIR(
470 TA
, getTypeSourceInfo()->getTypeLoc().getLocalSourceRange())));
473 TEST(Introspection
, SourceLocations_TA_Decl
) {
474 if (!NodeIntrospection::hasIntrospectionSupport())
477 buildASTFromCode(R
"cpp(
478 template<void(*Ty)()>
485 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
486 auto &Ctx
= AST
->getASTContext();
487 auto &TU
= *Ctx
.getTranslationUnitDecl();
489 auto BoundNodes
= ast_matchers::match(
490 decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU
, Ctx
);
492 EXPECT_EQ(BoundNodes
.size(), 1u);
494 const auto *TA
= BoundNodes
[0].getNodeAs
<TemplateArgumentLoc
>("ta");
496 auto Result
= NodeIntrospection::GetLocations(*TA
);
498 auto ExpectedLocations
=
499 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
501 EXPECT_THAT(ExpectedLocations
,
502 UnorderedElementsAre(STRING_LOCATION_PAIR(TA
, getLocation())));
504 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
506 EXPECT_THAT(ExpectedRanges
,
507 UnorderedElementsAre(STRING_LOCATION_PAIR(TA
, getSourceRange())));
510 TEST(Introspection
, SourceLocations_TA_Nullptr
) {
511 if (!NodeIntrospection::hasIntrospectionSupport())
514 buildASTFromCode(R
"cpp(
515 template<void(*Ty)()>
522 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
523 auto &Ctx
= AST
->getASTContext();
524 auto &TU
= *Ctx
.getTranslationUnitDecl();
526 auto BoundNodes
= ast_matchers::match(
527 decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU
, Ctx
);
529 EXPECT_EQ(BoundNodes
.size(), 1u);
531 const auto *TA
= BoundNodes
[0].getNodeAs
<TemplateArgumentLoc
>("ta");
533 auto Result
= NodeIntrospection::GetLocations(*TA
);
535 auto ExpectedLocations
=
536 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
538 EXPECT_THAT(ExpectedLocations
,
539 UnorderedElementsAre(STRING_LOCATION_PAIR(TA
, getLocation())));
541 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
543 EXPECT_THAT(ExpectedRanges
,
544 UnorderedElementsAre(STRING_LOCATION_PAIR(TA
, getSourceRange())));
547 TEST(Introspection
, SourceLocations_TA_Integral
) {
548 if (!NodeIntrospection::hasIntrospectionSupport())
551 buildASTFromCode(R
"cpp(
558 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
559 auto &Ctx
= AST
->getASTContext();
560 auto &TU
= *Ctx
.getTranslationUnitDecl();
562 auto BoundNodes
= ast_matchers::match(
563 decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU
, Ctx
);
565 EXPECT_EQ(BoundNodes
.size(), 1u);
567 const auto *TA
= BoundNodes
[0].getNodeAs
<TemplateArgumentLoc
>("ta");
569 auto Result
= NodeIntrospection::GetLocations(*TA
);
571 auto ExpectedLocations
=
572 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
574 EXPECT_THAT(ExpectedLocations
,
575 UnorderedElementsAre(STRING_LOCATION_PAIR(TA
, getLocation())));
577 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
579 EXPECT_THAT(ExpectedRanges
,
580 UnorderedElementsAre(STRING_LOCATION_PAIR(TA
, getSourceRange())));
583 TEST(Introspection
, SourceLocations_TA_Template
) {
584 if (!NodeIntrospection::hasIntrospectionSupport())
587 buildASTFromCode(R
"cpp(
588 template<typename T> class A;
589 template <template <typename> class T> void foo();
595 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
596 auto &Ctx
= AST
->getASTContext();
597 auto &TU
= *Ctx
.getTranslationUnitDecl();
599 auto BoundNodes
= ast_matchers::match(
600 decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU
, Ctx
);
602 EXPECT_EQ(BoundNodes
.size(), 1u);
604 const auto *TA
= BoundNodes
[0].getNodeAs
<TemplateArgumentLoc
>("ta");
606 auto Result
= NodeIntrospection::GetLocations(*TA
);
608 auto ExpectedLocations
=
609 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
613 UnorderedElementsAre(STRING_LOCATION_PAIR(TA
, getLocation()),
614 STRING_LOCATION_PAIR(TA
, getTemplateNameLoc())));
616 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
618 EXPECT_THAT(ExpectedRanges
,
619 UnorderedElementsAre(STRING_LOCATION_PAIR(TA
, getSourceRange())));
622 TEST(Introspection
, SourceLocations_TA_TemplateExpansion
) {
623 if (!NodeIntrospection::hasIntrospectionSupport())
625 auto AST
= buildASTFromCodeWithArgs(
627 template<template<typename> class ...> class B { };
628 template<template<typename> class ...T> class C {
629 B<T...> testTemplateExpansion;
632 {"-fno-delayed-template-parsing"}, "foo.cpp", "clang-tool",
633 std::make_shared
<PCHContainerOperations
>());
634 auto &Ctx
= AST
->getASTContext();
635 auto &TU
= *Ctx
.getTranslationUnitDecl();
637 auto BoundNodes
= ast_matchers::match(
638 decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU
, Ctx
);
640 EXPECT_EQ(BoundNodes
.size(), 1u);
642 const auto *TA
= BoundNodes
[0].getNodeAs
<TemplateArgumentLoc
>("ta");
644 auto Result
= NodeIntrospection::GetLocations(*TA
);
646 auto ExpectedLocations
=
647 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
651 UnorderedElementsAre(STRING_LOCATION_PAIR(TA
, getLocation()),
652 STRING_LOCATION_PAIR(TA
, getTemplateNameLoc()),
653 STRING_LOCATION_PAIR(TA
, getTemplateEllipsisLoc())));
655 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
657 EXPECT_THAT(ExpectedRanges
,
658 UnorderedElementsAre(STRING_LOCATION_PAIR(TA
, getSourceRange())));
661 TEST(Introspection
, SourceLocations_TA_Expression
) {
662 if (!NodeIntrospection::hasIntrospectionSupport())
665 buildASTFromCode(R
"cpp(
666 template<int, int = 0> class testExpr;
667 template<int I> class testExpr<I> { };
669 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
670 auto &Ctx
= AST
->getASTContext();
671 auto &TU
= *Ctx
.getTranslationUnitDecl();
673 auto BoundNodes
= ast_matchers::match(
674 decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU
, Ctx
);
676 EXPECT_EQ(BoundNodes
.size(), 1u);
678 const auto *TA
= BoundNodes
[0].getNodeAs
<TemplateArgumentLoc
>("ta");
680 auto Result
= NodeIntrospection::GetLocations(*TA
);
682 auto ExpectedLocations
=
683 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
685 EXPECT_THAT(ExpectedLocations
,
686 UnorderedElementsAre(STRING_LOCATION_PAIR(TA
, getLocation())));
688 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
690 EXPECT_THAT(ExpectedRanges
,
691 UnorderedElementsAre(STRING_LOCATION_PAIR(TA
, getSourceRange())));
694 TEST(Introspection
, SourceLocations_TA_Pack
) {
695 if (!NodeIntrospection::hasIntrospectionSupport())
697 auto AST
= buildASTFromCodeWithArgs(
699 template<typename... T> class A {};
705 {"-fno-delayed-template-parsing"}, "foo.cpp", "clang-tool",
706 std::make_shared
<PCHContainerOperations
>());
707 auto &Ctx
= AST
->getASTContext();
708 auto &TU
= *Ctx
.getTranslationUnitDecl();
710 auto BoundNodes
= ast_matchers::match(
711 decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU
, Ctx
);
713 EXPECT_EQ(BoundNodes
.size(), 1u);
715 const auto *TA
= BoundNodes
[0].getNodeAs
<TemplateArgumentLoc
>("ta");
717 auto Result
= NodeIntrospection::GetLocations(*TA
);
719 auto ExpectedLocations
=
720 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
723 EXPECT_THAT(ExpectedLocations
,
724 UnorderedElementsAre(
725 STRING_LOCATION_PAIR(TA
, getLocation()),
726 STRING_LOCATION_PAIR(TA
,
727 getTypeSourceInfo()->getTypeLoc().getAs
<clang::BuiltinTypeLoc
>().getBuiltinLoc()),
728 STRING_LOCATION_PAIR(TA
,
729 getTypeSourceInfo()->getTypeLoc().getAs
<clang::BuiltinTypeLoc
>().getNameLoc()),
730 STRING_LOCATION_PAIR(
731 TA
, getTypeSourceInfo()->getTypeLoc().getBeginLoc()),
732 STRING_LOCATION_PAIR(
733 TA
, getTypeSourceInfo()->getTypeLoc().getEndLoc())
737 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
741 UnorderedElementsAre(
742 STRING_LOCATION_PAIR(TA
, getSourceRange()),
743 STRING_LOCATION_PAIR(
744 TA
, getTypeSourceInfo()->getTypeLoc().getSourceRange()),
745 STRING_LOCATION_PAIR(
746 TA
, getTypeSourceInfo()->getTypeLoc().getLocalSourceRange())));
749 TEST(Introspection
, SourceLocations_CXXCtorInitializer_base
) {
750 if (!NodeIntrospection::hasIntrospectionSupport())
753 buildASTFromCode(R
"cpp(
761 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
762 auto &Ctx
= AST
->getASTContext();
763 auto &TU
= *Ctx
.getTranslationUnitDecl();
765 auto BoundNodes
= ast_matchers::match(
766 decl(hasDescendant(cxxConstructorDecl(
767 hasAnyConstructorInitializer(cxxCtorInitializer().bind("init"))))),
770 EXPECT_EQ(BoundNodes
.size(), 1u);
772 const auto *CtorInit
= BoundNodes
[0].getNodeAs
<CXXCtorInitializer
>("init");
774 auto Result
= NodeIntrospection::GetLocations(CtorInit
);
776 auto ExpectedLocations
=
777 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
782 UnorderedElementsAre(
783 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getBeginLoc()),
784 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getEndLoc()),
785 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getBeginLoc()),
786 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getEndLoc()),
787 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getNextTypeLoc().getBeginLoc()),
788 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getNextTypeLoc().getEndLoc()),
789 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc()),
790 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getNextTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc()),
791 STRING_LOCATION_PAIR(CtorInit
, getLParenLoc()),
792 STRING_LOCATION_PAIR(CtorInit
, getRParenLoc()),
793 STRING_LOCATION_PAIR(CtorInit
, getSourceLocation()),
794 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc()),
795 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc()),
796 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getBeginLoc()),
797 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getEndLoc()),
798 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getBeginLoc()),
799 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getEndLoc()),
800 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getBeginLoc()),
801 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getEndLoc())
805 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
810 UnorderedElementsAre(
811 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getNextTypeLoc().getLocalSourceRange()),
812 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getNextTypeLoc().getSourceRange()),
813 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getSourceRange()),
814 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getLocalSourceRange()),
815 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getSourceRange()),
816 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getLocalSourceRange()),
817 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getSourceRange()),
818 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getSourceRange()),
819 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getLocalSourceRange()),
820 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getSourceRange()),
821 STRING_LOCATION_PAIR(CtorInit
, getSourceRange())));
825 TEST(Introspection
, SourceLocations_CXXCtorInitializer_member
) {
826 if (!NodeIntrospection::hasIntrospectionSupport())
829 buildASTFromCode(R
"cpp(
835 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
836 auto &Ctx
= AST
->getASTContext();
837 auto &TU
= *Ctx
.getTranslationUnitDecl();
839 auto BoundNodes
= ast_matchers::match(
840 decl(hasDescendant(cxxConstructorDecl(
841 hasAnyConstructorInitializer(cxxCtorInitializer().bind("init"))))),
844 EXPECT_EQ(BoundNodes
.size(), 1u);
846 const auto *CtorInit
= BoundNodes
[0].getNodeAs
<CXXCtorInitializer
>("init");
848 auto Result
= NodeIntrospection::GetLocations(CtorInit
);
850 auto ExpectedLocations
=
851 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
853 EXPECT_THAT(ExpectedLocations
,
854 UnorderedElementsAre(
855 STRING_LOCATION_PAIR(CtorInit
, getLParenLoc()),
856 STRING_LOCATION_PAIR(CtorInit
, getMemberLocation()),
857 STRING_LOCATION_PAIR(CtorInit
, getRParenLoc()),
858 STRING_LOCATION_PAIR(CtorInit
, getSourceLocation())));
860 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
862 EXPECT_THAT(ExpectedRanges
, UnorderedElementsAre(STRING_LOCATION_PAIR(
863 CtorInit
, getSourceRange())));
866 TEST(Introspection
, SourceLocations_CXXCtorInitializer_ctor
) {
867 if (!NodeIntrospection::hasIntrospectionSupport())
870 buildASTFromCode(R
"cpp(
876 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
877 auto &Ctx
= AST
->getASTContext();
878 auto &TU
= *Ctx
.getTranslationUnitDecl();
880 auto BoundNodes
= ast_matchers::match(
881 decl(hasDescendant(cxxConstructorDecl(
882 hasAnyConstructorInitializer(cxxCtorInitializer().bind("init"))))),
885 EXPECT_EQ(BoundNodes
.size(), 1u);
887 const auto *CtorInit
= BoundNodes
[0].getNodeAs
<CXXCtorInitializer
>("init");
889 auto Result
= NodeIntrospection::GetLocations(CtorInit
);
891 auto ExpectedLocations
=
892 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
897 UnorderedElementsAre(
898 STRING_LOCATION_PAIR(CtorInit
, getLParenLoc()),
899 STRING_LOCATION_PAIR(CtorInit
, getRParenLoc()),
900 STRING_LOCATION_PAIR(CtorInit
, getSourceLocation()),
901 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getBeginLoc()),
902 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getEndLoc()),
903 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getBeginLoc()),
904 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getEndLoc()),
905 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getBeginLoc()),
906 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getEndLoc()),
907 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc()),
908 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc())
912 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
917 UnorderedElementsAre(
918 STRING_LOCATION_PAIR(CtorInit
, getSourceRange()),
919 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getSourceRange()),
920 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getLocalSourceRange()),
921 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getSourceRange()),
922 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getLocalSourceRange()),
923 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getSourceRange())
928 TEST(Introspection
, SourceLocations_CXXCtorInitializer_pack
) {
929 if (!NodeIntrospection::hasIntrospectionSupport())
931 auto AST
= buildASTFromCodeWithArgs(
933 template<typename... T>
937 template<typename... T>
938 struct D : Templ<T...> {
939 D(T... t) : Templ<T>(t)... {}
942 {"-fno-delayed-template-parsing"}, "foo.cpp", "clang-tool",
943 std::make_shared
<PCHContainerOperations
>());
944 auto &Ctx
= AST
->getASTContext();
945 auto &TU
= *Ctx
.getTranslationUnitDecl();
947 auto BoundNodes
= ast_matchers::match(
948 decl(hasDescendant(cxxConstructorDecl(
949 hasAnyConstructorInitializer(cxxCtorInitializer().bind("init"))))),
952 EXPECT_EQ(BoundNodes
.size(), 1u);
954 const auto *CtorInit
= BoundNodes
[0].getNodeAs
<CXXCtorInitializer
>("init");
956 auto Result
= NodeIntrospection::GetLocations(CtorInit
);
958 auto ExpectedLocations
=
959 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
961 llvm::sort(ExpectedLocations
);
965 llvm::makeArrayRef(ExpectedLocations
),
966 (ArrayRef
<std::pair
<std::string
, SourceLocation
>>{
967 STRING_LOCATION_STDPAIR(CtorInit
, getBaseClassLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getLAngleLoc()),
968 STRING_LOCATION_STDPAIR(CtorInit
, getBaseClassLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getRAngleLoc()),
969 STRING_LOCATION_STDPAIR(CtorInit
, getBaseClassLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getTemplateNameLoc()),
970 STRING_LOCATION_STDPAIR(CtorInit
, getBaseClassLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getBeginLoc()),
971 STRING_LOCATION_STDPAIR(CtorInit
, getBaseClassLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getEndLoc()),
972 STRING_LOCATION_STDPAIR(CtorInit
, getBaseClassLoc().getBeginLoc()),
973 STRING_LOCATION_STDPAIR(CtorInit
, getBaseClassLoc().getEndLoc()),
974 STRING_LOCATION_STDPAIR(CtorInit
, getBaseClassLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getLAngleLoc()),
975 STRING_LOCATION_STDPAIR(CtorInit
, getBaseClassLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getRAngleLoc()),
976 STRING_LOCATION_STDPAIR(CtorInit
, getBaseClassLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getTemplateNameLoc()),
977 STRING_LOCATION_STDPAIR(CtorInit
, getBaseClassLoc().getNextTypeLoc().getBeginLoc()),
978 STRING_LOCATION_STDPAIR(CtorInit
, getBaseClassLoc().getNextTypeLoc().getEndLoc()),
979 STRING_LOCATION_STDPAIR(CtorInit
, getEllipsisLoc()),
980 STRING_LOCATION_STDPAIR(CtorInit
, getLParenLoc()),
981 STRING_LOCATION_STDPAIR(CtorInit
, getMemberLocation()),
982 STRING_LOCATION_STDPAIR(CtorInit
, getRParenLoc()),
983 STRING_LOCATION_STDPAIR(CtorInit
, getSourceLocation()),
984 STRING_LOCATION_STDPAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getLAngleLoc()),
985 STRING_LOCATION_STDPAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getRAngleLoc()),
986 STRING_LOCATION_STDPAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getTemplateNameLoc()),
987 STRING_LOCATION_STDPAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getBeginLoc()),
988 STRING_LOCATION_STDPAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getEndLoc()),
989 STRING_LOCATION_STDPAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getBeginLoc()),
990 STRING_LOCATION_STDPAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getEndLoc()),
991 STRING_LOCATION_STDPAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getLAngleLoc()),
992 STRING_LOCATION_STDPAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getRAngleLoc()),
993 STRING_LOCATION_STDPAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getTemplateNameLoc()),
994 STRING_LOCATION_STDPAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getBeginLoc()),
995 STRING_LOCATION_STDPAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getEndLoc())
999 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1004 UnorderedElementsAre(
1005 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getLocalSourceRange()),
1006 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getLocalSourceRange()),
1007 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getNextTypeLoc().getLocalSourceRange()),
1008 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getLocalSourceRange()),
1009 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getSourceRange()),
1010 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getSourceRange()),
1011 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getSourceRange()),
1012 STRING_LOCATION_PAIR(CtorInit
, getBaseClassLoc().getNextTypeLoc().getSourceRange()),
1013 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getSourceRange()),
1014 STRING_LOCATION_PAIR(CtorInit
, getTypeSourceInfo()->getTypeLoc().getSourceRange()),
1015 STRING_LOCATION_PAIR(CtorInit
, getSourceRange())
1020 TEST(Introspection
, SourceLocations_CXXBaseSpecifier_plain
) {
1021 if (!NodeIntrospection::hasIntrospectionSupport())
1024 buildASTFromCode(R
"cpp(
1028 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
1029 auto &Ctx
= AST
->getASTContext();
1030 auto &TU
= *Ctx
.getTranslationUnitDecl();
1032 auto BoundNodes
= ast_matchers::match(
1033 decl(hasDescendant(cxxRecordDecl(hasDirectBase(
1034 cxxBaseSpecifier(hasType(asString("A"))).bind("base"))))),
1037 EXPECT_EQ(BoundNodes
.size(), 1u);
1039 const auto *Base
= BoundNodes
[0].getNodeAs
<CXXBaseSpecifier
>("base");
1041 auto Result
= NodeIntrospection::GetLocations(Base
);
1043 auto ExpectedLocations
=
1044 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
1047 EXPECT_THAT(ExpectedLocations
,
1048 UnorderedElementsAre(
1049 STRING_LOCATION_PAIR(Base
, getBaseTypeLoc()),
1050 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getBeginLoc()),
1051 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getBeginLoc()),
1052 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getBeginLoc()),
1053 STRING_LOCATION_PAIR(Base
, getBeginLoc()),
1054 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getEndLoc()),
1055 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getEndLoc()),
1056 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getEndLoc()),
1057 STRING_LOCATION_PAIR(Base
, getEndLoc()),
1058 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc()),
1059 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc())
1063 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1066 EXPECT_THAT(ExpectedRanges
, UnorderedElementsAre(
1067 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getLocalSourceRange()),
1068 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getLocalSourceRange()),
1069 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getSourceRange()),
1070 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getSourceRange()),
1071 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getSourceRange()),
1072 STRING_LOCATION_PAIR(Base
, getSourceRange())
1077 TEST(Introspection
, SourceLocations_CXXBaseSpecifier_accessspec
) {
1078 if (!NodeIntrospection::hasIntrospectionSupport())
1081 buildASTFromCode(R
"cpp(
1083 class B : public A {};
1085 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
1086 auto &Ctx
= AST
->getASTContext();
1087 auto &TU
= *Ctx
.getTranslationUnitDecl();
1089 auto BoundNodes
= ast_matchers::match(
1090 decl(hasDescendant(cxxRecordDecl(hasDirectBase(
1091 cxxBaseSpecifier(hasType(asString("A"))).bind("base"))))),
1094 EXPECT_EQ(BoundNodes
.size(), 1u);
1096 const auto *Base
= BoundNodes
[0].getNodeAs
<CXXBaseSpecifier
>("base");
1098 auto Result
= NodeIntrospection::GetLocations(Base
);
1100 auto ExpectedLocations
=
1101 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
1104 EXPECT_THAT(ExpectedLocations
,
1105 UnorderedElementsAre(
1106 STRING_LOCATION_PAIR(Base
, getBeginLoc()),
1107 STRING_LOCATION_PAIR(Base
, getBaseTypeLoc()),
1108 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getBeginLoc()),
1109 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getBeginLoc()),
1110 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getBeginLoc()),
1111 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getEndLoc()),
1112 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getEndLoc()),
1113 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getEndLoc()),
1114 STRING_LOCATION_PAIR(Base
, getEndLoc()),
1115 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc()),
1116 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc())
1120 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1123 EXPECT_THAT(ExpectedRanges
, UnorderedElementsAre(
1124 STRING_LOCATION_PAIR(Base
, getSourceRange()),
1125 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getLocalSourceRange()),
1126 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getLocalSourceRange()),
1127 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getSourceRange()),
1128 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getSourceRange()),
1129 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getSourceRange())
1134 TEST(Introspection
, SourceLocations_CXXBaseSpecifier_virtual
) {
1135 if (!NodeIntrospection::hasIntrospectionSupport())
1138 buildASTFromCode(R
"cpp(
1141 class C : virtual B, A {};
1143 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
1144 auto &Ctx
= AST
->getASTContext();
1145 auto &TU
= *Ctx
.getTranslationUnitDecl();
1147 auto BoundNodes
= ast_matchers::match(
1148 decl(hasDescendant(cxxRecordDecl(hasDirectBase(
1149 cxxBaseSpecifier(hasType(asString("A"))).bind("base"))))),
1152 EXPECT_EQ(BoundNodes
.size(), 1u);
1154 const auto *Base
= BoundNodes
[0].getNodeAs
<CXXBaseSpecifier
>("base");
1156 auto Result
= NodeIntrospection::GetLocations(Base
);
1158 auto ExpectedLocations
=
1159 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
1162 EXPECT_THAT(ExpectedLocations
,
1163 UnorderedElementsAre(
1164 STRING_LOCATION_PAIR(Base
, getBaseTypeLoc()),
1165 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getBeginLoc()),
1166 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getBeginLoc()),
1167 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getBeginLoc()),
1168 STRING_LOCATION_PAIR(Base
, getBeginLoc()),
1169 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getEndLoc()),
1170 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getEndLoc()),
1171 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getEndLoc()),
1172 STRING_LOCATION_PAIR(Base
, getEndLoc()),
1173 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc()),
1174 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc())
1178 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1181 EXPECT_THAT(ExpectedRanges
, UnorderedElementsAre(
1182 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getLocalSourceRange()),
1183 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getLocalSourceRange()),
1184 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getSourceRange()),
1185 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getSourceRange()),
1186 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getSourceRange()),
1187 STRING_LOCATION_PAIR(Base
, getSourceRange())
1192 TEST(Introspection
, SourceLocations_CXXBaseSpecifier_template_base
) {
1193 if (!NodeIntrospection::hasIntrospectionSupport())
1196 buildASTFromCode(R
"cpp(
1197 template<typename T, typename U>
1199 class B : A<int, bool> {};
1201 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
1202 auto &Ctx
= AST
->getASTContext();
1203 auto &TU
= *Ctx
.getTranslationUnitDecl();
1206 ast_matchers::match(decl(hasDescendant(cxxRecordDecl(
1207 hasDirectBase(cxxBaseSpecifier().bind("base"))))),
1210 EXPECT_EQ(BoundNodes
.size(), 1u);
1212 const auto *Base
= BoundNodes
[0].getNodeAs
<CXXBaseSpecifier
>("base");
1214 auto Result
= NodeIntrospection::GetLocations(Base
);
1216 auto ExpectedLocations
=
1217 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
1220 EXPECT_THAT(ExpectedLocations
,
1221 UnorderedElementsAre(
1222 STRING_LOCATION_PAIR(Base
, getBaseTypeLoc()),
1223 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getBeginLoc()),
1224 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getBeginLoc()),
1225 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getBeginLoc()),
1226 STRING_LOCATION_PAIR(Base
, getBeginLoc()),
1227 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getTemplateNameLoc()),
1228 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getTemplateNameLoc()),
1229 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getLAngleLoc()),
1230 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getLAngleLoc()),
1231 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getEndLoc()),
1232 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getEndLoc()),
1233 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getEndLoc()),
1234 STRING_LOCATION_PAIR(Base
, getEndLoc()),
1235 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getRAngleLoc()),
1236 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getAs
<clang::TemplateSpecializationTypeLoc
>().getRAngleLoc())
1240 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1243 EXPECT_THAT(ExpectedRanges
, UnorderedElementsAre(
1244 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getLocalSourceRange()),
1245 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getLocalSourceRange()),
1246 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::ElaboratedTypeLoc
>().getNamedTypeLoc().getSourceRange()),
1247 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getNextTypeLoc().getSourceRange()),
1248 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getSourceRange()),
1249 STRING_LOCATION_PAIR(Base
, getSourceRange())
1254 TEST(Introspection
, SourceLocations_CXXBaseSpecifier_pack
) {
1255 if (!NodeIntrospection::hasIntrospectionSupport())
1257 auto AST
= buildASTFromCodeWithArgs(
1259 template<typename... T>
1260 struct Templ : T... {
1263 {"-fno-delayed-template-parsing"}, "foo.cpp", "clang-tool",
1264 std::make_shared
<PCHContainerOperations
>());
1265 auto &Ctx
= AST
->getASTContext();
1266 auto &TU
= *Ctx
.getTranslationUnitDecl();
1269 ast_matchers::match(decl(hasDescendant(cxxRecordDecl(
1270 hasDirectBase(cxxBaseSpecifier().bind("base"))))),
1273 EXPECT_EQ(BoundNodes
.size(), 1u);
1275 const auto *Base
= BoundNodes
[0].getNodeAs
<CXXBaseSpecifier
>("base");
1277 auto Result
= NodeIntrospection::GetLocations(Base
);
1279 auto ExpectedLocations
=
1280 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
1283 EXPECT_THAT(ExpectedLocations
,
1284 UnorderedElementsAre(
1285 STRING_LOCATION_PAIR(Base
, getBaseTypeLoc()),
1286 STRING_LOCATION_PAIR(Base
, getEllipsisLoc()),
1287 STRING_LOCATION_PAIR(Base
, getBeginLoc()),
1288 STRING_LOCATION_PAIR(Base
, getEndLoc()),
1289 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getEndLoc()),
1290 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc()),
1291 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getBeginLoc())
1295 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1298 EXPECT_THAT(ExpectedRanges
, UnorderedElementsAre(
1299 STRING_LOCATION_PAIR(Base
, getSourceRange()),
1300 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getSourceRange()),
1301 STRING_LOCATION_PAIR(Base
, getTypeSourceInfo()->getTypeLoc().getLocalSourceRange())
1306 TEST(Introspection
, SourceLocations_FunctionProtoTypeLoc
) {
1307 if (!NodeIntrospection::hasIntrospectionSupport())
1310 buildASTFromCode(R
"cpp(
1313 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
1314 auto &Ctx
= AST
->getASTContext();
1315 auto &TU
= *Ctx
.getTranslationUnitDecl();
1317 auto BoundNodes
= ast_matchers::match(
1318 decl(hasDescendant(loc(functionProtoType()).bind("tl"))), TU
, Ctx
);
1320 EXPECT_EQ(BoundNodes
.size(), 1u);
1322 const auto *TL
= BoundNodes
[0].getNodeAs
<TypeLoc
>("tl");
1323 auto Result
= NodeIntrospection::GetLocations(*TL
);
1325 auto ExpectedLocations
=
1326 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
1328 llvm::sort(ExpectedLocations
);
1332 llvm::makeArrayRef(ExpectedLocations
),
1333 (ArrayRef
<std::pair
<std::string
, SourceLocation
>>{
1334 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::FunctionTypeLoc
>().getLParenLoc()),
1335 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::FunctionTypeLoc
>().getLocalRangeBegin()),
1336 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::FunctionTypeLoc
>().getLocalRangeEnd()),
1337 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::FunctionTypeLoc
>().getRParenLoc()),
1338 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::BuiltinTypeLoc
>().getBuiltinLoc()),
1339 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getAs
<clang::BuiltinTypeLoc
>().getNameLoc()),
1340 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getBeginLoc()),
1341 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getEndLoc()),
1342 STRING_LOCATION_STDPAIR(TL
, getBeginLoc()),
1343 STRING_LOCATION_STDPAIR(TL
, getEndLoc()),
1344 STRING_LOCATION_STDPAIR(TL
, getNextTypeLoc().getAs
<clang::BuiltinTypeLoc
>().getBuiltinLoc()),
1345 STRING_LOCATION_STDPAIR(TL
, getNextTypeLoc().getAs
<clang::BuiltinTypeLoc
>().getNameLoc()),
1346 STRING_LOCATION_STDPAIR(TL
, getNextTypeLoc().getBeginLoc()),
1347 STRING_LOCATION_STDPAIR(TL
, getNextTypeLoc().getEndLoc())
1351 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1356 UnorderedElementsAre(
1357 STRING_LOCATION_PAIR(TL
, getAs
<clang::FunctionTypeLoc
>().getParensRange()),
1358 STRING_LOCATION_PAIR(TL
, getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getLocalSourceRange()),
1359 STRING_LOCATION_PAIR(TL
, getAs
<clang::FunctionTypeLoc
>().getReturnLoc().getSourceRange()),
1360 STRING_LOCATION_PAIR(TL
, getLocalSourceRange()),
1361 STRING_LOCATION_PAIR(TL
, getNextTypeLoc().getLocalSourceRange()),
1362 STRING_LOCATION_PAIR(TL
, getNextTypeLoc().getSourceRange()),
1363 STRING_LOCATION_PAIR(TL
, getSourceRange())
1368 TEST(Introspection
, SourceLocations_PointerTypeLoc
) {
1369 if (!NodeIntrospection::hasIntrospectionSupport())
1372 buildASTFromCode(R
"cpp(
1375 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
1376 auto &Ctx
= AST
->getASTContext();
1377 auto &TU
= *Ctx
.getTranslationUnitDecl();
1379 auto BoundNodes
= ast_matchers::match(
1381 varDecl(hasName("i"), hasDescendant(loc(pointerType()).bind("tl"))))),
1384 EXPECT_EQ(BoundNodes
.size(), 1u);
1386 const auto *TL
= BoundNodes
[0].getNodeAs
<TypeLoc
>("tl");
1387 auto Result
= NodeIntrospection::GetLocations(*TL
);
1389 auto ExpectedLocations
=
1390 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
1392 llvm::sort(ExpectedLocations
);
1396 llvm::makeArrayRef(ExpectedLocations
),
1397 (ArrayRef
<std::pair
<std::string
, SourceLocation
>>{
1398 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::PointerTypeLoc
>().getPointeeLoc().getAs
<clang::BuiltinTypeLoc
>().getBuiltinLoc()),
1399 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::PointerTypeLoc
>().getPointeeLoc().getAs
<clang::BuiltinTypeLoc
>().getNameLoc()),
1400 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::PointerTypeLoc
>().getPointeeLoc().getBeginLoc()),
1401 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::PointerTypeLoc
>().getPointeeLoc().getEndLoc()),
1402 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::PointerTypeLoc
>().getSigilLoc()),
1403 STRING_LOCATION_STDPAIR(TL
, getAs
<clang::PointerTypeLoc
>().getStarLoc()),
1404 STRING_LOCATION_STDPAIR(TL
, getBeginLoc()),
1405 STRING_LOCATION_STDPAIR(TL
, getEndLoc()),
1406 STRING_LOCATION_STDPAIR(TL
, getNextTypeLoc().getAs
<clang::BuiltinTypeLoc
>().getBuiltinLoc()),
1407 STRING_LOCATION_STDPAIR(TL
, getNextTypeLoc().getAs
<clang::BuiltinTypeLoc
>().getNameLoc()),
1408 STRING_LOCATION_STDPAIR(TL
, getNextTypeLoc().getBeginLoc()),
1409 STRING_LOCATION_STDPAIR(TL
, getNextTypeLoc().getEndLoc())
1413 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1418 UnorderedElementsAre(
1419 STRING_LOCATION_PAIR(TL
, getAs
<clang::PointerTypeLoc
>().getPointeeLoc().getLocalSourceRange()),
1420 STRING_LOCATION_PAIR(TL
, getAs
<clang::PointerTypeLoc
>().getPointeeLoc().getSourceRange()),
1421 STRING_LOCATION_PAIR(TL
, getLocalSourceRange()),
1422 STRING_LOCATION_PAIR(TL
, getNextTypeLoc().getLocalSourceRange()),
1423 STRING_LOCATION_PAIR(TL
, getNextTypeLoc().getSourceRange()),
1424 STRING_LOCATION_PAIR(TL
, getSourceRange())
1430 // This test doesn't work on windows due to use of the typeof extension.
1431 TEST(Introspection
, SourceLocations_TypeOfTypeLoc
) {
1432 if (!NodeIntrospection::hasIntrospectionSupport())
1435 buildASTFromCode(R
"cpp(
1436 typeof (static_cast<void *>(0)) i;
1438 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
1439 auto &Ctx
= AST
->getASTContext();
1440 auto &TU
= *Ctx
.getTranslationUnitDecl();
1442 auto BoundNodes
= ast_matchers::match(
1444 varDecl(hasName("i"), hasDescendant(loc(type()).bind("tl"))))),
1447 EXPECT_EQ(BoundNodes
.size(), 1u);
1449 const auto *TL
= BoundNodes
[0].getNodeAs
<TypeLoc
>("tl");
1450 auto Result
= NodeIntrospection::GetLocations(*TL
);
1452 auto ExpectedLocations
=
1453 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
1455 EXPECT_THAT(ExpectedLocations
,
1456 UnorderedElementsAre(
1457 STRING_LOCATION_PAIR(TL
, getBeginLoc()),
1458 STRING_LOCATION_PAIR(TL
, getEndLoc()),
1459 STRING_LOCATION_PAIR(
1460 TL
, getAs
<clang::TypeOfExprTypeLoc
>().getTypeofLoc()),
1461 STRING_LOCATION_PAIR(
1462 TL
, getAs
<clang::TypeOfExprTypeLoc
>().getLParenLoc()),
1463 STRING_LOCATION_PAIR(
1464 TL
, getAs
<clang::TypeOfExprTypeLoc
>().getRParenLoc())));
1466 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1468 EXPECT_THAT(ExpectedRanges
,
1469 UnorderedElementsAre(
1470 STRING_LOCATION_PAIR(TL
, getLocalSourceRange()),
1471 STRING_LOCATION_PAIR(TL
, getSourceRange()),
1472 STRING_LOCATION_PAIR(
1473 TL
, getAs
<clang::TypeOfExprTypeLoc
>().getParensRange())));
1477 TEST(Introspection
, SourceLocations_DeclarationNameInfo_Dtor
) {
1478 if (!NodeIntrospection::hasIntrospectionSupport())
1481 buildASTFromCode(R
"cpp(
1487 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
1488 auto &Ctx
= AST
->getASTContext();
1489 auto &TU
= *Ctx
.getTranslationUnitDecl();
1491 auto BoundNodes
= ast_matchers::match(
1492 decl(hasDescendant(cxxDestructorDecl(hasName("~Foo")).bind("dtor"))), TU
,
1495 EXPECT_EQ(BoundNodes
.size(), 1u);
1497 const auto *Dtor
= BoundNodes
[0].getNodeAs
<CXXDestructorDecl
>("dtor");
1498 auto NI
= Dtor
->getNameInfo();
1499 auto Result
= NodeIntrospection::GetLocations(NI
);
1501 auto ExpectedLocations
=
1502 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
1504 llvm::sort(ExpectedLocations
);
1508 llvm::makeArrayRef(ExpectedLocations
),
1509 (ArrayRef
<std::pair
<std::string
, SourceLocation
>>{
1510 STRING_LOCATION_STDPAIR((&NI
), getBeginLoc()),
1511 STRING_LOCATION_STDPAIR((&NI
), getEndLoc()),
1512 STRING_LOCATION_STDPAIR((&NI
), getLoc()),
1513 STRING_LOCATION_STDPAIR((&NI
),
1514 getNamedTypeInfo()->getTypeLoc().getAs
<clang::TypeSpecTypeLoc
>().getNameLoc()),
1515 STRING_LOCATION_STDPAIR(
1516 (&NI
), getNamedTypeInfo()->getTypeLoc().getBeginLoc()),
1517 STRING_LOCATION_STDPAIR(
1518 (&NI
), getNamedTypeInfo()->getTypeLoc().getEndLoc())}));
1521 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1525 UnorderedElementsAre(
1526 STRING_LOCATION_PAIR(
1527 (&NI
), getNamedTypeInfo()->getTypeLoc().getLocalSourceRange()),
1528 STRING_LOCATION_PAIR(
1529 (&NI
), getNamedTypeInfo()->getTypeLoc().getSourceRange()),
1530 STRING_LOCATION_PAIR((&NI
), getSourceRange())));
1533 TEST(Introspection
, SourceLocations_DeclarationNameInfo_CRef
) {
1534 if (!NodeIntrospection::hasIntrospectionSupport())
1537 auto AST
= buildASTFromCodeWithArgs(
1539 template<typename T>
1542 template <typename U>
1546 template<typename T>
1550 mc.template pushBack<int>();
1553 {"-fno-delayed-template-parsing"}, "foo.cpp", "clang-tool",
1554 std::make_shared
<PCHContainerOperations
>());
1556 auto &Ctx
= AST
->getASTContext();
1557 auto &TU
= *Ctx
.getTranslationUnitDecl();
1559 auto BoundNodes
= ast_matchers::match(
1560 decl(hasDescendant(cxxDependentScopeMemberExpr(hasMemberName("pushBack")).bind("member"))), TU
,
1563 EXPECT_EQ(BoundNodes
.size(), 1u);
1565 const auto *Member
= BoundNodes
[0].getNodeAs
<CXXDependentScopeMemberExpr
>("member");
1566 auto Result
= NodeIntrospection::GetLocations(Member
);
1568 auto ExpectedLocations
=
1569 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
1571 llvm::sort(ExpectedLocations
);
1574 llvm::makeArrayRef(ExpectedLocations
),
1575 (ArrayRef
<std::pair
<std::string
, SourceLocation
>>{
1576 STRING_LOCATION_STDPAIR(Member
, getBeginLoc()),
1577 STRING_LOCATION_STDPAIR(Member
, getEndLoc()),
1578 STRING_LOCATION_STDPAIR(Member
, getExprLoc()),
1579 STRING_LOCATION_STDPAIR(Member
, getLAngleLoc()),
1580 STRING_LOCATION_STDPAIR(Member
, getMemberLoc()),
1581 STRING_LOCATION_STDPAIR(Member
, getMemberNameInfo().getBeginLoc()),
1582 STRING_LOCATION_STDPAIR(Member
, getMemberNameInfo().getEndLoc()),
1583 STRING_LOCATION_STDPAIR(Member
, getMemberNameInfo().getLoc()),
1584 STRING_LOCATION_STDPAIR(Member
, getOperatorLoc()),
1585 STRING_LOCATION_STDPAIR(Member
, getRAngleLoc()),
1586 STRING_LOCATION_STDPAIR(Member
, getTemplateKeywordLoc())
1589 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1593 UnorderedElementsAre(
1594 STRING_LOCATION_PAIR(Member
, getMemberNameInfo().getSourceRange()),
1595 STRING_LOCATION_PAIR(Member
, getSourceRange())
1599 TEST(Introspection
, SourceLocations_DeclarationNameInfo_ConvOp
) {
1600 if (!NodeIntrospection::hasIntrospectionSupport())
1603 buildASTFromCode(R
"cpp(
1606 bool operator==(const Foo&) const { return false; }
1609 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
1610 auto &Ctx
= AST
->getASTContext();
1611 auto &TU
= *Ctx
.getTranslationUnitDecl();
1613 auto BoundNodes
= ast_matchers::match(
1614 decl(hasDescendant(cxxMethodDecl().bind("opeq"))), TU
, Ctx
);
1616 EXPECT_EQ(BoundNodes
.size(), 1u);
1618 const auto *Opeq
= BoundNodes
[0].getNodeAs
<CXXMethodDecl
>("opeq");
1619 auto NI
= Opeq
->getNameInfo();
1620 auto Result
= NodeIntrospection::GetLocations(NI
);
1622 auto ExpectedLocations
=
1623 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
1625 llvm::sort(ExpectedLocations
);
1627 EXPECT_EQ(llvm::makeArrayRef(ExpectedLocations
),
1628 (ArrayRef
<std::pair
<std::string
, SourceLocation
>>{
1629 STRING_LOCATION_STDPAIR((&NI
), getBeginLoc()),
1630 STRING_LOCATION_STDPAIR((&NI
), getEndLoc()),
1631 STRING_LOCATION_STDPAIR((&NI
), getLoc())}));
1633 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1635 EXPECT_THAT(ExpectedRanges
,
1636 UnorderedElementsAre(
1637 STRING_LOCATION_PAIR((&NI
), getSourceRange()),
1638 STRING_LOCATION_PAIR((&NI
), getCXXOperatorNameRange())));
1641 TEST(Introspection
, SourceLocations_DeclarationNameInfo_LitOp
) {
1642 if (!NodeIntrospection::hasIntrospectionSupport())
1645 buildASTFromCode(R
"cpp(
1646 long double operator"" _identity ( long double val )
1651 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
1652 auto &Ctx
= AST
->getASTContext();
1653 auto &TU
= *Ctx
.getTranslationUnitDecl();
1655 auto BoundNodes
= ast_matchers::match(
1656 decl(hasDescendant(functionDecl().bind("litop"))), TU
, Ctx
);
1658 EXPECT_EQ(BoundNodes
.size(), 1u);
1660 const auto *LitOp
= BoundNodes
[0].getNodeAs
<FunctionDecl
>("litop");
1661 auto NI
= LitOp
->getNameInfo();
1662 auto Result
= NodeIntrospection::GetLocations(NI
);
1664 auto ExpectedLocations
=
1665 FormatExpected
<SourceLocation
>(Result
.LocationAccessors
);
1667 llvm::sort(ExpectedLocations
);
1669 EXPECT_EQ(llvm::makeArrayRef(ExpectedLocations
),
1670 (ArrayRef
<std::pair
<std::string
, SourceLocation
>>{
1671 STRING_LOCATION_STDPAIR((&NI
), getBeginLoc()),
1672 STRING_LOCATION_STDPAIR((&NI
), getCXXLiteralOperatorNameLoc()),
1673 STRING_LOCATION_STDPAIR((&NI
), getEndLoc()),
1674 STRING_LOCATION_STDPAIR((&NI
), getLoc())}));
1676 auto ExpectedRanges
= FormatExpected
<SourceRange
>(Result
.RangeAccessors
);
1678 EXPECT_THAT(ExpectedRanges
, UnorderedElementsAre(STRING_LOCATION_PAIR(
1679 (&NI
), getSourceRange())));