Skip to content

Commit c501131

Browse files
authored
Merge pull request #26816 from jrose-apple/cant-stand-the-rain
Fix a pair of leaks related to FileUnit destructors not being run
2 parents aed25d1 + 5c785d4 commit c501131

File tree

12 files changed

+46
-62
lines changed

12 files changed

+46
-62
lines changed

include/swift/AST/Module.h

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,10 @@ static inline unsigned alignOfFileUnit();
644644
/// FileUnit is an abstract base class; its subclasses represent different
645645
/// sorts of containers that can each provide a set of decls, e.g. a source
646646
/// file. A module can contain several file-units.
647+
#pragma clang diagnostic push
648+
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
647649
class FileUnit : public DeclContext {
650+
#pragma clang diagnostic pop
648651
virtual void anchor();
649652

650653
// FIXME: Stick this in a PointerIntPair.
@@ -655,8 +658,6 @@ class FileUnit : public DeclContext {
655658
: DeclContext(DeclContextKind::FileUnit, &M), Kind(kind) {
656659
}
657660

658-
virtual ~FileUnit() = default;
659-
660661
public:
661662
FileUnitKind getKind() const {
662663
return Kind;
@@ -874,10 +875,6 @@ class FileUnit : public DeclContext {
874875
return getParentModule()->getName().str();
875876
}
876877

877-
/// If this is a module imported from a parseable interface, return the path
878-
/// to the interface file, otherwise an empty StringRef.
879-
virtual StringRef getParseableInterface() const { return {}; }
880-
881878
/// Traverse the decls within this file.
882879
///
883880
/// \returns true if traversal was aborted, false if it completed
@@ -897,13 +894,7 @@ class FileUnit : public DeclContext {
897894
// Make placement new and vanilla new/delete illegal for FileUnits.
898895
void *operator new(size_t Bytes) throw() = delete;
899896
void *operator new(size_t Bytes, void *Mem) throw() = delete;
900-
901-
protected:
902-
// Unfortunately we can't remove this altogether because the virtual
903-
// destructor requires it to be accessible.
904-
void operator delete(void *Data) throw() {
905-
llvm_unreachable("Don't use operator delete on a SourceFile");
906-
}
897+
void operator delete(void *Data) throw() = delete;
907898

908899
public:
909900
// Only allow allocation of FileUnits using the allocator in ASTContext
@@ -1386,35 +1377,23 @@ class BuiltinUnit final : public FileUnit {
13861377
};
13871378

13881379
/// Represents an externally-loaded file of some kind.
1380+
#pragma clang diagnostic push
1381+
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
13891382
class LoadedFile : public FileUnit {
1383+
#pragma clang diagnostic pop
13901384
protected:
1391-
~LoadedFile() = default;
13921385
LoadedFile(FileUnitKind Kind, ModuleDecl &M) noexcept
13931386
: FileUnit(Kind, M) {
13941387
assert(classof(this) && "invalid kind");
13951388
}
1396-
1397-
/// A map from private/fileprivate decls to the file they were defined in.
1398-
llvm::DenseMap<const ValueDecl *, Identifier> FilenameForPrivateDecls;
1399-
14001389
public:
1401-
14021390
/// Returns an arbitrary string representing the storage backing this file.
14031391
///
14041392
/// This is usually a filesystem path.
14051393
virtual StringRef getFilename() const;
14061394

1407-
void addFilenameForPrivateDecl(const ValueDecl *decl, Identifier id) {
1408-
assert(!FilenameForPrivateDecls.count(decl) ||
1409-
FilenameForPrivateDecls[decl] == id);
1410-
FilenameForPrivateDecls[decl] = id;
1411-
}
1412-
1413-
StringRef getFilenameForPrivateDecl(const ValueDecl *decl) {
1414-
auto it = FilenameForPrivateDecls.find(decl);
1415-
if (it == FilenameForPrivateDecls.end())
1416-
return StringRef();
1417-
return it->second.str();
1395+
virtual StringRef getFilenameForPrivateDecl(const ValueDecl *decl) const {
1396+
return StringRef();
14181397
}
14191398

14201399
/// Look up an operator declaration.

include/swift/ClangImporter/ClangModule.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ class ClangModuleUnit final : public LoadedFile {
3939
/// The metadata of the underlying Clang module.
4040
clang::ExternalASTSource::ASTSourceDescriptor ASTSourceDescriptor;
4141

42-
~ClangModuleUnit() = default;
43-
4442
public:
4543
/// True if the given Module contains an imported Clang module unit.
4644
static bool hasClangModule(ModuleDecl *M);

include/swift/Serialization/ModuleFile.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class ModuleFile
7878
/// The target the module was built for.
7979
StringRef TargetTriple;
8080

81+
/// The name of the module interface this module was compiled from.
82+
///
83+
/// Empty if this module didn't come from an interface file.
84+
StringRef ModuleInterfacePath;
85+
8186
/// The Swift compatibility version in use when this module was built.
8287
version::Version CompatibilityVersion;
8388

@@ -391,6 +396,7 @@ class ModuleFile
391396
std::unique_ptr<SerializedObjCMethodTable> ObjCMethods;
392397

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

395401
TinyPtrVector<Decl *> ImportDecls;
396402

@@ -762,6 +768,8 @@ class ModuleFile
762768
void getDisplayDecls(SmallVectorImpl<Decl*> &results);
763769

764770
StringRef getModuleFilename() const {
771+
if (!ModuleInterfacePath.empty())
772+
return ModuleInterfacePath;
765773
// FIXME: This seems fragile, maybe store the filename separately ?
766774
return ModuleInputBuffer->getBufferIdentifier();
767775
}

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,8 @@ class SerializedASTFile final : public LoadedFile {
251251

252252
ModuleFile &File;
253253

254-
/// The parseable interface this module was generated from if any.
255-
/// Used for debug info.
256-
std::string ParseableInterface;
257-
258254
bool IsSIB;
259255

260-
~SerializedASTFile() = default;
261-
262256
SerializedASTFile(ModuleDecl &M, ModuleFile &file, bool isSIB = false)
263257
: LoadedFile(FileUnitKind::SerializedAST, M), File(file), IsSIB(isSIB) {}
264258

@@ -278,6 +272,9 @@ class SerializedASTFile final : public LoadedFile {
278272
DeclName name, NLKind lookupKind,
279273
SmallVectorImpl<ValueDecl*> &results) const override;
280274

275+
virtual StringRef
276+
getFilenameForPrivateDecl(const ValueDecl *decl) const override;
277+
281278
virtual TypeDecl *lookupLocalType(StringRef MangledName) const override;
282279

283280
virtual OpaqueTypeDecl *
@@ -351,13 +348,6 @@ class SerializedASTFile final : public LoadedFile {
351348

352349
virtual const clang::Module *getUnderlyingClangModule() const override;
353350

354-
/// If this is a module imported from a parseable interface, return the path
355-
/// to the interface file, otherwise an empty StringRef.
356-
virtual StringRef getParseableInterface() const override {
357-
return ParseableInterface;
358-
}
359-
void setParseableInterface(StringRef PI) { ParseableInterface = PI; }
360-
361351
virtual bool getAllGenericSignatures(
362352
SmallVectorImpl<GenericSignature*> &genericSignatures)
363353
override;

include/swift/Serialization/Validation.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ struct ValidationInfo {
9292
class ExtendedValidationInfo {
9393
SmallVector<StringRef, 4> ExtraClangImporterOpts;
9494
StringRef SDKPath;
95-
StringRef ParseableInterface;
9695
struct {
9796
unsigned ArePrivateImportsEnabled : 1;
9897
unsigned IsSIB : 1;
@@ -114,8 +113,6 @@ class ExtendedValidationInfo {
114113
void addExtraClangImporterOption(StringRef option) {
115114
ExtraClangImporterOpts.push_back(option);
116115
}
117-
StringRef getParseableInterface() const { return ParseableInterface; }
118-
void setParseableInterface(StringRef PI) { ParseableInterface = PI; }
119116

120117
bool isSIB() const { return Bits.IsSIB; }
121118
void setIsSIB(bool val) {

lib/AST/Module.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555

5656
using namespace swift;
5757

58+
static_assert(IsTriviallyDestructible<FileUnit>::value,
59+
"FileUnits are BumpPtrAllocated; the d'tor may not be called");
60+
static_assert(IsTriviallyDestructible<LoadedFile>::value,
61+
"LoadedFiles are BumpPtrAllocated; the d'tor may not be called");
62+
5863
//===----------------------------------------------------------------------===//
5964
// Builtin Module Name lookup
6065
//===----------------------------------------------------------------------===//
@@ -1249,10 +1254,6 @@ StringRef ModuleDecl::getModuleFilename() const {
12491254
// per-file names. Modules can consist of more than one file.
12501255
StringRef Result;
12511256
for (auto F : getFiles()) {
1252-
Result = F->getParseableInterface();
1253-
if (!Result.empty())
1254-
return Result;
1255-
12561257
if (auto SF = dyn_cast<SourceFile>(F)) {
12571258
if (!Result.empty())
12581259
return StringRef();

lib/ClangImporter/ClangImporter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3168,6 +3168,9 @@ void ClangImporter::verifyAllModules() {
31683168
// ClangModule Implementation
31693169
//===----------------------------------------------------------------------===//
31703170

3171+
static_assert(IsTriviallyDestructible<ClangModuleUnit>::value,
3172+
"ClangModuleUnits are BumpPtrAllocated; the d'tor is not called");
3173+
31713174
ClangModuleUnit::ClangModuleUnit(ModuleDecl &M,
31723175
ClangImporter::Implementation &owner,
31733176
const clang::Module *clangModule)

lib/ClangImporter/DWARFImporter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ void DWARFImporterDelegate::anchor() {}
2020
/// Represents a Clang module that was "imported" from debug info. Since all the
2121
/// loading of types is done on demand, this class is effectively empty.
2222
class DWARFModuleUnit final : public LoadedFile {
23-
~DWARFModuleUnit() = default;
2423
ClangImporter::Implementation &Owner;
2524

2625
public:
@@ -96,6 +95,9 @@ class DWARFModuleUnit final : public LoadedFile {
9695
}
9796
};
9897

98+
static_assert(IsTriviallyDestructible<DWARFModuleUnit>::value,
99+
"DWARFModuleUnits are BumpPtrAllocated; the d'tor is not called");
100+
99101
ModuleDecl *ClangImporter::Implementation::loadModuleDWARF(
100102
SourceLoc importLoc, ArrayRef<std::pair<Identifier, SourceLoc>> path) {
101103
// There's no importing from debug info if no importer is installed.

lib/FrontendTool/FrontendTool.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ static bool emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
268268
for (auto &module : ctxt.LoadedModules) {
269269
ModuleDecl *loadedDecl = module.second;
270270
assert(loadedDecl && "Expected loaded module to be non-null.");
271+
if (loadedDecl == mainModule)
272+
continue;
271273
assert(!loadedDecl->getModuleFilename().empty()
272274
&& "Don't know how to handle modules with empty names.");
273275
pathToModuleDecl.insert(

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/ModuleFile.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ using namespace swift::serialization;
3838
using namespace llvm::support;
3939
using llvm::Expected;
4040

41+
static_assert(IsTriviallyDestructible<SerializedASTFile>::value,
42+
"SerializedASTFiles are BumpPtrAllocated; d'tors are not called");
43+
4144
static bool checkModuleSignature(llvm::BitstreamCursor &cursor,
4245
ArrayRef<unsigned char> signature) {
4346
for (unsigned char byte : signature)
@@ -1352,8 +1355,7 @@ ModuleFile::ModuleFile(
13521355
break;
13531356
}
13541357
case input_block::PARSEABLE_INTERFACE_PATH: {
1355-
if (extInfo)
1356-
extInfo->setParseableInterface(blobData);
1358+
ModuleInterfacePath = blobData;
13571359
break;
13581360
}
13591361
default:

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,6 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
548548
// We've loaded the file. Now try to bring it into the AST.
549549
auto fileUnit = new (Ctx) SerializedASTFile(M, *loadedModuleFile,
550550
extendedInfo.isSIB());
551-
fileUnit->setParseableInterface(extendedInfo.getParseableInterface());
552551
M.addFile(*fileUnit);
553552
if (extendedInfo.isTestable())
554553
M.setTestingEnabled();
@@ -956,6 +955,11 @@ void SerializedASTFile::lookupValue(ModuleDecl::AccessPathTy accessPath,
956955
File.lookupValue(name, results);
957956
}
958957

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

0 commit comments

Comments
 (0)