Skip to content

Commit 3763408

Browse files
committed
[clang-format] Add DiagHandler for getStyle function
It allows to control of error output for the function.
1 parent 6c9bce8 commit 3763408

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

clang/include/clang/Format/Format.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5387,10 +5387,11 @@ extern const char *DefaultFallbackStyle;
53875387
/// \returns FormatStyle as specified by ``StyleName``. If ``StyleName`` is
53885388
/// "file" and no file is found, returns ``FallbackStyle``. If no style could be
53895389
/// determined, returns an Error.
5390-
Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
5391-
StringRef FallbackStyle, StringRef Code = "",
5392-
llvm::vfs::FileSystem *FS = nullptr,
5393-
bool AllowUnknownOptions = false);
5390+
Expected<FormatStyle>
5391+
getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyle,
5392+
StringRef Code = "", llvm::vfs::FileSystem *FS = nullptr,
5393+
bool AllowUnknownOptions = false,
5394+
llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr);
53945395

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

clang/lib/Format/Format.cpp

Lines changed: 15 additions & 12 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) {
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-
Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
3960-
StringRef FallbackStyleName, StringRef Code,
3961-
llvm::vfs::FileSystem *FS,
3962-
bool AllowUnknownOptions) {
3962+
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))
@@ -3972,7 +3975,7 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39723975
StringRef Source = "<command-line>";
39733976
if (std::error_code ec =
39743977
parseConfiguration(llvm::MemoryBufferRef(StyleName, Source), &Style,
3975-
AllowUnknownOptions)) {
3978+
AllowUnknownOptions, DiagHandler)) {
39763979
return make_string_error("Error parsing -style: " + ec.message());
39773980
}
39783981

@@ -3992,7 +3995,8 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39923995
StyleName.starts_with_insensitive("file:")) {
39933996
auto ConfigFile = StyleName.substr(5);
39943997
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
3995-
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
3998+
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
3999+
DiagHandler);
39964000
if (auto EC = Text.getError()) {
39974001
return make_string_error("Error reading " + ConfigFile + ": " +
39984002
EC.message());
@@ -4027,12 +4031,10 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40274031
// Reset possible inheritance
40284032
Style.InheritsParentConfig = false;
40294033

4030-
auto dropDiagnosticHandler = [](const llvm::SMDiagnostic &, void *) {};
4031-
40324034
auto applyChildFormatTexts = [&](FormatStyle *Style) {
40334035
for (const auto &MemBuf : llvm::reverse(ChildFormatTextToApply)) {
40344036
auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
4035-
dropDiagnosticHandler);
4037+
DiagHandler);
40364038
// It was already correctly parsed.
40374039
assert(!EC);
40384040
static_cast<void>(EC);
@@ -4066,7 +4068,8 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40664068
}
40674069

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

0 commit comments

Comments
 (0)