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 ) 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" @@ -325,12 +324,9 @@ IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions(); DiagOpts->ShowColors = (ShowColors && !NoShowColors); - TextDiagnosticPrinter *DiagsBuffer = - new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts, false); - IntrusiveRefCntPtr DiagID(new DiagnosticIDs()); IntrusiveRefCntPtr Diags( - new DiagnosticsEngine(DiagID, &*DiagOpts, DiagsBuffer)); + new DiagnosticsEngine(DiagID, &*DiagOpts)); IntrusiveRefCntPtr InMemoryFileSystem( new llvm::vfs::InMemoryFileSystem); @@ -339,24 +335,35 @@ 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]"); + SmallVector Lines; + Code->getBuffer().split(Lines, "\n", /*MaxSplit=*/-1, + /*KeepEmpty=*/true); unsigned Errors = 0; - DiagsBuffer->BeginSourceFile(LangOptions(), nullptr); if (WarnFormat && !NoWarnFormat) { + ArrayRef> Ranges; for (const auto &R : Replaces) { - Diags->Report( - Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset()), - ID); + PresumedLoc PLoc = Sources.getPresumedLoc( + Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset())); + + StringRef Line; + if (PLoc.getLine() < Lines.size() && PLoc.getLine() > 0) + Line = Lines[PLoc.getLine() - 1]; + + SMDiagnostic Diags( + llvm::SourceMgr(), SMLoc(), AssumedFileName, PLoc.getLine(), + PLoc.getColumn() - 1, + WarningsAsErrors ? SourceMgr::DiagKind::DK_Error + : SourceMgr::DiagKind::DK_Warning, + "code should be clang-formatted [-Wclang-format-violations]", Line, + ArrayRef>()); + + Diags.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors)); Errors++; if (ErrorLimit && Errors >= ErrorLimit) break; } } - DiagsBuffer->EndSourceFile(); return WarningsAsErrors; }