Skip to content

Commit 764e2b8

Browse files
committed
Sink private-file import support down to SerializedASTFile
Implementing it in LoadedFile is nice in theory, but causes a leak in practice because that type is ASTContext-allocated and usually never destroyed. https://bugs.swift.org/browse/SR-11366
1 parent e479e13 commit 764e2b8

File tree

5 files changed

+15
-22
lines changed

5 files changed

+15
-22
lines changed

include/swift/AST/Module.h

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,28 +1387,14 @@ class LoadedFile : public FileUnit {
13871387
: FileUnit(Kind, M) {
13881388
assert(classof(this) && "invalid kind");
13891389
}
1390-
1391-
/// A map from private/fileprivate decls to the file they were defined in.
1392-
llvm::DenseMap<const ValueDecl *, Identifier> FilenameForPrivateDecls;
1393-
13941390
public:
1395-
13961391
/// Returns an arbitrary string representing the storage backing this file.
13971392
///
13981393
/// This is usually a filesystem path.
13991394
virtual StringRef getFilename() const;
14001395

1401-
void addFilenameForPrivateDecl(const ValueDecl *decl, Identifier id) {
1402-
assert(!FilenameForPrivateDecls.count(decl) ||
1403-
FilenameForPrivateDecls[decl] == id);
1404-
FilenameForPrivateDecls[decl] = id;
1405-
}
1406-
1407-
StringRef getFilenameForPrivateDecl(const ValueDecl *decl) {
1408-
auto it = FilenameForPrivateDecls.find(decl);
1409-
if (it == FilenameForPrivateDecls.end())
1410-
return StringRef();
1411-
return it->second.str();
1396+
virtual StringRef getFilenameForPrivateDecl(const ValueDecl *decl) const {
1397+
return StringRef();
14121398
}
14131399

14141400
/// Look up an operator declaration.

include/swift/Serialization/ModuleFile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ class ModuleFile
396396
std::unique_ptr<SerializedObjCMethodTable> ObjCMethods;
397397

398398
llvm::DenseMap<const ValueDecl *, Identifier> PrivateDiscriminatorsByValue;
399+
llvm::DenseMap<const ValueDecl *, StringRef> FilenamesForPrivateValues;
399400

400401
TinyPtrVector<Decl *> ImportDecls;
401402

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ class SerializedASTFile final : public LoadedFile {
274274
DeclName name, NLKind lookupKind,
275275
SmallVectorImpl<ValueDecl*> &results) const override;
276276

277+
virtual StringRef
278+
getFilenameForPrivateDecl(const ValueDecl *decl) const override;
279+
277280
virtual TypeDecl *lookupLocalType(StringRef MangledName) const override;
278281

279282
virtual OpaqueTypeDecl *

lib/Serialization/Deserialization.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,7 @@ class swift::DeclDeserializer {
22542254

22552255
Identifier privateDiscriminator;
22562256
unsigned localDiscriminator = 0;
2257-
Identifier filenameForPrivate;
2257+
StringRef filenameForPrivate;
22582258

22592259
void AddAttribute(DeclAttribute *Attr) {
22602260
// Advance the linked list.
@@ -2308,10 +2308,8 @@ class swift::DeclDeserializer {
23082308
if (localDiscriminator != 0)
23092309
value->setLocalDiscriminator(localDiscriminator);
23102310

2311-
if (!filenameForPrivate.empty()) {
2312-
auto *loadedFile = cast<LoadedFile>(MF.getFile());
2313-
loadedFile->addFilenameForPrivateDecl(value, filenameForPrivate);
2314-
}
2311+
if (!filenameForPrivate.empty())
2312+
MF.FilenamesForPrivateValues[value] = filenameForPrivate;
23152313
}
23162314

23172315
decl->setValidationToChecked();
@@ -4202,7 +4200,7 @@ llvm::Error DeclDeserializer::deserializeDeclAttributes() {
42024200
} else if (recordID == decls_block::FILENAME_FOR_PRIVATE) {
42034201
IdentifierID filenameID;
42044202
decls_block::FilenameForPrivateLayout::readRecord(scratch, filenameID);
4205-
filenameForPrivate = MF.getIdentifier(filenameID);
4203+
filenameForPrivate = MF.getIdentifierText(filenameID);
42064204
} else {
42074205
return llvm::Error::success();
42084206
}

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,11 @@ void SerializedASTFile::lookupValue(ModuleDecl::AccessPathTy accessPath,
955955
File.lookupValue(name, results);
956956
}
957957

958+
StringRef
959+
SerializedASTFile::getFilenameForPrivateDecl(const ValueDecl *decl) const {
960+
return File.FilenamesForPrivateValues.lookup(decl);
961+
}
962+
958963
TypeDecl *SerializedASTFile::lookupLocalType(llvm::StringRef MangledName) const{
959964
return File.lookupLocalType(MangledName);
960965
}

0 commit comments

Comments
 (0)