2016-02-09 22:08:49 +08:00
|
|
|
//===--- MisplacedWideningCastCheck.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-02-09 22:08:49 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MisplacedWideningCastCheck.h"
|
2016-11-08 15:50:19 +08:00
|
|
|
#include "../utils/Matchers.h"
|
2016-02-09 22:08:49 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2018-02-28 22:47:20 +08:00
|
|
|
namespace bugprone {
|
2016-02-09 22:08:49 +08:00
|
|
|
|
2016-04-06 20:04:51 +08:00
|
|
|
MisplacedWideningCastCheck::MisplacedWideningCastCheck(
|
|
|
|
StringRef Name, ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
2017-04-19 22:55:58 +08:00
|
|
|
CheckImplicitCasts(Options.get("CheckImplicitCasts", false)) {}
|
2016-04-06 20:04:51 +08:00
|
|
|
|
|
|
|
void MisplacedWideningCastCheck::storeOptions(
|
|
|
|
ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "CheckImplicitCasts", CheckImplicitCasts);
|
|
|
|
}
|
|
|
|
|
2016-02-09 22:08:49 +08:00
|
|
|
void MisplacedWideningCastCheck::registerMatchers(MatchFinder *Finder) {
|
2016-04-07 22:52:52 +08:00
|
|
|
const auto Calc =
|
|
|
|
expr(anyOf(binaryOperator(
|
|
|
|
anyOf(hasOperatorName("+"), hasOperatorName("-"),
|
|
|
|
hasOperatorName("*"), hasOperatorName("<<"))),
|
|
|
|
unaryOperator(hasOperatorName("~"))),
|
|
|
|
hasType(isInteger()))
|
|
|
|
.bind("Calc");
|
|
|
|
|
2016-05-31 23:26:56 +08:00
|
|
|
const auto ExplicitCast = explicitCastExpr(hasDestinationType(isInteger()),
|
|
|
|
has(ignoringParenImpCasts(Calc)));
|
2016-04-07 22:52:52 +08:00
|
|
|
const auto ImplicitCast =
|
2016-05-31 23:26:56 +08:00
|
|
|
implicitCastExpr(hasImplicitDestinationType(isInteger()),
|
|
|
|
has(ignoringParenImpCasts(Calc)));
|
2016-04-07 22:52:52 +08:00
|
|
|
const auto Cast = expr(anyOf(ExplicitCast, ImplicitCast)).bind("Cast");
|
2016-02-09 22:08:49 +08:00
|
|
|
|
2016-04-06 20:04:51 +08:00
|
|
|
Finder->addMatcher(varDecl(hasInitializer(Cast)), this);
|
|
|
|
Finder->addMatcher(returnStmt(hasReturnValue(Cast)), this);
|
|
|
|
Finder->addMatcher(callExpr(hasAnyArgument(Cast)), this);
|
2016-02-09 22:08:49 +08:00
|
|
|
Finder->addMatcher(binaryOperator(hasOperatorName("="), hasRHS(Cast)), this);
|
2016-04-06 20:04:51 +08:00
|
|
|
Finder->addMatcher(
|
2016-11-08 15:50:19 +08:00
|
|
|
binaryOperator(matchers::isComparisonOperator(), hasEitherOperand(Cast)),
|
2016-04-06 20:04:51 +08:00
|
|
|
this);
|
2016-02-09 22:08:49 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 22:52:52 +08:00
|
|
|
static unsigned getMaxCalculationWidth(const ASTContext &Context,
|
|
|
|
const Expr *E) {
|
2016-02-09 22:08:49 +08:00
|
|
|
E = E->IgnoreParenImpCasts();
|
|
|
|
|
|
|
|
if (const auto *Bop = dyn_cast<BinaryOperator>(E)) {
|
|
|
|
unsigned LHSWidth = getMaxCalculationWidth(Context, Bop->getLHS());
|
|
|
|
unsigned RHSWidth = getMaxCalculationWidth(Context, Bop->getRHS());
|
|
|
|
if (Bop->getOpcode() == BO_Mul)
|
|
|
|
return LHSWidth + RHSWidth;
|
|
|
|
if (Bop->getOpcode() == BO_Add)
|
|
|
|
return std::max(LHSWidth, RHSWidth) + 1;
|
|
|
|
if (Bop->getOpcode() == BO_Rem) {
|
2018-11-28 22:04:26 +08:00
|
|
|
Expr::EvalResult Result;
|
|
|
|
if (Bop->getRHS()->EvaluateAsInt(Result, Context))
|
|
|
|
return Result.Val.getInt().getActiveBits();
|
2016-02-09 22:08:49 +08:00
|
|
|
} else if (Bop->getOpcode() == BO_Shl) {
|
2018-11-28 22:04:26 +08:00
|
|
|
Expr::EvalResult Result;
|
|
|
|
if (Bop->getRHS()->EvaluateAsInt(Result, Context)) {
|
2016-02-09 22:08:49 +08:00
|
|
|
// We don't handle negative values and large values well. It is assumed
|
|
|
|
// that compiler warnings are written for such values so the user will
|
|
|
|
// fix that.
|
2018-11-28 22:04:26 +08:00
|
|
|
return LHSWidth + Result.Val.getInt().getExtValue();
|
2016-02-09 22:08:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown bitcount, assume there is truncation.
|
|
|
|
return 1024U;
|
|
|
|
}
|
|
|
|
} else if (const auto *Uop = dyn_cast<UnaryOperator>(E)) {
|
|
|
|
// There is truncation when ~ is used.
|
|
|
|
if (Uop->getOpcode() == UO_Not)
|
|
|
|
return 1024U;
|
|
|
|
|
|
|
|
QualType T = Uop->getType();
|
|
|
|
return T->isIntegerType() ? Context.getIntWidth(T) : 1024U;
|
|
|
|
} else if (const auto *I = dyn_cast<IntegerLiteral>(E)) {
|
|
|
|
return I->getValue().getActiveBits();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Context.getIntWidth(E->getType());
|
|
|
|
}
|
|
|
|
|
2016-04-07 22:52:52 +08:00
|
|
|
static int relativeIntSizes(BuiltinType::Kind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::UShort:
|
|
|
|
return 2;
|
|
|
|
case BuiltinType::Short:
|
|
|
|
return 2;
|
|
|
|
case BuiltinType::UInt:
|
|
|
|
return 3;
|
|
|
|
case BuiltinType::Int:
|
|
|
|
return 3;
|
|
|
|
case BuiltinType::ULong:
|
|
|
|
return 4;
|
|
|
|
case BuiltinType::Long:
|
|
|
|
return 4;
|
|
|
|
case BuiltinType::ULongLong:
|
|
|
|
return 5;
|
|
|
|
case BuiltinType::LongLong:
|
|
|
|
return 5;
|
|
|
|
case BuiltinType::UInt128:
|
|
|
|
return 6;
|
|
|
|
case BuiltinType::Int128:
|
|
|
|
return 6;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-06 20:04:51 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 22:52:52 +08:00
|
|
|
static int relativeCharSizes(BuiltinType::Kind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::Char16:
|
|
|
|
return 2;
|
|
|
|
case BuiltinType::Char32:
|
|
|
|
return 3;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-06 20:04:51 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 22:52:52 +08:00
|
|
|
static int relativeCharSizesW(BuiltinType::Kind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::WChar_U:
|
|
|
|
return 2;
|
|
|
|
case BuiltinType::WChar_S:
|
|
|
|
return 2;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-06 20:04:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isFirstWider(BuiltinType::Kind First, BuiltinType::Kind Second) {
|
|
|
|
int FirstSize, SecondSize;
|
2016-04-07 22:52:52 +08:00
|
|
|
if ((FirstSize = relativeIntSizes(First)) != 0 &&
|
|
|
|
(SecondSize = relativeIntSizes(Second)) != 0)
|
2016-04-06 20:04:51 +08:00
|
|
|
return FirstSize > SecondSize;
|
2016-04-07 22:52:52 +08:00
|
|
|
if ((FirstSize = relativeCharSizes(First)) != 0 &&
|
|
|
|
(SecondSize = relativeCharSizes(Second)) != 0)
|
2016-04-06 20:04:51 +08:00
|
|
|
return FirstSize > SecondSize;
|
2016-04-07 22:52:52 +08:00
|
|
|
if ((FirstSize = relativeCharSizesW(First)) != 0 &&
|
|
|
|
(SecondSize = relativeCharSizesW(Second)) != 0)
|
2016-04-06 20:04:51 +08:00
|
|
|
return FirstSize > SecondSize;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-09 22:08:49 +08:00
|
|
|
void MisplacedWideningCastCheck::check(const MatchFinder::MatchResult &Result) {
|
2016-04-06 20:04:51 +08:00
|
|
|
const auto *Cast = Result.Nodes.getNodeAs<CastExpr>("Cast");
|
|
|
|
if (!CheckImplicitCasts && isa<ImplicitCastExpr>(Cast))
|
|
|
|
return;
|
2018-08-10 06:42:26 +08:00
|
|
|
if (Cast->getBeginLoc().isMacroID())
|
2016-02-09 22:08:49 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
const auto *Calc = Result.Nodes.getNodeAs<Expr>("Calc");
|
2018-08-10 06:42:26 +08:00
|
|
|
if (Calc->getBeginLoc().isMacroID())
|
2016-02-09 22:08:49 +08:00
|
|
|
return;
|
|
|
|
|
2017-08-29 14:25:24 +08:00
|
|
|
if (Cast->isTypeDependent() || Cast->isValueDependent() ||
|
|
|
|
Calc->isTypeDependent() || Calc->isValueDependent())
|
|
|
|
return;
|
|
|
|
|
2016-02-09 22:08:49 +08:00
|
|
|
ASTContext &Context = *Result.Context;
|
|
|
|
|
|
|
|
QualType CastType = Cast->getType();
|
|
|
|
QualType CalcType = Calc->getType();
|
|
|
|
|
2016-02-12 15:51:10 +08:00
|
|
|
// Explicit truncation using cast.
|
|
|
|
if (Context.getIntWidth(CastType) < Context.getIntWidth(CalcType))
|
2016-02-09 22:08:49 +08:00
|
|
|
return;
|
|
|
|
|
2016-02-12 15:51:10 +08:00
|
|
|
// If CalcType and CastType have same size then there is no real danger, but
|
|
|
|
// there can be a portability problem.
|
2016-04-06 20:04:51 +08:00
|
|
|
|
2016-02-12 15:51:10 +08:00
|
|
|
if (Context.getIntWidth(CastType) == Context.getIntWidth(CalcType)) {
|
2016-04-06 20:04:51 +08:00
|
|
|
const auto *CastBuiltinType =
|
|
|
|
dyn_cast<BuiltinType>(CastType->getUnqualifiedDesugaredType());
|
|
|
|
const auto *CalcBuiltinType =
|
|
|
|
dyn_cast<BuiltinType>(CalcType->getUnqualifiedDesugaredType());
|
2018-12-05 16:29:56 +08:00
|
|
|
if (!CastBuiltinType || !CalcBuiltinType)
|
|
|
|
return;
|
|
|
|
if (!isFirstWider(CastBuiltinType->getKind(), CalcBuiltinType->getKind()))
|
2016-02-12 15:51:10 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't write a warning if we can easily see that the result is not
|
|
|
|
// truncated.
|
2016-02-09 22:08:49 +08:00
|
|
|
if (Context.getIntWidth(CalcType) >= getMaxCalculationWidth(Context, Calc))
|
|
|
|
return;
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(Cast->getBeginLoc(), "either cast from %0 to %1 is ineffective, or "
|
2016-02-09 22:08:49 +08:00
|
|
|
"there is loss of precision before the conversion")
|
|
|
|
<< CalcType << CastType;
|
|
|
|
}
|
|
|
|
|
2018-02-28 22:47:20 +08:00
|
|
|
} // namespace bugprone
|
2016-02-09 22:08:49 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|