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