clang-tools 10.0.0

UseBoolLiteralsCheck.cpp
Go to the documentation of this file.
1 //===--- UseBoolLiteralsCheck.cpp - clang-tidy-----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "UseBoolLiteralsCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang {
17 namespace tidy {
18 namespace modernize {
19
20 UseBoolLiteralsCheck::UseBoolLiteralsCheck(StringRef Name,
21 ClangTidyContext *Context)
22 : ClangTidyCheck(Name, Context),
23 IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
24
25 void UseBoolLiteralsCheck::registerMatchers(MatchFinder *Finder) {
26 if (!getLangOpts().CPlusPlus)
27 return;
28
29 Finder->addMatcher(
30 implicitCastExpr(
31 has(ignoringParenImpCasts(integerLiteral().bind("literal"))),
32 hasImplicitDestinationType(qualType(booleanType())),
33 unless(isInTemplateInstantiation()),
34 anyOf(hasParent(explicitCastExpr().bind("cast")), anything())),
35 this);
36
37 Finder->addMatcher(
38 conditionalOperator(
39 hasParent(implicitCastExpr(
40 hasImplicitDestinationType(qualType(booleanType())),
41 unless(isInTemplateInstantiation()))),
42 eachOf(hasTrueExpression(
43 ignoringParenImpCasts(integerLiteral().bind("literal"))),
44 hasFalseExpression(
45 ignoringParenImpCasts(integerLiteral().bind("literal"))))),
46 this);
47 }
48
49 void UseBoolLiteralsCheck::check(const MatchFinder::MatchResult &Result) {
50 const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>("literal");
51 const auto *Cast = Result.Nodes.getNodeAs<Expr>("cast");
52 bool LiteralBooleanValue = Literal->getValue().getBoolValue();
53
54 if (Literal->isInstantiationDependent())
55 return;
56
57 const Expr *Expression = Cast ? Cast : Literal;
58
59 bool InMacro = Expression->getBeginLoc().isMacroID();
60
61 if (InMacro && IgnoreMacros)
62 return;
63
64 auto Diag =
65 diag(Expression->getExprLoc(),
66 "converting integer literal to bool, use bool literal instead");
67
68 if (!InMacro)
69 Diag << FixItHint::CreateReplacement(
70 Expression->getSourceRange(), LiteralBooleanValue ? "true" : "false");
71 }
72
73 } // namespace modernize
74 } // namespace tidy
75 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::modernize::UseBoolLiteralsCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UseBoolLiteralsCheck.cpp:25
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
UseBoolLiteralsCheck.h
clang::tidy::modernize::UseBoolLiteralsCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: UseBoolLiteralsCheck.cpp:49
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21