1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
//===--- UseBoolLiteralsCheck.cpp - clang-tidy-----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "UseBoolLiteralsCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang::tidy::modernize {
UseBoolLiteralsCheck::UseBoolLiteralsCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
void UseBoolLiteralsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
}
void UseBoolLiteralsCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
traverse(
TK_AsIs,
implicitCastExpr(
has(ignoringParenImpCasts(integerLiteral().bind("literal"))),
hasImplicitDestinationType(qualType(booleanType())),
unless(isInTemplateInstantiation()),
anyOf(hasParent(explicitCastExpr().bind("cast")), anything()))),
this);
Finder->addMatcher(
traverse(TK_AsIs,
conditionalOperator(
hasParent(implicitCastExpr(
hasImplicitDestinationType(qualType(booleanType())),
unless(isInTemplateInstantiation()))),
eachOf(hasTrueExpression(ignoringParenImpCasts(
integerLiteral().bind("literal"))),
hasFalseExpression(ignoringParenImpCasts(
integerLiteral().bind("literal")))))),
this);
}
void UseBoolLiteralsCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>("literal");
const auto *Cast = Result.Nodes.getNodeAs<Expr>("cast");
bool LiteralBooleanValue = Literal->getValue().getBoolValue();
if (Literal->isInstantiationDependent())
return;
const Expr *Expression = Cast ? Cast : Literal;
bool InMacro = Expression->getBeginLoc().isMacroID();
if (InMacro && IgnoreMacros)
return;
auto Diag =
diag(Expression->getExprLoc(),
"converting integer literal to bool, use bool literal instead");
if (!InMacro)
Diag << FixItHint::CreateReplacement(
Expression->getSourceRange(), LiteralBooleanValue ? "true" : "false");
}
} // namespace clang::tidy::modernize
|