Skip to content

Commit 92fd41c

Browse files
committed
Cache failed clang::Decl's so we can avoid reparsing
1 parent d3d2d75 commit 92fd41c

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3342,7 +3342,8 @@ std::string ClangImporter::getClangModuleHash() const {
33423342
}
33433343

33443344
Decl *ClangImporter::importDeclCached(const clang::NamedDecl *ClangDecl) {
3345-
return Impl.importDeclCached(ClangDecl, Impl.CurrentVersion);
3345+
return Impl.importDeclCached(ClangDecl, Impl.CurrentVersion)
3346+
.getValueOr(nullptr);
33463347
}
33473348

33483349
void ClangImporter::printStatistics() const {

lib/ClangImporter/ImportDecl.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4587,7 +4587,8 @@ namespace {
45874587

45884588
// While importing the DeclContext, we might have imported the decl
45894589
// itself.
4590-
if (auto Known = Impl.importDeclCached(decl, getVersion()))
4590+
if (auto Known =
4591+
Impl.importDeclCached(decl, getVersion()).getValueOr(nullptr))
45914592
return Known;
45924593

45934594
ImportedName importedName;
@@ -5615,7 +5616,8 @@ namespace {
56155616

56165617
// While importing the DeclContext, we might have imported the decl
56175618
// itself.
5618-
if (auto Known = Impl.importDeclCached(decl, getVersion()))
5619+
if (auto Known =
5620+
Impl.importDeclCached(decl, getVersion()).getValueOr(nullptr))
56195621
return Known;
56205622

56215623
return importObjCPropertyDecl(decl, dc);
@@ -8271,16 +8273,16 @@ void SwiftDeclConverter::importInheritedConstructors(
82718273
}
82728274
}
82738275

8274-
Decl *ClangImporter::Implementation::importDeclCached(
8275-
const clang::NamedDecl *ClangDecl,
8276-
ImportNameVersion version,
8276+
Optional<Decl *> ClangImporter::Implementation::importDeclCached(
8277+
const clang::NamedDecl *ClangDecl, ImportNameVersion version,
82778278
bool UseCanonical) {
8278-
auto Known = ImportedDecls.find(
8279-
{ UseCanonical? ClangDecl->getCanonicalDecl(): ClangDecl, version });
8279+
std::pair<const clang::Decl *, Version> Key = {
8280+
UseCanonical ? ClangDecl->getCanonicalDecl() : ClangDecl, version};
8281+
auto Known = ImportedDecls.find(Key);
82808282
if (Known != ImportedDecls.end())
82818283
return Known->second;
82828284

8283-
return nullptr;
8285+
return None;
82848286
}
82858287

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

90239025
auto Canon = cast<clang::NamedDecl>(UseCanonicalDecl? ClangDecl->getCanonicalDecl(): ClangDecl);
9024-
9025-
if (auto Known = importDeclCached(Canon, version, UseCanonicalDecl)) {
9026-
if (!SuperfluousTypedefsAreTransparent &&
9027-
SuperfluousTypedefs.count(Canon))
9026+
if (Optional<Decl *> Known =
9027+
importDeclCached(Canon, version, UseCanonicalDecl)) {
9028+
if (!SuperfluousTypedefsAreTransparent && SuperfluousTypedefs.count(Canon))
90289029
return nullptr;
9029-
return Known;
9030+
return Known.getValue();
90309031
}
90319032

90329033
bool TypedefIsSuperfluous = false;
@@ -9035,8 +9036,10 @@ Decl *ClangImporter::Implementation::importDeclAndCacheImpl(
90359036
startedImportingEntity();
90369037
Decl *Result = importDeclImpl(ClangDecl, version, TypedefIsSuperfluous,
90379038
HadForwardDeclaration);
9038-
if (!Result)
9039+
if (!Result) {
9040+
ImportedDecls[{Canon, version}] = nullptr;
90399041
return nullptr;
9042+
}
90409043

90419044
if (TypedefIsSuperfluous) {
90429045
SuperfluousTypedefs.insert(Canon);

lib/ClangImporter/ImporterImpl.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
415415
llvm::SmallDenseMap<ModuleDecl *, SourceFile *> ClangSwiftAttrSourceFiles;
416416

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

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

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

857859
Decl *importDeclImpl(const clang::NamedDecl *ClangDecl, Version version,
858860
bool &TypedefIsSuperfluous, bool &HadForwardDeclaration);

lib/ClangImporter/Serializability.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class SerializationPathFinder {
6666

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

0 commit comments

Comments
 (0)