• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
UseBoolLiteralsCheck(StringRef Name,ClangTidyContext * Context)20 UseBoolLiteralsCheck::UseBoolLiteralsCheck(StringRef Name,
21                                            ClangTidyContext *Context)
22     : ClangTidyCheck(Name, Context),
23       IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
24 
storeOptions(ClangTidyOptions::OptionMap & Opts)25 void UseBoolLiteralsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
26   Options.store(Opts, "IgnoreMacros", IgnoreMacros);
27 }
28 
registerMatchers(MatchFinder * Finder)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 
check(const MatchFinder::MatchResult & Result)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
80