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/ASTContext.h" 14 #include "clang/AST/Mangle.h" 15 #include "clang/ASTMatchers/ASTMatchFinder.h" 16 #include "clang/Basic/LLVM.h" 17 #include "clang/Basic/TargetInfo.h" 18 #include "clang/Tooling/Tooling.h" 19 #include "llvm/IR/DataLayout.h" 20 #include "gtest/gtest.h" 21 22 using namespace clang::ast_matchers; 23 using namespace clang::tooling; 24 using namespace clang; 25 26 TEST(Decl, CleansUpAPValues) { 27 MatchFinder Finder; 28 std::unique_ptr<FrontendActionFactory> Factory( 29 newFrontendActionFactory(&Finder)); 30 31 // This is a regression test for a memory leak in APValues for structs that 32 // allocate memory. This test only fails if run under valgrind with full leak 33 // checking enabled. 34 std::vector<std::string> Args(1, "-std=c++11"); 35 Args.push_back("-fno-ms-extensions"); 36 ASSERT_TRUE(runToolOnCodeWithArgs( 37 Factory->create(), 38 "struct X { int a; }; constexpr X x = { 42 };" 39 "union Y { constexpr Y(int a) : a(a) {} int a; }; constexpr Y y = { 42 };" 40 "constexpr int z[2] = { 42, 43 };" 41 "constexpr int __attribute__((vector_size(16))) v1 = {};" 42 "\n#ifdef __SIZEOF_INT128__\n" 43 "constexpr __uint128_t large_int = 0xffffffffffffffff;" 44 "constexpr __uint128_t small_int = 1;" 45 "\n#endif\n" 46 "constexpr double d1 = 42.42;" 47 "constexpr long double d2 = 42.42;" 48 "constexpr _Complex long double c1 = 42.0i;" 49 "constexpr _Complex long double c2 = 42.0;" 50 "template<int N> struct A : A<N-1> {};" 51 "template<> struct A<0> { int n; }; A<50> a;" 52 "constexpr int &r = a.n;" 53 "constexpr int A<50>::*p = &A<50>::n;" 54 "void f() { foo: bar: constexpr int k = __builtin_constant_p(0) ?" 55 " (char*)&&foo - (char*)&&bar : 0; }", 56 Args)); 57 58 // FIXME: Once this test starts breaking we can test APValue::needsCleanup 59 // for ComplexInt. 60 ASSERT_FALSE(runToolOnCodeWithArgs( 61 Factory->create(), 62 "constexpr _Complex __uint128_t c = 0xffffffffffffffff;", 63 Args)); 64 } 65 66 TEST(Decl, AsmLabelAttr) { 67 // Create two method decls: `f` and `g`. 68 StringRef Code = R"( 69 struct S { 70 void f() {} 71 void g() {} 72 }; 73 )"; 74 auto AST = 75 tooling::buildASTFromCodeWithArgs(Code, {"-target", "i386-apple-darwin"}); 76 ASTContext &Ctx = AST->getASTContext(); 77 assert(Ctx.getTargetInfo().getDataLayout().getGlobalPrefix() && 78 "Expected target to have a global prefix"); 79 DiagnosticsEngine &Diags = AST->getDiagnostics(); 80 SourceManager &SM = AST->getSourceManager(); 81 FileID MainFileID = SM.getMainFileID(); 82 83 // Find the method decls within the AST. 84 SmallVector<Decl *, 1> Decls; 85 AST->findFileRegionDecls(MainFileID, Code.find('{'), 0, Decls); 86 ASSERT_TRUE(Decls.size() == 1); 87 CXXRecordDecl *DeclS = cast<CXXRecordDecl>(Decls[0]); 88 NamedDecl *DeclF = *DeclS->method_begin(); 89 NamedDecl *DeclG = *(++DeclS->method_begin()); 90 91 // Attach asm labels to the decls: one literal, and one not. 92 DeclF->addAttr(::new (Ctx) AsmLabelAttr(Ctx, SourceLocation(), "foo", 93 /*LiteralLabel=*/true)); 94 DeclG->addAttr(::new (Ctx) AsmLabelAttr(Ctx, SourceLocation(), "goo", 95 /*LiteralLabel=*/false)); 96 97 // Mangle the decl names. 98 std::string MangleF, MangleG; 99 std::unique_ptr<ItaniumMangleContext> MC( 100 ItaniumMangleContext::create(Ctx, Diags)); 101 { 102 llvm::raw_string_ostream OS_F(MangleF); 103 llvm::raw_string_ostream OS_G(MangleG); 104 MC->mangleName(DeclF, OS_F); 105 MC->mangleName(DeclG, OS_G); 106 } 107 108 ASSERT_TRUE(0 == MangleF.compare("\x01" "foo")); 109 ASSERT_TRUE(0 == MangleG.compare("goo")); 110 } 111