[PATCH] D69854: [clang-format] [RELAND] Remove the dependency on frontend

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 6 09:41:04 PST 2019


This revision was automatically updated to reflect the committed changes.
MyDeveloperDay marked 2 inline comments as done.
Closed by commit rG3ddac7e56363: [clang-format] [RELAND] Remove the dependency on frontend (authored by MyDeveloperDay).

Changed prior to commit:
  https://reviews.llvm.org/D69854?vs=227899&id=228086#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D69854

Files:
  clang/tools/clang-format/CMakeLists.txt
  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
@@ -18,7 +18,6 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/Version.h"
 #include "clang/Format/Format.h"
-#include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Rewrite/Core/Rewriter.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
@@ -300,12 +299,9 @@
   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
   DiagOpts->ShowColors = (ShowColors && !NoShowColors);
 
-  TextDiagnosticPrinter *DiagsBuffer =
-      new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts, false);
-
   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
-      new DiagnosticsEngine(DiagID, &*DiagOpts, DiagsBuffer));
+      new DiagnosticsEngine(DiagID, &*DiagOpts));
 
   IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
       new llvm::vfs::InMemoryFileSystem);
@@ -314,24 +310,41 @@
   FileID FileID = createInMemoryFile(AssumedFileName, Code.get(), Sources,
                                      Files, InMemoryFileSystem.get());
 
-  const unsigned ID = Diags->getCustomDiagID(
-      WarningsAsErrors ? clang::DiagnosticsEngine::Error
-                       : clang::DiagnosticsEngine::Warning,
-      "code should be clang-formatted [-Wclang-format-violations]");
+  FileManager &FileMgr = Sources.getFileManager();
+  llvm::ErrorOr<const FileEntry *> FileEntryPtr =
+      FileMgr.getFile(AssumedFileName);
 
   unsigned Errors = 0;
-  DiagsBuffer->BeginSourceFile(LangOptions(), nullptr);
   if (WarnFormat && !NoWarnFormat) {
+    llvm::SourceMgr Mgr;
     for (const auto &R : Replaces) {
-      Diags->Report(
-          Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset()),
-          ID);
+      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 = 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(),
+          WarningsAsErrors ? SourceMgr::DiagKind::DK_Error
+                           : SourceMgr::DiagKind::DK_Warning,
+          "code should be clang-formatted [-Wclang-format-violations]", Line,
+          ArrayRef<std::pair<unsigned, unsigned>>());
+
+      Diag.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors));
       Errors++;
       if (ErrorLimit && Errors >= ErrorLimit)
         break;
     }
   }
-  DiagsBuffer->EndSourceFile();
   return WarningsAsErrors;
 }
 
Index: clang/tools/clang-format/CMakeLists.txt
===================================================================
--- clang/tools/clang-format/CMakeLists.txt
+++ clang/tools/clang-format/CMakeLists.txt
@@ -7,7 +7,6 @@
 set(CLANG_FORMAT_LIB_DEPS
   clangBasic
   clangFormat
-  clangFrontend
   clangRewrite
   clangToolingCore
   )


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69854.228086.patch
Type: text/x-patch
Size: 3502 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191106/4643c9a9/attachment-0001.bin>


More information about the cfe-commits mailing list