10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
20 UseBoolLiteralsCheck::UseBoolLiteralsCheck(StringRef
Name,
23 IgnoreMacros(Options.getLocalOrGlobal(
"IgnoreMacros", true)) {}
32 ast_type_traits::TK_AsIs,
34 has(ignoringParenImpCasts(integerLiteral().bind(
"literal"))),
35 hasImplicitDestinationType(qualType(booleanType())),
36 unless(isInTemplateInstantiation()),
37 anyOf(hasParent(explicitCastExpr().bind(
"cast")), anything()))),
41 traverse(ast_type_traits::TK_AsIs,
43 hasParent(implicitCastExpr(
44 hasImplicitDestinationType(qualType(booleanType())),
45 unless(isInTemplateInstantiation()))),
46 eachOf(hasTrueExpression(ignoringParenImpCasts(
47 integerLiteral().bind(
"literal"))),
48 hasFalseExpression(ignoringParenImpCasts(
49 integerLiteral().bind(
"literal")))))),
54 const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>(
"literal");
55 const auto *Cast = Result.Nodes.getNodeAs<Expr>(
"cast");
56 bool LiteralBooleanValue = Literal->getValue().getBoolValue();
58 if (Literal->isInstantiationDependent())
61 const Expr *Expression = Cast ? Cast : Literal;
63 bool InMacro = Expression->getBeginLoc().isMacroID();
65 if (InMacro && IgnoreMacros)
69 diag(Expression->getExprLoc(),
70 "converting integer literal to bool, use bool literal instead");
73 Diag << FixItHint::CreateReplacement(
74 Expression->getSourceRange(), LiteralBooleanValue ?
"true" :
"false");