diff options
5 files changed, 42 insertions, 3 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp index 26d9287..edb6761 100644 --- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp @@ -277,7 +277,13 @@ public: } 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 @@ private: 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 c4dad24..ccc6f3d 100644 --- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.h +++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.h @@ -64,6 +64,7 @@ private: 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 f202e051..07d7888 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -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``. 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 18ab84b2..3d00d5b 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 0000000..7d0cfe7 --- /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); +} |