DeclTest.cpp 4.0 KB

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