Skip to content

Commit df00267

Browse files
committed
clang-format: Add a consumer to diagnostics engine
Contributed by dmikis (Kirill Dmitrenko)! Otherwise problems like trying to format readonly file in-place led to crashes. I've added reviewers by looking at `git blame` and other reviews to the changed file, so may have missed someone. Reviewed By: krasimir Differential Revision: https://reviews.llvm.org/D90121
1 parent b376176 commit df00267

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

clang/tools/clang-format/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_clang_tool(clang-format
77
set(CLANG_FORMAT_LIB_DEPS
88
clangBasic
99
clangFormat
10+
clangFrontend
1011
clangRewrite
1112
clangToolingCore
1213
)

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "clang/Basic/SourceManager.h"
1919
#include "clang/Basic/Version.h"
2020
#include "clang/Format/Format.h"
21+
#include "clang/Frontend/TextDiagnosticPrinter.h"
2122
#include "clang/Rewrite/Core/Rewriter.h"
2223
#include "llvm/Support/CommandLine.h"
2324
#include "llvm/Support/FileSystem.h"
@@ -423,9 +424,11 @@ static bool format(StringRef FileName) {
423424
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
424425
new llvm::vfs::InMemoryFileSystem);
425426
FileManager Files(FileSystemOptions(), InMemoryFileSystem);
427+
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions());
428+
TextDiagnosticPrinter DiagnosticsConsumer(errs(), &*DiagOpts);
426429
DiagnosticsEngine Diagnostics(
427-
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
428-
new DiagnosticOptions);
430+
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts,
431+
&DiagnosticsConsumer, false);
429432
SourceManager Sources(Diagnostics, Files);
430433
FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files,
431434
InMemoryFileSystem.get());

0 commit comments

Comments
 (0)