diff --git a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp index 26d9287f07049..edb67614bd558 100644 --- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp @@ -277,7 +277,13 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { } 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; } @@ -583,6 +589,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { 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 +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); diff --git a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.h b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.h index c4dad24ec3998..ccc6f3d879fc0 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 f202e051aad16..07d788851f8d1 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -496,6 +496,11 @@ Changes in existing checks ` check to ignore false-positives in initializer list of record. +- Improved :doc:`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 ` check by extending the default value of the `Types` option to include ``std::span``. 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 18ab84b26a259..3d00d5b043a60 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,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 @@ -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`. 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 0000000000000..7d0cfe7e27dc2 --- /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: {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); +}