clang-tools  9.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
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.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: Rename.cpp:36
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.