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