@@ -304,17 +304,27 @@ static SmallVector<unsigned, 1> getSourceBufferStack(
304
304
}
305
305
}
306
306
307
+ void *PrintingDiagnosticConsumer::getSourceFileSyntax (
308
+ SourceManager &sourceMgr, unsigned bufferID, StringRef displayName) {
309
+ auto known = sourceFileSyntax.find ({&sourceMgr, bufferID});
310
+ if (known != sourceFileSyntax.end ())
311
+ return known->second ;
312
+
313
+ auto bufferContents = sourceMgr.getEntireTextForBuffer (bufferID);
314
+ auto sourceFile = swift_ASTGen_parseSourceFile (
315
+ bufferContents.data (), bufferContents.size (),
316
+ " module" , displayName.str ().c_str (), /* ctx*/ nullptr );
317
+
318
+ sourceFileSyntax[{&sourceMgr, bufferID}] = sourceFile;
319
+ return sourceFile;
320
+ }
321
+
307
322
void PrintingDiagnosticConsumer::queueBuffer (
308
323
SourceManager &sourceMgr, unsigned bufferID) {
309
324
QueuedBuffer knownSourceFile = queuedBuffers[bufferID];
310
325
if (knownSourceFile)
311
326
return ;
312
327
313
- auto bufferContents = sourceMgr.getEntireTextForBuffer (bufferID);
314
- auto sourceFile = swift_ASTGen_parseSourceFile (
315
- bufferContents.data (), bufferContents.size (),
316
- " module" , " file.swift" , /* ctx*/ nullptr );
317
-
318
328
// Find the parent and position in parent, if there is one.
319
329
int parentID = -1 ;
320
330
int positionInParent = 0 ;
@@ -345,6 +355,7 @@ void PrintingDiagnosticConsumer::queueBuffer(
345
355
sourceMgr.getLocForBufferStart (bufferID)).str ();
346
356
}
347
357
358
+ auto sourceFile = getSourceFileSyntax (sourceMgr, bufferID, displayName);
348
359
swift_ASTGen_addQueuedSourceFile (
349
360
queuedDiagnostics, bufferID, sourceFile,
350
361
(const uint8_t *)displayName.data (), displayName.size (),
@@ -423,9 +434,6 @@ void PrintingDiagnosticConsumer::flush(bool includeTrailingBreak) {
423
434
}
424
435
swift_ASTGen_destroyQueuedDiagnostics (queuedDiagnostics);
425
436
queuedDiagnostics = nullptr ;
426
- for (const auto &buffer : queuedBuffers) {
427
- swift_ASTGen_destroySourceFile (buffer.second );
428
- }
429
437
queuedBuffers.clear ();
430
438
431
439
if (includeTrailingBreak)
@@ -566,4 +574,9 @@ SourceManager::GetMessage(SourceLoc Loc, llvm::SourceMgr::DiagKind Kind,
566
574
PrintingDiagnosticConsumer::PrintingDiagnosticConsumer (
567
575
llvm::raw_ostream &stream)
568
576
: Stream(stream) {}
569
- PrintingDiagnosticConsumer::~PrintingDiagnosticConsumer () = default ;
577
+
578
+ PrintingDiagnosticConsumer::~PrintingDiagnosticConsumer () {
579
+ for (const auto &sourceFileSyntax : sourceFileSyntax) {
580
+ swift_ASTGen_destroySourceFile (sourceFileSyntax.second );
581
+ }
582
+ }
0 commit comments