18
18
#include " clang/Basic/SourceManager.h"
19
19
#include " clang/Basic/Version.h"
20
20
#include " clang/Format/Format.h"
21
- #include " clang/Frontend/TextDiagnosticPrinter.h"
22
21
#include " clang/Rewrite/Core/Rewriter.h"
23
22
#include " llvm/Support/CommandLine.h"
24
23
#include " llvm/Support/FileSystem.h"
@@ -300,12 +299,9 @@ emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,
300
299
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions ();
301
300
DiagOpts->ShowColors = (ShowColors && !NoShowColors);
302
301
303
- TextDiagnosticPrinter *DiagsBuffer =
304
- new TextDiagnosticPrinter (llvm::errs (), &*DiagOpts, false );
305
-
306
302
IntrusiveRefCntPtr<DiagnosticIDs> DiagID (new DiagnosticIDs ());
307
303
IntrusiveRefCntPtr<DiagnosticsEngine> Diags (
308
- new DiagnosticsEngine (DiagID, &*DiagOpts, DiagsBuffer ));
304
+ new DiagnosticsEngine (DiagID, &*DiagOpts));
309
305
310
306
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem (
311
307
new llvm::vfs::InMemoryFileSystem);
@@ -314,24 +310,41 @@ emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,
314
310
FileID FileID = createInMemoryFile (AssumedFileName, Code.get (), Sources,
315
311
Files, InMemoryFileSystem.get ());
316
312
317
- const unsigned ID = Diags->getCustomDiagID (
318
- WarningsAsErrors ? clang::DiagnosticsEngine::Error
319
- : clang::DiagnosticsEngine::Warning,
320
- " code should be clang-formatted [-Wclang-format-violations]" );
313
+ FileManager &FileMgr = Sources.getFileManager ();
314
+ llvm::ErrorOr<const FileEntry *> FileEntryPtr =
315
+ FileMgr.getFile (AssumedFileName);
321
316
322
317
unsigned Errors = 0 ;
323
- DiagsBuffer->BeginSourceFile (LangOptions (), nullptr );
324
318
if (WarnFormat && !NoWarnFormat) {
319
+ llvm::SourceMgr Mgr;
325
320
for (const auto &R : Replaces) {
326
- Diags->Report (
327
- Sources.getLocForStartOfFile (FileID).getLocWithOffset (R.getOffset ()),
328
- ID);
321
+ PresumedLoc PLoc = Sources.getPresumedLoc (
322
+ Sources.getLocForStartOfFile (FileID).getLocWithOffset (R.getOffset ()));
323
+
324
+ SourceLocation LineBegin =
325
+ Sources.translateFileLineCol (FileEntryPtr.get (), PLoc.getLine (), 1 );
326
+ SourceLocation NextLineBegin = Sources.translateFileLineCol (
327
+ FileEntryPtr.get (), PLoc.getLine () + 1 , 1 );
328
+
329
+ const char *StartBuf = Sources.getCharacterData (LineBegin);
330
+ const char *EndBuf = Sources.getCharacterData (NextLineBegin);
331
+
332
+ StringRef Line (StartBuf, (EndBuf - StartBuf) - 1 );
333
+
334
+ SMDiagnostic Diag (
335
+ Mgr, SMLoc::getFromPointer (StartBuf), AssumedFileName,
336
+ PLoc.getLine (), PLoc.getColumn (),
337
+ WarningsAsErrors ? SourceMgr::DiagKind::DK_Error
338
+ : SourceMgr::DiagKind::DK_Warning,
339
+ " code should be clang-formatted [-Wclang-format-violations]" , Line,
340
+ ArrayRef<std::pair<unsigned , unsigned >>());
341
+
342
+ Diag.print (nullptr , llvm::errs (), (ShowColors && !NoShowColors));
329
343
Errors++;
330
344
if (ErrorLimit && Errors >= ErrorLimit)
331
345
break ;
332
346
}
333
347
}
334
- DiagsBuffer->EndSourceFile ();
335
348
return WarningsAsErrors;
336
349
}
337
350
0 commit comments