[clang][ASTImporter] Add isNewDecl

Summary:
Add a new function with which we can query if a Decl had been newly
created during the import process. This feature is a must if we want to
have a different static analysis strategy for such newly created
declarations.

This is a dependent patch that is needed for the new CTU implementation
discribed at
https://discourse.llvm.org/t/rfc-much-faster-cross-translation-unit-ctu-analysis-implementation/61728

Differential Revision:
https://reviews.llvm.org/D123685
This commit is contained in:
Gabor Marton 2022-05-13 14:57:14 +02:00
parent fcfb86483b
commit 25ac078a96
3 changed files with 40 additions and 0 deletions

View File

@ -38,6 +38,9 @@ class ASTImporterSharedState {
/// never cleared (like ImportedFromDecls).
llvm::DenseMap<Decl *, ImportError> ImportErrors;
/// Set of the newly created declarations.
llvm::DenseSet<Decl *> NewDecls;
// FIXME put ImportedFromDecls here!
// And from that point we can better encapsulate the lookup table.
@ -73,6 +76,10 @@ public:
void setImportDeclError(Decl *To, ImportError Error) {
ImportErrors[To] = Error;
}
bool isNewDecl(const Decl *ToD) const { return NewDecls.count(ToD); }
void markAsNewDecl(Decl *ToD) { NewDecls.insert(ToD); }
};
} // namespace clang

View File

@ -285,6 +285,7 @@ namespace clang {
ToD = CreateFun(std::forward<Args>(args)...);
// Keep track of imported Decls.
Importer.RegisterImportedDecl(FromD, ToD);
Importer.SharedState->markAsNewDecl(ToD);
InitializeImportedDecl(FromD, ToD);
return false; // A new Decl is created.
}

View File

@ -7698,6 +7698,38 @@ TEST_P(ASTImporterOptionSpecificTestBase,
EXPECT_TRUE(ToX->getInClassInitializer());
}
TEST_P(ASTImporterOptionSpecificTestBase, isNewDecl) {
Decl *FromTU = getTuDecl(
R"(
int bar() {
return 0;
}
void other() {
bar();
}
)",
Lang_CXX11);
Decl *ToTU = getToTuDecl(
R"(
int bar() {
return 0;
}
)",
Lang_CXX11);
auto *FromOther = FirstDeclMatcher<FunctionDecl>().match(
FromTU, functionDecl(hasName("other")));
ASSERT_TRUE(FromOther);
auto *ToOther = Import(FromOther, Lang_CXX11);
ASSERT_TRUE(ToOther);
auto *ToBar = FirstDeclMatcher<FunctionDecl>().match(
ToTU, functionDecl(hasName("bar")));
EXPECT_TRUE(SharedStatePtr->isNewDecl(ToOther));
EXPECT_FALSE(SharedStatePtr->isNewDecl(ToBar));
}
INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
DefaultTestValuesForRunOptions);