From: Alexander Kornienko Date: Thu, 29 Jan 2015 15:17:13 +0000 (+0000) Subject: [clang-tidy] Fix some false positives in google-readability-casting X-Git-Tag: llvmorg-3.7.0-rc1~13736 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=78070fbae59b29b5fc7b970b948a9991cc9e9bbc;p=platform%2Fupstream%2Fllvm.git [clang-tidy] Fix some false positives in google-readability-casting Summary: Ignore C-style casts in extern "C" {} sections. Be more careful when detecting redundant casts between typedefs to the same type - emit a more specific warning and don't automatically fix them. Reviewers: klimek Reviewed By: klimek Subscribers: curdeius, cfe-commits Differential Revision: http://reviews.llvm.org/D7247 llvm-svn: 227444 --- diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp index 093250d..20440fe 100644 --- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -68,19 +68,30 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { if (CastExpr->getTypeAsWritten()->isVoidType()) return; - QualType SourceType = - CastExpr->getSubExprAsWritten()->getType().getCanonicalType(); - QualType DestType = CastExpr->getTypeAsWritten().getCanonicalType(); + QualType SourceType = CastExpr->getSubExprAsWritten()->getType(); + QualType DestType = CastExpr->getTypeAsWritten(); if (SourceType == DestType) { - diag(CastExpr->getLocStart(), "Redundant cast to the same type.") + diag(CastExpr->getLocStart(), "redundant cast to the same type") << FixItHint::CreateRemoval(ParenRange); return; } + SourceType = SourceType.getCanonicalType(); + DestType = DestType.getCanonicalType(); + if (SourceType == DestType) { + diag(CastExpr->getLocStart(), + "possibly redundant cast between typedefs of the same type"); + return; + } + // The rest of this check is only relevant to C++. if (!Result.Context->getLangOpts().CPlusPlus) return; + // Ignore code inside extern "C" {} blocks. + if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context) + .empty()) + return; // Leave type spelling exactly as it was (unlike // getTypeAsWritten().getAsString() which would spell enum types 'enum X'). @@ -94,7 +105,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { diag(CastExpr->getLocStart(), "C-style casts are discouraged. %0"); auto ReplaceWithCast = [&](StringRef CastType) { - diag_builder << ("Use " + CastType + ".").str(); + diag_builder << ("Use " + CastType).str(); const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts(); std::string CastText = (CastType + "<" + DestTypeString + ">").str(); @@ -108,6 +119,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { } diag_builder << FixItHint::CreateReplacement(ParenRange, CastText); }; + // Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics. switch (CastExpr->getCastKind()) { case CK_NoOp: @@ -145,7 +157,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { break; } - diag_builder << "Use static_cast/const_cast/reinterpret_cast."; + diag_builder << "Use static_cast/const_cast/reinterpret_cast"; } } // namespace readability diff --git a/clang-tools-extra/test/clang-tidy/google-readability-casting.c b/clang-tools-extra/test/clang-tidy/google-readability-casting.c index 55ee095..9765365 100644 --- a/clang-tools-extra/test/clang-tidy/google-readability-casting.c +++ b/clang-tools-extra/test/clang-tidy/google-readability-casting.c @@ -3,7 +3,7 @@ void f(const char *cpc) { const char *cpc2 = (const char*)cpc; - // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: Redundant cast to the same type. [google-readability-casting] + // 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; } diff --git a/clang-tools-extra/test/clang-tidy/google-readability-casting.cpp b/clang-tools-extra/test/clang-tidy/google-readability-casting.cpp index 97df300..a8c1989 100644 --- a/clang-tools-extra/test/clang-tidy/google-readability-casting.cpp +++ b/clang-tools-extra/test/clang-tidy/google-readability-casting.cpp @@ -9,67 +9,83 @@ 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-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type [google-readability-casting] // CHECK-FIXES: const char *cpc2 = cpc; + typedef const char *Typedef1; + typedef const char *Typedef2; + Typedef1 t1; + (Typedef2)t1; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: possibly redundant cast between typedefs of the same type [google-readability-casting] + // CHECK-FIXES: {{^}} (Typedef2)t1; + (const char*)t1; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: possibly redundant cast {{.*}} + // CHECK-FIXES: {{^}} (const char*)t1; + (Typedef1)cpc; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: possibly redundant cast {{.*}} + // CHECK-FIXES: {{^}} (Typedef1)cpc; + (Typedef1)t1; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant cast to the same type + // CHECK-FIXES: {{^}} t1; + char *pc = (char*)cpc; - // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged. Use const_cast. {{.*}} + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged. Use const_cast [google-readability-casting] // CHECK-FIXES: char *pc = const_cast(cpc); char *pc2 = (char*)(cpc + 33); - // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}} + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}. Use const_cast {{.*}} // CHECK-FIXES: char *pc2 = const_cast(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-MESSAGES: :[[@LINE-1]]:14: warning: {{.*}}. Use const_cast {{.*}} // CHECK-FIXES: char &rc = const_cast(crc); char &rc2 = (char&)*cpc; - // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}} + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}. Use const_cast {{.*}} // CHECK-FIXES: char &rc2 = const_cast(*cpc); char ** const* const* ppcpcpc; char ****ppppc = (char****)ppcpcpc; - // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: C-style casts are discouraged. Use const_cast. {{.*}} + // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: {{.*}}. Use const_cast {{.*}} // CHECK-FIXES: char ****ppppc = const_cast(ppcpcpc); char ***pppc = (char***)*(ppcpcpc); - // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: C-style casts are discouraged. Use const_cast. {{.*}} + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: {{.*}}. Use const_cast {{.*}} // CHECK-FIXES: char ***pppc = const_cast(*(ppcpcpc)); char ***pppc2 = (char***)(*ppcpcpc); - // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged. Use const_cast. {{.*}} + // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: {{.*}}. Use const_cast {{.*}} // CHECK-FIXES: char ***pppc2 = const_cast(*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-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}. Use const_cast {{.*}} + // CHECK-MESSAGES: :[[@LINE-2]]:22: warning: {{.*}}. Use reinterpret_cast {{.*}} // CHECK-FIXES: char *pc5 = const_cast(reinterpret_cast(cpv)); int b1 = (int)b; - // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting] + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: {{.*}}. Use static_cast {{.*}} // CHECK-FIXES: int b1 = static_cast(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] + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}. Use static_cast/const_cast/reinterpret_cast {{.*}} 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-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}. Use static_cast/const_cast/reinterpret_cast {{.*}} const char *pc3 = (const char*)cpv; - // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: C-style casts are discouraged. Use reinterpret_cast. [google-readability-casting] + // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: {{.*}}. Use reinterpret_cast {{.*}} // CHECK-FIXES: const char *pc3 = reinterpret_cast(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-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}. Use static_cast/const_cast/reinterpret_cast {{.*}} // 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-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}. Use static_cast {{.*}} // CHECK-FIXES: b1 = static_cast(Enum1); Enum e = (Enum)b1; - // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting] + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: {{.*}}. Use static_cast {{.*}} // CHECK-FIXES: Enum e = static_cast(b1); // CHECK-MESSAGES-NOT: warning: @@ -84,10 +100,10 @@ void f(int a, double b, const char *cpc, const void *cpv, X *pX) { template 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-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}. 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-MESSAGES: :[[@LINE-1]]:11: warning: redundant cast to the same type // CHECK-FIXES: int j = n; } @@ -95,10 +111,10 @@ template 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-MESSAGES: :[[@LINE-1]]:13: warning: {{.*}}. 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-MESSAGES: :[[@LINE-1]]:13: warning: redundant cast to the same type // CHECK-FIXES: int l = n; } }; @@ -110,6 +126,15 @@ void test_templates() { TemplateStruct().f(1.0, 42); } +extern "C" { +void extern_c_code(const char *cpc) { + const char *cpc2 = (const char*)cpc; + // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type + // CHECK-FIXES: const char *cpc2 = cpc; + char *pc = (char*)cpc; +} +} + #define CAST(type, value) (type)(value) void macros(double d) { int i = CAST(int, d);