summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test
diff options
context:
space:
mode:
authorDaniel Marjamaki <daniel.marjamaki@evidente.se>2016-02-09 14:08:49 +0000
committerDaniel Marjamaki <daniel.marjamaki@evidente.se>2016-02-09 14:08:49 +0000
commitad3293744a40a1a84b171abd92b20e119f28c493 (patch)
tree1efa1ac9d9354ce149351c5be55736045088098e /clang-tools-extra/test
parent880a65bba5151925a49b116a879370cd2d707458 (diff)
downloadbcm5719-llvm-ad3293744a40a1a84b171abd92b20e119f28c493.tar.gz
bcm5719-llvm-ad3293744a40a1a84b171abd92b20e119f28c493.zip
[clang-tidy] Add 'misc-misplaced-widening-cast' check.
Reviewers: alexfh Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D16310 llvm-svn: 260223
Diffstat (limited to 'clang-tools-extra/test')
-rw-r--r--clang-tools-extra/test/clang-tidy/misc-misplaced-widening-cast.cpp63
1 files changed, 63 insertions, 0 deletions
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
new file mode 100644
index 00000000000..68db857c704
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/misc-misplaced-widening-cast.cpp
@@ -0,0 +1,63 @@
+// RUN: %check_clang_tidy %s misc-misplaced-widening-cast %t
+
+void assign(int a, int b) {
+ long l;
+
+ l = a * b;
+ l = (long)(a * b);
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long' is ineffective, or there is loss of precision before the conversion [misc-misplaced-widening-cast]
+ l = (long)a * b;
+
+ l = (long)(a << 8);
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
+ l = (long)b << 8;
+
+ l = static_cast<long>(a * b);
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long' is ineffective, or there is loss of precision before the conversion [misc-misplaced-widening-cast]
+}
+
+void init(unsigned int n) {
+ long l = (long)(n << 8);
+ // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: either cast from 'unsigned int'
+}
+
+long ret(int a) {
+ return (long)(a * 1000);
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: either cast from 'int' to 'long'
+}
+
+void dontwarn1(unsigned char a, int i, unsigned char *p) {
+ long l;
+ // The result is a 9 bit value, there is no truncation in the implicit cast.
+ l = (long)(a + 15);
+ // The result is a 12 bit value, there is no truncation in the implicit cast.
+ l = (long)(a << 4);
+ // The result is a 3 bit value, there is no truncation in the implicit cast.
+ l = (long)((i%5)+1);
+ // The result is a 16 bit value, there is no truncation in the implicit cast.
+ l = (long)(((*p)<<8) + *(p+1));
+}
+
+template <class T> struct DontWarn2 {
+ void assign(T a, T b) {
+ long l;
+ l = (long)(a * b);
+ }
+};
+DontWarn2<int> DW2;
+
+// Cast is not suspicious when casting macro.
+#define A (X<<2)
+long macro1(int X) {
+ return (long)A;
+}
+
+// Don't warn about cast in macro.
+#define B(X,Y) (long)(X*Y)
+long macro2(int x, int y) {
+ return B(x,y);
+}
+
+void floatingpoint(float a, float b) {
+ double d = (double)(a * b); // Currently we don't warn for this.
+}
OpenPOWER on IntegriCloud