Skip to content

Commit

Permalink
[clang-tidy] Add option to ignore macros in `readability-simplify-boo…
Browse files Browse the repository at this point in the history
…lean-expr` check (#78043)
  • Loading branch information
SimplyDanny committed Jan 14, 2024
1 parent 21b2f30 commit 5295ca1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor<Visitor> {
}

bool dataTraverseStmtPre(Stmt *S) {
if (S && !shouldIgnore(S))
if (!S) {
return true;
}
if (Check->IgnoreMacros && S->getBeginLoc().isMacroID()) {
return false;
}
if (!shouldIgnore(S))
StmtStack.push_back(S);
return true;
}
Expand Down Expand Up @@ -583,6 +589,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)),
Expand Down Expand Up @@ -671,6 +678,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,11 @@ Changes in existing checks
<clang-tidy/checks/readability/non-const-parameter>` check to ignore
false-positives in initializer list of record.

- Improved :doc:`readability-simplify-boolean-expr
<clang-tidy/checks/readability/simplify-boolean-expr>` check by adding the
new option `IgnoreMacros` that allows to ignore boolean expressions originating
from expanded macros.

- Improved :doc:`readability-simplify-subscript-expr
<clang-tidy/checks/readability/simplify-subscript-expr>` check by extending
the default value of the `Types` option to include ``std::span``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ Examples:
Options
-------

.. option:: IgnoreMacros

If `true`, ignore boolean expressions originating from expanded macros.
Default is `false`.

.. option:: ChainedConditionalReturn

If `true`, conditional boolean return statements at the end of an
Expand All @@ -99,8 +104,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`.

Expand Down
Original file line number Diff line number Diff line change
@@ -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: {readability-simplify-boolean-expr.IgnoreMacros: 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);
}

0 comments on commit 5295ca1

Please sign in to comment.