Skip to content

Commit fefa937

Browse files
committed
De-register temporary buffer 'SourceFile's created during availability attribute argument parsing
Otherwise these source file objects linger in the source manager but they refer to a fully-temporary 'ASTContext' which does not exist after parsing is complete, which means they cannot be used in any way and attempting to do so would lead to a crash.
1 parent 71daf47 commit fefa937

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

include/swift/Basic/SourceManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ class SourceManager {
381381
/// Record the source file as having the given buffer ID.
382382
void recordSourceFile(unsigned bufferID, SourceFile *sourceFile);
383383

384+
/// Remove the source file for the given buffer ID from records.
385+
void deleteSourceFile(unsigned bufferID);
386+
384387
/// Retrieve the source files for the given buffer ID.
385388
llvm::TinyPtrVector<SourceFile *>
386389
getSourceFilesForBufferID(unsigned bufferID) const;

lib/Basic/SourceLoc.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ void SourceManager::recordSourceFile(unsigned bufferID, SourceFile *sourceFile){
219219
bufferIDToSourceFiles[bufferID].push_back(sourceFile);
220220
}
221221

222+
void SourceManager::deleteSourceFile(unsigned bufferID){
223+
bufferIDToSourceFiles.erase(bufferID);
224+
}
225+
222226
llvm::TinyPtrVector<SourceFile *>
223227
SourceManager::getSourceFilesForBufferID(unsigned bufferID) const {
224228
auto found = bufferIDToSourceFiles.find(bufferID);

lib/Parse/Parser.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,8 @@ struct ParserUnit::Implementation {
11991199
SerializationOptions SerializationOpts;
12001200
DiagnosticEngine Diags;
12011201
ASTContext &Ctx;
1202+
SourceManager &SM;
1203+
unsigned BufferID;
12021204
SourceFile *SF;
12031205
std::unique_ptr<Parser> TheParser;
12041206

@@ -1208,7 +1210,8 @@ struct ParserUnit::Implementation {
12081210
SILOpts(SILOptions()), Diags(SM),
12091211
Ctx(*ASTContext::get(LangOpts, TypeCheckerOpts, SILOpts, SearchPathOpts,
12101212
clangImporterOpts, symbolGraphOpts, CASOpts,
1211-
SerializationOpts, SM, Diags)) {
1213+
SerializationOpts, SM, Diags)),
1214+
SM(SM), BufferID(BufferID) {
12121215
registerParseRequestFunctions(Ctx.evaluator);
12131216

12141217
auto parsingOpts = SourceFile::getDefaultParsingOptions(LangOpts);
@@ -1222,6 +1225,7 @@ struct ParserUnit::Implementation {
12221225

12231226
~Implementation() {
12241227
TheParser.reset();
1228+
SM.deleteSourceFile(BufferID);
12251229
delete &Ctx;
12261230
}
12271231
};

0 commit comments

Comments
 (0)