clang-tools  11.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 
26  Options.store(Opts, "IgnoreMacros", IgnoreMacros);
27 }
28 
29 void UseBoolLiteralsCheck::registerMatchers(MatchFinder *Finder) {
30  Finder->addMatcher(
31  traverse(
32  ast_type_traits::TK_AsIs,
33  implicitCastExpr(
34  has(ignoringParenImpCasts(integerLiteral().bind("literal"))),
35  hasImplicitDestinationType(qualType(booleanType())),
36  unless(isInTemplateInstantiation()),
37  anyOf(hasParent(explicitCastExpr().bind("cast")), anything()))),
38  this);
39 
40  Finder->addMatcher(
41  traverse(ast_type_traits::TK_AsIs,
42  conditionalOperator(
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")))))),
50  this);
51 }
52 
53 void UseBoolLiteralsCheck::check(const MatchFinder::MatchResult &Result) {
54  const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>("literal");
55  const auto *Cast = Result.Nodes.getNodeAs<Expr>("cast");
56  bool LiteralBooleanValue = Literal->getValue().getBoolValue();
57 
58  if (Literal->isInstantiationDependent())
59  return;
60 
61  const Expr *Expression = Cast ? Cast : Literal;
62 
63  bool InMacro = Expression->getBeginLoc().isMacroID();
64 
65  if (InMacro && IgnoreMacros)
66  return;
67 
68  auto Diag =
69  diag(Expression->getExprLoc(),
70  "converting integer literal to bool, use bool literal instead");
71 
72  if (!InMacro)
73  Diag << FixItHint::CreateReplacement(
74  Expression->getSourceRange(), LiteralBooleanValue ? "true" : "false");
75 }
76 
77 } // namespace modernize
78 } // namespace tidy
79 } // namespace clang
clang::tidy::modernize::UseBoolLiteralsCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UseBoolLiteralsCheck.cpp:29
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:114
clang::tidy::modernize::UseBoolLiteralsCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: UseBoolLiteralsCheck.cpp:25
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:471
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
UseBoolLiteralsCheck.h
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:55
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:53
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyCheck::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidyCheck.cpp:152
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:111