Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 1 | //===--- ImplicitBoolConversionCheck.cpp - clang-tidy----------------------===// |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 9 | #include "ImplicitBoolConversionCheck.h" |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | #include "clang/Lex/Lexer.h" |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 13 | #include "clang/Tooling/FixIt.h" |
Alexander Kornienko | 4e001b5 | 2017-04-29 12:06:45 +0000 | [diff] [blame] | 14 | #include <queue> |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace clang::ast_matchers; |
| 17 | |
| 18 | namespace clang { |
| 19 | namespace tidy { |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 20 | namespace readability { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 24 | AST_MATCHER(Stmt, isMacroExpansion) { |
| 25 | SourceManager &SM = Finder->getASTContext().getSourceManager(); |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 26 | SourceLocation Loc = Node.getBeginLoc(); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 27 | return SM.isMacroBodyExpansion(Loc) || SM.isMacroArgExpansion(Loc); |
| 28 | } |
| 29 | |
| 30 | bool isNULLMacroExpansion(const Stmt *Statement, ASTContext &Context) { |
| 31 | SourceManager &SM = Context.getSourceManager(); |
| 32 | const LangOptions &LO = Context.getLangOpts(); |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 33 | SourceLocation Loc = Statement->getBeginLoc(); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 34 | return SM.isMacroBodyExpansion(Loc) && |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 35 | Lexer::getImmediateMacroName(Loc, SM, LO) == "NULL"; |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | AST_MATCHER(Stmt, isNULLMacroExpansion) { |
| 39 | return isNULLMacroExpansion(&Node, Finder->getASTContext()); |
| 40 | } |
| 41 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 42 | StringRef getZeroLiteralToCompareWithForType(CastKind CastExprKind, |
| 43 | QualType Type, |
| 44 | ASTContext &Context) { |
| 45 | switch (CastExprKind) { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 46 | case CK_IntegralToBoolean: |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 47 | return Type->isUnsignedIntegerType() ? "0u" : "0"; |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 48 | |
| 49 | case CK_FloatingToBoolean: |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 50 | return Context.hasSameType(Type, Context.FloatTy) ? "0.0f" : "0.0"; |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 51 | |
| 52 | case CK_PointerToBoolean: |
| 53 | case CK_MemberPointerToBoolean: // Fall-through on purpose. |
| 54 | return Context.getLangOpts().CPlusPlus11 ? "nullptr" : "0"; |
| 55 | |
| 56 | default: |
Benjamin Kramer | 6d505c0 | 2015-10-25 22:03:00 +0000 | [diff] [blame] | 57 | llvm_unreachable("Unexpected cast kind"); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 58 | } |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | bool isUnaryLogicalNotOperator(const Stmt *Statement) { |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 62 | const auto *UnaryOperatorExpr = dyn_cast<UnaryOperator>(Statement); |
| 63 | return UnaryOperatorExpr && UnaryOperatorExpr->getOpcode() == UO_LNot; |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | bool areParensNeededForOverloadedOperator(OverloadedOperatorKind OperatorKind) { |
| 67 | switch (OperatorKind) { |
| 68 | case OO_New: |
| 69 | case OO_Delete: // Fall-through on purpose. |
| 70 | case OO_Array_New: |
| 71 | case OO_Array_Delete: |
| 72 | case OO_ArrowStar: |
| 73 | case OO_Arrow: |
| 74 | case OO_Call: |
| 75 | case OO_Subscript: |
| 76 | return false; |
| 77 | |
| 78 | default: |
| 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | bool areParensNeededForStatement(const Stmt *Statement) { |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 84 | if (const auto *OperatorCall = dyn_cast<CXXOperatorCallExpr>(Statement)) { |
| 85 | return areParensNeededForOverloadedOperator(OperatorCall->getOperator()); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 88 | return isa<BinaryOperator>(Statement) || isa<UnaryOperator>(Statement); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 91 | void fixGenericExprCastToBool(DiagnosticBuilder &Diag, |
| 92 | const ImplicitCastExpr *Cast, const Stmt *Parent, |
| 93 | ASTContext &Context) { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 94 | // In case of expressions like (! integer), we should remove the redundant not |
| 95 | // operator and use inverted comparison (integer == 0). |
| 96 | bool InvertComparison = |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 97 | Parent != nullptr && isUnaryLogicalNotOperator(Parent); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 98 | if (InvertComparison) { |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 99 | SourceLocation ParentStartLoc = Parent->getBeginLoc(); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 100 | SourceLocation ParentEndLoc = |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 101 | cast<UnaryOperator>(Parent)->getSubExpr()->getBeginLoc(); |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 102 | Diag << FixItHint::CreateRemoval( |
| 103 | CharSourceRange::getCharRange(ParentStartLoc, ParentEndLoc)); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 104 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 105 | Parent = Context.getParents(*Parent)[0].get<Stmt>(); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 108 | const Expr *SubExpr = Cast->getSubExpr(); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 109 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 110 | bool NeedInnerParens = areParensNeededForStatement(SubExpr); |
| 111 | bool NeedOuterParens = |
| 112 | Parent != nullptr && areParensNeededForStatement(Parent); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 113 | |
| 114 | std::string StartLocInsertion; |
| 115 | |
| 116 | if (NeedOuterParens) { |
| 117 | StartLocInsertion += "("; |
| 118 | } |
| 119 | if (NeedInnerParens) { |
| 120 | StartLocInsertion += "("; |
| 121 | } |
| 122 | |
| 123 | if (!StartLocInsertion.empty()) { |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 124 | Diag << FixItHint::CreateInsertion(Cast->getBeginLoc(), StartLocInsertion); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | std::string EndLocInsertion; |
| 128 | |
| 129 | if (NeedInnerParens) { |
| 130 | EndLocInsertion += ")"; |
| 131 | } |
| 132 | |
| 133 | if (InvertComparison) { |
| 134 | EndLocInsertion += " == "; |
| 135 | } else { |
| 136 | EndLocInsertion += " != "; |
| 137 | } |
| 138 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 139 | EndLocInsertion += getZeroLiteralToCompareWithForType( |
| 140 | Cast->getCastKind(), SubExpr->getType(), Context); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 141 | |
| 142 | if (NeedOuterParens) { |
| 143 | EndLocInsertion += ")"; |
| 144 | } |
| 145 | |
| 146 | SourceLocation EndLoc = Lexer::getLocForEndOfToken( |
Stephen Kelly | c09197e | 2018-08-09 22:43:02 +0000 | [diff] [blame] | 147 | Cast->getEndLoc(), 0, Context.getSourceManager(), Context.getLangOpts()); |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 148 | Diag << FixItHint::CreateInsertion(EndLoc, EndLocInsertion); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 151 | StringRef getEquivalentBoolLiteralForExpr(const Expr *Expression, |
| 152 | ASTContext &Context) { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 153 | if (isNULLMacroExpansion(Expression, Context)) { |
| 154 | return "false"; |
| 155 | } |
| 156 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 157 | if (const auto *IntLit = dyn_cast<IntegerLiteral>(Expression)) { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 158 | return (IntLit->getValue() == 0) ? "false" : "true"; |
| 159 | } |
| 160 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 161 | if (const auto *FloatLit = dyn_cast<FloatingLiteral>(Expression)) { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 162 | llvm::APFloat FloatLitAbsValue = FloatLit->getValue(); |
| 163 | FloatLitAbsValue.clearSign(); |
| 164 | return (FloatLitAbsValue.bitcastToAPInt() == 0) ? "false" : "true"; |
| 165 | } |
| 166 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 167 | if (const auto *CharLit = dyn_cast<CharacterLiteral>(Expression)) { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 168 | return (CharLit->getValue() == 0) ? "false" : "true"; |
| 169 | } |
| 170 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 171 | if (isa<StringLiteral>(Expression->IgnoreCasts())) { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 172 | return "true"; |
| 173 | } |
| 174 | |
| 175 | return StringRef(); |
| 176 | } |
| 177 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 178 | void fixGenericExprCastFromBool(DiagnosticBuilder &Diag, |
| 179 | const ImplicitCastExpr *Cast, |
| 180 | ASTContext &Context, StringRef OtherType) { |
| 181 | const Expr *SubExpr = Cast->getSubExpr(); |
| 182 | bool NeedParens = !isa<ParenExpr>(SubExpr); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 183 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 184 | Diag << FixItHint::CreateInsertion( |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 185 | Cast->getBeginLoc(), |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 186 | (Twine("static_cast<") + OtherType + ">" + (NeedParens ? "(" : "")) |
| 187 | .str()); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 188 | |
| 189 | if (NeedParens) { |
| 190 | SourceLocation EndLoc = Lexer::getLocForEndOfToken( |
Stephen Kelly | c09197e | 2018-08-09 22:43:02 +0000 | [diff] [blame] | 191 | Cast->getEndLoc(), 0, Context.getSourceManager(), |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 192 | Context.getLangOpts()); |
| 193 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 194 | Diag << FixItHint::CreateInsertion(EndLoc, ")"); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 198 | StringRef getEquivalentForBoolLiteral(const CXXBoolLiteralExpr *BoolLiteral, |
| 199 | QualType DestType, ASTContext &Context) { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 200 | // Prior to C++11, false literal could be implicitly converted to pointer. |
| 201 | if (!Context.getLangOpts().CPlusPlus11 && |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 202 | (DestType->isPointerType() || DestType->isMemberPointerType()) && |
| 203 | BoolLiteral->getValue() == false) { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 204 | return "0"; |
| 205 | } |
| 206 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 207 | if (DestType->isFloatingType()) { |
| 208 | if (Context.hasSameType(DestType, Context.FloatTy)) { |
| 209 | return BoolLiteral->getValue() ? "1.0f" : "0.0f"; |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 210 | } |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 211 | return BoolLiteral->getValue() ? "1.0" : "0.0"; |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 214 | if (DestType->isUnsignedIntegerType()) { |
| 215 | return BoolLiteral->getValue() ? "1u" : "0u"; |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 216 | } |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 217 | return BoolLiteral->getValue() ? "1" : "0"; |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 220 | bool isCastAllowedInCondition(const ImplicitCastExpr *Cast, |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 221 | ASTContext &Context) { |
Alexander Kornienko | 4e001b5 | 2017-04-29 12:06:45 +0000 | [diff] [blame] | 222 | std::queue<const Stmt *> Q; |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 223 | Q.push(Cast); |
Alexander Kornienko | 4e001b5 | 2017-04-29 12:06:45 +0000 | [diff] [blame] | 224 | while (!Q.empty()) { |
| 225 | for (const auto &N : Context.getParents(*Q.front())) { |
| 226 | const Stmt *S = N.get<Stmt>(); |
| 227 | if (!S) |
| 228 | return false; |
Alexander Kornienko | f89e0bb | 2017-05-08 15:22:09 +0000 | [diff] [blame] | 229 | if (isa<IfStmt>(S) || isa<ConditionalOperator>(S) || isa<ForStmt>(S) || |
| 230 | isa<WhileStmt>(S) || isa<BinaryConditionalOperator>(S)) |
Alexander Kornienko | 4e001b5 | 2017-04-29 12:06:45 +0000 | [diff] [blame] | 231 | return true; |
| 232 | if (isa<ParenExpr>(S) || isa<ImplicitCastExpr>(S) || |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 233 | isUnaryLogicalNotOperator(S) || |
Alexander Kornienko | 4e001b5 | 2017-04-29 12:06:45 +0000 | [diff] [blame] | 234 | (isa<BinaryOperator>(S) && cast<BinaryOperator>(S)->isLogicalOp())) { |
| 235 | Q.push(S); |
| 236 | } else { |
| 237 | return false; |
| 238 | } |
| 239 | } |
| 240 | Q.pop(); |
| 241 | } |
| 242 | return false; |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | } // anonymous namespace |
| 246 | |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 247 | ImplicitBoolConversionCheck::ImplicitBoolConversionCheck( |
| 248 | StringRef Name, ClangTidyContext *Context) |
Haojian Wu | 80c1c9f | 2016-08-16 11:15:05 +0000 | [diff] [blame] | 249 | : ClangTidyCheck(Name, Context), |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 250 | AllowIntegerConditions(Options.get("AllowIntegerConditions", false)), |
| 251 | AllowPointerConditions(Options.get("AllowPointerConditions", false)) {} |
Haojian Wu | 80c1c9f | 2016-08-16 11:15:05 +0000 | [diff] [blame] | 252 | |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 253 | void ImplicitBoolConversionCheck::storeOptions( |
| 254 | ClangTidyOptions::OptionMap &Opts) { |
| 255 | Options.store(Opts, "AllowIntegerConditions", AllowIntegerConditions); |
| 256 | Options.store(Opts, "AllowPointerConditions", AllowPointerConditions); |
Haojian Wu | 80c1c9f | 2016-08-16 11:15:05 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 259 | void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) { |
Alexander Kornienko | e133140 | 2017-05-16 15:44:42 +0000 | [diff] [blame] | 260 | auto exceptionCases = |
Alexander Kornienko | 50de3ad | 2017-05-16 16:40:46 +0000 | [diff] [blame] | 261 | expr(anyOf(allOf(isMacroExpansion(), unless(isNULLMacroExpansion())), |
Alexander Kornienko | c5016c0 | 2018-10-02 11:38:41 +0000 | [diff] [blame] | 262 | has(ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1)))))), |
Alexander Kornienko | 50de3ad | 2017-05-16 16:40:46 +0000 | [diff] [blame] | 263 | hasParent(explicitCastExpr()))); |
Alexander Kornienko | c6ba6fc | 2017-05-04 16:06:08 +0000 | [diff] [blame] | 264 | auto implicitCastFromBool = implicitCastExpr( |
Alexander Kornienko | c6ba6fc | 2017-05-04 16:06:08 +0000 | [diff] [blame] | 265 | anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating), |
| 266 | // Prior to C++11 cast from bool literal to pointer was allowed. |
| 267 | allOf(anyOf(hasCastKind(CK_NullToPointer), |
| 268 | hasCastKind(CK_NullToMemberPointer)), |
| 269 | hasSourceExpression(cxxBoolLiteral()))), |
Alexander Kornienko | 50de3ad | 2017-05-16 16:40:46 +0000 | [diff] [blame] | 270 | hasSourceExpression(expr(hasType(booleanType()))), |
| 271 | unless(exceptionCases)); |
Alexander Kornienko | c6ba6fc | 2017-05-04 16:06:08 +0000 | [diff] [blame] | 272 | auto boolXor = |
| 273 | binaryOperator(hasOperatorName("^"), hasLHS(implicitCastFromBool), |
| 274 | hasRHS(implicitCastFromBool)); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 275 | Finder->addMatcher( |
| 276 | implicitCastExpr( |
Alexander Kornienko | 50de3ad | 2017-05-16 16:40:46 +0000 | [diff] [blame] | 277 | anyOf(hasCastKind(CK_IntegralToBoolean), |
| 278 | hasCastKind(CK_FloatingToBoolean), |
| 279 | hasCastKind(CK_PointerToBoolean), |
| 280 | hasCastKind(CK_MemberPointerToBoolean)), |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 281 | // Exclude case of using if or while statements with variable |
| 282 | // declaration, e.g.: |
| 283 | // if (int var = functionCall()) {} |
| 284 | unless( |
| 285 | hasParent(stmt(anyOf(ifStmt(), whileStmt()), has(declStmt())))), |
Alexander Kornienko | 50de3ad | 2017-05-16 16:40:46 +0000 | [diff] [blame] | 286 | // Exclude cases common to implicit cast to and from bool. |
| 287 | unless(exceptionCases), unless(has(boolXor)), |
Kazuaki Ishizaki | dd5571d | 2020-04-05 15:28:11 +0900 | [diff] [blame^] | 288 | // Retrieve also parent statement, to check if we need additional |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 289 | // parens in replacement. |
Alexander Kornienko | e133140 | 2017-05-16 15:44:42 +0000 | [diff] [blame] | 290 | anyOf(hasParent(stmt().bind("parentStmt")), anything()), |
| 291 | unless(isInTemplateInstantiation()), |
| 292 | unless(hasAncestor(functionTemplateDecl()))) |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 293 | .bind("implicitCastToBool"), |
| 294 | this); |
| 295 | |
Nathan James | 97572fa | 2020-03-10 00:42:21 +0000 | [diff] [blame] | 296 | auto boolComparison = binaryOperator(hasAnyOperatorName("==", "!="), |
| 297 | hasLHS(implicitCastFromBool), |
| 298 | hasRHS(implicitCastFromBool)); |
| 299 | auto boolOpAssignment = binaryOperator(hasAnyOperatorName("|=", "&="), |
| 300 | hasLHS(expr(hasType(booleanType())))); |
Malcolm Parsons | d8851b5d | 2018-11-27 16:23:39 +0000 | [diff] [blame] | 301 | auto bitfieldAssignment = binaryOperator( |
| 302 | hasLHS(memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1)))))); |
| 303 | auto bitfieldConstruct = cxxConstructorDecl(hasDescendant(cxxCtorInitializer( |
| 304 | withInitializer(equalsBoundNode("implicitCastFromBool")), |
| 305 | forField(hasBitWidth(1))))); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 306 | Finder->addMatcher( |
| 307 | implicitCastExpr( |
Alexander Kornienko | b92cb07 | 2017-05-04 15:34:31 +0000 | [diff] [blame] | 308 | implicitCastFromBool, |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 309 | // Exclude comparisons of bools, as they are always cast to integers |
| 310 | // in such context: |
| 311 | // bool_expr_a == bool_expr_b |
| 312 | // bool_expr_a != bool_expr_b |
Malcolm Parsons | d8851b5d | 2018-11-27 16:23:39 +0000 | [diff] [blame] | 313 | unless(hasParent(binaryOperator(anyOf( |
| 314 | boolComparison, boolXor, boolOpAssignment, bitfieldAssignment)))), |
| 315 | implicitCastExpr().bind("implicitCastFromBool"), |
| 316 | unless(hasParent(bitfieldConstruct)), |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 317 | // Check also for nested casts, for example: bool -> int -> float. |
| 318 | anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")), |
Alexander Kornienko | e133140 | 2017-05-16 15:44:42 +0000 | [diff] [blame] | 319 | anything()), |
| 320 | unless(isInTemplateInstantiation()), |
Malcolm Parsons | d8851b5d | 2018-11-27 16:23:39 +0000 | [diff] [blame] | 321 | unless(hasAncestor(functionTemplateDecl()))), |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 322 | this); |
| 323 | } |
| 324 | |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 325 | void ImplicitBoolConversionCheck::check( |
| 326 | const MatchFinder::MatchResult &Result) { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 327 | if (const auto *CastToBool = |
| 328 | Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastToBool")) { |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 329 | const auto *Parent = Result.Nodes.getNodeAs<Stmt>("parentStmt"); |
| 330 | return handleCastToBool(CastToBool, Parent, *Result.Context); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | if (const auto *CastFromBool = |
| 334 | Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastFromBool")) { |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 335 | const auto *NextImplicitCast = |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 336 | Result.Nodes.getNodeAs<ImplicitCastExpr>("furtherImplicitCast"); |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 337 | return handleCastFromBool(CastFromBool, NextImplicitCast, *Result.Context); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 341 | void ImplicitBoolConversionCheck::handleCastToBool(const ImplicitCastExpr *Cast, |
| 342 | const Stmt *Parent, |
| 343 | ASTContext &Context) { |
| 344 | if (AllowPointerConditions && |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 345 | (Cast->getCastKind() == CK_PointerToBoolean || |
| 346 | Cast->getCastKind() == CK_MemberPointerToBoolean) && |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 347 | isCastAllowedInCondition(Cast, Context)) { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 348 | return; |
| 349 | } |
| 350 | |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 351 | if (AllowIntegerConditions && Cast->getCastKind() == CK_IntegralToBoolean && |
| 352 | isCastAllowedInCondition(Cast, Context)) { |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 356 | auto Diag = diag(Cast->getBeginLoc(), "implicit conversion %0 -> bool") |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 357 | << Cast->getSubExpr()->getType(); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 358 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 359 | StringRef EquivalentLiteral = |
| 360 | getEquivalentBoolLiteralForExpr(Cast->getSubExpr(), Context); |
| 361 | if (!EquivalentLiteral.empty()) { |
| 362 | Diag << tooling::fixit::createReplacement(*Cast, EquivalentLiteral); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 363 | } else { |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 364 | fixGenericExprCastToBool(Diag, Cast, Parent, Context); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 368 | void ImplicitBoolConversionCheck::handleCastFromBool( |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 369 | const ImplicitCastExpr *Cast, const ImplicitCastExpr *NextImplicitCast, |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 370 | ASTContext &Context) { |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 371 | QualType DestType = |
| 372 | NextImplicitCast ? NextImplicitCast->getType() : Cast->getType(); |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 373 | auto Diag = diag(Cast->getBeginLoc(), "implicit conversion bool -> %0") |
Alexander Kornienko | f1a6552 | 2017-08-08 14:53:52 +0000 | [diff] [blame] | 374 | << DestType; |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 375 | |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 376 | if (const auto *BoolLiteral = |
| 377 | dyn_cast<CXXBoolLiteralExpr>(Cast->getSubExpr())) { |
| 378 | Diag << tooling::fixit::createReplacement( |
| 379 | *Cast, getEquivalentForBoolLiteral(BoolLiteral, DestType, Context)); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 380 | } else { |
Alexander Kornienko | cbe8d16 | 2017-05-04 15:34:23 +0000 | [diff] [blame] | 381 | fixGenericExprCastFromBool(Diag, Cast, Context, DestType.getAsString()); |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 385 | } // namespace readability |
Piotr Dziwinski | 7f1b509 | 2015-10-25 15:31:25 +0000 | [diff] [blame] | 386 | } // namespace tidy |
| 387 | } // namespace clang |