1//===- unittests/AST/DeclTest.cpp --- Declaration tests -------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Unit tests for Decl nodes in the AST.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/Decl.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/Mangle.h"
17#include "clang/ASTMatchers/ASTMatchFinder.h"
18#include "clang/ASTMatchers/ASTMatchers.h"
19#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/LLVM.h"
21#include "clang/Basic/TargetInfo.h"
22#include "clang/Lex/Lexer.h"
23#include "clang/Tooling/Tooling.h"
24#include "llvm/IR/DataLayout.h"
25#include "llvm/Testing/Annotations/Annotations.h"
26#include "gtest/gtest.h"
27
28using namespace clang::ast_matchers;
29using namespace clang::tooling;
30using namespace clang;
31
32TEST(Decl, CleansUpAPValues) {
33 MatchFinder Finder;
34 std::unique_ptr<FrontendActionFactory> Factory(
35 newFrontendActionFactory(ConsumerFactory: &Finder));
36
37 // This is a regression test for a memory leak in APValues for structs that
38 // allocate memory. This test only fails if run under valgrind with full leak
39 // checking enabled.
40 std::vector<std::string> Args(1, "-std=c++11");
41 Args.push_back(x: "-fno-ms-extensions");
42 ASSERT_TRUE(runToolOnCodeWithArgs(
43 Factory->create(),
44 "struct X { int a; }; constexpr X x = { 42 };"
45 "union Y { constexpr Y(int a) : a(a) {} int a; }; constexpr Y y = { 42 };"
46 "constexpr int z[2] = { 42, 43 };"
47 "constexpr int __attribute__((vector_size(16))) v1 = {};"
48 "\n#ifdef __SIZEOF_INT128__\n"
49 "constexpr __uint128_t large_int = 0xffffffffffffffff;"
50 "constexpr __uint128_t small_int = 1;"
51 "\n#endif\n"
52 "constexpr double d1 = 42.42;"
53 "constexpr long double d2 = 42.42;"
54 "constexpr _Complex long double c1 = 42.0i;"
55 "constexpr _Complex long double c2 = 42.0;"
56 "template<int N> struct A : A<N-1> {};"
57 "template<> struct A<0> { int n; }; A<50> a;"
58 "constexpr int &r = a.n;"
59 "constexpr int A<50>::*p = &A<50>::n;"
60 "void f() { foo: bar: constexpr int k = __builtin_constant_p(0) ?"
61 " (char*)&&foo - (char*)&&bar : 0; }",
62 Args));
63
64 // FIXME: Once this test starts breaking we can test APValue::needsCleanup
65 // for ComplexInt.
66 ASSERT_FALSE(runToolOnCodeWithArgs(
67 Factory->create(),
68 "constexpr _Complex __uint128_t c = 0xffffffffffffffff;",
69 Args));
70}
71
72TEST(Decl, AsmLabelAttr) {
73 // Create two method decls: `f` and `g`.
74 StringRef Code = R"(
75 struct S {
76 void f() {}
77 void g() {}
78 };
79 )";
80 auto AST =
81 tooling::buildASTFromCodeWithArgs(Code, Args: {"-target", "i386-apple-darwin"});
82 ASTContext &Ctx = AST->getASTContext();
83 assert(Ctx.getTargetInfo().getUserLabelPrefix() == StringRef("_") &&
84 "Expected target to have a global prefix");
85 DiagnosticsEngine &Diags = AST->getDiagnostics();
86
87 const auto *DeclS =
88 selectFirst<CXXRecordDecl>(BoundTo: "d", Results: match(Matcher: cxxRecordDecl().bind(ID: "d"), Context&: Ctx));
89 NamedDecl *DeclF = *DeclS->method_begin();
90 NamedDecl *DeclG = *(++DeclS->method_begin());
91
92 // Attach asm labels to the decls: one literal, and one not.
93 DeclF->addAttr(AsmLabelAttr::Create(Ctx, "foo", /*LiteralLabel=*/true));
94 DeclG->addAttr(AsmLabelAttr::Create(Ctx, "goo", /*LiteralLabel=*/false));
95
96 // Mangle the decl names.
97 std::string MangleF, MangleG;
98 std::unique_ptr<ItaniumMangleContext> MC(
99 ItaniumMangleContext::create(Context&: Ctx, Diags));
100 {
101 llvm::raw_string_ostream OS_F(MangleF);
102 llvm::raw_string_ostream OS_G(MangleG);
103 MC->mangleName(GD: DeclF, OS_F);
104 MC->mangleName(GD: DeclG, OS_G);
105 }
106
107 ASSERT_EQ(MangleF, "\x01"
108 "foo");
109 ASSERT_EQ(MangleG, "goo");
110}
111
112TEST(Decl, MangleDependentSizedArray) {
113 StringRef Code = R"(
114 template <int ...N>
115 int A[] = {N...};
116
117 template <typename T, int N>
118 struct S {
119 T B[N];
120 };
121 )";
122 auto AST =
123 tooling::buildASTFromCodeWithArgs(Code, Args: {"-target", "i386-apple-darwin"});
124 ASTContext &Ctx = AST->getASTContext();
125 assert(Ctx.getTargetInfo().getUserLabelPrefix() == StringRef("_") &&
126 "Expected target to have a global prefix");
127 DiagnosticsEngine &Diags = AST->getDiagnostics();
128
129 const auto *DeclA =
130 selectFirst<VarDecl>(BoundTo: "A", Results: match(Matcher: varDecl().bind(ID: "A"), Context&: Ctx));
131 const auto *DeclB =
132 selectFirst<FieldDecl>(BoundTo: "B", Results: match(Matcher: fieldDecl().bind(ID: "B"), Context&: Ctx));
133
134 std::string MangleA, MangleB;
135 llvm::raw_string_ostream OS_A(MangleA), OS_B(MangleB);
136 std::unique_ptr<ItaniumMangleContext> MC(
137 ItaniumMangleContext::create(Context&: Ctx, Diags));
138
139 MC->mangleCanonicalTypeName(T: DeclA->getType(), OS_A);
140 MC->mangleCanonicalTypeName(T: DeclB->getType(), OS_B);
141
142 ASSERT_EQ(MangleA, "_ZTSA_i");
143 ASSERT_EQ(MangleB, "_ZTSAT0__T_");
144}
145
146TEST(Decl, ConceptDecl) {
147 llvm::StringRef Code(R"(
148 template<class T>
149 concept integral = __is_integral(T);
150 )");
151
152 auto AST = tooling::buildASTFromCodeWithArgs(Code, Args: {"-std=c++20"});
153 ASTContext &Ctx = AST->getASTContext();
154
155 const auto *Decl =
156 selectFirst<ConceptDecl>(BoundTo: "decl", Results: match(Matcher: conceptDecl().bind(ID: "decl"), Context&: Ctx));
157 ASSERT_TRUE(Decl != nullptr);
158 EXPECT_EQ(Decl->getName(), "integral");
159}
160
161TEST(Decl, EnumDeclRange) {
162 llvm::Annotations Code(R"(
163 typedef int Foo;
164 [[enum Bar : Foo]];)");
165 auto AST = tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{});
166 ASTContext &Ctx = AST->getASTContext();
167 const auto &SM = Ctx.getSourceManager();
168
169 const auto *Bar =
170 selectFirst<TagDecl>(BoundTo: "Bar", Results: match(Matcher: enumDecl().bind(ID: "Bar"), Context&: Ctx));
171 auto BarRange =
172 Lexer::getAsCharRange(Range: Bar->getSourceRange(), SM, LangOpts: Ctx.getLangOpts());
173 EXPECT_EQ(SM.getFileOffset(BarRange.getBegin()), Code.range().Begin);
174 EXPECT_EQ(SM.getFileOffset(BarRange.getEnd()), Code.range().End);
175}
176
177TEST(Decl, IsInExportDeclContext) {
178 llvm::Annotations Code(R"(
179 export module m;
180 export template <class T>
181 void f() {})");
182 auto AST =
183 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
184 ASTContext &Ctx = AST->getASTContext();
185
186 const auto *f =
187 selectFirst<FunctionDecl>(BoundTo: "f", Results: match(Matcher: functionDecl().bind(ID: "f"), Context&: Ctx));
188 EXPECT_TRUE(f->isInExportDeclContext());
189}
190
191TEST(Decl, InConsistLinkageForTemplates) {
192 llvm::Annotations Code(R"(
193 export module m;
194 export template <class T>
195 void f() {}
196
197 template <>
198 void f<int>() {}
199
200 export template <class T>
201 class C {};
202
203 template<>
204 class C<int> {};
205 )");
206
207 auto AST =
208 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
209 ASTContext &Ctx = AST->getASTContext();
210
211 llvm::SmallVector<ast_matchers::BoundNodes, 2> Funcs =
212 match(Matcher: functionDecl().bind(ID: "f"), Context&: Ctx);
213
214 EXPECT_EQ(Funcs.size(), 2U);
215 const FunctionDecl *TemplateF = Funcs[0].getNodeAs<FunctionDecl>(ID: "f");
216 const FunctionDecl *SpecializedF = Funcs[1].getNodeAs<FunctionDecl>(ID: "f");
217 EXPECT_EQ(TemplateF->getLinkageInternal(),
218 SpecializedF->getLinkageInternal());
219
220 llvm::SmallVector<ast_matchers::BoundNodes, 1> ClassTemplates =
221 match(Matcher: classTemplateDecl().bind(ID: "C"), Context&: Ctx);
222 llvm::SmallVector<ast_matchers::BoundNodes, 1> ClassSpecializations =
223 match(Matcher: classTemplateSpecializationDecl().bind(ID: "C"), Context&: Ctx);
224
225 EXPECT_EQ(ClassTemplates.size(), 1U);
226 EXPECT_EQ(ClassSpecializations.size(), 1U);
227 const NamedDecl *TemplatedC = ClassTemplates[0].getNodeAs<NamedDecl>(ID: "C");
228 const NamedDecl *SpecializedC = ClassSpecializations[0].getNodeAs<NamedDecl>(ID: "C");
229 EXPECT_EQ(TemplatedC->getLinkageInternal(),
230 SpecializedC->getLinkageInternal());
231}
232
233TEST(Decl, ModuleAndInternalLinkage) {
234 llvm::Annotations Code(R"(
235 export module M;
236 static int a;
237 static int f(int x);
238
239 int b;
240 int g(int x);)");
241
242 auto AST =
243 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
244 ASTContext &Ctx = AST->getASTContext();
245
246 const auto *a =
247 selectFirst<VarDecl>(BoundTo: "a", Results: match(Matcher: varDecl(hasName(Name: "a")).bind(ID: "a"), Context&: Ctx));
248 const auto *f = selectFirst<FunctionDecl>(
249 BoundTo: "f", Results: match(Matcher: functionDecl(hasName(Name: "f")).bind(ID: "f"), Context&: Ctx));
250
251 EXPECT_EQ(a->getFormalLinkage(), Linkage::Internal);
252 EXPECT_EQ(f->getFormalLinkage(), Linkage::Internal);
253
254 const auto *b =
255 selectFirst<VarDecl>(BoundTo: "b", Results: match(Matcher: varDecl(hasName(Name: "b")).bind(ID: "b"), Context&: Ctx));
256 const auto *g = selectFirst<FunctionDecl>(
257 BoundTo: "g", Results: match(Matcher: functionDecl(hasName(Name: "g")).bind(ID: "g"), Context&: Ctx));
258
259 EXPECT_EQ(b->getFormalLinkage(), Linkage::Module);
260 EXPECT_EQ(g->getFormalLinkage(), Linkage::Module);
261}
262
263TEST(Decl, GetNonTransparentDeclContext) {
264 llvm::Annotations Code(R"(
265 export module m3;
266 export template <class> struct X {
267 template <class Self> friend void f(Self &&self) {
268 (Self&)self;
269 }
270 };)");
271
272 auto AST =
273 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
274 ASTContext &Ctx = AST->getASTContext();
275
276 auto *f = selectFirst<FunctionDecl>(
277 BoundTo: "f", Results: match(Matcher: functionDecl(hasName(Name: "f")).bind(ID: "f"), Context&: Ctx));
278
279 EXPECT_TRUE(f->getNonTransparentDeclContext()->isFileContext());
280}
281
282TEST(Decl, MemberFunctionInModules) {
283 llvm::Annotations Code(R"(
284 module;
285 class G {
286 void bar() {}
287 };
288 export module M;
289 class A {
290 void foo() {}
291 };
292 )");
293
294 auto AST =
295 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
296 ASTContext &Ctx = AST->getASTContext();
297
298 auto *foo = selectFirst<FunctionDecl>(
299 BoundTo: "foo", Results: match(Matcher: functionDecl(hasName(Name: "foo")).bind(ID: "foo"), Context&: Ctx));
300
301 // The function defined within a class definition is not implicitly inline
302 // if it is not attached to global module
303 EXPECT_FALSE(foo->isInlined());
304
305 auto *bar = selectFirst<FunctionDecl>(
306 BoundTo: "bar", Results: match(Matcher: functionDecl(hasName(Name: "bar")).bind(ID: "bar"), Context&: Ctx));
307
308 // In global module, the function defined within a class definition is
309 // implicitly inline.
310 EXPECT_TRUE(bar->isInlined());
311}
312
313TEST(Decl, MemberFunctionInHeaderUnit) {
314 llvm::Annotations Code(R"(
315 class foo {
316 public:
317 int memFn() {
318 return 43;
319 }
320 };
321 )");
322
323 auto AST = tooling::buildASTFromCodeWithArgs(
324 Code: Code.code(), Args: {"-std=c++20", " -xc++-user-header ", "-emit-header-unit"});
325 ASTContext &Ctx = AST->getASTContext();
326
327 auto *memFn = selectFirst<FunctionDecl>(
328 BoundTo: "memFn", Results: match(Matcher: functionDecl(hasName(Name: "memFn")).bind(ID: "memFn"), Context&: Ctx));
329
330 EXPECT_TRUE(memFn->isInlined());
331}
332
333TEST(Decl, FriendFunctionWithinClassInHeaderUnit) {
334 llvm::Annotations Code(R"(
335 class foo {
336 int value;
337 public:
338 foo(int v) : value(v) {}
339
340 friend int getFooValue(foo f) {
341 return f.value;
342 }
343 };
344 )");
345
346 auto AST = tooling::buildASTFromCodeWithArgs(
347 Code: Code.code(), Args: {"-std=c++20", " -xc++-user-header ", "-emit-header-unit"});
348 ASTContext &Ctx = AST->getASTContext();
349
350 auto *getFooValue = selectFirst<FunctionDecl>(
351 BoundTo: "getFooValue",
352 Results: match(Matcher: functionDecl(hasName(Name: "getFooValue")).bind(ID: "getFooValue"), Context&: Ctx));
353
354 EXPECT_TRUE(getFooValue->isInlined());
355}
356
357TEST(Decl, FunctionDeclBitsShouldNotOverlapWithCXXConstructorDeclBits) {
358 llvm::Annotations Code(R"(
359 struct A {
360 A() : m() {}
361 int m;
362 };
363
364 A f() { return A(); }
365 )");
366
367 auto AST = tooling::buildASTFromCodeWithArgs(Code: Code.code(), Args: {"-std=c++14"});
368 ASTContext &Ctx = AST->getASTContext();
369
370 auto HasCtorInit =
371 hasAnyConstructorInitializer(InnerMatcher: cxxCtorInitializer(isMemberInitializer()));
372 auto ImpMoveCtor =
373 cxxConstructorDecl(isMoveConstructor(), isImplicit(), HasCtorInit)
374 .bind(ID: "MoveCtor");
375
376 auto *ToImpMoveCtor =
377 selectFirst<CXXConstructorDecl>(BoundTo: "MoveCtor", Results: match(Matcher: ImpMoveCtor, Context&: Ctx));
378
379 EXPECT_TRUE(ToImpMoveCtor->getNumCtorInitializers() == 1);
380 EXPECT_FALSE(ToImpMoveCtor->FriendConstraintRefersToEnclosingTemplate());
381}
382
383TEST(Decl, NoProtoFunctionDeclAttributes) {
384 llvm::Annotations Code(R"(
385 void f();
386 )");
387
388 auto AST = tooling::buildASTFromCodeWithArgs(
389 Code: Code.code(),
390 /*Args=*/{"-target", "i386-apple-darwin", "-x", "objective-c",
391 "-std=c89"});
392 ASTContext &Ctx = AST->getASTContext();
393
394 auto *f = selectFirst<FunctionDecl>(
395 BoundTo: "f", Results: match(Matcher: functionDecl(hasName(Name: "f")).bind(ID: "f"), Context&: Ctx));
396
397 const auto *FPT = f->getType()->getAs<FunctionNoProtoType>();
398
399 // Functions without prototypes always have 0 initialized qualifiers
400 EXPECT_FALSE(FPT->isConst());
401 EXPECT_FALSE(FPT->isVolatile());
402 EXPECT_FALSE(FPT->isRestrict());
403}
404
405TEST(Decl, ImplicitlyDeclaredAllocationFunctionsInModules) {
406 // C++ [basic.stc.dynamic.general]p2:
407 // The library provides default definitions for the global allocation
408 // and deallocation functions. Some global allocation and deallocation
409 // functions are replaceable ([new.delete]); these are attached to the
410 // global module ([module.unit]).
411
412 llvm::Annotations Code(R"(
413 export module base;
414
415 export struct Base {
416 virtual void hello() = 0;
417 virtual ~Base() = default;
418 };
419 )");
420
421 auto AST =
422 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
423 ASTContext &Ctx = AST->getASTContext();
424
425 // void* operator new(std::size_t);
426 auto *SizedOperatorNew = selectFirst<FunctionDecl>(
427 BoundTo: "operator new",
428 Results: match(Matcher: functionDecl(hasName(Name: "operator new"), parameterCountIs(N: 1),
429 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: isUnsignedInteger())))
430 .bind(ID: "operator new"),
431 Context&: Ctx));
432 ASSERT_TRUE(SizedOperatorNew->getOwningModule());
433 EXPECT_TRUE(SizedOperatorNew->isFromExplicitGlobalModule());
434
435 // void* operator new(std::size_t, std::align_val_t);
436 auto *SizedAlignedOperatorNew = selectFirst<FunctionDecl>(
437 BoundTo: "operator new",
438 Results: match(Matcher: functionDecl(
439 hasName(Name: "operator new"), parameterCountIs(N: 2),
440 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: isUnsignedInteger())),
441 hasParameter(N: 1, InnerMatcher: hasType(InnerMatcher: enumDecl(hasName(Name: "std::align_val_t")))))
442 .bind(ID: "operator new"),
443 Context&: Ctx));
444 ASSERT_TRUE(SizedAlignedOperatorNew->getOwningModule());
445 EXPECT_TRUE(SizedAlignedOperatorNew->isFromExplicitGlobalModule());
446
447 // void* operator new[](std::size_t);
448 auto *SizedArrayOperatorNew = selectFirst<FunctionDecl>(
449 BoundTo: "operator new[]",
450 Results: match(Matcher: functionDecl(hasName(Name: "operator new[]"), parameterCountIs(N: 1),
451 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: isUnsignedInteger())))
452 .bind(ID: "operator new[]"),
453 Context&: Ctx));
454 ASSERT_TRUE(SizedArrayOperatorNew->getOwningModule());
455 EXPECT_TRUE(SizedArrayOperatorNew->isFromExplicitGlobalModule());
456
457 // void* operator new[](std::size_t, std::align_val_t);
458 auto *SizedAlignedArrayOperatorNew = selectFirst<FunctionDecl>(
459 BoundTo: "operator new[]",
460 Results: match(Matcher: functionDecl(
461 hasName(Name: "operator new[]"), parameterCountIs(N: 2),
462 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: isUnsignedInteger())),
463 hasParameter(N: 1, InnerMatcher: hasType(InnerMatcher: enumDecl(hasName(Name: "std::align_val_t")))))
464 .bind(ID: "operator new[]"),
465 Context&: Ctx));
466 ASSERT_TRUE(SizedAlignedArrayOperatorNew->getOwningModule());
467 EXPECT_TRUE(
468 SizedAlignedArrayOperatorNew->isFromExplicitGlobalModule());
469
470 // void operator delete(void*) noexcept;
471 auto *Delete = selectFirst<FunctionDecl>(
472 BoundTo: "operator delete",
473 Results: match(Matcher: functionDecl(
474 hasName(Name: "operator delete"), parameterCountIs(N: 1),
475 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: pointerType(pointee(voidType())))))
476 .bind(ID: "operator delete"),
477 Context&: Ctx));
478 ASSERT_TRUE(Delete->getOwningModule());
479 EXPECT_TRUE(Delete->isFromExplicitGlobalModule());
480
481 // void operator delete(void*, std::align_val_t) noexcept;
482 auto *AlignedDelete = selectFirst<FunctionDecl>(
483 BoundTo: "operator delete",
484 Results: match(Matcher: functionDecl(
485 hasName(Name: "operator delete"), parameterCountIs(N: 2),
486 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: pointerType(pointee(voidType())))),
487 hasParameter(N: 1, InnerMatcher: hasType(InnerMatcher: enumDecl(hasName(Name: "std::align_val_t")))))
488 .bind(ID: "operator delete"),
489 Context&: Ctx));
490 ASSERT_TRUE(AlignedDelete->getOwningModule());
491 EXPECT_TRUE(AlignedDelete->isFromExplicitGlobalModule());
492
493 // Sized deallocation is not enabled by default. So we skip it here.
494
495 // void operator delete[](void*) noexcept;
496 auto *ArrayDelete = selectFirst<FunctionDecl>(
497 BoundTo: "operator delete[]",
498 Results: match(Matcher: functionDecl(
499 hasName(Name: "operator delete[]"), parameterCountIs(N: 1),
500 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: pointerType(pointee(voidType())))))
501 .bind(ID: "operator delete[]"),
502 Context&: Ctx));
503 ASSERT_TRUE(ArrayDelete->getOwningModule());
504 EXPECT_TRUE(ArrayDelete->isFromExplicitGlobalModule());
505
506 // void operator delete[](void*, std::align_val_t) noexcept;
507 auto *AlignedArrayDelete = selectFirst<FunctionDecl>(
508 BoundTo: "operator delete[]",
509 Results: match(Matcher: functionDecl(
510 hasName(Name: "operator delete[]"), parameterCountIs(N: 2),
511 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: pointerType(pointee(voidType())))),
512 hasParameter(N: 1, InnerMatcher: hasType(InnerMatcher: enumDecl(hasName(Name: "std::align_val_t")))))
513 .bind(ID: "operator delete[]"),
514 Context&: Ctx));
515 ASSERT_TRUE(AlignedArrayDelete->getOwningModule());
516 EXPECT_TRUE(AlignedArrayDelete->isFromExplicitGlobalModule());
517}
518
519TEST(Decl, TemplateArgumentDefaulted) {
520 llvm::Annotations Code(R"cpp(
521 template<typename T1, typename T2>
522 struct Alloc {};
523
524 template <typename T1,
525 typename T2 = double,
526 int T3 = 42,
527 typename T4 = Alloc<T1, T2>>
528 struct Foo {
529 };
530
531 Foo<char, int, 42, Alloc<char, int>> X;
532 )cpp");
533
534 auto AST =
535 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
536 ASTContext &Ctx = AST->getASTContext();
537
538 auto const *CTSD = selectFirst<ClassTemplateSpecializationDecl>(
539 BoundTo: "id",
540 Results: match(Matcher: classTemplateSpecializationDecl(hasName(Name: "Foo")).bind(ID: "id"), Context&: Ctx));
541 ASSERT_NE(CTSD, nullptr);
542 auto const &ArgList = CTSD->getTemplateArgs();
543
544 EXPECT_FALSE(ArgList.get(0).getIsDefaulted());
545 EXPECT_FALSE(ArgList.get(1).getIsDefaulted());
546 EXPECT_TRUE(ArgList.get(2).getIsDefaulted());
547 EXPECT_TRUE(ArgList.get(3).getIsDefaulted());
548}
549
550TEST(Decl, CXXDestructorDeclsShouldHaveWellFormedNameInfoRanges) {
551 // GH71161
552 llvm::Annotations Code(R"cpp(
553template <typename T> struct Resource {
554 ~Resource(); // 1
555};
556template <typename T>
557Resource<T>::~Resource() {} // 2,3
558
559void instantiate_template() {
560 Resource<int> x;
561}
562)cpp");
563
564 auto AST = tooling::buildASTFromCode(Code: Code.code());
565 ASTContext &Ctx = AST->getASTContext();
566
567 const auto &SM = Ctx.getSourceManager();
568 auto GetNameInfoRange = [&SM](const BoundNodes &Match) {
569 const auto *D = Match.getNodeAs<CXXDestructorDecl>(ID: "dtor");
570 return D->getNameInfo().getSourceRange().printToString(SM);
571 };
572
573 auto Matches = match(Matcher: findAll(Matcher: cxxDestructorDecl().bind(ID: "dtor")),
574 Node: *Ctx.getTranslationUnitDecl(), Context&: Ctx);
575 ASSERT_EQ(Matches.size(), 3U);
576 EXPECT_EQ(GetNameInfoRange(Matches[0]), "<input.cc:3:3, col:4>");
577 EXPECT_EQ(GetNameInfoRange(Matches[1]), "<input.cc:6:14, col:15>");
578 EXPECT_EQ(GetNameInfoRange(Matches[2]), "<input.cc:6:14, col:15>");
579}
580

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of clang/unittests/AST/DeclTest.cpp