DeclTest.cpp 2.2 KB

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