Skip to content

Commit 15fe681

Browse files
committed
[Serialized diagnostics] Eliminate some undefined behavior.
When emitting generated source file buffers, we could end up reallocating the DenseMap that tracks file IDs while still holding a reference to it. Hilarity ensues. Stop that hilarity by taking a copy. Additionally, make sure we don't emit an invalid source range, and instead put in empty source locations.
1 parent c431221 commit 15fe681

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

lib/Frontend/SerializedDiagnosticConsumer.cpp

+15-9
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,15 @@ unsigned SerializedDiagnosticConsumer::getEmitFile(
225225
// NOTE: Using Filename.data() here relies on SourceMgr using
226226
// const char* as buffer identifiers. This is fast, but may
227227
// be brittle. We can always switch over to using a StringMap.
228-
unsigned &entry = State->Files[Filename.data()];
229-
if (entry)
230-
return entry;
228+
unsigned &existingEntry = State->Files[Filename.data()];
229+
if (existingEntry)
230+
return existingEntry;
231231

232232
// Lazily generate the record for the file. Note that in
233233
// practice we only expect there to be one file, but this is
234234
// general and is what the diagnostic file expects.
235-
entry = State->Files.size();
235+
unsigned entry = State->Files.size();
236+
existingEntry = entry;
236237
RecordData Record;
237238
Record.push_back(RECORD_FILENAME);
238239
Record.push_back(entry);
@@ -255,11 +256,16 @@ unsigned SerializedDiagnosticConsumer::getEmitFile(
255256
// The source range that this buffer was generated from, expressed as
256257
// offsets into the original buffer.
257258
auto originalFilename = SM.getDisplayNameForLoc(generatedInfo->originalSourceRange.Start);
258-
addRangeToRecord(
259-
Lexer::getCharSourceRangeFromSourceRange(
260-
SM, generatedInfo->originalSourceRange),
261-
SM, originalFilename, Record
262-
);
259+
if (generatedInfo->originalSourceRange.isValid()) {
260+
addRangeToRecord(
261+
Lexer::getCharSourceRangeFromSourceRange(
262+
SM, generatedInfo->originalSourceRange),
263+
SM, originalFilename, Record
264+
);
265+
} else {
266+
addLocToRecord(SourceLoc(), SM, originalFilename, Record); // Start
267+
addLocToRecord(SourceLoc(), SM, originalFilename, Record); // End
268+
}
263269

264270
// Contents of the buffer.
265271
auto sourceText = SM.getEntireTextForBuffer(bufferID);

0 commit comments

Comments
 (0)