xref: /aosp_15_r20/external/clang/unittests/AST/DeclTest.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===- unittests/AST/DeclTest.cpp --- Declaration tests -------------------===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // Unit tests for Decl nodes in the AST.
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li 
14*67e74705SXin Li #include "clang/ASTMatchers/ASTMatchFinder.h"
15*67e74705SXin Li #include "clang/Tooling/Tooling.h"
16*67e74705SXin Li #include "gtest/gtest.h"
17*67e74705SXin Li 
18*67e74705SXin Li using namespace clang::ast_matchers;
19*67e74705SXin Li using namespace clang::tooling;
20*67e74705SXin Li 
TEST(Decl,CleansUpAPValues)21*67e74705SXin Li TEST(Decl, CleansUpAPValues) {
22*67e74705SXin Li   MatchFinder Finder;
23*67e74705SXin Li   std::unique_ptr<FrontendActionFactory> Factory(
24*67e74705SXin Li       newFrontendActionFactory(&Finder));
25*67e74705SXin Li 
26*67e74705SXin Li   // This is a regression test for a memory leak in APValues for structs that
27*67e74705SXin Li   // allocate memory. This test only fails if run under valgrind with full leak
28*67e74705SXin Li   // checking enabled.
29*67e74705SXin Li   std::vector<std::string> Args(1, "-std=c++11");
30*67e74705SXin Li   Args.push_back("-fno-ms-extensions");
31*67e74705SXin Li   ASSERT_TRUE(runToolOnCodeWithArgs(
32*67e74705SXin Li       Factory->create(),
33*67e74705SXin Li       "struct X { int a; }; constexpr X x = { 42 };"
34*67e74705SXin Li       "union Y { constexpr Y(int a) : a(a) {} int a; }; constexpr Y y = { 42 };"
35*67e74705SXin Li       "constexpr int z[2] = { 42, 43 };"
36*67e74705SXin Li       "constexpr int __attribute__((vector_size(16))) v1 = {};"
37*67e74705SXin Li       "\n#ifdef __SIZEOF_INT128__\n"
38*67e74705SXin Li       "constexpr __uint128_t large_int = 0xffffffffffffffff;"
39*67e74705SXin Li       "constexpr __uint128_t small_int = 1;"
40*67e74705SXin Li       "\n#endif\n"
41*67e74705SXin Li       "constexpr double d1 = 42.42;"
42*67e74705SXin Li       "constexpr long double d2 = 42.42;"
43*67e74705SXin Li       "constexpr _Complex long double c1 = 42.0i;"
44*67e74705SXin Li       "constexpr _Complex long double c2 = 42.0;"
45*67e74705SXin Li       "template<int N> struct A : A<N-1> {};"
46*67e74705SXin Li       "template<> struct A<0> { int n; }; A<50> a;"
47*67e74705SXin Li       "constexpr int &r = a.n;"
48*67e74705SXin Li       "constexpr int A<50>::*p = &A<50>::n;"
49*67e74705SXin Li       "void f() { foo: bar: constexpr int k = __builtin_constant_p(0) ?"
50*67e74705SXin Li       "                         (char*)&&foo - (char*)&&bar : 0; }",
51*67e74705SXin Li       Args));
52*67e74705SXin Li 
53*67e74705SXin Li   // FIXME: Once this test starts breaking we can test APValue::needsCleanup
54*67e74705SXin Li   // for ComplexInt.
55*67e74705SXin Li   ASSERT_FALSE(runToolOnCodeWithArgs(
56*67e74705SXin Li       Factory->create(),
57*67e74705SXin Li       "constexpr _Complex __uint128_t c = 0xffffffffffffffff;",
58*67e74705SXin Li       Args));
59*67e74705SXin Li }
60