[clang-tools-extra] r218760 - [clang-tidy] Handle c-style casts from/to enums.
Alexander Kornienko
alexfh at google.com
Wed Oct 1 05:47:53 PDT 2014
Author: alexfh
Date: Wed Oct 1 07:47:53 2014
New Revision: 218760
URL: http://llvm.org/viewvc/llvm-project?rev=218760&view=rev
Log:
[clang-tidy] Handle c-style casts from/to enums.
Summary: Convert c-style casts between integral end enum types to static_cast<>.
Reviewers: klimek
Reviewed By: klimek
Subscribers: curdeius, cfe-commits
Differential Revision: http://reviews.llvm.org/D5558
Modified:
clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp
Modified: clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp?rev=218760&r1=218759&r2=218760&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp Wed Oct 1 07:47:53 2014
@@ -82,7 +82,12 @@ void AvoidCStyleCastsCheck::check(const
if (!Result.Context->getLangOpts().CPlusPlus)
return;
- std::string DestTypeString = CastExpr->getTypeAsWritten().getAsString();
+ // Leave type spelling exactly as it was.
+ StringRef DestTypeString = Lexer::getSourceText(
+ CharSourceRange::getTokenRange(
+ CastExpr->getLParenLoc().getLocWithOffset(1),
+ CastExpr->getRParenLoc().getLocWithOffset(-1)),
+ *Result.SourceManager, Result.Context->getLangOpts());
auto diag_builder =
diag(CastExpr->getLocStart(), "C-style casts are discouraged. %0");
@@ -117,7 +122,13 @@ void AvoidCStyleCastsCheck::check(const
ReplaceWithCast("const_cast");
return;
}
- if (SourceType->isBuiltinType() && DestType->isBuiltinType()) {
+ // FALLTHROUGH
+ case clang::CK_IntegralCast:
+ // Convert integral and no-op casts between builtin types and enums to
+ // static_cast. A cast from enum to integer may be unnecessary, but it's
+ // still retained.
+ if ((SourceType->isBuiltinType() || SourceType->isEnumeralType()) &&
+ (DestType->isBuiltinType() || DestType->isEnumeralType())) {
ReplaceWithCast("static_cast");
return;
}
Modified: clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp?rev=218760&r1=218759&r2=218760&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp Wed Oct 1 07:47:53 2014
@@ -3,6 +3,7 @@
bool g() { return false; }
+enum Enum { Enum1 };
struct X {};
struct Y : public X {};
@@ -13,39 +14,38 @@ void f(int a, double b, const char *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);
+ // 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);
+ // 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);
+ // 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);
+ // 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);
+ // CHECK-FIXES: char ****ppppc = const_cast<char****>(ppcpcpc);
char ***pppc = (char***)*(ppcpcpc);
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: C-style casts are discouraged. Use const_cast. {{.*}}
- // CHECK-FIXES: char ***pppc = const_cast<char ***>(*(ppcpcpc));
+ // CHECK-FIXES: char ***pppc = const_cast<char***>(*(ppcpcpc));
char ***pppc2 = (char***)(*ppcpcpc);
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged. Use const_cast. {{.*}}
- // CHECK-FIXES: char ***pppc2 = const_cast<char ***>(*ppcpcpc);
+ // CHECK-FIXES: char ***pppc2 = const_cast<char***>(*ppcpcpc);
char *pc5 = (char*)(const char*)(cpv);
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}}
// CHECK-MESSAGES: :[[@LINE-2]]:22: warning: C-style casts are discouraged. Use reinterpret_cast. {{.*}}
- // CHECK-FIXES: char *pc5 = const_cast<char *>(reinterpret_cast<const char *>(cpv));
-
+ // CHECK-FIXES: char *pc5 = const_cast<char*>(reinterpret_cast<const char*>(cpv));
int b1 = (int)b;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting]
@@ -58,12 +58,20 @@ void f(int a, double b, const char *cpc,
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);
+ // 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;
+ b1 = (int)Enum1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting]
+ // CHECK-FIXES: b1 = static_cast<int>(Enum1);
+
+ Enum e = (Enum)b1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting]
+ // CHECK-FIXES: Enum e = static_cast<Enum>(b1);
+
// CHECK-MESSAGES-NOT: warning:
int b2 = int(b);
int b3 = static_cast<double>(b);
More information about the cfe-commits
mailing list