[PATCH] D69921: [clang-format] refactor the use of the SMDiagnostics in replacement warnings
MyDeveloperDay via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 6 13:34:34 PST 2019
MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: thakis, klimek, mitchell-stellar, vlad.tsyrklevich.
MyDeveloperDay added projects: clang-format, clang.
Review comments in D69854: [clang-format] [RELAND] Remove the dependency on frontend <https://reviews.llvm.org/D69854> recommended a simpler approach of creating the SMDiagnostics to remove much of the complexity. (thanks @thakis)
@vlad.tsyrklevich I've rebuilt on both Windows and Linux (running Linux with Address and Undefined sanitizers) over the clang code base
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D69921
Files:
clang/tools/clang-format/ClangFormat.cpp
Index: clang/tools/clang-format/ClangFormat.cpp
===================================================================
--- clang/tools/clang-format/ClangFormat.cpp
+++ clang/tools/clang-format/ClangFormat.cpp
@@ -292,56 +292,27 @@
static bool
emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,
const std::unique_ptr<llvm::MemoryBuffer> &Code) {
- if (Replaces.empty()) {
+ if (Replaces.empty())
return false;
- }
-
- IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
- DiagOpts->ShowColors = (ShowColors && !NoShowColors);
-
- IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
- IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
- new DiagnosticsEngine(DiagID, &*DiagOpts));
-
- IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
- new llvm::vfs::InMemoryFileSystem);
- FileManager Files(FileSystemOptions(), InMemoryFileSystem);
- SourceManager Sources(*Diags, Files);
- FileID FileID = createInMemoryFile(AssumedFileName, Code.get(), Sources,
- Files, InMemoryFileSystem.get());
-
- FileManager &FileMgr = Sources.getFileManager();
- llvm::ErrorOr<const FileEntry *> FileEntryPtr =
- FileMgr.getFile(AssumedFileName);
unsigned Errors = 0;
if (WarnFormat && !NoWarnFormat) {
llvm::SourceMgr Mgr;
+ ArrayRef<SMRange> Ranges;
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);
+ const char *StartBuf = Code->getBufferStart();
- StringRef Line(StartBuf, (EndBuf - StartBuf) - 1);
+ Mgr.AddNewSourceBuffer(
+ MemoryBuffer::getMemBuffer(StartBuf, AssumedFileName), SMLoc());
- SMDiagnostic Diag(
- Mgr, SMLoc::getFromPointer(StartBuf), AssumedFileName,
- PLoc.getLine(), PLoc.getColumn(),
+ SMDiagnostic Diag = Mgr.GetMessage(
+ SMLoc::getFromPointer(StartBuf + R.getOffset()),
WarningsAsErrors ? SourceMgr::DiagKind::DK_Error
: SourceMgr::DiagKind::DK_Warning,
- "code should be clang-formatted [-Wclang-format-violations]", Line,
- ArrayRef<std::pair<unsigned, unsigned>>());
+ "code should be clang-formatted [-Wclang-format-violations]", Ranges);
Diag.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors));
- Errors++;
- if (ErrorLimit && Errors >= ErrorLimit)
+ if (ErrorLimit && ++Errors >= ErrorLimit)
break;
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69921.228131.patch
Type: text/x-patch
Size: 2980 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191106/2f872154/attachment.bin>
More information about the cfe-commits
mailing list