Commit ec66603a authored by paulhoad's avatar paulhoad
Browse files

[clang-format] Remove the dependency on frontend

Summary:
Address review comments from {D68554} by trying to drop the dependency again on Frontend whilst keeping the same format diagnostic messages

Not completely happy with having to do a split in order to get the StringRef for the Line the error occurred on, but could see a way to use SourceManager and SourceLocation to give me a single line?

But this removes the dependency on frontend which should keep the binary size down.

Reviewers: thakis, klimek, mitchell-stellar

Reviewed By: klimek

Subscribers: mgorny, cfe-commits

Tags: #clang, #clang-format

Differential Revision: https://reviews.llvm.org/D68969
parent 3c8e0551
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@ add_clang_tool(clang-format
set(CLANG_FORMAT_LIB_DEPS
  clangBasic
  clangFormat
  clangFrontend
  clangRewrite
  clangToolingCore
  )
+26 −14
Original line number Diff line number Diff line
@@ -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"
@@ -325,12 +324,9 @@ emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,
  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);
@@ -339,24 +335,40 @@ emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,
  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) {
    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 Diags(
          llvm::SourceMgr(), SMLoc(), 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>>());

      Diags.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors));
      Errors++;
      if (ErrorLimit && Errors >= ErrorLimit)
        break;
    }
  }
  DiagsBuffer->EndSourceFile();
  return WarningsAsErrors;
}