From efed314118c7c287a71b8a8d67953a98d8a718d5 Mon Sep 17 00:00:00 2001 From: Vlad Tsyrklevich Date: Tue, 29 Oct 2019 10:20:38 -0700 Subject: [PATCH] Revert "[clang-format] Remove the dependency on frontend" This reverts commit ec66603ac7ea655be5c2c5f508c5bf0d5eaeb65b. It was causing ubsan failures like the following on the ubsan bot: llvm/lib/Support/SourceMgr.cpp:440:48: runtime error: pointer index expression with base 0x000000000000 overflowed to 0xfffffffffffffffa --- clang/tools/clang-format/CMakeLists.txt | 1 + clang/tools/clang-format/ClangFormat.cpp | 40 +++++++++++--------------------- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/clang/tools/clang-format/CMakeLists.txt b/clang/tools/clang-format/CMakeLists.txt index 35ecdb1..28ac4fb 100644 --- a/clang/tools/clang-format/CMakeLists.txt +++ b/clang/tools/clang-format/CMakeLists.txt @@ -7,6 +7,7 @@ add_clang_tool(clang-format set(CLANG_FORMAT_LIB_DEPS clangBasic clangFormat + clangFrontend clangRewrite clangToolingCore ) diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index cbbb52b..db00d41 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -18,6 +18,7 @@ #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" @@ -299,9 +300,12 @@ emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName, 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)); + new DiagnosticsEngine(DiagID, &*DiagOpts, DiagsBuffer)); IntrusiveRefCntPtr InMemoryFileSystem( new llvm::vfs::InMemoryFileSystem); @@ -310,40 +314,24 @@ emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName, FileID FileID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files, InMemoryFileSystem.get()); - FileManager &FileMgr = Sources.getFileManager(); - llvm::ErrorOr FileEntryPtr = - FileMgr.getFile(AssumedFileName); + const unsigned ID = Diags->getCustomDiagID( + WarningsAsErrors ? clang::DiagnosticsEngine::Error + : clang::DiagnosticsEngine::Warning, + "code should be clang-formatted [-Wclang-format-violations]"); unsigned Errors = 0; + DiagsBuffer->BeginSourceFile(LangOptions(), nullptr); if (WarnFormat && !NoWarnFormat) { 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 = 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>()); - - Diags.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors)); + Diags->Report( + Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset()), + ID); Errors++; if (ErrorLimit && Errors >= ErrorLimit) break; } } + DiagsBuffer->EndSourceFile(); return WarningsAsErrors; } -- 2.7.4