clang-tools 6.0.0

UseBoolLiteralsCheck.cpp
Go to the documentation of this file.
1 //===--- UseBoolLiteralsCheck.cpp - clang-tidy-----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "UseBoolLiteralsCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace tidy {
19 namespace modernize {
20
21 UseBoolLiteralsCheck::UseBoolLiteralsCheck(StringRef Name,
22 ClangTidyContext *Context)
23 : ClangTidyCheck(Name, Context),
24 IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
25
26 void UseBoolLiteralsCheck::registerMatchers(MatchFinder *Finder) {
27 if (!getLangOpts().CPlusPlus)
28 return;
29
30 Finder->addMatcher(
31 implicitCastExpr(
32 has(ignoringParenImpCasts(integerLiteral().bind("literal"))),
33 hasImplicitDestinationType(qualType(booleanType())),
34 unless(isInTemplateInstantiation()),
35 anyOf(hasParent(explicitCastExpr().bind("cast")), anything())),
36 this);
37
38 Finder->addMatcher(
39 conditionalOperator(
40 hasParent(implicitCastExpr(
41 hasImplicitDestinationType(qualType(booleanType())),
42 unless(isInTemplateInstantiation()))),
43 eachOf(hasTrueExpression(
44 ignoringParenImpCasts(integerLiteral().bind("literal"))),
45 hasFalseExpression(
46 ignoringParenImpCasts(integerLiteral().bind("literal"))))),
47 this);
48 }
49
50 void UseBoolLiteralsCheck::check(const MatchFinder::MatchResult &Result) {
51 const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>("literal");
52 const auto *Cast = Result.Nodes.getNodeAs<Expr>("cast");
53 bool LiteralBooleanValue = Literal->getValue().getBoolValue();
54
55 if (Literal->isInstantiationDependent())
56 return;
57
58 const Expr *Expression = Cast ? Cast : Literal;
59
60 bool InMacro = Expression->getLocStart().isMacroID();
61
62 if (InMacro && IgnoreMacros)
63 return;
64
65 auto Diag =
66 diag(Expression->getExprLoc(),
67 "converting integer literal to bool, use bool literal instead");
68
69 if (!InMacro)
70 Diag << FixItHint::CreateReplacement(
71 Expression->getSourceRange(), LiteralBooleanValue ? "true" : "false");
72 }
73
74 } // namespace modernize
75 } // namespace tidy
76 } // namespace clang
clang::ast_matchers
Definition: ClangTidyDiagnosticConsumer.h:27
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
clang::tidy::ClangTidyCheck::getLangOpts
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:187
clang::tidy::modernize::UseBoolLiteralsCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UseBoolLiteralsCheck.cpp:26
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
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:50
clang
Definition: AndroidTidyModule.cpp:28
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:104
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: ClangTidy.cpp:416