<div dir="ltr">I've reverted this commit as it was causing UBSan failures on the ubsan bot. These failures looked like:<div>llvm/lib/Support/SourceMgr.cpp:440:48: runtime error: pointer index expression with base 0x000000000000 overflowed to 0xfffffffffffffffa<br></div><div><br></div><div>Looking at a backtrace, this line was reached from the `Diags.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors));` call introduced in this change.</div><div><br></div><div>I apologize for the delay in this revert, normally reverts happen in under a day but in this case this change was committed right as we switched to GitHub and it took a while to get the sanitizers buildbots working properly and track down failures.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 24, 2019 at 11:17 AM via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Author: paulhoad<br>
Date: 2019-10-24T19:03:57+01:00<br>
New Revision: ec66603ac7ea655be5c2c5f508c5bf0d5eaeb65b<br>
<br>
URL: <a href="https://github.com/llvm/llvm-project/commit/ec66603ac7ea655be5c2c5f508c5bf0d5eaeb65b" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/ec66603ac7ea655be5c2c5f508c5bf0d5eaeb65b</a><br>
DIFF: <a href="https://github.com/llvm/llvm-project/commit/ec66603ac7ea655be5c2c5f508c5bf0d5eaeb65b.diff" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/ec66603ac7ea655be5c2c5f508c5bf0d5eaeb65b.diff</a><br>
<br>
LOG: [clang-format] Remove the dependency on frontend<br>
<br>
Summary:<br>
Address review comments from {D68554} by trying to drop the dependency again on Frontend whilst keeping the same format diagnostic messages<br>
<br>
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?<br>
<br>
But this removes the dependency on frontend which should keep the binary size down.<br>
<br>
Reviewers: thakis, klimek, mitchell-stellar<br>
<br>
Reviewed By: klimek<br>
<br>
Subscribers: mgorny, cfe-commits<br>
<br>
Tags: #clang, #clang-format<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D68969" rel="noreferrer" target="_blank">https://reviews.llvm.org/D68969</a><br>
<br>
Added: <br>
<br>
<br>
Modified: <br>
clang/tools/clang-format/CMakeLists.txt<br>
clang/tools/clang-format/ClangFormat.cpp<br>
<br>
Removed: <br>
<br>
<br>
<br>
################################################################################<br>
diff --git a/clang/tools/clang-format/CMakeLists.txt b/clang/tools/clang-format/CMakeLists.txt<br>
index 28ac4fb5913e..35ecdb11253c 100644<br>
--- a/clang/tools/clang-format/CMakeLists.txt<br>
+++ b/clang/tools/clang-format/CMakeLists.txt<br>
@@ -7,7 +7,6 @@ add_clang_tool(clang-format<br>
set(CLANG_FORMAT_LIB_DEPS<br>
clangBasic<br>
clangFormat<br>
- clangFrontend<br>
clangRewrite<br>
clangToolingCore<br>
)<br>
<br>
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp<br>
index f39c18bae3ff..a10541d88f07 100644<br>
--- a/clang/tools/clang-format/ClangFormat.cpp<br>
+++ b/clang/tools/clang-format/ClangFormat.cpp<br>
@@ -18,7 +18,6 @@<br>
#include "clang/Basic/SourceManager.h"<br>
#include "clang/Basic/Version.h"<br>
#include "clang/Format/Format.h"<br>
-#include "clang/Frontend/TextDiagnosticPrinter.h"<br>
#include "clang/Rewrite/Core/Rewriter.h"<br>
#include "llvm/Support/CommandLine.h"<br>
#include "llvm/Support/FileSystem.h"<br>
@@ -325,12 +324,9 @@ emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,<br>
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();<br>
DiagOpts->ShowColors = (ShowColors && !NoShowColors);<br>
<br>
- TextDiagnosticPrinter *DiagsBuffer =<br>
- new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts, false);<br>
-<br>
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());<br>
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(<br>
- new DiagnosticsEngine(DiagID, &*DiagOpts, DiagsBuffer));<br>
+ new DiagnosticsEngine(DiagID, &*DiagOpts));<br>
<br>
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(<br>
new llvm::vfs::InMemoryFileSystem);<br>
@@ -339,24 +335,40 @@ emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,<br>
FileID FileID = createInMemoryFile(AssumedFileName, Code.get(), Sources,<br>
Files, InMemoryFileSystem.get());<br>
<br>
- const unsigned ID = Diags->getCustomDiagID(<br>
- WarningsAsErrors ? clang::DiagnosticsEngine::Error<br>
- : clang::DiagnosticsEngine::Warning,<br>
- "code should be clang-formatted [-Wclang-format-violations]");<br>
+ FileManager &FileMgr = Sources.getFileManager();<br>
+ llvm::ErrorOr<const FileEntry *> FileEntryPtr =<br>
+ FileMgr.getFile(AssumedFileName);<br>
<br>
unsigned Errors = 0;<br>
- DiagsBuffer->BeginSourceFile(LangOptions(), nullptr);<br>
if (WarnFormat && !NoWarnFormat) {<br>
for (const auto &R : Replaces) {<br>
- Diags->Report(<br>
- Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset()),<br>
- ID);<br>
+ PresumedLoc PLoc = Sources.getPresumedLoc(<br>
+ Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset()));<br>
+<br>
+ SourceLocation LineBegin =<br>
+ Sources.translateFileLineCol(FileEntryPtr.get(), PLoc.getLine(), 1);<br>
+ SourceLocation NextLineBegin = Sources.translateFileLineCol(<br>
+ FileEntryPtr.get(), PLoc.getLine() + 1, 1);<br>
+<br>
+ const char *StartBuf = Sources.getCharacterData(LineBegin);<br>
+ const char *EndBuf = Sources.getCharacterData(NextLineBegin);<br>
+<br>
+ StringRef Line(StartBuf, (EndBuf - StartBuf) - 1);<br>
+<br>
+ SMDiagnostic Diags(<br>
+ llvm::SourceMgr(), SMLoc(), AssumedFileName, PLoc.getLine(),<br>
+ PLoc.getColumn(),<br>
+ WarningsAsErrors ? SourceMgr::DiagKind::DK_Error<br>
+ : SourceMgr::DiagKind::DK_Warning,<br>
+ "code should be clang-formatted [-Wclang-format-violations]", Line,<br>
+ ArrayRef<std::pair<unsigned, unsigned>>());<br>
+<br>
+ Diags.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors));<br>
Errors++;<br>
if (ErrorLimit && Errors >= ErrorLimit)<br>
break;<br>
}<br>
}<br>
- DiagsBuffer->EndSourceFile();<br>
return WarningsAsErrors;<br>
}<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div>