[clang-tools-extra] r260665 - [clang-tidy] improve misc-misplaced-widening-cast so it also detects portability problems.
Daniel Marjamaki via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 11 23:51:10 PST 2016
Author: danielmarjamaki
Date: Fri Feb 12 01:51:10 2016
New Revision: 260665
URL: http://llvm.org/viewvc/llvm-project?rev=260665&view=rev
Log:
[clang-tidy] improve misc-misplaced-widening-cast so it also detects portability problems.
Reviewers: alexfh
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D17140
Modified:
clang-tools-extra/trunk/clang-tidy/misc/MisplacedWideningCastCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/misc-misplaced-widening-cast.cpp
Modified: clang-tools-extra/trunk/clang-tidy/misc/MisplacedWideningCastCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/MisplacedWideningCastCheck.cpp?rev=260665&r1=260664&r2=260665&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/MisplacedWideningCastCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/MisplacedWideningCastCheck.cpp Fri Feb 12 01:51:10 2016
@@ -27,8 +27,7 @@ void MisplacedWideningCastCheck::registe
auto Cast = explicitCastExpr(anyOf(cStyleCastExpr(), cxxStaticCastExpr(),
cxxReinterpretCastExpr()),
- hasDestinationType(isInteger()),
- has(Calc))
+ hasDestinationType(isInteger()), has(Calc))
.bind("Cast");
Finder->addMatcher(varDecl(has(Cast)), this);
@@ -90,9 +89,29 @@ void MisplacedWideningCastCheck::check(c
QualType CastType = Cast->getType();
QualType CalcType = Calc->getType();
- if (Context.getIntWidth(CastType) <= Context.getIntWidth(CalcType))
+ // Explicit truncation using cast.
+ if (Context.getIntWidth(CastType) < Context.getIntWidth(CalcType))
return;
+ // If CalcType and CastType have same size then there is no real danger, but
+ // there can be a portability problem.
+ if (Context.getIntWidth(CastType) == Context.getIntWidth(CalcType)) {
+ if (CalcType->isSpecificBuiltinType(BuiltinType::Int)) {
+ // There should be a warning when casting from int to long or long long.
+ if (!CastType->isSpecificBuiltinType(BuiltinType::Long) &&
+ !CastType->isSpecificBuiltinType(BuiltinType::LongLong))
+ return;
+ } else if (CalcType->isSpecificBuiltinType(BuiltinType::Long)) {
+ // There should be a warning when casting from long to long long.
+ if (!CastType->isSpecificBuiltinType(BuiltinType::LongLong))
+ return;
+ } else {
+ return;
+ }
+ }
+
+ // Don't write a warning if we can easily see that the result is not
+ // truncated.
if (Context.getIntWidth(CalcType) >= getMaxCalculationWidth(Context, Calc))
return;
Modified: clang-tools-extra/trunk/test/clang-tidy/misc-misplaced-widening-cast.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-misplaced-widening-cast.cpp?rev=260665&r1=260664&r2=260665&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-misplaced-widening-cast.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-misplaced-widening-cast.cpp Fri Feb 12 01:51:10 2016
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s misc-misplaced-widening-cast %t -- -- -target x86_64-unknown-unknown
+// RUN: %check_clang_tidy %s misc-misplaced-widening-cast %t
void assign(int a, int b) {
long l;
More information about the cfe-commits
mailing list