[PATCH] D69921: [clang-format] refactor the use of the SMDiagnostics in replacement warnings

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 12 01:21:05 PST 2019


MyDeveloperDay updated this revision to Diff 228826.
MyDeveloperDay added a comment.

Move adding the source file to the source mgr outside the replacements loop


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69921/new/

https://reviews.llvm.org/D69921

Files:
  clang/tools/clang-format/ClangFormat.cpp


Index: clang/tools/clang-format/ClangFormat.cpp
===================================================================
--- clang/tools/clang-format/ClangFormat.cpp
+++ clang/tools/clang-format/ClangFormat.cpp
@@ -292,56 +292,25 @@
 static bool
 emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,
                         const std::unique_ptr<llvm::MemoryBuffer> &Code) {
-  if (Replaces.empty()) {
+  if (Replaces.empty())
     return false;
-  }
-
-  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
-  DiagOpts->ShowColors = (ShowColors && !NoShowColors);
-
-  IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
-  IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
-      new DiagnosticsEngine(DiagID, &*DiagOpts));
-
-  IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
-      new llvm::vfs::InMemoryFileSystem);
-  FileManager Files(FileSystemOptions(), InMemoryFileSystem);
-  SourceManager Sources(*Diags, Files);
-  FileID FileID = createInMemoryFile(AssumedFileName, Code.get(), Sources,
-                                     Files, InMemoryFileSystem.get());
-
-  FileManager &FileMgr = Sources.getFileManager();
-  llvm::ErrorOr<const FileEntry *> FileEntryPtr =
-      FileMgr.getFile(AssumedFileName);
 
   unsigned Errors = 0;
   if (WarnFormat && !NoWarnFormat) {
     llvm::SourceMgr Mgr;
-    for (const auto &R : Replaces) {
-      PresumedLoc PLoc = Sources.getPresumedLoc(
-          Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset()));
-
-      SourceLocation LineBegin =
-          Sources.translateFileLineCol(FileEntryPtr.get(), PLoc.getLine(), 1);
-      SourceLocation NextLineBegin = Sources.translateFileLineCol(
-          FileEntryPtr.get(), PLoc.getLine() + 1, 1);
+    const char *StartBuf = Code->getBufferStart();
 
-      const char *StartBuf = Sources.getCharacterData(LineBegin);
-      const char *EndBuf = Sources.getCharacterData(NextLineBegin);
-
-      StringRef Line(StartBuf, (EndBuf - StartBuf) - 1);
-
-      SMDiagnostic Diag(
-          Mgr, SMLoc::getFromPointer(StartBuf), AssumedFileName,
-          PLoc.getLine(), PLoc.getColumn(),
+    Mgr.AddNewSourceBuffer(
+        MemoryBuffer::getMemBuffer(StartBuf, AssumedFileName), SMLoc());
+    for (const auto &R : Replaces) {
+      SMDiagnostic Diag = Mgr.GetMessage(
+          SMLoc::getFromPointer(StartBuf + R.getOffset()),
           WarningsAsErrors ? SourceMgr::DiagKind::DK_Error
                            : SourceMgr::DiagKind::DK_Warning,
-          "code should be clang-formatted [-Wclang-format-violations]", Line,
-          ArrayRef<std::pair<unsigned, unsigned>>());
+          "code should be clang-formatted [-Wclang-format-violations]");
 
       Diag.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors));
-      Errors++;
-      if (ErrorLimit && Errors >= ErrorLimit)
+      if (ErrorLimit && ++Errors >= ErrorLimit)
         break;
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69921.228826.patch
Type: text/x-patch
Size: 2973 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191112/2ad012a4/attachment-0001.bin>


More information about the cfe-commits mailing list