Skip to content

Commit cddb225

Browse files
committed
[ClangImporter] Assert on AlternativeDecl insertion.
Adds assertion that we're not clobbering existing alternative decls, and removes all naked accesses. NFC.
1 parent 15f5367 commit cddb225

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3360,13 +3360,13 @@ namespace {
33603360
// declaration.
33613361
if (auto imported = VisitObjCMethodDecl(decl, dc,
33623362
/*forceClassMethod=*/true))
3363-
Impl.AlternateDecls[result] = cast<ValueDecl>(imported);
3363+
Impl.setAlternateDecl(result, cast<ValueDecl>(imported));
33643364
} else if (auto factory = importFactoryMethodAsConstructor(
33653365
result, decl, selector, dc)) {
33663366
// We imported the factory method as an initializer, so
33673367
// record it as an alternate declaration.
33683368
if (*factory)
3369-
Impl.AlternateDecls[result] = *factory;
3369+
Impl.setAlternateDecl(result, *factory);
33703370
}
33713371

33723372
}
@@ -4971,7 +4971,7 @@ SwiftDeclConverter::getImplicitProperty(ImportedName importedName,
49714971
SourceLoc());
49724972

49734973
// Make the property the alternate declaration for the getter.
4974-
Impl.AlternateDecls[swiftGetter] = property;
4974+
Impl.setAlternateDecl(swiftGetter, property);
49754975

49764976
return property;
49774977
}
@@ -5021,7 +5021,7 @@ SwiftDeclConverter::importFactoryMethodAsConstructor(
50215021
/// Record the initializer as an alternative declaration for the
50225022
/// member.
50235023
if (result) {
5024-
Impl.AlternateDecls[member] = result;
5024+
Impl.setAlternateDecl(member, result);
50255025

50265026
if (swift3Name)
50275027
markAsSwift2Variant(result, *swift3Name);
@@ -5736,7 +5736,7 @@ SwiftDeclConverter::importSubscript(Decl *decl,
57365736
TypeLoc::withoutLoc(elementContextTy), dc);
57375737

57385738
/// Record the subscript as an alternative declaration.
5739-
Impl.AlternateDecls[associateWithSetter ? setter : getter] = subscript;
5739+
Impl.setAlternateDecl(associateWithSetter ? setter : getter, subscript);
57405740

57415741
subscript->makeComputed(SourceLoc(), getterThunk, setterThunk, nullptr,
57425742
SourceLoc());

lib/ClangImporter/ImporterImpl.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,11 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
427427
}
428428
};
429429

430+
/// A mapping from imported declarations to their "alternate" declarations,
431+
/// for cases where a single Clang declaration is imported to two
432+
/// different Swift declarations.
433+
llvm::DenseMap<Decl *, ValueDecl *> AlternateDecls;
434+
430435
public:
431436
/// \brief Keep track of enum constant values that have been imported.
432437
llvm::DenseMap<std::pair<const clang::EnumDecl *, llvm::APSInt>,
@@ -440,11 +445,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
440445
ConstructorDecl *>
441446
Constructors;
442447

443-
/// A mapping from imported declarations to their "alternate" declarations,
444-
/// for cases where a single Clang declaration is imported to two
445-
/// different Swift declarations.
446-
llvm::DenseMap<Decl *, ValueDecl *> AlternateDecls;
447-
448448
/// Retrieve the alternative declaration for the given imported
449449
/// Swift declaration.
450450
ValueDecl *getAlternateDecl(Decl *decl) {
@@ -453,6 +453,14 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
453453
return known->second;
454454
}
455455

456+
/// Set the alternative decl
457+
void setAlternateDecl(Decl *forDecl, ValueDecl *altDecl) {
458+
assert((!AlternateDecls.count(forDecl) ||
459+
AlternateDecls[forDecl] == altDecl) &&
460+
"clobbering already-set alternative");
461+
AlternateDecls[forDecl] = altDecl;
462+
}
463+
456464
private:
457465
/// \brief NSObject, imported into Swift.
458466
Type NSObjectTy;

0 commit comments

Comments
 (0)