summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/avoid-c-style-casts.cpp
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2014-07-14 07:37:05 +0000
committerAlexander Kornienko <alexfh@google.com>2014-07-14 07:37:05 +0000
commit7366616a9370430a55fd96621180b423c2252193 (patch)
tree8ee72f953f9186600c1129ba09027c15d7c3d69e /clang-tools-extra/test/clang-tidy/avoid-c-style-casts.cpp
parent24189d4c863e72772f08cdedac77f4a7816ee2f3 (diff)
downloadbcm5719-llvm-7366616a9370430a55fd96621180b423c2252193.tar.gz
bcm5719-llvm-7366616a9370430a55fd96621180b423c2252193.zip
Suggest automated replacements of C-style casts with C++ casts.
Summary: This patch implements a subset of possible replacements of C-style casts with const_cast/static_cast/reinterpret_cast. This should cover a large portion of cases in real code. Handling of a few more cases may be implemented eventually. Reviewers: sbenza, djasper Reviewed By: djasper Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D4478 llvm-svn: 212924
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/avoid-c-style-casts.cpp')
-rw-r--r--clang-tools-extra/test/clang-tidy/avoid-c-style-casts.cpp99
1 files changed, 91 insertions, 8 deletions
diff --git a/clang-tools-extra/test/clang-tidy/avoid-c-style-casts.cpp b/clang-tools-extra/test/clang-tidy/avoid-c-style-casts.cpp
index 8b9feb43d93..a81c26246b0 100644
--- a/clang-tools-extra/test/clang-tidy/avoid-c-style-casts.cpp
+++ b/clang-tools-extra/test/clang-tidy/avoid-c-style-casts.cpp
@@ -1,14 +1,56 @@
-// RUN: clang-tidy -checks=-*,google-readability-casting %s -- | FileCheck %s
-
-// CHECK-NOT: warning:
+// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s google-readability-casting %t
+// REQUIRES: shell
bool g() { return false; }
-void f(int a, double b) {
+struct X {};
+struct Y : public X {};
+
+void f(int a, double b, const char *cpc, const void *cpv, X *pX) {
+ const char *cpc2 = (const char*)cpc;
+ // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: Redundant cast to the same type. [google-readability-casting]
+ // CHECK-FIXES: const char *cpc2 = cpc;
+
+ char *pc = (char*)cpc;
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged. Use const_cast. {{.*}}
+ // CHECK-FIXES: char *pc = const_cast<char *>(cpc);
+
+ char *pc2 = (char*)(cpc + 33);
+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}}
+ // CHECK-FIXES: char *pc2 = const_cast<char *>((cpc + 33));
+
+ const char &crc = *cpc;
+ char &rc = (char&)crc;
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged. Use const_cast. {{.*}}
+ // CHECK-FIXES: char &rc = const_cast<char &>(crc);
+
+ char &rc2 = (char&)*cpc;
+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}}
+ // CHECK-FIXES: char &rc2 = const_cast<char &>(*cpc);
+
+ char ** const* const* ppcpcpc;
+ char ****ppppc = (char****)ppcpcpc;
+ // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: C-style casts are discouraged. Use const_cast. {{.*}}
+ // CHECK-FIXES: char ****ppppc = const_cast<char ****>(ppcpcpc);
+
int b1 = (int)b;
- // CHECK: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast{{.*}}
+ // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting]
+ // CHECK-FIXES: int b1 = static_cast<int>(b);
+
+ Y *pB = (Y*)pX;
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast. [google-readability-casting]
+ Y &rB = (Y&)*pX;
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast. [google-readability-casting]
- // CHECK-NOT: warning:
+ const char *pc3 = (const char*)cpv;
+ // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: C-style casts are discouraged. Use reinterpret_cast. [google-readability-casting]
+ // CHECK-FIXES: const char *pc3 = reinterpret_cast<const char *>(cpv);
+
+ char *pc4 = (char*)cpv;
+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast. [google-readability-casting]
+ // CHECK-FIXES: char *pc4 = (char*)cpv;
+
+ // CHECK-MESSAGES-NOT: warning:
int b2 = int(b);
int b3 = static_cast<double>(b);
int b4 = b;
@@ -17,8 +59,49 @@ void f(int a, double b) {
return (void)g();
}
-// CHECK-NOT: warning:
+template <typename T>
+void template_function(T t, int n) {
+ int i = (int)t;
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast.
+ // CHECK-FIXES: int i = (int)t;
+ int j = (int)n;
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: Redundant cast to the same type.
+ // CHECK-FIXES: int j = n;
+}
+
+template <typename T>
+struct TemplateStruct {
+ void f(T t, int n) {
+ int k = (int)t;
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast.
+ // CHECK-FIXES: int k = (int)t;
+ int l = (int)n;
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: Redundant cast to the same type.
+ // CHECK-FIXES: int l = n;
+ }
+};
+
+void test_templates() {
+ template_function(1, 42);
+ template_function(1.0, 42);
+ TemplateStruct<int>().f(1, 42);
+ TemplateStruct<double>().f(1.0, 42);
+}
+
+#define CAST(type, value) (type)(value)
+void macros(double d) {
+ int i = CAST(int, d);
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast.
+ // CHECK-FIXES: #define CAST(type, value) (type)(value)
+ // CHECK-FIXES: int i = CAST(int, d);
+}
+
enum E { E1 = 1 };
template <E e>
-struct A { static const E ee = e; };
+struct A {
+ // Usage of template argument e = E1 is represented as (E)1 in the AST for
+ // some reason. We have a special treatment of this case to avoid warnings
+ // here.
+ static const E ee = e;
+};
struct B : public A<E1> {};
OpenPOWER on IntegriCloud