[clang] [clang-format] Add DiagHandler for getStyle function (PR #91317)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 13 06:00:30 PDT 2024


https://github.com/pointhex updated https://github.com/llvm/llvm-project/pull/91317

>From 6c8cc11d2d4591b06514f5f16e88ffd30d149488 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                | 27 +++++++++++---------
 clang/unittests/Format/ConfigParseTest.cpp | 29 ++++++++++++++++++++++
 3 files changed, 49 insertions(+), 16 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..a6400dc5f1d32 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());
@@ -4027,12 +4031,10 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
   // Reset possible inheritance
   Style.InheritsParentConfig = false;
 
-  auto dropDiagnosticHandler = [](const llvm::SMDiagnostic &, void *) {};
-
   auto applyChildFormatTexts = [&](FormatStyle *Style) {
     for (const auto &MemBuf : llvm::reverse(ChildFormatTextToApply)) {
       auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
-                                   dropDiagnosticHandler);
+                                   DiagHandler);
       // It was already correctly parsed.
       assert(!EC);
       static_cast<void>(EC);
@@ -4066,7 +4068,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..b3719118a0832 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -1452,6 +1452,35 @@ TEST(ConfigParseTest, GetStyleOfSpecificFile) {
   ASSERT_EQ(*Style, getGoogleStyle());
 }
 
+TEST(ConfigParseTest, GetStyleOutput) {
+    // With output
+    ::testing::internal::CaptureStderr();
+    llvm::vfs::InMemoryFileSystem FS;
+    auto Style = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS, false);
+
+    const std::string output = ::testing::internal::GetCapturedStderr();
+
+    ASSERT_FALSE((bool)Style);
+    ASSERT_FALSE(output.empty());
+    llvm::consumeError(Style.takeError());
+
+    // Without output
+    ::testing::internal::CaptureStderr();
+    auto Style1 = getStyle("{invalid_key=invalid_value}",
+                           "a.h",
+                           "LLVM",
+                           "",
+                           &FS,
+                           false,
+                           [](const llvm::SMDiagnostic &, void *) {});
+
+    const std::string output1 = ::testing::internal::GetCapturedStderr();
+
+    ASSERT_FALSE((bool)Style1);
+    ASSERT_TRUE(output1.empty());
+    llvm::consumeError(Style1.takeError());
+}
+
 } // namespace
 } // namespace format
 } // namespace clang



More information about the cfe-commits mailing list