Skip to content

Commit 32b5344

Browse files
committed
[ClangFormat] Add DiagHandler for getStyle function
It allows to control of error output for the function.
1 parent cc54129 commit 32b5344

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

clang/include/clang/Format/Format.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5385,7 +5385,8 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
53855385
StringRef FallbackStyle,
53865386
StringRef Code = "",
53875387
llvm::vfs::FileSystem *FS = nullptr,
5388-
bool AllowUnknownOptions = false);
5388+
bool AllowUnknownOptions = false,
5389+
llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr);
53895390

53905391
// Guesses the language from the ``FileName`` and ``Code`` to be formatted.
53915392
// Defaults to FormatStyle::LK_Cpp.

clang/lib/Format/Format.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,20 +3946,22 @@ const char *DefaultFallbackStyle = "LLVM";
39463946

39473947
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
39483948
loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS,
3949-
FormatStyle *Style, bool AllowUnknownOptions) {
3949+
FormatStyle *Style, bool AllowUnknownOptions,
3950+
llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr) {
39503951
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
39513952
FS->getBufferForFile(ConfigFile.str());
39523953
if (auto EC = Text.getError())
39533954
return EC;
3954-
if (auto EC = parseConfiguration(*Text.get(), Style, AllowUnknownOptions))
3955+
if (auto EC = parseConfiguration(*Text.get(), Style, AllowUnknownOptions, DiagHandler))
39553956
return EC;
39563957
return Text;
39573958
}
39583959

39593960
llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39603961
StringRef FallbackStyleName,
39613962
StringRef Code, llvm::vfs::FileSystem *FS,
3962-
bool AllowUnknownOptions) {
3963+
bool AllowUnknownOptions,
3964+
llvm::SourceMgr::DiagHandlerTy DiagHandler) {
39633965
FormatStyle Style = getLLVMStyle(guessLanguage(FileName, Code));
39643966
FormatStyle FallbackStyle = getNoStyle();
39653967
if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle))
@@ -3973,7 +3975,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39733975
StringRef Source = "<command-line>";
39743976
if (std::error_code ec =
39753977
parseConfiguration(llvm::MemoryBufferRef(StyleName, Source), &Style,
3976-
AllowUnknownOptions)) {
3978+
AllowUnknownOptions, DiagHandler)) {
39773979
return make_string_error("Error parsing -style: " + ec.message());
39783980
}
39793981

@@ -3993,7 +3995,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39933995
StyleName.starts_with_insensitive("file:")) {
39943996
auto ConfigFile = StyleName.substr(5);
39953997
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
3996-
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
3998+
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions, DiagHandler);
39973999
if (auto EC = Text.getError()) {
39984000
return make_string_error("Error reading " + ConfigFile + ": " +
39994001
EC.message());
@@ -4028,12 +4030,13 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40284030
// Reset possible inheritance
40294031
Style.InheritsParentConfig = false;
40304032

4031-
auto dropDiagnosticHandler = [](const llvm::SMDiagnostic &, void *) {};
4033+
auto diagHandlerOrDropHandling =
4034+
DiagHandler ? DiagHandler : [](llvm::SMDiagnostic const &, void *) {};
40324035

40334036
auto applyChildFormatTexts = [&](FormatStyle *Style) {
40344037
for (const auto &MemBuf : llvm::reverse(ChildFormatTextToApply)) {
40354038
auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
4036-
dropDiagnosticHandler);
4039+
diagHandlerOrDropHandling);
40374040
// It was already correctly parsed.
40384041
assert(!EC);
40394042
static_cast<void>(EC);
@@ -4067,7 +4070,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40674070
}
40684071

40694072
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
4070-
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
4073+
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions, DiagHandler);
40714074
if (auto EC = Text.getError()) {
40724075
if (EC != ParseError::Unsuitable) {
40734076
return make_string_error("Error reading " + ConfigFile + ": " +

0 commit comments

Comments
 (0)