11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Lex/Lexer.h" 21 UseBoolLiteralsCheck::UseBoolLiteralsCheck(StringRef
Name,
24 IgnoreMacros(Options.getLocalOrGlobal(
"IgnoreMacros", true)) {}
32 has(ignoringParenImpCasts(integerLiteral().bind(
"literal"))),
33 hasImplicitDestinationType(qualType(booleanType())),
34 unless(isInTemplateInstantiation()),
35 anyOf(hasParent(explicitCastExpr().bind(
"cast")), anything())),
40 hasParent(implicitCastExpr(
41 hasImplicitDestinationType(qualType(booleanType())),
42 unless(isInTemplateInstantiation()))),
43 eachOf(hasTrueExpression(
44 ignoringParenImpCasts(integerLiteral().bind(
"literal"))),
46 ignoringParenImpCasts(integerLiteral().bind(
"literal"))))),
51 const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>(
"literal");
52 const auto *Cast = Result.Nodes.getNodeAs<Expr>(
"cast");
53 bool LiteralBooleanValue = Literal->getValue().getBoolValue();
55 if (Literal->isInstantiationDependent())
58 const Expr *Expression = Cast ? Cast : Literal;
60 bool InMacro = Expression->getLocStart().isMacroID();
62 if (InMacro && IgnoreMacros)
66 diag(Expression->getExprLoc(),
67 "converting integer literal to bool, use bool literal instead");
70 Diag << FixItHint::CreateReplacement(
71 Expression->getSourceRange(), LiteralBooleanValue ?
"true" :
"false");
LangOptions getLangOpts() const
Returns the language options from the context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Base class for all clang-tidy checks.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.