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)) {}
31 has(ignoringParenImpCasts(integerLiteral().bind(
"literal"))),
32 hasImplicitDestinationType(qualType(booleanType())),
33 unless(isInTemplateInstantiation()),
34 anyOf(hasParent(explicitCastExpr().bind(
"cast")), anything())),
39 hasParent(implicitCastExpr(
40 hasImplicitDestinationType(qualType(booleanType())),
41 unless(isInTemplateInstantiation()))),
42 eachOf(hasTrueExpression(
43 ignoringParenImpCasts(integerLiteral().bind(
"literal"))),
45 ignoringParenImpCasts(integerLiteral().bind(
"literal"))))),
50 const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>(
"literal");
51 const auto *Cast = Result.Nodes.getNodeAs<Expr>(
"cast");
52 bool LiteralBooleanValue = Literal->getValue().getBoolValue();
54 if (Literal->isInstantiationDependent())
57 const Expr *Expression = Cast ? Cast : Literal;
59 bool InMacro = Expression->getBeginLoc().isMacroID();
61 if (InMacro && IgnoreMacros)
65 diag(Expression->getExprLoc(),
66 "converting integer literal to bool, use bool literal instead");
69 Diag << FixItHint::CreateReplacement(
70 Expression->getSourceRange(), LiteralBooleanValue ?
"true" :
"false");
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
static constexpr llvm::StringLiteral Name
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.