2016-05-11 19:33:16 +08:00
|
|
|
//===--- UseBoolLiteralsCheck.cpp - clang-tidy-----------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2016-05-11 19:33:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
namespace tidy {
|
|
|
|
namespace modernize {
|
|
|
|
|
2017-07-17 22:43:06 +08:00
|
|
|
UseBoolLiteralsCheck::UseBoolLiteralsCheck(StringRef Name,
|
|
|
|
ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
|
|
|
IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
|
|
|
|
|
2016-05-11 19:33:16 +08:00
|
|
|
void UseBoolLiteralsCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Finder->addMatcher(
|
|
|
|
implicitCastExpr(
|
2016-05-31 23:26:56 +08:00
|
|
|
has(ignoringParenImpCasts(integerLiteral().bind("literal"))),
|
2016-05-11 19:33:16 +08:00
|
|
|
hasImplicitDestinationType(qualType(booleanType())),
|
|
|
|
unless(isInTemplateInstantiation()),
|
|
|
|
anyOf(hasParent(explicitCastExpr().bind("cast")), anything())),
|
|
|
|
this);
|
2016-08-09 01:11:56 +08:00
|
|
|
|
|
|
|
Finder->addMatcher(
|
|
|
|
conditionalOperator(
|
|
|
|
hasParent(implicitCastExpr(
|
|
|
|
hasImplicitDestinationType(qualType(booleanType())),
|
|
|
|
unless(isInTemplateInstantiation()))),
|
|
|
|
eachOf(hasTrueExpression(
|
|
|
|
ignoringParenImpCasts(integerLiteral().bind("literal"))),
|
|
|
|
hasFalseExpression(
|
|
|
|
ignoringParenImpCasts(integerLiteral().bind("literal"))))),
|
|
|
|
this);
|
2016-05-11 19:33:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
bool InMacro = Expression->getBeginLoc().isMacroID();
|
2017-07-17 22:43:06 +08:00
|
|
|
|
|
|
|
if (InMacro && IgnoreMacros)
|
|
|
|
return;
|
|
|
|
|
2016-05-11 19:33:16 +08:00
|
|
|
auto Diag =
|
|
|
|
diag(Expression->getExprLoc(),
|
|
|
|
"converting integer literal to bool, use bool literal instead");
|
|
|
|
|
2017-07-17 22:43:06 +08:00
|
|
|
if (!InMacro)
|
2016-05-11 19:33:16 +08:00
|
|
|
Diag << FixItHint::CreateReplacement(
|
|
|
|
Expression->getSourceRange(), LiteralBooleanValue ? "true" : "false");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace modernize
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|