Skip to content

[c++-interop] For failed imports in ClangImporter, cache them regardless. #41173

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

Merged
Merged
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
9 changes: 6 additions & 3 deletions include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,12 @@ class ClangImporter final : public ClangModuleLoader {

std::string getClangModuleHash() const;

/// If we already imported a given decl, return the corresponding Swift decl.
/// Otherwise, return nullptr.
Decl *importDeclCached(const clang::NamedDecl *ClangDecl);
/// If we already imported a given decl successfully, return the corresponding
/// Swift decl as an Optional<Decl *>, but if we previously tried and failed
/// to import said decl then return nullptr.
/// Otherwise, if we have never encountered this decl previously then return
/// None.
Optional<Decl *> importDeclCached(const clang::NamedDecl *ClangDecl);

// Returns true if it is expected that the macro is ignored.
bool shouldIgnoreMacro(StringRef Name, const clang::MacroInfo *Macro);
Expand Down
3 changes: 2 additions & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3631,7 +3631,8 @@ std::string ClangImporter::getClangModuleHash() const {
return Impl.Invocation->getModuleHash(Impl.Instance->getDiagnostics());
}

Decl *ClangImporter::importDeclCached(const clang::NamedDecl *ClangDecl) {
Optional<Decl *>
ClangImporter::importDeclCached(const clang::NamedDecl *ClangDecl) {
return Impl.importDeclCached(ClangDecl, Impl.CurrentVersion);
}

Expand Down
45 changes: 27 additions & 18 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4841,8 +4841,9 @@ namespace {

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

ImportedName importedName;
std::tie(importedName, std::ignore) = importFullName(decl);
Expand Down Expand Up @@ -4926,8 +4927,11 @@ namespace {
}

private:
static bool isAcceptableResult(Decl *fn,
Optional<AccessorInfo> accessorInfo) {
static bool isAcceptableResultOrNull(Decl *fn,
Optional<AccessorInfo> accessorInfo) {
if (nullptr == fn)
return true;

// We can't safely re-use the same declaration if it disagrees
// in accessor-ness.
auto accessor = dyn_cast<AccessorDecl>(fn);
Expand Down Expand Up @@ -5038,7 +5042,7 @@ namespace {
getVersion()});
if (known != Impl.ImportedDecls.end()) {
auto decl = known->second;
if (isAcceptableResult(decl, accessorInfo))
if (isAcceptableResultOrNull(decl, accessorInfo))
return decl;
}
}
Expand All @@ -5056,7 +5060,7 @@ namespace {
if (isActiveSwiftVersion()) {
if (isMethodAlreadyImported(selector, importedName, isInstance, dc,
[&](AbstractFunctionDecl *fn) {
return isAcceptableResult(fn, accessorInfo);
return isAcceptableResultOrNull(fn, accessorInfo);
})) {
return nullptr;
}
Expand Down Expand Up @@ -5190,7 +5194,7 @@ namespace {
getVersion()});
if (known != Impl.ImportedDecls.end()) {
auto decl = known->second;
if (isAcceptableResult(decl, accessorInfo))
if (isAcceptableResultOrNull(decl, accessorInfo))
return decl;
}
}
Expand Down Expand Up @@ -5885,8 +5889,9 @@ namespace {

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

return importObjCPropertyDecl(decl, dc);
}
Expand Down Expand Up @@ -8589,16 +8594,16 @@ void SwiftDeclConverter::importInheritedConstructors(
}
}

Decl *ClangImporter::Implementation::importDeclCached(
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())
return Known->second;
if (Known == ImportedDecls.end())
return None;

return nullptr;
return Known->second;
}

/// Checks if we don't need to import the typedef itself. If the typedef
Expand Down Expand Up @@ -9451,11 +9456,12 @@ Decl *ClangImporter::Implementation::importDeclAndCacheImpl(

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

if (auto Known = importDeclCached(Canon, version, UseCanonicalDecl)) {
auto Known = importDeclCached(Canon, version, UseCanonicalDecl);
if (Known.hasValue()) {
if (!SuperfluousTypedefsAreTransparent &&
SuperfluousTypedefs.count(Canon))
return nullptr;
return Known;
return Known.getValue();
}

bool TypedefIsSuperfluous = false;
Expand All @@ -9464,8 +9470,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 Expand Up @@ -9633,8 +9641,9 @@ ClangImporter::Implementation::importDeclForDeclContext(

// There's a cycle. Is the declaration imported enough to break the cycle
// gracefully? If so, we'll have it in the decl cache.
if (auto cached = importDeclCached(contextDecl, version, useCanonicalDecl))
return cached;
auto cached = importDeclCached(contextDecl, version, useCanonicalDecl);
if (cached.hasValue())
return cached.getValue();

// Can't break it? Warn and return nullptr, which is at least better than
// stack overflow by recursion.
Expand Down
5 changes: 3 additions & 2 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,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
5 changes: 4 additions & 1 deletion lib/ClangImporter/Serializability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ 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)) {
Optional<Decl *> swiftDeclOpt =
Impl.importDeclCached(decl, Impl.CurrentVersion);
if (swiftDeclOpt.hasValue() && swiftDeclOpt.getValue()) {
auto swiftDecl = swiftDeclOpt.getValue();
// 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
8 changes: 0 additions & 8 deletions test/ClangImporter/attr-swift_name.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ func test(_ i: Int) {
// CHECK: SWIFT_NAME(MutuallyCircularNameA.Inner) @interface MutuallyCircularNameB : NSObject @end
// CHECK-NOT: {{warning|note}}:
// CHECK: note: please report this issue to the owners of 'ObjCIRExtras'
// CHECK-NOT: warning:

// CHECK: warning: cycle detected while resolving 'MutuallyCircularNameA' in swift_name attribute for 'MutuallyCircularNameB'
// CHECK: SWIFT_NAME(MutuallyCircularNameA.Inner) @interface MutuallyCircularNameB : NSObject @end
// CHECK-NOT: {{warning|note}}:
// CHECK: note: while resolving 'MutuallyCircularNameB' in swift_name attribute for 'MutuallyCircularNameA'
// CHECK: SWIFT_NAME(MutuallyCircularNameB.Inner) @interface MutuallyCircularNameA : NSObject @end
// CHECK-NOT: {{warning|note}}:
// CHECK: note: please report this issue to the owners of 'ObjCIRExtras'
// CHECK-NOT: warning:
}