Skip to content

Commit 5295ca1

Browse files
authored
[clang-tidy] Add option to ignore macros in readability-simplify-boolean-expr check (llvm#78043)
1 parent 21b2f30 commit 5295ca1

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,13 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor<Visitor> {
277277
}
278278

279279
bool dataTraverseStmtPre(Stmt *S) {
280-
if (S && !shouldIgnore(S))
280+
if (!S) {
281+
return true;
282+
}
283+
if (Check->IgnoreMacros && S->getBeginLoc().isMacroID()) {
284+
return false;
285+
}
286+
if (!shouldIgnore(S))
281287
StmtStack.push_back(S);
282288
return true;
283289
}
@@ -583,6 +589,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor<Visitor> {
583589
SimplifyBooleanExprCheck::SimplifyBooleanExprCheck(StringRef Name,
584590
ClangTidyContext *Context)
585591
: ClangTidyCheck(Name, Context),
592+
IgnoreMacros(Options.get("IgnoreMacros", false)),
586593
ChainedConditionalReturn(Options.get("ChainedConditionalReturn", false)),
587594
ChainedConditionalAssignment(
588595
Options.get("ChainedConditionalAssignment", false)),
@@ -671,6 +678,7 @@ void SimplifyBooleanExprCheck::reportBinOp(const ASTContext &Context,
671678
}
672679

673680
void SimplifyBooleanExprCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
681+
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
674682
Options.store(Opts, "ChainedConditionalReturn", ChainedConditionalReturn);
675683
Options.store(Opts, "ChainedConditionalAssignment",
676684
ChainedConditionalAssignment);

clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class SimplifyBooleanExprCheck : public ClangTidyCheck {
6464
StringRef Description, SourceRange ReplacementRange,
6565
StringRef Replacement);
6666

67+
const bool IgnoreMacros;
6768
const bool ChainedConditionalReturn;
6869
const bool ChainedConditionalAssignment;
6970
const bool SimplifyDeMorgan;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,11 @@ Changes in existing checks
496496
<clang-tidy/checks/readability/non-const-parameter>` check to ignore
497497
false-positives in initializer list of record.
498498

499+
- Improved :doc:`readability-simplify-boolean-expr
500+
<clang-tidy/checks/readability/simplify-boolean-expr>` check by adding the
501+
new option `IgnoreMacros` that allows to ignore boolean expressions originating
502+
from expanded macros.
503+
499504
- Improved :doc:`readability-simplify-subscript-expr
500505
<clang-tidy/checks/readability/simplify-subscript-expr>` check by extending
501506
the default value of the `Types` option to include ``std::span``.

clang-tools-extra/docs/clang-tidy/checks/readability/simplify-boolean-expr.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ Examples:
8282
Options
8383
-------
8484

85+
.. option:: IgnoreMacros
86+
87+
If `true`, ignore boolean expressions originating from expanded macros.
88+
Default is `false`.
89+
8590
.. option:: ChainedConditionalReturn
8691

8792
If `true`, conditional boolean return statements at the end of an
@@ -99,8 +104,8 @@ Options
99104

100105
.. option:: SimplifyDeMorganRelaxed
101106

102-
If `true`, :option:`SimplifyDeMorgan` will also transform negated
103-
conjunctions and disjunctions where there is no negation on either operand.
107+
If `true`, :option:`SimplifyDeMorgan` will also transform negated
108+
conjunctions and disjunctions where there is no negation on either operand.
104109
This option has no effect if :option:`SimplifyDeMorgan` is `false`.
105110
Default is `false`.
106111

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %check_clang_tidy -check-suffixes=,MACROS %s readability-simplify-boolean-expr %t
2+
3+
// Ignore expressions in macros.
4+
// RUN: %check_clang_tidy %s readability-simplify-boolean-expr %t \
5+
// RUN: -- -config="{CheckOptions: {readability-simplify-boolean-expr.IgnoreMacros: true}}" \
6+
// RUN: --
7+
8+
#define NEGATE(expr) !(expr)
9+
10+
bool without_macro(bool a, bool b) {
11+
return !(!a && b);
12+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: boolean expression can be simplified by DeMorgan's theorem
13+
// CHECK-FIXES: return a || !b;
14+
}
15+
16+
bool macro(bool a, bool b) {
17+
return NEGATE(!a && b);
18+
// CHECK-MESSAGES-MACROS: :[[@LINE-1]]:12: warning: boolean expression can be simplified by DeMorgan's theorem
19+
// CHECK-FIXES: return NEGATE(!a && b);
20+
}

0 commit comments

Comments
 (0)