Skip to content

[clang-format] Add DiagHandler parameter to format::getStyle() #91317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 16, 2024

Conversation

pointhex
Copy link
Contributor

@pointhex pointhex commented May 7, 2024

It allows to control of error output for the function.

Closes #94205.

Copy link

github-actions bot commented May 7, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented May 7, 2024

@llvm/pr-subscribers-clang-format

Author: None (pointhex)

Changes

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 --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 + ": " +

Copy link

github-actions bot commented May 9, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@mydeveloperday
Copy link
Contributor

is there a github issue for this?

@pointhex
Copy link
Contributor Author

@mydeveloperday There is no issue, It is my request. I implemented it without creating an issue or suggestion. Should I?

@pointhex pointhex force-pushed the addToGetStyleDiagHandler branch 2 times, most recently from 9cc8bf3 to 3a0808d Compare May 14, 2024 13:30
@HazardyKnusperkeks
Copy link
Contributor

For the record, I see no harm in this PR and would also merge it without an issue. Just giving @mydeveloperday a bit more time to answer. :)

@pointhex
Copy link
Contributor Author

pointhex commented Jun 3, 2024

Hi @mydeveloperday, friendly reminder :)

@mydeveloperday
Copy link
Contributor

Please log an issue explaining the problem you are trying solve, its not clear to me what problem led you to this.

@mydeveloperday mydeveloperday requested a review from owenca June 3, 2024 09:55
@mydeveloperday mydeveloperday changed the title [ClangFormat] Add DiagHandler for getStyle function [clang-format] Add DiagHandler for getStyle function Jun 3, 2024
@pointhex
Copy link
Contributor Author

pointhex commented Jun 3, 2024

Done: #94205

Copy link
Contributor

@HazardyKnusperkeks HazardyKnusperkeks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I see you want to incorporate it into QtCreator I'm even more in favor of adding it. ;)

Copy link
Contributor

@mydeveloperday mydeveloperday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a major issue, but I think it needs a unit test otherwise we might forget how you are using it.

@mydeveloperday
Copy link
Contributor

I also think we need @owenca opinion

Copy link
Contributor

@owenca owenca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you call parseConfiguration() instead of getStyle() in QtCreator while the user is entering code? It seems inefficient to call getStyle() all the time.

@pointhex
Copy link
Contributor Author

pointhex commented Jun 4, 2024

Can you call parseConfiguration() instead of getStyle() in QtCreator while the user is entering code? It seems inefficient to call getStyle() all the time.

I do it in the places where I can use parseConfiguration instead. But in some cases I need getStyle and it's also called quite often. I have one optimisation in my mind, but I would like to have that patch as well anyway.

Am I right that no one has a major issue with the patch? So fixing the "const" issue and adding a unit test will let that in?

@pointhex pointhex force-pushed the addToGetStyleDiagHandler branch from 4a2cefa to 3763408 Compare June 6, 2024 09:37
@pointhex
Copy link
Contributor Author

@mydeveloperday @owenca Kind reminder.

@owenca
Copy link
Contributor

owenca commented Jun 11, 2024

Am I right that no one has a major issue with the patch? So fixing the "const" issue and adding a unit test will let that in?

Can you add a unit test?

@pointhex
Copy link
Contributor Author

pointhex commented Jun 11, 2024

Can you add a unit test?

Yes, no problem, I just didn't get if it is needed or not.

@owenca
Copy link
Contributor

owenca commented Jun 12, 2024

@mydeveloperday requested it. See #91317 (review).

There are calls to getStyle() in ConfigParseTest.cpp. Maybe add a test there?

@pointhex pointhex force-pushed the addToGetStyleDiagHandler branch from 3763408 to 6c8cc11 Compare June 13, 2024 13:00
@pointhex
Copy link
Contributor Author

I used to redirect output. I guess that is enough. Maybe, you know a better solution for that.

    auto Style = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS, false);
    ...
    const std::string output = ::testing::internal::GetCapturedStderr();

Comment on lines 4030 to 4031
auto dropDiagnosticHandler = [](const llvm::SMDiagnostic &, void *) {};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry! We do need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem.

Comment on lines 4036 to 4037
auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
dropDiagnosticHandler);
DiagHandler);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

   auto applyChildFormatTexts = [&](FormatStyle *Style) {
     for (const auto &MemBuf : llvm::reverse(ChildFormatTextToApply)) {
-      auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
-                                   DiagHandler);
+      auto EC =
+          parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
+                             DiagHandler ? DiagHandler : dropDiagnosticHandler);
       // It was already correctly parsed.
       assert(!EC);
       static_cast<void>(EC);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 1467 to 1481
// 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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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());
// 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());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 1456 to 1465
// 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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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());
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());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

It allows to control of error output for the function.
@pointhex pointhex force-pushed the addToGetStyleDiagHandler branch from 6c8cc11 to c1e0ad6 Compare June 14, 2024 09:18
@owenca owenca changed the title [clang-format] Add DiagHandler for getStyle function [clang-format] Add DiagHandler parameter to format::getStyle() Jun 15, 2024
@owenca owenca merged commit fe9aef0 into llvm:main Jun 16, 2024
5 of 7 checks passed
Copy link

@pointhex Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@pointhex
Copy link
Contributor Author

@owenca Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Impossible to redirect diagnostic output getStyle function
5 participants