Skip to content

Commit

Permalink
[clang][ASTImporter] Add isNewDecl
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Gabor Marton committed May 18, 2022
1 parent fcfb864 commit 25ac078
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions clang/include/clang/AST/ASTImporterSharedState.h
Expand Up @@ -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.

Expand Down Expand Up @@ -73,6 +76,10 @@ class ASTImporterSharedState {
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
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/ASTImporter.cpp
Expand Up @@ -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.
}
Expand Down
32 changes: 32 additions & 0 deletions clang/unittests/AST/ASTImporterTest.cpp
Expand Up @@ -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);

Expand Down

0 comments on commit 25ac078

Please sign in to comment.