diff options
| author | Alexander Kornienko <alexfh@google.com> | 2014-10-01 12:47:53 +0000 |
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2014-10-01 12:47:53 +0000 |
| commit | 50913723314c05af389f11ef988d511ac6e32f33 (patch) | |
| tree | f9a712a0d2ccde6a5c356ca4bdbd79f38edb4c6a | |
| parent | b68af953d96f41c1ba9c71f54fdeea15b292f3fc (diff) | |
| download | bcm5719-llvm-50913723314c05af389f11ef988d511ac6e32f33.tar.gz bcm5719-llvm-50913723314c05af389f11ef988d511ac6e32f33.zip | |
[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
llvm-svn: 218760
| -rw-r--r-- | clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp | 15 | ||||
| -rw-r--r-- | clang-tools-extra/test/clang-tidy/avoid-c-style-casts.cpp | 28 |
2 files changed, 31 insertions, 12 deletions
diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp index e1161726f1a..ed6ff9198e0 100644 --- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -82,7 +82,12 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { 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 MatchFinder::MatchResult &Result) { 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; } 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 afb1eaaad87..414fa8bbcab 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 @@ -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, const void *cpv, X *pX) { 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 void *cpv, X *pX) { 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); |

