2014-06-30 06:19:53 +08:00
|
|
|
//===--- AvoidCStyleCastsCheck.cpp - clang-tidy -----------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AvoidCStyleCastsCheck.h"
|
2014-07-14 15:37:05 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2014-06-30 06:19:53 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
2014-07-14 15:37:05 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2014-06-30 06:19:53 +08:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace readability {
|
|
|
|
|
|
|
|
void
|
|
|
|
AvoidCStyleCastsCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
|
|
|
|
Finder->addMatcher(
|
|
|
|
cStyleCastExpr(
|
|
|
|
// Filter out (EnumType)IntegerLiteral construct, which is generated
|
|
|
|
// for non-type template arguments of enum types.
|
|
|
|
// FIXME: Remove this once this is fixed in the AST.
|
2014-07-14 15:37:05 +08:00
|
|
|
unless(hasParent(substNonTypeTemplateParmExpr())),
|
|
|
|
// Avoid matches in template instantiations.
|
2014-09-03 21:30:28 +08:00
|
|
|
unless(isInTemplateInstantiation())).bind("cast"),
|
2014-06-30 06:19:53 +08:00
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
2014-07-14 15:37:05 +08:00
|
|
|
bool needsConstCast(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();
|
|
|
|
}
|
2014-07-16 21:38:48 +08:00
|
|
|
|
2014-06-30 06:19:53 +08:00
|
|
|
void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("cast");
|
|
|
|
|
2014-08-16 08:53:20 +08:00
|
|
|
auto ParenRange = CharSourceRange::getTokenRange(CastExpr->getLParenLoc(),
|
|
|
|
CastExpr->getRParenLoc());
|
|
|
|
// Ignore casts in macros.
|
|
|
|
if (ParenRange.getBegin().isMacroID() || ParenRange.getEnd().isMacroID())
|
|
|
|
return;
|
|
|
|
|
2014-06-30 06:19:53 +08:00
|
|
|
// Casting to void is an idiomatic way to mute "unused variable" and similar
|
|
|
|
// warnings.
|
|
|
|
if (CastExpr->getTypeAsWritten()->isVoidType())
|
|
|
|
return;
|
|
|
|
|
2014-07-14 15:37:05 +08:00
|
|
|
QualType SourceType =
|
|
|
|
CastExpr->getSubExprAsWritten()->getType().getCanonicalType();
|
|
|
|
QualType DestType = CastExpr->getTypeAsWritten().getCanonicalType();
|
|
|
|
|
|
|
|
if (SourceType == DestType) {
|
|
|
|
diag(CastExpr->getLocStart(), "Redundant cast to the same type.")
|
|
|
|
<< FixItHint::CreateRemoval(ParenRange);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-22 07:39:28 +08:00
|
|
|
// The rest of this check is only relevant to C++.
|
|
|
|
if (!Result.Context->getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
2014-10-01 23:50:31 +08:00
|
|
|
// Leave type spelling exactly as it was (unlike
|
|
|
|
// getTypeAsWritten().getAsString() which would spell enum types 'enum X').
|
2014-10-01 20:47:53 +08:00
|
|
|
StringRef DestTypeString = Lexer::getSourceText(
|
|
|
|
CharSourceRange::getTokenRange(
|
|
|
|
CastExpr->getLParenLoc().getLocWithOffset(1),
|
|
|
|
CastExpr->getRParenLoc().getLocWithOffset(-1)),
|
|
|
|
*Result.SourceManager, Result.Context->getLangOpts());
|
2014-07-14 15:37:05 +08:00
|
|
|
|
|
|
|
auto diag_builder =
|
|
|
|
diag(CastExpr->getLocStart(), "C-style casts are discouraged. %0");
|
|
|
|
|
|
|
|
auto ReplaceWithCast = [&](StringRef CastType) {
|
|
|
|
diag_builder << ("Use " + CastType + ".").str();
|
2014-07-16 21:38:48 +08:00
|
|
|
|
|
|
|
const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();
|
|
|
|
std::string CastText = (CastType + "<" + DestTypeString + ">").str();
|
|
|
|
if (!isa<ParenExpr>(SubExpr)) {
|
|
|
|
CastText.push_back('(');
|
|
|
|
diag_builder << FixItHint::CreateInsertion(
|
|
|
|
Lexer::getLocForEndOfToken(SubExpr->getLocEnd(), 0,
|
|
|
|
*Result.SourceManager,
|
|
|
|
Result.Context->getLangOpts()),
|
|
|
|
")");
|
|
|
|
}
|
|
|
|
diag_builder << FixItHint::CreateReplacement(ParenRange, CastText);
|
2014-07-14 15:37:05 +08:00
|
|
|
};
|
|
|
|
// Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics.
|
|
|
|
switch (CastExpr->getCastKind()) {
|
|
|
|
case CK_NoOp:
|
|
|
|
if (needsConstCast(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;
|
|
|
|
}
|
2014-10-01 20:47:53 +08:00
|
|
|
// 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())) {
|
2014-07-14 15:37:05 +08:00
|
|
|
ReplaceWithCast("static_cast");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CK_BitCast:
|
|
|
|
// FIXME: Suggest const_cast<...>(reinterpret_cast<...>(...)) replacement.
|
|
|
|
if (!needsConstCast(SourceType, DestType)) {
|
|
|
|
ReplaceWithCast("reinterpret_cast");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
diag_builder << "Use static_cast/const_cast/reinterpret_cast.";
|
2014-06-30 06:19:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace readability
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|