Skip to content

Commit 3a0808d

Browse files
committed
[ClangFormat] Add DiagHandler for getStyle function
It allows to control of error output for the function.
1 parent bf7a0f9 commit 3a0808d

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

clang/include/clang/Format/Format.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5381,11 +5381,11 @@ extern const char *DefaultFallbackStyle;
53815381
/// \returns FormatStyle as specified by ``StyleName``. If ``StyleName`` is
53825382
/// "file" and no file is found, returns ``FallbackStyle``. If no style could be
53835383
/// determined, returns an Error.
5384-
llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
5385-
StringRef FallbackStyle,
5386-
StringRef Code = "",
5387-
llvm::vfs::FileSystem *FS = nullptr,
5388-
bool AllowUnknownOptions = false);
5384+
llvm::Expected<FormatStyle>
5385+
getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyle,
5386+
StringRef Code = "", llvm::vfs::FileSystem *FS = nullptr,
5387+
bool AllowUnknownOptions = false,
5388+
llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr);
53895389

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

clang/lib/Format/Format.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,20 +3946,23 @@ 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,
3956+
DiagHandler)) {
39553957
return EC;
3958+
}
39563959
return Text;
39573960
}
39583961

3959-
llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
3960-
StringRef FallbackStyleName,
3961-
StringRef Code, llvm::vfs::FileSystem *FS,
3962-
bool AllowUnknownOptions) {
3962+
llvm::Expected<FormatStyle>
3963+
getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyleName,
3964+
StringRef Code, llvm::vfs::FileSystem *FS, bool AllowUnknownOptions,
3965+
llvm::SourceMgr::DiagHandlerTy DiagHandler) {
39633966
FormatStyle Style = getLLVMStyle(guessLanguage(FileName, Code));
39643967
FormatStyle FallbackStyle = getNoStyle();
39653968
if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle))
@@ -3973,7 +3976,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39733976
StringRef Source = "<command-line>";
39743977
if (std::error_code ec =
39753978
parseConfiguration(llvm::MemoryBufferRef(StyleName, Source), &Style,
3976-
AllowUnknownOptions)) {
3979+
AllowUnknownOptions, DiagHandler)) {
39773980
return make_string_error("Error parsing -style: " + ec.message());
39783981
}
39793982

@@ -3993,7 +3996,8 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39933996
StyleName.starts_with_insensitive("file:")) {
39943997
auto ConfigFile = StyleName.substr(5);
39953998
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
3996-
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
3999+
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
4000+
DiagHandler);
39974001
if (auto EC = Text.getError()) {
39984002
return make_string_error("Error reading " + ConfigFile + ": " +
39994003
EC.message());
@@ -4028,12 +4032,13 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40284032
// Reset possible inheritance
40294033
Style.InheritsParentConfig = false;
40304034

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

40334038
auto applyChildFormatTexts = [&](FormatStyle *Style) {
40344039
for (const auto &MemBuf : llvm::reverse(ChildFormatTextToApply)) {
40354040
auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
4036-
dropDiagnosticHandler);
4041+
diagHandlerOrDropHandling);
40374042
// It was already correctly parsed.
40384043
assert(!EC);
40394044
static_cast<void>(EC);
@@ -4067,7 +4072,8 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40674072
}
40684073

40694074
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
4070-
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
4075+
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
4076+
DiagHandler);
40714077
if (auto EC = Text.getError()) {
40724078
if (EC != ParseError::Unsuitable) {
40734079
return make_string_error("Error reading " + ConfigFile + ": " +

0 commit comments

Comments
 (0)