[clang] [ClangFormat] Add DiagHandler for getStyle function (PR #91317)
via cfe-commits
cfe-commits at lists.llvm.org
Tue May 7 03:29:31 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: None (pointhex)
<details>
<summary>Changes</summary>
It allows to control of error output for the function.
---
Full diff: https://github.com/llvm/llvm-project/pull/91317.diff
2 Files Affected:
- (modified) clang/include/clang/Format/Format.h (+2-1)
- (modified) clang/lib/Format/Format.cpp (+11-8)
``````````diff
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 74893f23210cd..aa6a2a16fa8ec 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -5385,7 +5385,8 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
StringRef FallbackStyle,
StringRef Code = "",
llvm::vfs::FileSystem *FS = nullptr,
- bool AllowUnknownOptions = false);
+ bool AllowUnknownOptions = false,
+ llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr);
// Guesses the language from the ``FileName`` and ``Code`` to be formatted.
// Defaults to FormatStyle::LK_Cpp.
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index c4eac1c99a663..cf8a2a050a83c 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3947,12 +3947,13 @@ const char *DefaultFallbackStyle = "LLVM";
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS,
- FormatStyle *Style, bool AllowUnknownOptions) {
+ FormatStyle *Style, bool AllowUnknownOptions,
+ llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
FS->getBufferForFile(ConfigFile.str());
if (auto EC = Text.getError())
return EC;
- if (auto EC = parseConfiguration(*Text.get(), Style, AllowUnknownOptions))
+ if (auto EC = parseConfiguration(*Text.get(), Style, AllowUnknownOptions, DiagHandler))
return EC;
return Text;
}
@@ -3960,7 +3961,8 @@ loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS,
llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
StringRef FallbackStyleName,
StringRef Code, llvm::vfs::FileSystem *FS,
- bool AllowUnknownOptions) {
+ bool AllowUnknownOptions,
+ llvm::SourceMgr::DiagHandlerTy DiagHandler) {
FormatStyle Style = getLLVMStyle(guessLanguage(FileName, Code));
FormatStyle FallbackStyle = getNoStyle();
if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle))
@@ -3974,7 +3976,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
StringRef Source = "<command-line>";
if (std::error_code ec =
parseConfiguration(llvm::MemoryBufferRef(StyleName, Source), &Style,
- AllowUnknownOptions)) {
+ AllowUnknownOptions, DiagHandler)) {
return make_string_error("Error parsing -style: " + ec.message());
}
@@ -3994,7 +3996,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
StyleName.starts_with_insensitive("file:")) {
auto ConfigFile = StyleName.substr(5);
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
- loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
+ loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions, DiagHandler);
if (auto EC = Text.getError()) {
return make_string_error("Error reading " + ConfigFile + ": " +
EC.message());
@@ -4029,12 +4031,13 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
// Reset possible inheritance
Style.InheritsParentConfig = false;
- auto dropDiagnosticHandler = [](const llvm::SMDiagnostic &, void *) {};
+ auto diagHandlerOrDropHandling =
+ DiagHandler ? DiagHandler : [](llvm::SMDiagnostic const &, void *) {};
auto applyChildFormatTexts = [&](FormatStyle *Style) {
for (const auto &MemBuf : llvm::reverse(ChildFormatTextToApply)) {
auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
- dropDiagnosticHandler);
+ diagHandlerOrDropHandling);
// It was already correctly parsed.
assert(!EC);
static_cast<void>(EC);
@@ -4068,7 +4071,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
}
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
- loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
+ loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions, DiagHandler);
if (auto EC = Text.getError()) {
if (EC != ParseError::Unsuitable) {
return make_string_error("Error reading " + ConfigFile + ": " +
``````````
</details>
https://github.com/llvm/llvm-project/pull/91317
More information about the cfe-commits
mailing list