Skip to content

Commit 3ddac7e

Browse files
[clang-format] [RELAND] Remove the dependency on frontend
Summary: relanding {D68969} after it failed UBSAN build caused by the passing of an invalid SMLoc() (nullptr) Reviewers: thakis, vlad.tsyrklevich, klimek, mitchell-stellar Reviewed By: thakis Subscribers: merge_guards_bot, mgorny, cfe-commits Tags: #clang-format, #clang Differential Revision: https://reviews.llvm.org/D69854
1 parent 7681435 commit 3ddac7e

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

clang/tools/clang-format/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ add_clang_tool(clang-format
77
set(CLANG_FORMAT_LIB_DEPS
88
clangBasic
99
clangFormat
10-
clangFrontend
1110
clangRewrite
1211
clangToolingCore
1312
)

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "clang/Basic/SourceManager.h"
1919
#include "clang/Basic/Version.h"
2020
#include "clang/Format/Format.h"
21-
#include "clang/Frontend/TextDiagnosticPrinter.h"
2221
#include "clang/Rewrite/Core/Rewriter.h"
2322
#include "llvm/Support/CommandLine.h"
2423
#include "llvm/Support/FileSystem.h"
@@ -300,12 +299,9 @@ emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,
300299
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
301300
DiagOpts->ShowColors = (ShowColors && !NoShowColors);
302301

303-
TextDiagnosticPrinter *DiagsBuffer =
304-
new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts, false);
305-
306302
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
307303
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
308-
new DiagnosticsEngine(DiagID, &*DiagOpts, DiagsBuffer));
304+
new DiagnosticsEngine(DiagID, &*DiagOpts));
309305

310306
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
311307
new llvm::vfs::InMemoryFileSystem);
@@ -314,24 +310,41 @@ emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,
314310
FileID FileID = createInMemoryFile(AssumedFileName, Code.get(), Sources,
315311
Files, InMemoryFileSystem.get());
316312

317-
const unsigned ID = Diags->getCustomDiagID(
318-
WarningsAsErrors ? clang::DiagnosticsEngine::Error
319-
: clang::DiagnosticsEngine::Warning,
320-
"code should be clang-formatted [-Wclang-format-violations]");
313+
FileManager &FileMgr = Sources.getFileManager();
314+
llvm::ErrorOr<const FileEntry *> FileEntryPtr =
315+
FileMgr.getFile(AssumedFileName);
321316

322317
unsigned Errors = 0;
323-
DiagsBuffer->BeginSourceFile(LangOptions(), nullptr);
324318
if (WarnFormat && !NoWarnFormat) {
319+
llvm::SourceMgr Mgr;
325320
for (const auto &R : Replaces) {
326-
Diags->Report(
327-
Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset()),
328-
ID);
321+
PresumedLoc PLoc = Sources.getPresumedLoc(
322+
Sources.getLocForStartOfFile(FileID).getLocWithOffset(R.getOffset()));
323+
324+
SourceLocation LineBegin =
325+
Sources.translateFileLineCol(FileEntryPtr.get(), PLoc.getLine(), 1);
326+
SourceLocation NextLineBegin = Sources.translateFileLineCol(
327+
FileEntryPtr.get(), PLoc.getLine() + 1, 1);
328+
329+
const char *StartBuf = Sources.getCharacterData(LineBegin);
330+
const char *EndBuf = Sources.getCharacterData(NextLineBegin);
331+
332+
StringRef Line(StartBuf, (EndBuf - StartBuf) - 1);
333+
334+
SMDiagnostic Diag(
335+
Mgr, SMLoc::getFromPointer(StartBuf), AssumedFileName,
336+
PLoc.getLine(), PLoc.getColumn(),
337+
WarningsAsErrors ? SourceMgr::DiagKind::DK_Error
338+
: SourceMgr::DiagKind::DK_Warning,
339+
"code should be clang-formatted [-Wclang-format-violations]", Line,
340+
ArrayRef<std::pair<unsigned, unsigned>>());
341+
342+
Diag.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors));
329343
Errors++;
330344
if (ErrorLimit && Errors >= ErrorLimit)
331345
break;
332346
}
333347
}
334-
DiagsBuffer->EndSourceFile();
335348
return WarningsAsErrors;
336349
}
337350

0 commit comments

Comments
 (0)