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