llvm-project
227 строк · 7.1 Кб
1//===--- MisplacedWideningCastCheck.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 "MisplacedWideningCastCheck.h"10#include "../utils/Matchers.h"11#include "clang/AST/ASTContext.h"12#include "clang/ASTMatchers/ASTMatchFinder.h"13
14using namespace clang::ast_matchers;15
16namespace clang::tidy::bugprone {17
18MisplacedWideningCastCheck::MisplacedWideningCastCheck(19StringRef Name, ClangTidyContext *Context)20: ClangTidyCheck(Name, Context),21CheckImplicitCasts(Options.get("CheckImplicitCasts", false)) {}22
23void MisplacedWideningCastCheck::storeOptions(24ClangTidyOptions::OptionMap &Opts) {25Options.store(Opts, "CheckImplicitCasts", CheckImplicitCasts);26}
27
28void MisplacedWideningCastCheck::registerMatchers(MatchFinder *Finder) {29const auto Calc =30expr(anyOf(binaryOperator(hasAnyOperatorName("+", "-", "*", "<<")),31unaryOperator(hasOperatorName("~"))),32hasType(isInteger()))33.bind("Calc");34
35const auto ExplicitCast = explicitCastExpr(hasDestinationType(isInteger()),36has(ignoringParenImpCasts(Calc)));37const auto ImplicitCast =38implicitCastExpr(hasImplicitDestinationType(isInteger()),39has(ignoringParenImpCasts(Calc)));40const auto Cast =41traverse(TK_AsIs, expr(anyOf(ExplicitCast, ImplicitCast)).bind("Cast"));42
43Finder->addMatcher(varDecl(hasInitializer(Cast)), this);44Finder->addMatcher(returnStmt(hasReturnValue(Cast)), this);45Finder->addMatcher(callExpr(hasAnyArgument(Cast)), this);46Finder->addMatcher(binaryOperator(hasOperatorName("="), hasRHS(Cast)), this);47Finder->addMatcher(48binaryOperator(isComparisonOperator(), hasEitherOperand(Cast)), this);49}
50
51static unsigned getMaxCalculationWidth(const ASTContext &Context,52const Expr *E) {53E = E->IgnoreParenImpCasts();54
55if (const auto *Bop = dyn_cast<BinaryOperator>(E)) {56unsigned LHSWidth = getMaxCalculationWidth(Context, Bop->getLHS());57unsigned RHSWidth = getMaxCalculationWidth(Context, Bop->getRHS());58if (Bop->getOpcode() == BO_Mul)59return LHSWidth + RHSWidth;60if (Bop->getOpcode() == BO_Add)61return std::max(LHSWidth, RHSWidth) + 1;62if (Bop->getOpcode() == BO_Rem) {63Expr::EvalResult Result;64if (Bop->getRHS()->EvaluateAsInt(Result, Context))65return Result.Val.getInt().getActiveBits();66} else if (Bop->getOpcode() == BO_Shl) {67Expr::EvalResult Result;68if (Bop->getRHS()->EvaluateAsInt(Result, Context)) {69// We don't handle negative values and large values well. It is assumed70// that compiler warnings are written for such values so the user will71// fix that.72return LHSWidth + Result.Val.getInt().getExtValue();73}74
75// Unknown bitcount, assume there is truncation.76return 1024U;77}78} else if (const auto *Uop = dyn_cast<UnaryOperator>(E)) {79// There is truncation when ~ is used.80if (Uop->getOpcode() == UO_Not)81return 1024U;82
83QualType T = Uop->getType();84return T->isIntegerType() ? Context.getIntWidth(T) : 1024U;85} else if (const auto *I = dyn_cast<IntegerLiteral>(E)) {86return I->getValue().getActiveBits();87}88
89return Context.getIntWidth(E->getType());90}
91
92static int relativeIntSizes(BuiltinType::Kind Kind) {93switch (Kind) {94case BuiltinType::UChar:95return 1;96case BuiltinType::SChar:97return 1;98case BuiltinType::Char_U:99return 1;100case BuiltinType::Char_S:101return 1;102case BuiltinType::UShort:103return 2;104case BuiltinType::Short:105return 2;106case BuiltinType::UInt:107return 3;108case BuiltinType::Int:109return 3;110case BuiltinType::ULong:111return 4;112case BuiltinType::Long:113return 4;114case BuiltinType::ULongLong:115return 5;116case BuiltinType::LongLong:117return 5;118case BuiltinType::UInt128:119return 6;120case BuiltinType::Int128:121return 6;122default:123return 0;124}125}
126
127static int relativeCharSizes(BuiltinType::Kind Kind) {128switch (Kind) {129case BuiltinType::UChar:130return 1;131case BuiltinType::SChar:132return 1;133case BuiltinType::Char_U:134return 1;135case BuiltinType::Char_S:136return 1;137case BuiltinType::Char16:138return 2;139case BuiltinType::Char32:140return 3;141default:142return 0;143}144}
145
146static int relativeCharSizesW(BuiltinType::Kind Kind) {147switch (Kind) {148case BuiltinType::UChar:149return 1;150case BuiltinType::SChar:151return 1;152case BuiltinType::Char_U:153return 1;154case BuiltinType::Char_S:155return 1;156case BuiltinType::WChar_U:157return 2;158case BuiltinType::WChar_S:159return 2;160default:161return 0;162}163}
164
165static bool isFirstWider(BuiltinType::Kind First, BuiltinType::Kind Second) {166int FirstSize = 0, SecondSize = 0;167if ((FirstSize = relativeIntSizes(First)) != 0 &&168(SecondSize = relativeIntSizes(Second)) != 0)169return FirstSize > SecondSize;170if ((FirstSize = relativeCharSizes(First)) != 0 &&171(SecondSize = relativeCharSizes(Second)) != 0)172return FirstSize > SecondSize;173if ((FirstSize = relativeCharSizesW(First)) != 0 &&174(SecondSize = relativeCharSizesW(Second)) != 0)175return FirstSize > SecondSize;176return false;177}
178
179void MisplacedWideningCastCheck::check(const MatchFinder::MatchResult &Result) {180const auto *Cast = Result.Nodes.getNodeAs<CastExpr>("Cast");181if (!CheckImplicitCasts && isa<ImplicitCastExpr>(Cast))182return;183if (Cast->getBeginLoc().isMacroID())184return;185
186const auto *Calc = Result.Nodes.getNodeAs<Expr>("Calc");187if (Calc->getBeginLoc().isMacroID())188return;189
190if (Cast->isTypeDependent() || Cast->isValueDependent() ||191Calc->isTypeDependent() || Calc->isValueDependent())192return;193
194ASTContext &Context = *Result.Context;195
196QualType CastType = Cast->getType();197QualType CalcType = Calc->getType();198
199// Explicit truncation using cast.200if (Context.getIntWidth(CastType) < Context.getIntWidth(CalcType))201return;202
203// If CalcType and CastType have same size then there is no real danger, but204// there can be a portability problem.205
206if (Context.getIntWidth(CastType) == Context.getIntWidth(CalcType)) {207const auto *CastBuiltinType =208dyn_cast<BuiltinType>(CastType->getUnqualifiedDesugaredType());209const auto *CalcBuiltinType =210dyn_cast<BuiltinType>(CalcType->getUnqualifiedDesugaredType());211if (!CastBuiltinType || !CalcBuiltinType)212return;213if (!isFirstWider(CastBuiltinType->getKind(), CalcBuiltinType->getKind()))214return;215}216
217// Don't write a warning if we can easily see that the result is not218// truncated.219if (Context.getIntWidth(CalcType) >= getMaxCalculationWidth(Context, Calc))220return;221
222diag(Cast->getBeginLoc(), "either cast from %0 to %1 is ineffective, or "223"there is loss of precision before the conversion")224<< CalcType << CastType;225}
226
227} // namespace clang::tidy::bugprone228