Kaydet (Commit) afcac82e authored tarafından Stephan Bergmann's avatar Stephan Bergmann

loplugin:cstylecast: warn about casts involving incomplete types

...the worst kind of all

Change-Id: I6b98a324735a79ed9060003b491acce733f74f06
üst 7e174e7e
...@@ -17,6 +17,13 @@ ...@@ -17,6 +17,13 @@
namespace { namespace {
QualType resolvePointers(QualType type) {
while (type->isPointerType()) {
type = type->getAs<PointerType>()->getPointeeType();
}
return type;
}
class CStyleCast: class CStyleCast:
public RecursiveASTVisitor<CStyleCast>, public loplugin::Plugin public RecursiveASTVisitor<CStyleCast>, public loplugin::Plugin
{ {
...@@ -58,9 +65,25 @@ bool CStyleCast::VisitCStyleCastExpr(const CStyleCastExpr * expr) { ...@@ -58,9 +65,25 @@ bool CStyleCast::VisitCStyleCastExpr(const CStyleCastExpr * expr) {
if( expr->getCastKind() == CK_NoOp ) { if( expr->getCastKind() == CK_NoOp ) {
return true; return true;
} }
// ignore pointer-type conversions for now std::string incompFrom;
std::string incompTo;
if( expr->getCastKind() == CK_BitCast ) { if( expr->getCastKind() == CK_BitCast ) {
return true; QualType t1 = resolvePointers(expr->getSubExprAsWritten()->getType());
QualType t2 = resolvePointers(expr->getType());
// Ignore "safe" casts for now that do not involve incomplete types (and
// can thus not be interpreted as either a static_cast or a
// reinterpret_cast, with potentially different results):
if (t1->isVoidType() || t2->isVoidType()
|| !(t1->isIncompleteType() || t2->isIncompleteType()))
{
return true;
}
if (t1->isIncompleteType()) {
incompFrom = "incomplete ";
}
if (t2->isIncompleteType()) {
incompTo = "incomplete ";
}
} }
// ignore stuff from inside templates for now // ignore stuff from inside templates for now
if( expr->getCastKind() == CK_Dependent ) { if( expr->getCastKind() == CK_Dependent ) {
...@@ -86,11 +109,11 @@ bool CStyleCast::VisitCStyleCastExpr(const CStyleCastExpr * expr) { ...@@ -86,11 +109,11 @@ bool CStyleCast::VisitCStyleCastExpr(const CStyleCastExpr * expr) {
} }
report( report(
DiagnosticsEngine::Warning, DiagnosticsEngine::Warning,
"c-style cast, type=%0, from=%1, to=%2, recommendedFix=%3", "c-style cast, type=%0, from=%1%2, to=%3%4, recommendedFix=%5",
expr->getSourceRange().getBegin()) expr->getSourceRange().getBegin())
<< expr->getCastKind() << expr->getCastKind()
<< expr->getSubExprAsWritten()->getType() << incompFrom << expr->getSubExprAsWritten()->getType()
<< expr->getType() << incompTo << expr->getType()
<< recommendedFix(expr->getCastKind()) << recommendedFix(expr->getCastKind())
<< expr->getSourceRange(); << expr->getSourceRange();
return true; return true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment