summaryrefslogtreecommitdiffstats
path: root/clang/tools/clang-format/ClangFormat.cpp
diff options
context:
space:
mode:
authorpaulhoad <mydeveloperday@gmail.com>2019-11-06 17:33:37 +0000
committerpaulhoad <mydeveloperday@gmail.com>2019-11-06 17:33:37 +0000
commit3ddac7e563633632d4a3e673f3224ee66e1a717e (patch)
treea94903f5bc66dfe7bc71f8570f8d39ffd8cbb748 /clang/tools/clang-format/ClangFormat.cpp
parent7681435de148cf7a1c7db1b0daba0497630989d2 (diff)
downloadbcm5719-llvm-3ddac7e563633632d4a3e673f3224ee66e1a717e.tar.gz
bcm5719-llvm-3ddac7e563633632d4a3e673f3224ee66e1a717e.zip
[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
Diffstat (limited to 'clang/tools/clang-format/ClangFormat.cpp')
-rw-r--r--clang/tools/clang-format/ClangFormat.cpp41
1 files changed, 27 insertions, 14 deletions
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index 8a2ec2dde03..aa6890ebff3 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/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"
@@ -300,12 +299,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);
@@ -314,24 +310,41 @@ 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) {
+ llvm::SourceMgr Mgr;
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 Diag(
+ Mgr, SMLoc::getFromPointer(StartBuf), 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>>());
+
+ Diag.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors));
Errors++;
if (ErrorLimit && Errors >= ErrorLimit)
break;
}
}
- DiagsBuffer->EndSourceFile();
return WarningsAsErrors;
}
OpenPOWER on IntegriCloud