[clang] [clang-format] Add DiagHandler for getStyle function (PR #91317)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 14 02:18:28 PDT 2024
https://github.com/pointhex updated https://github.com/llvm/llvm-project/pull/91317
>From c1e0ad6ee57a95fa4321bbe91aa754167da9fb3b Mon Sep 17 00:00:00 2001
From: Artem Sokolovskii <artem.sokolovskii at qt.io>
Date: Tue, 7 May 2024 12:27:29 +0200
Subject: [PATCH] [clang-format] Add DiagHandler for getStyle function
It allows to control of error output for the function.
---
clang/include/clang/Format/Format.h | 9 ++++----
clang/lib/Format/Format.cpp | 25 +++++++++++++---------
clang/unittests/Format/ConfigParseTest.cpp | 21 ++++++++++++++++++
3 files changed, 41 insertions(+), 14 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 4fd6e013df25b..3bbc362fc8bc7 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -5387,10 +5387,11 @@ extern const char *DefaultFallbackStyle;
/// \returns FormatStyle as specified by ``StyleName``. If ``StyleName`` is
/// "file" and no file is found, returns ``FallbackStyle``. If no style could be
/// determined, returns an Error.
-Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
- StringRef FallbackStyle, StringRef Code = "",
- llvm::vfs::FileSystem *FS = nullptr,
- bool AllowUnknownOptions = false);
+Expected<FormatStyle>
+getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyle,
+ StringRef Code = "", llvm::vfs::FileSystem *FS = nullptr,
+ 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 c015e03fa15e7..b1def18f9b7da 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3946,20 +3946,23 @@ 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) {
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;
}
-Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
- StringRef FallbackStyleName, StringRef Code,
- llvm::vfs::FileSystem *FS,
- bool AllowUnknownOptions) {
+Expected<FormatStyle>
+getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyleName,
+ StringRef Code, llvm::vfs::FileSystem *FS, bool AllowUnknownOptions,
+ llvm::SourceMgr::DiagHandlerTy DiagHandler) {
FormatStyle Style = getLLVMStyle(guessLanguage(FileName, Code));
FormatStyle FallbackStyle = getNoStyle();
if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle))
@@ -3972,7 +3975,7 @@ 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());
}
@@ -3992,7 +3995,8 @@ 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());
@@ -4032,7 +4036,7 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
auto applyChildFormatTexts = [&](FormatStyle *Style) {
for (const auto &MemBuf : llvm::reverse(ChildFormatTextToApply)) {
auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
- dropDiagnosticHandler);
+ DiagHandler ? DiagHandler : dropDiagnosticHandler);
// It was already correctly parsed.
assert(!EC);
static_cast<void>(EC);
@@ -4066,7 +4070,8 @@ 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 + ": " +
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index ff3ced38a1f31..2cfa18bd6136c 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -1452,6 +1452,27 @@ TEST(ConfigParseTest, GetStyleOfSpecificFile) {
ASSERT_EQ(*Style, getGoogleStyle());
}
+TEST(ConfigParseTest, GetStyleOutput) {
+ llvm::vfs::InMemoryFileSystem FS;
+
+ // Don't suppress output.
+ testing::internal::CaptureStderr();
+ auto Style = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS,
+ /*AllowUnknownOptions=*/true);
+ const auto output = testing::internal::GetCapturedStderr();
+ ASSERT_TRUE((bool)Style);
+ ASSERT_FALSE(output.empty());
+
+ // Suppress stderr.
+ testing::internal::CaptureStderr();
+ auto Style1 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS,
+ /*AllowUnknownOptions=*/true,
+ [](const llvm::SMDiagnostic &, void *) {});
+ const auto output1 = testing::internal::GetCapturedStderr();
+ ASSERT_TRUE((bool)Style1);
+ ASSERT_TRUE(output1.empty());
+}
+
} // namespace
} // namespace format
} // namespace clang
More information about the cfe-commits
mailing list