75 const ImplicitCastExpr *Cast,
76 const Stmt *Parent, ASTContext &Context,
77 bool UseUpperCaseLiteralSuffix) {
80 const bool InvertComparison =
82 if (InvertComparison) {
83 const SourceLocation ParentStartLoc = Parent->getBeginLoc();
84 const SourceLocation ParentEndLoc =
85 cast<UnaryOperator>(Parent)->getSubExpr()->getBeginLoc();
86 Diag << FixItHint::CreateRemoval(
87 CharSourceRange::getCharRange(ParentStartLoc, ParentEndLoc));
89 Parent = Context.getParents(*Parent)[0].get<Stmt>();
92 const Expr *SubExpr = Cast->getSubExpr();
94 const bool NeedInnerParens =
96 const bool NeedOuterParens =
99 std::string StartLocInsertion;
102 StartLocInsertion +=
'(';
104 StartLocInsertion +=
'(';
106 if (!StartLocInsertion.empty())
107 Diag << FixItHint::CreateInsertion(Cast->getBeginLoc(), StartLocInsertion);
109 std::string EndLocInsertion;
112 EndLocInsertion +=
')';
114 if (InvertComparison)
115 EndLocInsertion +=
" == ";
117 EndLocInsertion +=
" != ";
120 Cast->getCastKind(), SubExpr->getType(), Context);
122 if (UseUpperCaseLiteralSuffix)
123 EndLocInsertion += ZeroLiteral.upper();
125 EndLocInsertion += ZeroLiteral;
128 EndLocInsertion +=
')';
130 const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
131 Cast->getEndLoc(), 0, Context.getSourceManager(), Context.getLangOpts());
132 Diag << FixItHint::CreateInsertion(EndLoc, EndLocInsertion);
136 ASTContext &Context) {
137 if (isNULLMacroExpansion(Expression, Context))
140 if (
const auto *IntLit = dyn_cast<IntegerLiteral>(Expression->IgnoreParens()))
141 return (IntLit->getValue() == 0) ?
"false" :
"true";
143 if (
const auto *FloatLit = dyn_cast<FloatingLiteral>(Expression)) {
144 llvm::APFloat FloatLitAbsValue = FloatLit->getValue();
145 FloatLitAbsValue.clearSign();
146 return (FloatLitAbsValue.bitcastToAPInt() == 0) ?
"false" :
"true";
149 if (
const auto *CharLit = dyn_cast<CharacterLiteral>(Expression))
150 return (CharLit->getValue() == 0) ?
"false" :
"true";
152 if (isa<StringLiteral>(Expression->IgnoreCasts()))
171 const ImplicitCastExpr *Cast,
173 StringRef OtherType) {
174 if (!Context.getLangOpts().CPlusPlus) {
175 Diag << FixItHint::CreateInsertion(Cast->getBeginLoc(),
176 (Twine(
"(") + OtherType +
")").str());
180 const Expr *SubExpr = Cast->getSubExpr();
181 const bool NeedParens = !isa<ParenExpr>(SubExpr->IgnoreImplicit());
184 Diag << FixItHint::CreateInsertion(
185 Cast->getBeginLoc(), (Twine() + (NeedSpace ?
" " :
"") +
"static_cast<" +
186 OtherType +
">" + (NeedParens ?
"(" :
""))
190 const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
191 Cast->getEndLoc(), 0, Context.getSourceManager(),
192 Context.getLangOpts());
194 Diag << FixItHint::CreateInsertion(EndLoc,
")");
200 QualType DestType, ASTContext &Context) {
202 if (!Context.getLangOpts().CPlusPlus11 &&
203 (DestType->isPointerType() || DestType->isMemberPointerType()) &&
204 BoolLiteral->getValue() ==
false) {
208 if (DestType->isFloatingType()) {
209 if (ASTContext::hasSameType(DestType, Context.FloatTy))
210 return BoolLiteral->getValue() ?
"1.0f" :
"0.0f";
211 return BoolLiteral->getValue() ?
"1.0" :
"0.0";
214 if (DestType->isUnsignedIntegerType())
215 return BoolLiteral->getValue() ?
"1u" :
"0u";
216 return BoolLiteral->getValue() ?
"1" :
"0";
277 auto ExceptionCases =
278 expr(anyOf(allOf(isMacroExpansion(), unless(isNULLMacroExpansion())),
279 has(ignoringImplicit(
280 memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1)))))),
281 hasParent(explicitCastExpr()),
282 expr(hasType(qualType().bind(
"type")),
283 hasParent(initListExpr(hasParent(explicitCastExpr(
284 hasType(qualType(equalsBoundNode(
"type"))))))))));
285 auto ImplicitCastFromBool = implicitCastExpr(
286 anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating),
288 allOf(anyOf(hasCastKind(CK_NullToPointer),
289 hasCastKind(CK_NullToMemberPointer)),
290 hasSourceExpression(cxxBoolLiteral()))),
291 hasSourceExpression(expr(hasType(booleanType()))));
293 binaryOperator(hasOperatorName(
"^"), hasLHS(ImplicitCastFromBool),
294 hasRHS(ImplicitCastFromBool));
295 auto ComparisonInCall = allOf(
296 hasParent(callExpr()),
297 hasSourceExpression(binaryOperator(hasAnyOperatorName(
"==",
"!="))));
299 auto IsInCompilerGeneratedFunction = hasAncestor(namedDecl(anyOf(
300 isImplicit(), functionDecl(isDefaulted()), functionTemplateDecl())));
305 anyOf(hasCastKind(CK_IntegralToBoolean),
306 hasCastKind(CK_FloatingToBoolean),
307 hasCastKind(CK_PointerToBoolean),
308 hasCastKind(CK_MemberPointerToBoolean)),
311 hasSourceExpression(ignoringParens(
312 binaryOperator(hasAnyOperatorName(
313 ">",
">=",
"==",
"!=",
"<",
"<=")))))),
318 stmt(anyOf(ifStmt(), whileStmt()), has(declStmt())))),
320 unless(ExceptionCases), unless(has(BoolXor)),
322 unless(ComparisonInCall),
325 optionally(hasParent(stmt().bind(
"parentStmt"))),
326 unless(isInTemplateInstantiation()),
327 unless(IsInCompilerGeneratedFunction))
328 .bind(
"implicitCastToBool")),
331 auto BoolComparison = binaryOperator(hasAnyOperatorName(
"==",
"!="),
332 hasLHS(ImplicitCastFromBool),
333 hasRHS(ImplicitCastFromBool));
334 auto BoolOpAssignment = binaryOperator(hasAnyOperatorName(
"|=",
"&="),
335 hasLHS(expr(hasType(booleanType()))));
336 auto BitfieldAssignment = binaryOperator(
337 hasLHS(memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1))))));
338 auto BitfieldConstruct = cxxConstructorDecl(hasDescendant(cxxCtorInitializer(
339 withInitializer(equalsBoundNode(
"implicitCastFromBool")),
340 forField(hasBitWidth(1)))));
341 auto BoolTernaryCondition = conditionalOperator(
342 hasCondition(equalsBoundNode(
"implicitCastFromBool")));
347 ImplicitCastFromBool,
348 implicitCastExpr().bind(
"implicitCastFromBool"),
349 unless(ExceptionCases),
355 binaryOperator(anyOf(BoolComparison, BoolXor,
356 BoolOpAssignment, BitfieldAssignment)))),
358 unless(allOf(isC(), hasParent(binaryOperator(
359 hasAnyOperatorName(
"&&",
"||"))))),
361 unless(allOf(isC(), hasCastKind(CK_IntegralCast),
362 hasParent(BoolTernaryCondition))),
363 unless(hasParent(BitfieldConstruct)),
366 hasParent(implicitCastExpr().bind(
"furtherImplicitCast"))),
367 unless(isInTemplateInstantiation()),
368 unless(IsInCompilerGeneratedFunction))),
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.