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