Skip to content

[C++ Interop] Cache failed imports. #36747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3342,7 +3342,8 @@ std::string ClangImporter::getClangModuleHash() const {
}

Decl *ClangImporter::importDeclCached(const clang::NamedDecl *ClangDecl) {
return Impl.importDeclCached(ClangDecl, Impl.CurrentVersion);
return Impl.importDeclCached(ClangDecl, Impl.CurrentVersion)
.getValueOr(nullptr);
}

void ClangImporter::printStatistics() const {
Expand Down
43 changes: 23 additions & 20 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2494,7 +2494,7 @@ namespace {
// case we need to bail here.
auto cachedResult =
Impl.ImportedDecls.find({decl->getCanonicalDecl(), getVersion()});
if (cachedResult != Impl.ImportedDecls.end())
if (cachedResult != Impl.ImportedDecls.end() && cachedResult->second != nullptr)
return cachedResult->second;
dc = cast<ExtensionDecl>(parent)
->getExtendedType()
Expand Down Expand Up @@ -3444,7 +3444,7 @@ namespace {
// this will cause an infinite loop.
auto alreadyImportedResult =
Impl.ImportedDecls.find({decl->getCanonicalDecl(), getVersion()});
if (alreadyImportedResult != Impl.ImportedDecls.end())
if (alreadyImportedResult != Impl.ImportedDecls.end() && alreadyImportedResult->second != nullptr)
return alreadyImportedResult->second;
result = Impl.createDeclWithClangNode<StructDecl>(
decl, AccessLevel::Public, Impl.importSourceLoc(decl->getBeginLoc()),
Expand Down Expand Up @@ -4587,7 +4587,8 @@ namespace {

// While importing the DeclContext, we might have imported the decl
// itself.
if (auto Known = Impl.importDeclCached(decl, getVersion()))
if (auto Known =
Impl.importDeclCached(decl, getVersion()).getValueOr(nullptr))
return Known;

ImportedName importedName;
Expand Down Expand Up @@ -4783,7 +4784,7 @@ namespace {
// methods.
auto known = Impl.ImportedDecls.find({decl->getCanonicalDecl(),
getVersion()});
if (known != Impl.ImportedDecls.end()) {
if (known != Impl.ImportedDecls.end() && known->second != nullptr) {
auto decl = known->second;
if (isAcceptableResult(decl, accessorInfo))
return decl;
Expand Down Expand Up @@ -4929,7 +4930,7 @@ namespace {
// methods.
auto known = Impl.ImportedDecls.find({decl->getCanonicalDecl(),
getVersion()});
if (known != Impl.ImportedDecls.end()) {
if (known != Impl.ImportedDecls.end() && known->second != nullptr) {
auto decl = known->second;
if (isAcceptableResult(decl, accessorInfo))
return decl;
Expand Down Expand Up @@ -5615,7 +5616,8 @@ namespace {

// While importing the DeclContext, we might have imported the decl
// itself.
if (auto Known = Impl.importDeclCached(decl, getVersion()))
if (auto Known =
Impl.importDeclCached(decl, getVersion()).getValueOr(nullptr))
return Known;

return importObjCPropertyDecl(decl, dc);
Expand Down Expand Up @@ -5735,7 +5737,7 @@ namespace {
if (dc == Impl.importDeclContextOf(decl, decl->getDeclContext())) {
auto known = Impl.ImportedDecls.find({decl->getCanonicalDecl(),
getVersion()});
if (known != Impl.ImportedDecls.end())
if (known != Impl.ImportedDecls.end() && known->second != nullptr)
return known->second;
}

Expand Down Expand Up @@ -8271,16 +8273,16 @@ void SwiftDeclConverter::importInheritedConstructors(
}
}

Decl *ClangImporter::Implementation::importDeclCached(
const clang::NamedDecl *ClangDecl,
ImportNameVersion version,
Optional<Decl *> ClangImporter::Implementation::importDeclCached(
const clang::NamedDecl *ClangDecl, ImportNameVersion version,
bool UseCanonical) {
auto Known = ImportedDecls.find(
{ UseCanonical? ClangDecl->getCanonicalDecl(): ClangDecl, version });
if (Known != ImportedDecls.end())
std::pair<const clang::Decl *, Version> Key = {
UseCanonical ? ClangDecl->getCanonicalDecl() : ClangDecl, version};
auto Known = ImportedDecls.find(Key);
if (Known != ImportedDecls.end() && Known->second != nullptr)
return Known->second;

return nullptr;
return None;
}

/// Checks if we don't need to import the typedef itself. If the typedef
Expand Down Expand Up @@ -9021,12 +9023,11 @@ Decl *ClangImporter::Implementation::importDeclAndCacheImpl(
Instance->getSourceManager(), "importing");

auto Canon = cast<clang::NamedDecl>(UseCanonicalDecl? ClangDecl->getCanonicalDecl(): ClangDecl);

if (auto Known = importDeclCached(Canon, version, UseCanonicalDecl)) {
if (!SuperfluousTypedefsAreTransparent &&
SuperfluousTypedefs.count(Canon))
if (Optional<Decl *> Known =
importDeclCached(Canon, version, UseCanonicalDecl)) {
if (!SuperfluousTypedefsAreTransparent && SuperfluousTypedefs.count(Canon))
return nullptr;
return Known;
return Known.getValue();
}

bool TypedefIsSuperfluous = false;
Expand All @@ -9035,8 +9036,10 @@ Decl *ClangImporter::Implementation::importDeclAndCacheImpl(
startedImportingEntity();
Decl *Result = importDeclImpl(ClangDecl, version, TypedefIsSuperfluous,
HadForwardDeclaration);
if (!Result)
if (!Result) {
ImportedDecls[{Canon, version}] = nullptr;
return nullptr;
}

if (TypedefIsSuperfluous) {
SuperfluousTypedefs.insert(Canon);
Expand Down
8 changes: 5 additions & 3 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
llvm::SmallDenseMap<ModuleDecl *, SourceFile *> ClangSwiftAttrSourceFiles;

public:
/// Mapping of already-imported declarations.
/// Mapping of already-imported declarations. If the import failed then it
/// maps to nullptr.
llvm::DenseMap<std::pair<const clang::Decl *, Version>, Decl *> ImportedDecls;

/// The set of "special" typedef-name declarations, which are
Expand Down Expand Up @@ -851,8 +852,9 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation

/// If we already imported a given decl, return the corresponding Swift decl.
/// Otherwise, return nullptr.
Decl *importDeclCached(const clang::NamedDecl *ClangDecl, Version version,
bool UseCanonicalDecl = true);
Optional<Decl *> importDeclCached(const clang::NamedDecl *ClangDecl,
Version version,
bool UseCanonicalDecl = true);

Decl *importDeclImpl(const clang::NamedDecl *ClangDecl, Version version,
bool &TypedefIsSuperfluous, bool &HadForwardDeclaration);
Expand Down
3 changes: 2 additions & 1 deletion lib/ClangImporter/Serializability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class SerializationPathFinder {

StableSerializationPath findImportedPath(const clang::NamedDecl *decl) {
// We've almost certainly imported this declaration, look for it.
if (auto swiftDecl = Impl.importDeclCached(decl, Impl.CurrentVersion)) {
if (auto swiftDecl = Impl.importDeclCached(decl, Impl.CurrentVersion)
.getValueOr(nullptr)) {
// The serialization code doesn't allow us to cross-reference
// typealias declarations directly. We could fix that, but it's
// easier to just avoid doing so and fall into the external-path code.
Expand Down