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_TRUE(0 == MangleF.compare("\x01" "foo"));
108 ASSERT_TRUE(0 == MangleG.compare("goo"));
109}
110
111TEST(Decl, MangleDependentSizedArray) {
112 StringRef Code = R"(
113 template <int ...N>
114 int A[] = {N...};
115
116 template <typename T, int N>
117 struct S {
118 T B[N];
119 };
120 )";
121 auto AST =
122 tooling::buildASTFromCodeWithArgs(Code, Args: {"-target", "i386-apple-darwin"});
123 ASTContext &Ctx = AST->getASTContext();
124 assert(Ctx.getTargetInfo().getUserLabelPrefix() == StringRef("_") &&
125 "Expected target to have a global prefix");
126 DiagnosticsEngine &Diags = AST->getDiagnostics();
127
128 const auto *DeclA =
129 selectFirst<VarDecl>(BoundTo: "A", Results: match(Matcher: varDecl().bind(ID: "A"), Context&: Ctx));
130 const auto *DeclB =
131 selectFirst<FieldDecl>(BoundTo: "B", Results: match(Matcher: fieldDecl().bind(ID: "B"), Context&: Ctx));
132
133 std::string MangleA, MangleB;
134 llvm::raw_string_ostream OS_A(MangleA), OS_B(MangleB);
135 std::unique_ptr<ItaniumMangleContext> MC(
136 ItaniumMangleContext::create(Context&: Ctx, Diags));
137
138 MC->mangleCanonicalTypeName(T: DeclA->getType(), OS_A);
139 MC->mangleCanonicalTypeName(T: DeclB->getType(), OS_B);
140
141 ASSERT_TRUE(0 == MangleA.compare("_ZTSA_i"));
142 ASSERT_TRUE(0 == MangleB.compare("_ZTSAT0__T_"));
143}
144
145TEST(Decl, ConceptDecl) {
146 llvm::StringRef Code(R"(
147 template<class T>
148 concept integral = __is_integral(T);
149 )");
150
151 auto AST = tooling::buildASTFromCodeWithArgs(Code, Args: {"-std=c++20"});
152 ASTContext &Ctx = AST->getASTContext();
153
154 const auto *Decl =
155 selectFirst<ConceptDecl>(BoundTo: "decl", Results: match(Matcher: conceptDecl().bind(ID: "decl"), Context&: Ctx));
156 ASSERT_TRUE(Decl != nullptr);
157 EXPECT_EQ(Decl->getName(), "integral");
158}
159
160TEST(Decl, EnumDeclRange) {
161 llvm::Annotations Code(R"(
162 typedef int Foo;
163 [[enum Bar : Foo]];)");
164 auto AST = tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{});
165 ASTContext &Ctx = AST->getASTContext();
166 const auto &SM = Ctx.getSourceManager();
167
168 const auto *Bar =
169 selectFirst<TagDecl>(BoundTo: "Bar", Results: match(Matcher: enumDecl().bind(ID: "Bar"), Context&: Ctx));
170 auto BarRange =
171 Lexer::getAsCharRange(Range: Bar->getSourceRange(), SM, LangOpts: Ctx.getLangOpts());
172 EXPECT_EQ(SM.getFileOffset(BarRange.getBegin()), Code.range().Begin);
173 EXPECT_EQ(SM.getFileOffset(BarRange.getEnd()), Code.range().End);
174}
175
176TEST(Decl, IsInExportDeclContext) {
177 llvm::Annotations Code(R"(
178 export module m;
179 export template <class T>
180 void f() {})");
181 auto AST =
182 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
183 ASTContext &Ctx = AST->getASTContext();
184
185 const auto *f =
186 selectFirst<FunctionDecl>(BoundTo: "f", Results: match(Matcher: functionDecl().bind(ID: "f"), Context&: Ctx));
187 EXPECT_TRUE(f->isInExportDeclContext());
188}
189
190TEST(Decl, InConsistLinkageForTemplates) {
191 llvm::Annotations Code(R"(
192 export module m;
193 export template <class T>
194 void f() {}
195
196 template <>
197 void f<int>() {}
198
199 export template <class T>
200 class C {};
201
202 template<>
203 class C<int> {};
204 )");
205
206 auto AST =
207 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
208 ASTContext &Ctx = AST->getASTContext();
209
210 llvm::SmallVector<ast_matchers::BoundNodes, 2> Funcs =
211 match(Matcher: functionDecl().bind(ID: "f"), Context&: Ctx);
212
213 EXPECT_EQ(Funcs.size(), 2U);
214 const FunctionDecl *TemplateF = Funcs[0].getNodeAs<FunctionDecl>(ID: "f");
215 const FunctionDecl *SpecializedF = Funcs[1].getNodeAs<FunctionDecl>(ID: "f");
216 EXPECT_EQ(TemplateF->getLinkageInternal(),
217 SpecializedF->getLinkageInternal());
218
219 llvm::SmallVector<ast_matchers::BoundNodes, 1> ClassTemplates =
220 match(Matcher: classTemplateDecl().bind(ID: "C"), Context&: Ctx);
221 llvm::SmallVector<ast_matchers::BoundNodes, 1> ClassSpecializations =
222 match(Matcher: classTemplateSpecializationDecl().bind(ID: "C"), Context&: Ctx);
223
224 EXPECT_EQ(ClassTemplates.size(), 1U);
225 EXPECT_EQ(ClassSpecializations.size(), 1U);
226 const NamedDecl *TemplatedC = ClassTemplates[0].getNodeAs<NamedDecl>(ID: "C");
227 const NamedDecl *SpecializedC = ClassSpecializations[0].getNodeAs<NamedDecl>(ID: "C");
228 EXPECT_EQ(TemplatedC->getLinkageInternal(),
229 SpecializedC->getLinkageInternal());
230}
231
232TEST(Decl, ModuleAndInternalLinkage) {
233 llvm::Annotations Code(R"(
234 export module M;
235 static int a;
236 static int f(int x);
237
238 int b;
239 int g(int x);)");
240
241 auto AST =
242 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
243 ASTContext &Ctx = AST->getASTContext();
244
245 const auto *a =
246 selectFirst<VarDecl>(BoundTo: "a", Results: match(Matcher: varDecl(hasName(Name: "a")).bind(ID: "a"), Context&: Ctx));
247 const auto *f = selectFirst<FunctionDecl>(
248 BoundTo: "f", Results: match(Matcher: functionDecl(hasName(Name: "f")).bind(ID: "f"), Context&: Ctx));
249
250 EXPECT_EQ(a->getFormalLinkage(), Linkage::Internal);
251 EXPECT_EQ(f->getFormalLinkage(), Linkage::Internal);
252
253 const auto *b =
254 selectFirst<VarDecl>(BoundTo: "b", Results: match(Matcher: varDecl(hasName(Name: "b")).bind(ID: "b"), Context&: Ctx));
255 const auto *g = selectFirst<FunctionDecl>(
256 BoundTo: "g", Results: match(Matcher: functionDecl(hasName(Name: "g")).bind(ID: "g"), Context&: Ctx));
257
258 EXPECT_EQ(b->getFormalLinkage(), Linkage::Module);
259 EXPECT_EQ(g->getFormalLinkage(), Linkage::Module);
260}
261
262TEST(Decl, GetNonTransparentDeclContext) {
263 llvm::Annotations Code(R"(
264 export module m3;
265 export template <class> struct X {
266 template <class Self> friend void f(Self &&self) {
267 (Self&)self;
268 }
269 };)");
270
271 auto AST =
272 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
273 ASTContext &Ctx = AST->getASTContext();
274
275 auto *f = selectFirst<FunctionDecl>(
276 BoundTo: "f", Results: match(Matcher: functionDecl(hasName(Name: "f")).bind(ID: "f"), Context&: Ctx));
277
278 EXPECT_TRUE(f->getNonTransparentDeclContext()->isFileContext());
279}
280
281TEST(Decl, MemberFunctionInModules) {
282 llvm::Annotations Code(R"(
283 module;
284 class G {
285 void bar() {}
286 };
287 export module M;
288 class A {
289 void foo() {}
290 };
291 )");
292
293 auto AST =
294 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
295 ASTContext &Ctx = AST->getASTContext();
296
297 auto *foo = selectFirst<FunctionDecl>(
298 BoundTo: "foo", Results: match(Matcher: functionDecl(hasName(Name: "foo")).bind(ID: "foo"), Context&: Ctx));
299
300 // The function defined within a class definition is not implicitly inline
301 // if it is not attached to global module
302 EXPECT_FALSE(foo->isInlined());
303
304 auto *bar = selectFirst<FunctionDecl>(
305 BoundTo: "bar", Results: match(Matcher: functionDecl(hasName(Name: "bar")).bind(ID: "bar"), Context&: Ctx));
306
307 // In global module, the function defined within a class definition is
308 // implicitly inline.
309 EXPECT_TRUE(bar->isInlined());
310}
311
312TEST(Decl, MemberFunctionInHeaderUnit) {
313 llvm::Annotations Code(R"(
314 class foo {
315 public:
316 int memFn() {
317 return 43;
318 }
319 };
320 )");
321
322 auto AST = tooling::buildASTFromCodeWithArgs(
323 Code: Code.code(), Args: {"-std=c++20", " -xc++-user-header ", "-emit-header-unit"});
324 ASTContext &Ctx = AST->getASTContext();
325
326 auto *memFn = selectFirst<FunctionDecl>(
327 BoundTo: "memFn", Results: match(Matcher: functionDecl(hasName(Name: "memFn")).bind(ID: "memFn"), Context&: Ctx));
328
329 EXPECT_TRUE(memFn->isInlined());
330}
331
332TEST(Decl, FriendFunctionWithinClassInHeaderUnit) {
333 llvm::Annotations Code(R"(
334 class foo {
335 int value;
336 public:
337 foo(int v) : value(v) {}
338
339 friend int getFooValue(foo f) {
340 return f.value;
341 }
342 };
343 )");
344
345 auto AST = tooling::buildASTFromCodeWithArgs(
346 Code: Code.code(), Args: {"-std=c++20", " -xc++-user-header ", "-emit-header-unit"});
347 ASTContext &Ctx = AST->getASTContext();
348
349 auto *getFooValue = selectFirst<FunctionDecl>(
350 BoundTo: "getFooValue",
351 Results: match(Matcher: functionDecl(hasName(Name: "getFooValue")).bind(ID: "getFooValue"), Context&: Ctx));
352
353 EXPECT_TRUE(getFooValue->isInlined());
354}
355
356TEST(Decl, FunctionDeclBitsShouldNotOverlapWithCXXConstructorDeclBits) {
357 llvm::Annotations Code(R"(
358 struct A {
359 A() : m() {}
360 int m;
361 };
362
363 A f() { return A(); }
364 )");
365
366 auto AST = tooling::buildASTFromCodeWithArgs(Code: Code.code(), Args: {"-std=c++14"});
367 ASTContext &Ctx = AST->getASTContext();
368
369 auto HasCtorInit =
370 hasAnyConstructorInitializer(InnerMatcher: cxxCtorInitializer(isMemberInitializer()));
371 auto ImpMoveCtor =
372 cxxConstructorDecl(isMoveConstructor(), isImplicit(), HasCtorInit)
373 .bind(ID: "MoveCtor");
374
375 auto *ToImpMoveCtor =
376 selectFirst<CXXConstructorDecl>(BoundTo: "MoveCtor", Results: match(Matcher: ImpMoveCtor, Context&: Ctx));
377
378 EXPECT_TRUE(ToImpMoveCtor->getNumCtorInitializers() == 1);
379 EXPECT_FALSE(ToImpMoveCtor->FriendConstraintRefersToEnclosingTemplate());
380}
381
382TEST(Decl, NoProtoFunctionDeclAttributes) {
383 llvm::Annotations Code(R"(
384 void f();
385 )");
386
387 auto AST = tooling::buildASTFromCodeWithArgs(
388 Code: Code.code(),
389 /*Args=*/{"-target", "i386-apple-darwin", "-x", "objective-c",
390 "-std=c89"});
391 ASTContext &Ctx = AST->getASTContext();
392
393 auto *f = selectFirst<FunctionDecl>(
394 BoundTo: "f", Results: match(Matcher: functionDecl(hasName(Name: "f")).bind(ID: "f"), Context&: Ctx));
395
396 const auto *FPT = f->getType()->getAs<FunctionNoProtoType>();
397
398 // Functions without prototypes always have 0 initialized qualifiers
399 EXPECT_FALSE(FPT->isConst());
400 EXPECT_FALSE(FPT->isVolatile());
401 EXPECT_FALSE(FPT->isRestrict());
402}
403
404TEST(Decl, ImplicitlyDeclaredAllocationFunctionsInModules) {
405 // C++ [basic.stc.dynamic.general]p2:
406 // The library provides default definitions for the global allocation
407 // and deallocation functions. Some global allocation and deallocation
408 // functions are replaceable ([new.delete]); these are attached to the
409 // global module ([module.unit]).
410
411 llvm::Annotations Code(R"(
412 export module base;
413
414 export struct Base {
415 virtual void hello() = 0;
416 virtual ~Base() = default;
417 };
418 )");
419
420 auto AST =
421 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
422 ASTContext &Ctx = AST->getASTContext();
423
424 // void* operator new(std::size_t);
425 auto *SizedOperatorNew = selectFirst<FunctionDecl>(
426 BoundTo: "operator new",
427 Results: match(Matcher: functionDecl(hasName(Name: "operator new"), parameterCountIs(N: 1),
428 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: isUnsignedInteger())))
429 .bind(ID: "operator new"),
430 Context&: Ctx));
431 ASSERT_TRUE(SizedOperatorNew->getOwningModule());
432 EXPECT_TRUE(SizedOperatorNew->isFromExplicitGlobalModule());
433
434 // void* operator new(std::size_t, std::align_val_t);
435 auto *SizedAlignedOperatorNew = selectFirst<FunctionDecl>(
436 BoundTo: "operator new",
437 Results: match(Matcher: functionDecl(
438 hasName(Name: "operator new"), parameterCountIs(N: 2),
439 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: isUnsignedInteger())),
440 hasParameter(N: 1, InnerMatcher: hasType(InnerMatcher: enumDecl(hasName(Name: "std::align_val_t")))))
441 .bind(ID: "operator new"),
442 Context&: Ctx));
443 ASSERT_TRUE(SizedAlignedOperatorNew->getOwningModule());
444 EXPECT_TRUE(SizedAlignedOperatorNew->isFromExplicitGlobalModule());
445
446 // void* operator new[](std::size_t);
447 auto *SizedArrayOperatorNew = selectFirst<FunctionDecl>(
448 BoundTo: "operator new[]",
449 Results: match(Matcher: functionDecl(hasName(Name: "operator new[]"), parameterCountIs(N: 1),
450 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: isUnsignedInteger())))
451 .bind(ID: "operator new[]"),
452 Context&: Ctx));
453 ASSERT_TRUE(SizedArrayOperatorNew->getOwningModule());
454 EXPECT_TRUE(SizedArrayOperatorNew->isFromExplicitGlobalModule());
455
456 // void* operator new[](std::size_t, std::align_val_t);
457 auto *SizedAlignedArrayOperatorNew = selectFirst<FunctionDecl>(
458 BoundTo: "operator new[]",
459 Results: match(Matcher: functionDecl(
460 hasName(Name: "operator new[]"), parameterCountIs(N: 2),
461 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: isUnsignedInteger())),
462 hasParameter(N: 1, InnerMatcher: hasType(InnerMatcher: enumDecl(hasName(Name: "std::align_val_t")))))
463 .bind(ID: "operator new[]"),
464 Context&: Ctx));
465 ASSERT_TRUE(SizedAlignedArrayOperatorNew->getOwningModule());
466 EXPECT_TRUE(
467 SizedAlignedArrayOperatorNew->isFromExplicitGlobalModule());
468
469 // void operator delete(void*) noexcept;
470 auto *Delete = selectFirst<FunctionDecl>(
471 BoundTo: "operator delete",
472 Results: match(Matcher: functionDecl(
473 hasName(Name: "operator delete"), parameterCountIs(N: 1),
474 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: pointerType(pointee(voidType())))))
475 .bind(ID: "operator delete"),
476 Context&: Ctx));
477 ASSERT_TRUE(Delete->getOwningModule());
478 EXPECT_TRUE(Delete->isFromExplicitGlobalModule());
479
480 // void operator delete(void*, std::align_val_t) noexcept;
481 auto *AlignedDelete = selectFirst<FunctionDecl>(
482 BoundTo: "operator delete",
483 Results: match(Matcher: functionDecl(
484 hasName(Name: "operator delete"), parameterCountIs(N: 2),
485 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: pointerType(pointee(voidType())))),
486 hasParameter(N: 1, InnerMatcher: hasType(InnerMatcher: enumDecl(hasName(Name: "std::align_val_t")))))
487 .bind(ID: "operator delete"),
488 Context&: Ctx));
489 ASSERT_TRUE(AlignedDelete->getOwningModule());
490 EXPECT_TRUE(AlignedDelete->isFromExplicitGlobalModule());
491
492 // Sized deallocation is not enabled by default. So we skip it here.
493
494 // void operator delete[](void*) noexcept;
495 auto *ArrayDelete = selectFirst<FunctionDecl>(
496 BoundTo: "operator delete[]",
497 Results: match(Matcher: functionDecl(
498 hasName(Name: "operator delete[]"), parameterCountIs(N: 1),
499 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: pointerType(pointee(voidType())))))
500 .bind(ID: "operator delete[]"),
501 Context&: Ctx));
502 ASSERT_TRUE(ArrayDelete->getOwningModule());
503 EXPECT_TRUE(ArrayDelete->isFromExplicitGlobalModule());
504
505 // void operator delete[](void*, std::align_val_t) noexcept;
506 auto *AlignedArrayDelete = selectFirst<FunctionDecl>(
507 BoundTo: "operator delete[]",
508 Results: match(Matcher: functionDecl(
509 hasName(Name: "operator delete[]"), parameterCountIs(N: 2),
510 hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: pointerType(pointee(voidType())))),
511 hasParameter(N: 1, InnerMatcher: hasType(InnerMatcher: enumDecl(hasName(Name: "std::align_val_t")))))
512 .bind(ID: "operator delete[]"),
513 Context&: Ctx));
514 ASSERT_TRUE(AlignedArrayDelete->getOwningModule());
515 EXPECT_TRUE(AlignedArrayDelete->isFromExplicitGlobalModule());
516}
517
518TEST(Decl, TemplateArgumentDefaulted) {
519 llvm::Annotations Code(R"cpp(
520 template<typename T1, typename T2>
521 struct Alloc {};
522
523 template <typename T1,
524 typename T2 = double,
525 int T3 = 42,
526 typename T4 = Alloc<T1, T2>>
527 struct Foo {
528 };
529
530 Foo<char, int, 42, Alloc<char, int>> X;
531 )cpp");
532
533 auto AST =
534 tooling::buildASTFromCodeWithArgs(Code: Code.code(), /*Args=*/{"-std=c++20"});
535 ASTContext &Ctx = AST->getASTContext();
536
537 auto const *CTSD = selectFirst<ClassTemplateSpecializationDecl>(
538 BoundTo: "id",
539 Results: match(Matcher: classTemplateSpecializationDecl(hasName(Name: "Foo")).bind(ID: "id"), Context&: Ctx));
540 ASSERT_NE(CTSD, nullptr);
541 auto const &ArgList = CTSD->getTemplateArgs();
542
543 EXPECT_FALSE(ArgList.get(0).getIsDefaulted());
544 EXPECT_FALSE(ArgList.get(1).getIsDefaulted());
545 EXPECT_TRUE(ArgList.get(2).getIsDefaulted());
546 EXPECT_TRUE(ArgList.get(3).getIsDefaulted());
547}
548

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