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