-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be 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 If you have received no comments on your PR for a week, you can request a review 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. |
@llvm/pr-subscribers-clang-format Author: None (pointhex) ChangesIt allows to control of error output for the function. Full diff: https://github.com/llvm/llvm-project/pull/91317.diff 2 Files Affected:
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 + ": " +
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
is there a github issue for this? |
@mydeveloperday There is no issue, It is my request. I implemented it without creating an issue or suggestion. Should I? |
9cc8bf3
to
3a0808d
Compare
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. :) |
Hi @mydeveloperday, friendly reminder :) |
Please log an issue explaining the problem you are trying solve, its not clear to me what problem led you to this. |
Done: #94205 |
There was a problem hiding this 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. ;)
There was a problem hiding this 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.
I also think we need @owenca opinion |
There was a problem hiding this 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.
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? |
4a2cefa
to
3763408
Compare
@mydeveloperday @owenca Kind reminder. |
Can you add a unit test? |
Yes, no problem, I just didn't get if it is needed or not. |
@mydeveloperday requested it. See #91317 (review). There are calls to |
3763408
to
6c8cc11
Compare
I used to redirect output. I guess that is enough. Maybe, you know a better solution for that.
|
clang/lib/Format/Format.cpp
Outdated
auto dropDiagnosticHandler = [](const llvm::SMDiagnostic &, void *) {}; | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem.
clang/lib/Format/Format.cpp
Outdated
auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions, | ||
dropDiagnosticHandler); | ||
DiagHandler); |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
// 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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
// 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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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()); |
There was a problem hiding this comment.
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.
6c8cc11
to
c1e0ad6
Compare
@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 Please check whether problems have been caused by your change specifically, as 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. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
@owenca Thanks a lot! |
It allows to control of error output for the function.
Closes #94205.