https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/91317
>From c1e0ad6ee57a95fa4321bbe91aa754167da9fb3b Mon Sep 17 00:00:00 2001 From: Artem Sokolovskii <artem.sokolovs...@qt.io> Date: Tue, 7 May 2024 12:27:29 +0200 Subject: [PATCH 1/3] [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 >From d84118756be8e752dd7402178812cd0e22abc0c0 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Fri, 14 Jun 2024 21:24:26 -0700 Subject: [PATCH 2/3] Fix formatting errors. --- clang/lib/Format/Format.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index b1def18f9b7da..cd21fbb2221ac 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3959,10 +3959,11 @@ loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS, return Text; } -Expected<FormatStyle> -getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyleName, - StringRef Code, llvm::vfs::FileSystem *FS, bool AllowUnknownOptions, - llvm::SourceMgr::DiagHandlerTy DiagHandler) { +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)) @@ -4035,8 +4036,9 @@ getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyleName, auto applyChildFormatTexts = [&](FormatStyle *Style) { for (const auto &MemBuf : llvm::reverse(ChildFormatTextToApply)) { - auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions, - DiagHandler ? DiagHandler : dropDiagnosticHandler); + auto EC = + parseConfiguration(*MemBuf, Style, AllowUnknownOptions, + DiagHandler ? DiagHandler : dropDiagnosticHandler); // It was already correctly parsed. assert(!EC); static_cast<void>(EC); >From 842bf9de3a931a0b74991fd6b943f39c8888c85e Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Fri, 14 Jun 2024 21:30:58 -0700 Subject: [PATCH 3/3] Capitalize and reuse variables. --- clang/unittests/Format/ConfigParseTest.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 2cfa18bd6136c..2513ab3939595 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -1459,18 +1459,18 @@ TEST(ConfigParseTest, GetStyleOutput) { testing::internal::CaptureStderr(); auto Style = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS, /*AllowUnknownOptions=*/true); - const auto output = testing::internal::GetCapturedStderr(); + auto Output = testing::internal::GetCapturedStderr(); ASSERT_TRUE((bool)Style); - ASSERT_FALSE(output.empty()); + 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()); + Style = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS, + /*AllowUnknownOptions=*/true, + [](const llvm::SMDiagnostic &, void *) {}); + Output = testing::internal::GetCapturedStderr(); + ASSERT_TRUE((bool)Style); + ASSERT_TRUE(Output.empty()); } } // namespace _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits