Hi djasper, sbenza,
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.
http://reviews.llvm.org/D4478
Files:
clang-tidy/google/AvoidCStyleCastsCheck.cpp
test/clang-tidy/avoid-c-style-casts.cpp
Index: clang-tidy/google/AvoidCStyleCastsCheck.cpp
===================================================================
--- clang-tidy/google/AvoidCStyleCastsCheck.cpp
+++ clang-tidy/google/AvoidCStyleCastsCheck.cpp
@@ -8,8 +8,10 @@
//===----------------------------------------------------------------------===//
#include "AvoidCStyleCastsCheck.h"
+#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
@@ -28,6 +30,27 @@
this);
}
+bool needConstCast(QualType SourceType, QualType DestType) {
+ SourceType = SourceType.getNonReferenceType();
+ DestType = DestType.getNonReferenceType();
+ while (SourceType->isPointerType() && DestType->isPointerType()) {
+ SourceType = SourceType->getPointeeType();
+ DestType = DestType->getPointeeType();
+ if (SourceType.isConstQualified() && !DestType.isConstQualified())
+ return true;
+ }
+ return false;
+}
+
+bool pointedTypesAreEqual(QualType SourceType, QualType DestType) {
+ SourceType = SourceType.getNonReferenceType();
+ DestType = DestType.getNonReferenceType();
+ while (SourceType->isPointerType() && DestType->isPointerType()) {
+ SourceType = SourceType->getPointeeType();
+ DestType = DestType->getPointeeType();
+ }
+ return SourceType.getUnqualifiedType() == DestType.getUnqualifiedType();
+}
void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("cast");
@@ -40,11 +63,66 @@
if (CastExpr->getTypeAsWritten()->isVoidType())
return;
- diag(CastExpr->getLocStart(), "C-style casts are discouraged. Use "
- "static_cast/const_cast/reinterpret_cast "
- "instead.");
- // FIXME: Suggest appropriate C++ cast. See [expr.cast] for cast notation
- // semantics.
+ QualType SourceType =
+ CastExpr->getSubExprAsWritten()->getType().getCanonicalType();
+ QualType DestType = CastExpr->getTypeAsWritten().getCanonicalType();
+
+ auto ParenRange = CharSourceRange::getTokenRange(CastExpr->getLParenLoc(),
+ CastExpr->getRParenLoc());
+ if (SourceType == DestType) {
+ diag(CastExpr->getLocStart(), "Redundant cast to the same type.")
+ << FixItHint::CreateRemoval(ParenRange);
+ return;
+ }
+
+ std::string DestTypeString = CastExpr->getTypeAsWritten().getAsString();
+
+ auto diag_builder =
+ diag(CastExpr->getLocStart(), "C-style casts are discouraged. %0");
+
+ auto ReplaceWithCast = [&](StringRef CastType) {
+ diag_builder << ("Use " + CastType + ".").str()
+ << FixItHint::CreateReplacement(
+ ParenRange,
+ (CastType + "<" + DestTypeString + ">(").str())
+ << FixItHint::CreateInsertion(
+ Lexer::getLocForEndOfToken(
+ CastExpr->getSubExprAsWritten()->getLocEnd(), 0,
+ *Result.SourceManager,
+ Result.Context->getLangOpts()),
+ ")");
+ };
+ // Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics.
+ switch (CastExpr->getCastKind()) {
+ case CK_NoOp:
+ if (needConstCast(SourceType, DestType) &&
+ pointedTypesAreEqual(SourceType, DestType)) {
+ ReplaceWithCast("const_cast");
+ return;
+ }
+ if (DestType->isReferenceType() &&
+ (SourceType.getNonReferenceType() ==
+ DestType.getNonReferenceType().withConst() ||
+ SourceType.getNonReferenceType() == DestType.getNonReferenceType())) {
+ ReplaceWithCast("const_cast");
+ return;
+ }
+ if (SourceType->isBuiltinType() && DestType->isBuiltinType()) {
+ ReplaceWithCast("static_cast");
+ return;
+ }
+ break;
+ case CK_BitCast:
+ // FIXME: Suggest const_cast<...>(reinterpret_cast<...>(...)) replacement.
+ if (!needConstCast(SourceType, DestType)) {
+ ReplaceWithCast("reinterpret_cast");
+ return;
+ }
+ default:
+ break;
+ }
+
+ diag_builder << "Use static_cast/const_cast/reinterpret_cast.";
}
} // namespace readability
Index: test/clang-tidy/avoid-c-style-casts.cpp
===================================================================
--- test/clang-tidy/avoid-c-style-casts.cpp
+++ test/clang-tidy/avoid-c-style-casts.cpp
@@ -1,23 +1,64 @@
-// 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]
+
+ 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-NOT: warning:
+ // CHECK-MESSAGES-NOT: warning:
int b2 = int(b);
int b3 = static_cast<double>(b);
int b4 = b;
double aa = a;
(void)b2;
return (void)g();
}
-// CHECK-NOT: warning:
enum E { E1 = 1 };
template <E e>
struct A { static const E ee = e; };
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits