diff options
| author | Daniel Marjamaki <daniel.marjamaki@evidente.se> | 2016-02-12 07:51:10 +0000 |
|---|---|---|
| committer | Daniel Marjamaki <daniel.marjamaki@evidente.se> | 2016-02-12 07:51:10 +0000 |
| commit | 79355776e32885052301e98df38767059f2a7c94 (patch) | |
| tree | 649dce8172e360aa542c6991442c73994c22b1a5 | |
| parent | 2543d2904853a5b80a83504c140601df45e91bcf (diff) | |
| download | bcm5719-llvm-79355776e32885052301e98df38767059f2a7c94.tar.gz bcm5719-llvm-79355776e32885052301e98df38767059f2a7c94.zip | |
[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
llvm-svn: 260665
| -rw-r--r-- | clang-tools-extra/clang-tidy/misc/MisplacedWideningCastCheck.cpp | 25 | ||||
| -rw-r--r-- | clang-tools-extra/test/clang-tidy/misc-misplaced-widening-cast.cpp | 2 |
2 files changed, 23 insertions, 4 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/MisplacedWideningCastCheck.cpp b/clang-tools-extra/clang-tidy/misc/MisplacedWideningCastCheck.cpp index d892528e60b..0006131f20f 100644 --- a/clang-tools-extra/clang-tidy/misc/MisplacedWideningCastCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/MisplacedWideningCastCheck.cpp @@ -27,8 +27,7 @@ void MisplacedWideningCastCheck::registerMatchers(MatchFinder *Finder) { 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(const MatchFinder::MatchResult &Result) { 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; diff --git a/clang-tools-extra/test/clang-tidy/misc-misplaced-widening-cast.cpp b/clang-tools-extra/test/clang-tidy/misc-misplaced-widening-cast.cpp index b7611ba0870..68db857c704 100644 --- a/clang-tools-extra/test/clang-tidy/misc-misplaced-widening-cast.cpp +++ b/clang-tools-extra/test/clang-tidy/misc-misplaced-widening-cast.cpp @@ -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; |

