Ericsson / clang

Cross Translation Unit analysis capability for Clang Static Analyzer. (Fork of official clang at http://llvm.org/git/clang)
http://clang.llvm.org/
Other
15 stars 10 forks source link

VarDecl init expr and definition is not imported properly #477

Closed martong closed 6 years ago

martong commented 6 years ago

The init expression of a VarDecl is overwritten in the "To" context if we import a VarDecl without an init expression (and with a definition). The following unittest fails:

TEST_P(ASTImporterTestBase,
       Maci) {
  auto StructA =
      R"(
      struct A {
        static const int a = 1 + 2;
      };
      )";
  Decl *ToTU = getToTuDecl(StructA, Lang_CXX);
  Decl *FromTU =
      getTuDecl(std::string(StructA) + "const int A::a;", Lang_CXX, "input1.cc");

  auto *FromD0 = FirstDeclMatcher<VarDecl>().match(FromTU, varDecl(hasName("a"))); // Decl with init
  auto *FromD1 = LastDeclMatcher<VarDecl>().match(FromTU, varDecl(hasName("a"))); // Decl with definition
  ASSERT_EQ(FromD0, FromD1->getPreviousDecl());
  ASSERT_TRUE(FromD0->getInit());
  ASSERT_FALSE(FromD0->isThisDeclarationADefinition());
  ASSERT_TRUE(FromD1->isThisDeclarationADefinition());
  ASSERT_FALSE(FromD1->getInit());

  auto *ToD = FirstDeclMatcher<VarDecl>().match(ToTU, varDecl(hasName("a"))); // Decl with init
  ASSERT_TRUE(ToD->getInit());
  ASSERT_FALSE(ToD->getDefinition());

  auto *ImportedD = cast<VarDecl>(Import(FromD1, Lang_CXX11));
  EXPECT_TRUE(ImportedD->getAnyInitializer());
  EXPECT_TRUE(ImportedD->getDefinition());
  //EXPECT_TRUE(false);
}