2017-08-08 22:53:52 +08:00
|
|
|
//===--- ImplicitBoolConversionCheck.cpp - clang-tidy----------------------===//
|
2015-10-25 23:31:25 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-08-08 22:53:52 +08:00
|
|
|
#include "ImplicitBoolConversionCheck.h"
|
2015-10-25 23:31:25 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
2017-05-04 23:34:23 +08:00
|
|
|
#include "clang/Tooling/FixIt.h"
|
2017-04-29 20:06:45 +08:00
|
|
|
#include <queue>
|
2015-10-25 23:31:25 +08:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2016-05-03 02:00:29 +08:00
|
|
|
namespace readability {
|
2015-10-25 23:31:25 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
AST_MATCHER(Stmt, isMacroExpansion) {
|
|
|
|
SourceManager &SM = Finder->getASTContext().getSourceManager();
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation Loc = Node.getBeginLoc();
|
2015-10-25 23:31:25 +08:00
|
|
|
return SM.isMacroBodyExpansion(Loc) || SM.isMacroArgExpansion(Loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNULLMacroExpansion(const Stmt *Statement, ASTContext &Context) {
|
|
|
|
SourceManager &SM = Context.getSourceManager();
|
|
|
|
const LangOptions &LO = Context.getLangOpts();
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation Loc = Statement->getBeginLoc();
|
2015-10-25 23:31:25 +08:00
|
|
|
return SM.isMacroBodyExpansion(Loc) &&
|
2017-05-04 23:34:23 +08:00
|
|
|
Lexer::getImmediateMacroName(Loc, SM, LO) == "NULL";
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AST_MATCHER(Stmt, isNULLMacroExpansion) {
|
|
|
|
return isNULLMacroExpansion(&Node, Finder->getASTContext());
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
StringRef getZeroLiteralToCompareWithForType(CastKind CastExprKind,
|
|
|
|
QualType Type,
|
|
|
|
ASTContext &Context) {
|
|
|
|
switch (CastExprKind) {
|
2015-10-25 23:31:25 +08:00
|
|
|
case CK_IntegralToBoolean:
|
2017-05-04 23:34:23 +08:00
|
|
|
return Type->isUnsignedIntegerType() ? "0u" : "0";
|
2015-10-25 23:31:25 +08:00
|
|
|
|
|
|
|
case CK_FloatingToBoolean:
|
2017-05-04 23:34:23 +08:00
|
|
|
return Context.hasSameType(Type, Context.FloatTy) ? "0.0f" : "0.0";
|
2015-10-25 23:31:25 +08:00
|
|
|
|
|
|
|
case CK_PointerToBoolean:
|
|
|
|
case CK_MemberPointerToBoolean: // Fall-through on purpose.
|
|
|
|
return Context.getLangOpts().CPlusPlus11 ? "nullptr" : "0";
|
|
|
|
|
|
|
|
default:
|
2015-10-26 06:03:00 +08:00
|
|
|
llvm_unreachable("Unexpected cast kind");
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isUnaryLogicalNotOperator(const Stmt *Statement) {
|
2017-05-04 23:34:23 +08:00
|
|
|
const auto *UnaryOperatorExpr = dyn_cast<UnaryOperator>(Statement);
|
|
|
|
return UnaryOperatorExpr && UnaryOperatorExpr->getOpcode() == UO_LNot;
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool areParensNeededForOverloadedOperator(OverloadedOperatorKind OperatorKind) {
|
|
|
|
switch (OperatorKind) {
|
|
|
|
case OO_New:
|
|
|
|
case OO_Delete: // Fall-through on purpose.
|
|
|
|
case OO_Array_New:
|
|
|
|
case OO_Array_Delete:
|
|
|
|
case OO_ArrowStar:
|
|
|
|
case OO_Arrow:
|
|
|
|
case OO_Call:
|
|
|
|
case OO_Subscript:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool areParensNeededForStatement(const Stmt *Statement) {
|
2017-05-04 23:34:23 +08:00
|
|
|
if (const auto *OperatorCall = dyn_cast<CXXOperatorCallExpr>(Statement)) {
|
|
|
|
return areParensNeededForOverloadedOperator(OperatorCall->getOperator());
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
return isa<BinaryOperator>(Statement) || isa<UnaryOperator>(Statement);
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
void fixGenericExprCastToBool(DiagnosticBuilder &Diag,
|
|
|
|
const ImplicitCastExpr *Cast, const Stmt *Parent,
|
|
|
|
ASTContext &Context) {
|
2015-10-25 23:31:25 +08:00
|
|
|
// In case of expressions like (! integer), we should remove the redundant not
|
|
|
|
// operator and use inverted comparison (integer == 0).
|
|
|
|
bool InvertComparison =
|
2017-05-04 23:34:23 +08:00
|
|
|
Parent != nullptr && isUnaryLogicalNotOperator(Parent);
|
2015-10-25 23:31:25 +08:00
|
|
|
if (InvertComparison) {
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation ParentStartLoc = Parent->getBeginLoc();
|
2015-10-25 23:31:25 +08:00
|
|
|
SourceLocation ParentEndLoc =
|
2018-08-10 06:42:26 +08:00
|
|
|
cast<UnaryOperator>(Parent)->getSubExpr()->getBeginLoc();
|
2017-05-04 23:34:23 +08:00
|
|
|
Diag << FixItHint::CreateRemoval(
|
|
|
|
CharSourceRange::getCharRange(ParentStartLoc, ParentEndLoc));
|
2015-10-25 23:31:25 +08:00
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
Parent = Context.getParents(*Parent)[0].get<Stmt>();
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
const Expr *SubExpr = Cast->getSubExpr();
|
2015-10-25 23:31:25 +08:00
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
bool NeedInnerParens = areParensNeededForStatement(SubExpr);
|
|
|
|
bool NeedOuterParens =
|
|
|
|
Parent != nullptr && areParensNeededForStatement(Parent);
|
2015-10-25 23:31:25 +08:00
|
|
|
|
|
|
|
std::string StartLocInsertion;
|
|
|
|
|
|
|
|
if (NeedOuterParens) {
|
|
|
|
StartLocInsertion += "(";
|
|
|
|
}
|
|
|
|
if (NeedInnerParens) {
|
|
|
|
StartLocInsertion += "(";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!StartLocInsertion.empty()) {
|
2018-08-10 06:42:26 +08:00
|
|
|
Diag << FixItHint::CreateInsertion(Cast->getBeginLoc(), StartLocInsertion);
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string EndLocInsertion;
|
|
|
|
|
|
|
|
if (NeedInnerParens) {
|
|
|
|
EndLocInsertion += ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (InvertComparison) {
|
|
|
|
EndLocInsertion += " == ";
|
|
|
|
} else {
|
|
|
|
EndLocInsertion += " != ";
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
EndLocInsertion += getZeroLiteralToCompareWithForType(
|
|
|
|
Cast->getCastKind(), SubExpr->getType(), Context);
|
2015-10-25 23:31:25 +08:00
|
|
|
|
|
|
|
if (NeedOuterParens) {
|
|
|
|
EndLocInsertion += ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
|
2018-08-10 06:43:02 +08:00
|
|
|
Cast->getEndLoc(), 0, Context.getSourceManager(), Context.getLangOpts());
|
2017-05-04 23:34:23 +08:00
|
|
|
Diag << FixItHint::CreateInsertion(EndLoc, EndLocInsertion);
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
StringRef getEquivalentBoolLiteralForExpr(const Expr *Expression,
|
|
|
|
ASTContext &Context) {
|
2015-10-25 23:31:25 +08:00
|
|
|
if (isNULLMacroExpansion(Expression, Context)) {
|
|
|
|
return "false";
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
if (const auto *IntLit = dyn_cast<IntegerLiteral>(Expression)) {
|
2015-10-25 23:31:25 +08:00
|
|
|
return (IntLit->getValue() == 0) ? "false" : "true";
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
if (const auto *FloatLit = dyn_cast<FloatingLiteral>(Expression)) {
|
2015-10-25 23:31:25 +08:00
|
|
|
llvm::APFloat FloatLitAbsValue = FloatLit->getValue();
|
|
|
|
FloatLitAbsValue.clearSign();
|
|
|
|
return (FloatLitAbsValue.bitcastToAPInt() == 0) ? "false" : "true";
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
if (const auto *CharLit = dyn_cast<CharacterLiteral>(Expression)) {
|
2015-10-25 23:31:25 +08:00
|
|
|
return (CharLit->getValue() == 0) ? "false" : "true";
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
if (isa<StringLiteral>(Expression->IgnoreCasts())) {
|
2015-10-25 23:31:25 +08:00
|
|
|
return "true";
|
|
|
|
}
|
|
|
|
|
|
|
|
return StringRef();
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
void fixGenericExprCastFromBool(DiagnosticBuilder &Diag,
|
|
|
|
const ImplicitCastExpr *Cast,
|
|
|
|
ASTContext &Context, StringRef OtherType) {
|
|
|
|
const Expr *SubExpr = Cast->getSubExpr();
|
|
|
|
bool NeedParens = !isa<ParenExpr>(SubExpr);
|
2015-10-25 23:31:25 +08:00
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
Diag << FixItHint::CreateInsertion(
|
2018-08-10 06:42:26 +08:00
|
|
|
Cast->getBeginLoc(),
|
2017-05-04 23:34:23 +08:00
|
|
|
(Twine("static_cast<") + OtherType + ">" + (NeedParens ? "(" : ""))
|
|
|
|
.str());
|
2015-10-25 23:31:25 +08:00
|
|
|
|
|
|
|
if (NeedParens) {
|
|
|
|
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
|
2018-08-10 06:43:02 +08:00
|
|
|
Cast->getEndLoc(), 0, Context.getSourceManager(),
|
2015-10-25 23:31:25 +08:00
|
|
|
Context.getLangOpts());
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
Diag << FixItHint::CreateInsertion(EndLoc, ")");
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
StringRef getEquivalentForBoolLiteral(const CXXBoolLiteralExpr *BoolLiteral,
|
|
|
|
QualType DestType, ASTContext &Context) {
|
2015-10-25 23:31:25 +08:00
|
|
|
// Prior to C++11, false literal could be implicitly converted to pointer.
|
|
|
|
if (!Context.getLangOpts().CPlusPlus11 &&
|
2017-05-04 23:34:23 +08:00
|
|
|
(DestType->isPointerType() || DestType->isMemberPointerType()) &&
|
|
|
|
BoolLiteral->getValue() == false) {
|
2015-10-25 23:31:25 +08:00
|
|
|
return "0";
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
if (DestType->isFloatingType()) {
|
|
|
|
if (Context.hasSameType(DestType, Context.FloatTy)) {
|
|
|
|
return BoolLiteral->getValue() ? "1.0f" : "0.0f";
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
2017-05-04 23:34:23 +08:00
|
|
|
return BoolLiteral->getValue() ? "1.0" : "0.0";
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
if (DestType->isUnsignedIntegerType()) {
|
|
|
|
return BoolLiteral->getValue() ? "1u" : "0u";
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
2017-05-04 23:34:23 +08:00
|
|
|
return BoolLiteral->getValue() ? "1" : "0";
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
|
2017-08-08 22:53:52 +08:00
|
|
|
bool isCastAllowedInCondition(const ImplicitCastExpr *Cast,
|
2015-10-25 23:31:25 +08:00
|
|
|
ASTContext &Context) {
|
2017-04-29 20:06:45 +08:00
|
|
|
std::queue<const Stmt *> Q;
|
2017-05-04 23:34:23 +08:00
|
|
|
Q.push(Cast);
|
2017-04-29 20:06:45 +08:00
|
|
|
while (!Q.empty()) {
|
|
|
|
for (const auto &N : Context.getParents(*Q.front())) {
|
|
|
|
const Stmt *S = N.get<Stmt>();
|
|
|
|
if (!S)
|
|
|
|
return false;
|
2017-05-08 23:22:09 +08:00
|
|
|
if (isa<IfStmt>(S) || isa<ConditionalOperator>(S) || isa<ForStmt>(S) ||
|
|
|
|
isa<WhileStmt>(S) || isa<BinaryConditionalOperator>(S))
|
2017-04-29 20:06:45 +08:00
|
|
|
return true;
|
|
|
|
if (isa<ParenExpr>(S) || isa<ImplicitCastExpr>(S) ||
|
2017-05-04 23:34:23 +08:00
|
|
|
isUnaryLogicalNotOperator(S) ||
|
2017-04-29 20:06:45 +08:00
|
|
|
(isa<BinaryOperator>(S) && cast<BinaryOperator>(S)->isLogicalOp())) {
|
|
|
|
Q.push(S);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Q.pop();
|
|
|
|
}
|
|
|
|
return false;
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2017-08-08 22:53:52 +08:00
|
|
|
ImplicitBoolConversionCheck::ImplicitBoolConversionCheck(
|
|
|
|
StringRef Name, ClangTidyContext *Context)
|
2016-08-16 19:15:05 +08:00
|
|
|
: ClangTidyCheck(Name, Context),
|
2017-08-08 22:53:52 +08:00
|
|
|
AllowIntegerConditions(Options.get("AllowIntegerConditions", false)),
|
|
|
|
AllowPointerConditions(Options.get("AllowPointerConditions", false)) {}
|
|
|
|
|
|
|
|
void ImplicitBoolConversionCheck::storeOptions(
|
|
|
|
ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "AllowIntegerConditions", AllowIntegerConditions);
|
|
|
|
Options.store(Opts, "AllowPointerConditions", AllowPointerConditions);
|
2016-08-16 19:15:05 +08:00
|
|
|
}
|
|
|
|
|
2017-08-08 22:53:52 +08:00
|
|
|
void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
|
2015-10-25 23:31:25 +08:00
|
|
|
// This check doesn't make much sense if we run it on language without
|
|
|
|
// built-in bool support.
|
|
|
|
if (!getLangOpts().Bool) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-16 23:44:42 +08:00
|
|
|
auto exceptionCases =
|
2017-05-17 00:40:46 +08:00
|
|
|
expr(anyOf(allOf(isMacroExpansion(), unless(isNULLMacroExpansion())),
|
2018-10-02 19:38:41 +08:00
|
|
|
has(ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1)))))),
|
2017-05-17 00:40:46 +08:00
|
|
|
hasParent(explicitCastExpr())));
|
2017-05-05 00:06:08 +08:00
|
|
|
auto implicitCastFromBool = implicitCastExpr(
|
|
|
|
anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating),
|
|
|
|
// Prior to C++11 cast from bool literal to pointer was allowed.
|
|
|
|
allOf(anyOf(hasCastKind(CK_NullToPointer),
|
|
|
|
hasCastKind(CK_NullToMemberPointer)),
|
|
|
|
hasSourceExpression(cxxBoolLiteral()))),
|
2017-05-17 00:40:46 +08:00
|
|
|
hasSourceExpression(expr(hasType(booleanType()))),
|
|
|
|
unless(exceptionCases));
|
2017-05-05 00:06:08 +08:00
|
|
|
auto boolXor =
|
|
|
|
binaryOperator(hasOperatorName("^"), hasLHS(implicitCastFromBool),
|
|
|
|
hasRHS(implicitCastFromBool));
|
2015-10-25 23:31:25 +08:00
|
|
|
Finder->addMatcher(
|
|
|
|
implicitCastExpr(
|
2017-05-17 00:40:46 +08:00
|
|
|
anyOf(hasCastKind(CK_IntegralToBoolean),
|
|
|
|
hasCastKind(CK_FloatingToBoolean),
|
|
|
|
hasCastKind(CK_PointerToBoolean),
|
|
|
|
hasCastKind(CK_MemberPointerToBoolean)),
|
2015-10-25 23:31:25 +08:00
|
|
|
// Exclude case of using if or while statements with variable
|
|
|
|
// declaration, e.g.:
|
|
|
|
// if (int var = functionCall()) {}
|
|
|
|
unless(
|
|
|
|
hasParent(stmt(anyOf(ifStmt(), whileStmt()), has(declStmt())))),
|
2017-05-17 00:40:46 +08:00
|
|
|
// Exclude cases common to implicit cast to and from bool.
|
|
|
|
unless(exceptionCases), unless(has(boolXor)),
|
2015-10-25 23:31:25 +08:00
|
|
|
// Retrive also parent statement, to check if we need additional
|
|
|
|
// parens in replacement.
|
2017-05-16 23:44:42 +08:00
|
|
|
anyOf(hasParent(stmt().bind("parentStmt")), anything()),
|
|
|
|
unless(isInTemplateInstantiation()),
|
|
|
|
unless(hasAncestor(functionTemplateDecl())))
|
2015-10-25 23:31:25 +08:00
|
|
|
.bind("implicitCastToBool"),
|
|
|
|
this);
|
|
|
|
|
2017-05-04 23:34:31 +08:00
|
|
|
auto boolComparison = binaryOperator(
|
|
|
|
anyOf(hasOperatorName("=="), hasOperatorName("!=")),
|
|
|
|
hasLHS(implicitCastFromBool), hasRHS(implicitCastFromBool));
|
2017-05-05 00:06:08 +08:00
|
|
|
auto boolOpAssignment =
|
|
|
|
binaryOperator(anyOf(hasOperatorName("|="), hasOperatorName("&=")),
|
|
|
|
hasLHS(expr(hasType(booleanType()))));
|
2015-10-25 23:31:25 +08:00
|
|
|
Finder->addMatcher(
|
|
|
|
implicitCastExpr(
|
2017-05-04 23:34:31 +08:00
|
|
|
implicitCastFromBool,
|
2015-10-25 23:31:25 +08:00
|
|
|
// Exclude comparisons of bools, as they are always cast to integers
|
|
|
|
// in such context:
|
|
|
|
// bool_expr_a == bool_expr_b
|
|
|
|
// bool_expr_a != bool_expr_b
|
2017-05-05 00:06:08 +08:00
|
|
|
unless(hasParent(binaryOperator(
|
|
|
|
anyOf(boolComparison, boolXor, boolOpAssignment)))),
|
2015-10-25 23:31:25 +08:00
|
|
|
// Check also for nested casts, for example: bool -> int -> float.
|
|
|
|
anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")),
|
2017-05-16 23:44:42 +08:00
|
|
|
anything()),
|
|
|
|
unless(isInTemplateInstantiation()),
|
|
|
|
unless(hasAncestor(functionTemplateDecl())))
|
2015-10-25 23:31:25 +08:00
|
|
|
.bind("implicitCastFromBool"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
2017-08-08 22:53:52 +08:00
|
|
|
void ImplicitBoolConversionCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
2015-10-25 23:31:25 +08:00
|
|
|
if (const auto *CastToBool =
|
|
|
|
Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastToBool")) {
|
2017-05-04 23:34:23 +08:00
|
|
|
const auto *Parent = Result.Nodes.getNodeAs<Stmt>("parentStmt");
|
|
|
|
return handleCastToBool(CastToBool, Parent, *Result.Context);
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto *CastFromBool =
|
|
|
|
Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastFromBool")) {
|
2017-05-04 23:34:23 +08:00
|
|
|
const auto *NextImplicitCast =
|
2015-10-25 23:31:25 +08:00
|
|
|
Result.Nodes.getNodeAs<ImplicitCastExpr>("furtherImplicitCast");
|
2017-05-04 23:34:23 +08:00
|
|
|
return handleCastFromBool(CastFromBool, NextImplicitCast, *Result.Context);
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 22:53:52 +08:00
|
|
|
void ImplicitBoolConversionCheck::handleCastToBool(const ImplicitCastExpr *Cast,
|
|
|
|
const Stmt *Parent,
|
|
|
|
ASTContext &Context) {
|
|
|
|
if (AllowPointerConditions &&
|
2017-05-04 23:34:23 +08:00
|
|
|
(Cast->getCastKind() == CK_PointerToBoolean ||
|
|
|
|
Cast->getCastKind() == CK_MemberPointerToBoolean) &&
|
2017-08-08 22:53:52 +08:00
|
|
|
isCastAllowedInCondition(Cast, Context)) {
|
2015-10-25 23:31:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-08 22:53:52 +08:00
|
|
|
if (AllowIntegerConditions && Cast->getCastKind() == CK_IntegralToBoolean &&
|
|
|
|
isCastAllowedInCondition(Cast, Context)) {
|
2015-10-25 23:31:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
auto Diag = diag(Cast->getBeginLoc(), "implicit conversion %0 -> bool")
|
2017-05-04 23:34:23 +08:00
|
|
|
<< Cast->getSubExpr()->getType();
|
2015-10-25 23:31:25 +08:00
|
|
|
|
2017-05-04 23:34:23 +08:00
|
|
|
StringRef EquivalentLiteral =
|
|
|
|
getEquivalentBoolLiteralForExpr(Cast->getSubExpr(), Context);
|
|
|
|
if (!EquivalentLiteral.empty()) {
|
|
|
|
Diag << tooling::fixit::createReplacement(*Cast, EquivalentLiteral);
|
2015-10-25 23:31:25 +08:00
|
|
|
} else {
|
2017-05-04 23:34:23 +08:00
|
|
|
fixGenericExprCastToBool(Diag, Cast, Parent, Context);
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 22:53:52 +08:00
|
|
|
void ImplicitBoolConversionCheck::handleCastFromBool(
|
2017-05-04 23:34:23 +08:00
|
|
|
const ImplicitCastExpr *Cast, const ImplicitCastExpr *NextImplicitCast,
|
2015-10-25 23:31:25 +08:00
|
|
|
ASTContext &Context) {
|
2017-05-04 23:34:23 +08:00
|
|
|
QualType DestType =
|
|
|
|
NextImplicitCast ? NextImplicitCast->getType() : Cast->getType();
|
2018-08-10 06:42:26 +08:00
|
|
|
auto Diag = diag(Cast->getBeginLoc(), "implicit conversion bool -> %0")
|
2017-08-08 22:53:52 +08:00
|
|
|
<< DestType;
|
2017-05-04 23:34:23 +08:00
|
|
|
|
|
|
|
if (const auto *BoolLiteral =
|
|
|
|
dyn_cast<CXXBoolLiteralExpr>(Cast->getSubExpr())) {
|
|
|
|
Diag << tooling::fixit::createReplacement(
|
|
|
|
*Cast, getEquivalentForBoolLiteral(BoolLiteral, DestType, Context));
|
2015-10-25 23:31:25 +08:00
|
|
|
} else {
|
2017-05-04 23:34:23 +08:00
|
|
|
fixGenericExprCastFromBool(Diag, Cast, Context, DestType.getAsString());
|
2015-10-25 23:31:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:00:29 +08:00
|
|
|
} // namespace readability
|
2015-10-25 23:31:25 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|