1515// ===----------------------------------------------------------------------===//
1616
1717#include " swift/Frontend/DiagnosticVerifier.h"
18+ #include " swift/Basic/ColorUtils.h"
1819#include " swift/Basic/SourceManager.h"
1920#include " swift/Parse/Lexer.h"
2021#include " llvm/ADT/STLExtras.h"
@@ -328,11 +329,11 @@ static void autoApplyFixes(SourceManager &SM, unsigned BufferID,
328329 if (!error)
329330 outs << Result;
330331}
332+ } // end anonymous namespace
331333
332334// / diagnostics for '<unknown>:0' should be considered as unexpected.
333- static bool
334- verifyUnknown (SourceManager &SM,
335- std::vector<CapturedDiagnosticInfo> &CapturedDiagnostics) {
335+ bool DiagnosticVerifier::verifyUnknown (
336+ std::vector<CapturedDiagnosticInfo> &CapturedDiagnostics) const {
336337 bool HadError = false ;
337338 for (unsigned i = 0 , e = CapturedDiagnostics.size (); i != e; ++i) {
338339 if (CapturedDiagnostics[i].Loc .isValid ())
@@ -346,11 +347,10 @@ verifyUnknown(SourceManager &SM,
346347 .str ();
347348
348349 auto diag = SM.GetMessage ({}, llvm::SourceMgr::DK_Error, Message, {}, {});
349- SM. getLLVMSourceMgr (). PrintMessage ( llvm::errs (), diag);
350+ printDiagnostic ( diag);
350351 }
351352 return HadError;
352353}
353- } // end anonymous namespace
354354
355355// / Return true if the given \p ExpectedFixIt is in the fix-its emitted by
356356// / diagnostic \p D.
@@ -377,6 +377,13 @@ bool DiagnosticVerifier::checkForFixIt(
377377 return false ;
378378}
379379
380+ void DiagnosticVerifier::printDiagnostic (const llvm::SMDiagnostic &Diag) const {
381+ raw_ostream &stream = llvm::errs ();
382+ ColoredStream coloredStream{stream};
383+ raw_ostream &out = UseColor ? coloredStream : stream;
384+ SM.getLLVMSourceMgr ().PrintMessage (out, Diag);
385+ }
386+
380387std::string
381388DiagnosticVerifier::renderFixits (ArrayRef<CapturedFixItInfo> ActualFixIts,
382389 unsigned BufferID,
@@ -1184,7 +1191,7 @@ DiagnosticVerifier::Result DiagnosticVerifier::verifyFile(unsigned BufferID) {
11841191
11851192 // Emit all of the queue'd up errors.
11861193 for (auto Err : Errors)
1187- SM. getLLVMSourceMgr (). PrintMessage ( llvm::errs (), Err);
1194+ printDiagnostic ( Err);
11881195
11891196 // If auto-apply fixits is on, rewrite the original source file.
11901197 if (AutoApplyFixes)
@@ -1214,10 +1221,11 @@ void DiagnosticVerifier::printRemainingDiagnostics() const {
12141221 break ;
12151222 }
12161223
1217- SM.getLLVMSourceMgr ().PrintMessage (
1218- llvm::errs (), getRawLoc (diag.Loc ), SMKind,
1219- " diagnostic produced elsewhere: " + diag.Message .str (),
1220- /* Ranges=*/ {}, {});
1224+ auto message =
1225+ SM.GetMessage (diag.Loc , SMKind,
1226+ " diagnostic produced elsewhere: " + diag.Message .str (),
1227+ /* Ranges=*/ {}, {});
1228+ printDiagnostic (message);
12211229 }
12221230}
12231231
@@ -1288,7 +1296,7 @@ bool DiagnosticVerifier::finishProcessing() {
12881296 Result.HadUnexpectedDiag |= FileResult.HadUnexpectedDiag ;
12891297 }
12901298 if (!IgnoreUnknown) {
1291- bool HadError = verifyUnknown (SM, CapturedDiagnostics);
1299+ bool HadError = verifyUnknown (CapturedDiagnostics);
12921300 Result.HadError |= HadError;
12931301 // For <unknown>, all errors are unexpected.
12941302 Result.HadUnexpectedDiag |= HadError;
0 commit comments