danielmarjamaki created this revision.
danielmarjamaki added a reviewer: alexfh.
danielmarjamaki added a subscriber: cfe-commits.
danielmarjamaki set the repository for this revision to rL LLVM.

The misc-misplaced-widening-cast currently only looks at the size to determine 
if a cast is widening. 

With this patch it will also warn when there are portability problems.

The test file should now be target independant.


Repository:
  rL LLVM

http://reviews.llvm.org/D17140

Files:
  clang-tidy/misc/MisplacedWideningCastCheck.cpp
  test/clang-tidy/misc-misplaced-widening-cast.cpp

Index: test/clang-tidy/misc-misplaced-widening-cast.cpp
===================================================================
--- test/clang-tidy/misc-misplaced-widening-cast.cpp
+++ 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;
Index: clang-tidy/misc/MisplacedWideningCastCheck.cpp
===================================================================
--- clang-tidy/misc/MisplacedWideningCastCheck.cpp
+++ clang-tidy/misc/MisplacedWideningCastCheck.cpp
@@ -27,8 +27,7 @@
 
   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 @@
   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;
 


Index: test/clang-tidy/misc-misplaced-widening-cast.cpp
===================================================================
--- test/clang-tidy/misc-misplaced-widening-cast.cpp
+++ 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;
Index: clang-tidy/misc/MisplacedWideningCastCheck.cpp
===================================================================
--- clang-tidy/misc/MisplacedWideningCastCheck.cpp
+++ clang-tidy/misc/MisplacedWideningCastCheck.cpp
@@ -27,8 +27,7 @@
 
   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 @@
   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;
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to