diff options
Diffstat (limited to 'compilerplugins/clang')
-rw-r--r-- | compilerplugins/clang/simplifybool.cxx | 32 | ||||
-rw-r--r-- | compilerplugins/clang/test/simplifybool.cxx | 32 |
2 files changed, 61 insertions, 3 deletions
diff --git a/compilerplugins/clang/simplifybool.cxx b/compilerplugins/clang/simplifybool.cxx index 091591f30988..260946a5fd98 100644 --- a/compilerplugins/clang/simplifybool.cxx +++ b/compilerplugins/clang/simplifybool.cxx @@ -7,10 +7,35 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include <cassert> + #include "plugin.hxx" namespace { +// Like clang::Stmt::IgnoreImplicit (lib/AST/Stmt.cpp), but also looking through implicit +// UserDefinedConversion's member function call: +Expr const * ignoreAllImplicit(Expr const * expr) { + if (auto const e = dyn_cast<ExprWithCleanups>(expr)) { + expr = e->getSubExpr(); + } + if (auto const e = dyn_cast<MaterializeTemporaryExpr>(expr)) { + expr = e->GetTemporaryExpr(); + } + if (auto const e = dyn_cast<CXXBindTemporaryExpr>(expr)) { + expr = e->getSubExpr(); + } + while (auto const e = dyn_cast<ImplicitCastExpr>(expr)) { + expr = e->getSubExpr(); + if (e->getCastKind() == CK_UserDefinedConversion) { + auto const ce = cast<CXXMemberCallExpr>(expr); + assert(ce->getNumArgs() == 0); + expr = ce->getImplicitObjectArgument(); + } + } + return expr; +} + Expr const * ignoreParenImpCastAndComma(Expr const * expr) { for (;;) { expr = expr->IgnoreParenImpCasts(); @@ -94,15 +119,16 @@ bool SimplifyBool::VisitUnaryLNot(UnaryOperator const * expr) { if (e->getLocStart().isMacroID()) return true; // double logical not of an int is an idiom to convert to bool - if (!e->IgnoreImpCasts()->getType()->isBooleanType()) + auto const sub = ignoreAllImplicit(e); + if (!sub->getType()->isBooleanType()) return true; report( DiagnosticsEngine::Warning, ("double logical negation expression of the form '!!A' (with A of type" " %0) can %select{logically|literally}1 be simplified as 'A'"), expr->getLocStart()) - << e->IgnoreImpCasts()->getType() - << e->IgnoreImpCasts()->getType()->isBooleanType() + << sub->getType() + << sub->getType()->isBooleanType() << expr->getSourceRange(); return true; } diff --git a/compilerplugins/clang/test/simplifybool.cxx b/compilerplugins/clang/test/simplifybool.cxx index 383e513b25e2..e6b52c33f3b6 100644 --- a/compilerplugins/clang/test/simplifybool.cxx +++ b/compilerplugins/clang/test/simplifybool.cxx @@ -15,4 +15,36 @@ void f1(int a, int b) } }; +// Consitently either warn about all or none of the below occurrences of "!!": + +enum E1 +{ + E1_1 = 1 +}; + +enum E2 +{ + E2_1 = 1 +}; +E2 operator&(E2 e1, E2 e2); +bool operator!(E2 e); + +enum class E3 +{ + E1 = 1 +}; +struct W +{ + operator bool(); +}; +W operator&(E3 e1, E3 e2); + +bool f0(int n) { return !!(n & 1); } + +bool f1(E1 e) { return !!(e & E1_1); } + +bool f2(E2 e) { return !!(e & E2_1); } + +bool f3(E3 e) { return !!(e & E3::E1); } + /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |