-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[clang-tidy] Add option to ignore macros in readability-simplify-boolean-expr
check
#78043
Merged
SimplyDanny
merged 8 commits into
llvm:main
from
SimplyDanny:ignore-macros-simplify-boolean
Jan 14, 2024
Merged
[clang-tidy] Add option to ignore macros in readability-simplify-boolean-expr
check
#78043
SimplyDanny
merged 8 commits into
llvm:main
from
SimplyDanny:ignore-macros-simplify-boolean
Jan 14, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang-tidy Author: Danny Mösch (SimplyDanny) ChangesFull diff: https://github.com/llvm/llvm-project/pull/78043.diff 5 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
index 26d9287f07049e..34a18325ce3173 100644
--- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
@@ -277,6 +277,9 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor<Visitor> {
}
bool dataTraverseStmtPre(Stmt *S) {
+ if (Check->IgnoreMacros && S->getBeginLoc().isMacroID()) {
+ return false;
+ }
if (S && !shouldIgnore(S))
StmtStack.push_back(S);
return true;
@@ -583,6 +586,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor<Visitor> {
SimplifyBooleanExprCheck::SimplifyBooleanExprCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
+ IgnoreMacros(Options.get("IgnoreMacros", false)),
ChainedConditionalReturn(Options.get("ChainedConditionalReturn", false)),
ChainedConditionalAssignment(
Options.get("ChainedConditionalAssignment", false)),
@@ -671,6 +675,7 @@ void SimplifyBooleanExprCheck::reportBinOp(const ASTContext &Context,
}
void SimplifyBooleanExprCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "IgnoreMacros", IgnoreMacros);
Options.store(Opts, "ChainedConditionalReturn", ChainedConditionalReturn);
Options.store(Opts, "ChainedConditionalAssignment",
ChainedConditionalAssignment);
diff --git a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.h b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.h
index c4dad24ec39985..ccc6f3d879fc02 100644
--- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.h
@@ -64,6 +64,7 @@ class SimplifyBooleanExprCheck : public ClangTidyCheck {
StringRef Description, SourceRange ReplacementRange,
StringRef Replacement);
+ const bool IgnoreMacros;
const bool ChainedConditionalReturn;
const bool ChainedConditionalAssignment;
const bool SimplifyDeMorgan;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 3437b6cf9b59e9..3b556418238db5 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -500,6 +500,9 @@ Changes in existing checks
<clang-tidy/checks/readability/static-accessed-through-instance>` check to
identify calls to static member functions with out-of-class inline definitions.
+- Added option `IgnoreMacros` to :doc:`readability-simplify-boolean-expr` check.
+ It makes the check ignore boolean expressions passed into macros.
+
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/simplify-boolean-expr.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/simplify-boolean-expr.rst
index 18ab84b26a2595..443bcadc9f12e6 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/readability/simplify-boolean-expr.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability/simplify-boolean-expr.rst
@@ -82,6 +82,10 @@ Examples:
Options
-------
+.. option:: IgnoreMacros
+
+ If `true`, ignore boolean expressions passed into macros. Default is `false`.
+
.. option:: ChainedConditionalReturn
If `true`, conditional boolean return statements at the end of an
@@ -99,8 +103,8 @@ Options
.. option:: SimplifyDeMorganRelaxed
- If `true`, :option:`SimplifyDeMorgan` will also transform negated
- conjunctions and disjunctions where there is no negation on either operand.
+ If `true`, :option:`SimplifyDeMorgan` will also transform negated
+ conjunctions and disjunctions where there is no negation on either operand.
This option has no effect if :option:`SimplifyDeMorgan` is `false`.
Default is `false`.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-macros.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-macros.cpp
new file mode 100644
index 00000000000000..55f586c168beb9
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-macros.cpp
@@ -0,0 +1,20 @@
+// RUN: %check_clang_tidy -check-suffixes=,MACROS %s readability-simplify-boolean-expr %t
+
+// Ignore expressions in macros.
+// RUN: %check_clang_tidy %s readability-simplify-boolean-expr %t
+// RUN: -- -config="{CheckOptions: [{key: readability-simplify-boolean-expr.IgnoreMacros, value: true}]}"
+// RUN: --
+
+#define NEGATE(expr) !(expr)
+
+bool without_macro(bool a, bool b) {
+ return !(!a && b);
+ // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: boolean expression can be simplified by DeMorgan's theorem
+ // CHECK-FIXES: return a || !b;
+}
+
+bool macro(bool a, bool b) {
+ return NEGATE(!a && b);
+ // CHECK-MESSAGES-MACROS: :[[@LINE-1]]:12: warning: boolean expression can be simplified by DeMorgan's theorem
+ // CHECK-FIXES: return NEGATE(!a && b);
+}
|
SimplyDanny
commented
Jan 13, 2024
clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-macros.cpp
Show resolved
Hide resolved
SimplyDanny
force-pushed
the
ignore-macros-simplify-boolean
branch
from
January 13, 2024 15:43
7dadf54
to
55d278f
Compare
PiotrZSL
reviewed
Jan 13, 2024
clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-macros.cpp
Outdated
Show resolved
Hide resolved
Thanks @PiotrZSL for the review! I've addressed your remarks. Would you support me with the test as well? |
PiotrZSL
reviewed
Jan 13, 2024
clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-macros.cpp
Outdated
Show resolved
Hide resolved
PiotrZSL
approved these changes
Jan 13, 2024
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.
LGTM
justinfargnoli
pushed a commit
to justinfargnoli/llvm-project
that referenced
this pull request
Jan 28, 2024
…lean-expr` check (llvm#78043)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #57975.