@@ -304,17 +304,27 @@ static SmallVector<unsigned, 1> getSourceBufferStack(
304304 }
305305}
306306
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+
307322void PrintingDiagnosticConsumer::queueBuffer (
308323 SourceManager &sourceMgr, unsigned bufferID) {
309324 QueuedBuffer knownSourceFile = queuedBuffers[bufferID];
310325 if (knownSourceFile)
311326 return ;
312327
313- auto bufferContents = sourceMgr.getEntireTextForBuffer (bufferID);
314- auto sourceFile = swift_ASTGen_parseSourceFile (
315- bufferContents.data (), bufferContents.size (),
316- " module" , " file.swift" , /* ctx*/ nullptr );
317-
318328 // Find the parent and position in parent, if there is one.
319329 int parentID = -1 ;
320330 int positionInParent = 0 ;
@@ -345,6 +355,7 @@ void PrintingDiagnosticConsumer::queueBuffer(
345355 sourceMgr.getLocForBufferStart (bufferID)).str ();
346356 }
347357
358+ auto sourceFile = getSourceFileSyntax (sourceMgr, bufferID, displayName);
348359 swift_ASTGen_addQueuedSourceFile (
349360 queuedDiagnostics, bufferID, sourceFile,
350361 (const uint8_t *)displayName.data (), displayName.size (),
@@ -423,9 +434,6 @@ void PrintingDiagnosticConsumer::flush(bool includeTrailingBreak) {
423434 }
424435 swift_ASTGen_destroyQueuedDiagnostics (queuedDiagnostics);
425436 queuedDiagnostics = nullptr ;
426- for (const auto &buffer : queuedBuffers) {
427- swift_ASTGen_destroySourceFile (buffer.second );
428- }
429437 queuedBuffers.clear ();
430438
431439 if (includeTrailingBreak)
@@ -566,4 +574,9 @@ SourceManager::GetMessage(SourceLoc Loc, llvm::SourceMgr::DiagKind Kind,
566574PrintingDiagnosticConsumer::PrintingDiagnosticConsumer (
567575 llvm::raw_ostream &stream)
568576 : 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