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 {
|
2015-03-05 21:46:14 +08:00
|
|
|
namespace google {
|
2014-06-30 06:19:53 +08:00
|
|
|
namespace readability {
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
void AvoidCStyleCastsCheck::registerMatchers(
|
|
|
|
ast_matchers::MatchFinder *Finder) {
|
2014-06-30 06:19:53 +08:00
|
|
|
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.
|
2016-11-08 15:50:19 +08:00
|
|
|
unless(isInTemplateInstantiation()))
|
|
|
|
.bind("cast"),
|
2014-06-30 06:19:53 +08:00
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
2015-03-23 20:49:15 +08:00
|
|
|
static bool needsConstCast(QualType SourceType, QualType DestType) {
|
2017-03-02 23:27:34 +08:00
|
|
|
while ((SourceType->isPointerType() && DestType->isPointerType()) ||
|
|
|
|
(SourceType->isReferenceType() && DestType->isReferenceType())) {
|
|
|
|
SourceType = SourceType->getPointeeType();
|
|
|
|
DestType = DestType->getPointeeType();
|
2017-02-18 17:45:00 +08:00
|
|
|
if (SourceType.isConstQualified() && !DestType.isConstQualified()) {
|
|
|
|
return (SourceType->isPointerType() == DestType->isPointerType()) &&
|
|
|
|
(SourceType->isReferenceType() == DestType->isReferenceType());
|
|
|
|
}
|
2014-07-14 15:37:05 +08:00
|
|
|
}
|
2017-03-02 23:27:34 +08:00
|
|
|
return false;
|
2014-07-14 15:37:05 +08:00
|
|
|
}
|
|
|
|
|
2017-02-18 17:45:00 +08:00
|
|
|
static bool pointedUnqualifiedTypesAreEqual(QualType T1, QualType T2) {
|
|
|
|
while ((T1->isPointerType() && T2->isPointerType()) ||
|
|
|
|
(T1->isReferenceType() && T2->isReferenceType())) {
|
|
|
|
T1 = T1->getPointeeType();
|
|
|
|
T2 = T2->getPointeeType();
|
2014-07-14 15:37:05 +08:00
|
|
|
}
|
2017-02-18 17:45:00 +08:00
|
|
|
return T1.getUnqualifiedType() == T2.getUnqualifiedType();
|
2014-07-14 15:37:05 +08:00
|
|
|
}
|
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");
|
2017-07-13 00:38:59 +08:00
|
|
|
|
2014-08-16 08:53:20 +08:00
|
|
|
// Ignore casts in macros.
|
2017-07-13 00:38:59 +08:00
|
|
|
if (CastExpr->getExprLoc().isMacroID())
|
2014-08-16 08:53:20 +08:00
|
|
|
return;
|
|
|
|
|
2014-06-30 06:19:53 +08:00
|
|
|
// Casting to void is an idiomatic way to mute "unused variable" and similar
|
|
|
|
// warnings.
|
2017-02-18 17:45:00 +08:00
|
|
|
if (CastExpr->getCastKind() == CK_ToVoid)
|
2014-06-30 06:19:53 +08:00
|
|
|
return;
|
|
|
|
|
2017-02-10 22:57:19 +08:00
|
|
|
auto isFunction = [](QualType T) {
|
|
|
|
T = T.getCanonicalType().getNonReferenceType();
|
|
|
|
return T->isFunctionType() || T->isFunctionPointerType() ||
|
|
|
|
T->isMemberFunctionPointerType();
|
|
|
|
};
|
|
|
|
|
2017-03-02 23:47:28 +08:00
|
|
|
const QualType DestTypeAsWritten =
|
|
|
|
CastExpr->getTypeAsWritten().getUnqualifiedType();
|
|
|
|
const QualType SourceTypeAsWritten =
|
|
|
|
CastExpr->getSubExprAsWritten()->getType().getUnqualifiedType();
|
|
|
|
const QualType SourceType = SourceTypeAsWritten.getCanonicalType();
|
|
|
|
const QualType DestType = DestTypeAsWritten.getCanonicalType();
|
2017-02-18 17:45:00 +08:00
|
|
|
|
2017-07-13 00:38:59 +08:00
|
|
|
auto ReplaceRange = CharSourceRange::getCharRange(
|
2018-08-10 06:42:26 +08:00
|
|
|
CastExpr->getLParenLoc(), CastExpr->getSubExprAsWritten()->getBeginLoc());
|
2017-07-13 00:38:59 +08:00
|
|
|
|
2017-02-18 17:45:00 +08:00
|
|
|
bool FnToFnCast =
|
|
|
|
isFunction(SourceTypeAsWritten) && isFunction(DestTypeAsWritten);
|
|
|
|
|
|
|
|
if (CastExpr->getCastKind() == CK_NoOp && !FnToFnCast) {
|
|
|
|
// Function pointer/reference casts may be needed to resolve ambiguities in
|
|
|
|
// case of overloaded functions, so detection of redundant casts is trickier
|
|
|
|
// in this case. Don't emit "redundant cast" warnings for function
|
|
|
|
// pointer/reference types.
|
|
|
|
if (SourceTypeAsWritten == DestTypeAsWritten) {
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(CastExpr->getBeginLoc(), "redundant cast to the same type")
|
2017-07-13 00:38:59 +08:00
|
|
|
<< FixItHint::CreateRemoval(ReplaceRange);
|
2017-02-18 17:45:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-01-29 23:17:13 +08:00
|
|
|
}
|
|
|
|
|
2014-09-22 07:39:28 +08:00
|
|
|
// The rest of this check is only relevant to C++.
|
[clang-tidy/google-readability-casting] Disable check for Objective-C++
Summary:
Previously, `google-readability-casting` was disabled for Objective-C.
The Google Objective-C++ style allows both Objective-C and
C++ style in the same file. Since clang-tidy doesn't have a good
way to allow multiple styles per file, this disables the
check for Objective-C++.
Test Plan: New tests added. Ran tests with:
% make -j16 check-clang-tools
Before diff, confirmed tests failed:
https://reviews.llvm.org/P8081
After diff, confirrmed tests passed.
Reviewers: alexfh, Wizard, hokein, stephanemoore
Reviewed By: alexfh, Wizard, stephanemoore
Subscribers: stephanemoore, cfe-commits, bkramer, klimek
Differential Revision: https://reviews.llvm.org/D46659
llvm-svn: 332516
2018-05-17 04:07:19 +08:00
|
|
|
// We also disable it for Objective-C++.
|
|
|
|
if (!getLangOpts().CPlusPlus || getLangOpts().ObjC1 || getLangOpts().ObjC2)
|
2014-09-22 07:39:28 +08:00
|
|
|
return;
|
2015-01-29 23:17:13 +08:00
|
|
|
// Ignore code inside extern "C" {} blocks.
|
|
|
|
if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context)
|
|
|
|
.empty())
|
|
|
|
return;
|
2015-05-21 22:08:56 +08:00
|
|
|
// Ignore code in .c files and headers included from them, even if they are
|
|
|
|
// compiled as C++.
|
|
|
|
if (getCurrentMainFile().endswith(".c"))
|
|
|
|
return;
|
2017-03-02 23:27:34 +08:00
|
|
|
|
|
|
|
SourceManager &SM = *Result.SourceManager;
|
|
|
|
|
2015-05-26 18:47:48 +08:00
|
|
|
// Ignore code in .c files #included in other files (which shouldn't be done,
|
|
|
|
// but people still do this for test and other purposes).
|
2018-08-10 06:42:26 +08:00
|
|
|
if (SM.getFilename(SM.getSpellingLoc(CastExpr->getBeginLoc())).endswith(".c"))
|
2015-05-26 18:47:48 +08:00
|
|
|
return;
|
2014-09-22 07:39:28 +08:00
|
|
|
|
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').
|
2015-05-26 18:47:48 +08:00
|
|
|
StringRef DestTypeString =
|
|
|
|
Lexer::getSourceText(CharSourceRange::getTokenRange(
|
|
|
|
CastExpr->getLParenLoc().getLocWithOffset(1),
|
|
|
|
CastExpr->getRParenLoc().getLocWithOffset(-1)),
|
2016-09-24 10:13:45 +08:00
|
|
|
SM, getLangOpts());
|
2014-07-14 15:37:05 +08:00
|
|
|
|
2017-02-10 22:57:19 +08:00
|
|
|
auto Diag =
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(CastExpr->getBeginLoc(), "C-style casts are discouraged; use %0");
|
2014-07-14 15:37:05 +08:00
|
|
|
|
2017-02-18 17:45:00 +08:00
|
|
|
auto ReplaceWithCast = [&](std::string CastText) {
|
2014-07-16 21:38:48 +08:00
|
|
|
const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();
|
|
|
|
if (!isa<ParenExpr>(SubExpr)) {
|
|
|
|
CastText.push_back('(');
|
2017-02-10 22:57:19 +08:00
|
|
|
Diag << FixItHint::CreateInsertion(
|
2015-05-26 18:47:48 +08:00
|
|
|
Lexer::getLocForEndOfToken(SubExpr->getLocEnd(), 0, SM,
|
2016-09-24 10:13:45 +08:00
|
|
|
getLangOpts()),
|
2014-07-16 21:38:48 +08:00
|
|
|
")");
|
|
|
|
}
|
2017-07-13 00:38:59 +08:00
|
|
|
Diag << FixItHint::CreateReplacement(ReplaceRange, CastText);
|
2014-07-14 15:37:05 +08:00
|
|
|
};
|
2017-02-18 17:45:00 +08:00
|
|
|
auto ReplaceWithNamedCast = [&](StringRef CastType) {
|
|
|
|
Diag << CastType;
|
2017-03-02 23:27:34 +08:00
|
|
|
ReplaceWithCast((CastType + "<" + DestTypeString + ">").str());
|
2017-02-18 17:45:00 +08:00
|
|
|
};
|
2015-01-29 23:17:13 +08:00
|
|
|
|
2014-07-14 15:37:05 +08:00
|
|
|
// Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics.
|
|
|
|
switch (CastExpr->getCastKind()) {
|
2017-02-10 22:57:19 +08:00
|
|
|
case CK_FunctionToPointerDecay:
|
2017-02-18 17:45:00 +08:00
|
|
|
ReplaceWithNamedCast("static_cast");
|
|
|
|
return;
|
|
|
|
case CK_ConstructorConversion:
|
2017-03-03 16:18:49 +08:00
|
|
|
if (!CastExpr->getTypeAsWritten().hasQualifiers() &&
|
2017-03-02 23:27:34 +08:00
|
|
|
DestTypeAsWritten->isRecordType() &&
|
|
|
|
!DestTypeAsWritten->isElaboratedTypeSpecifier()) {
|
|
|
|
Diag << "constructor call syntax";
|
|
|
|
// FIXME: Validate DestTypeString, maybe.
|
|
|
|
ReplaceWithCast(DestTypeString.str());
|
|
|
|
} else {
|
|
|
|
ReplaceWithNamedCast("static_cast");
|
|
|
|
}
|
2017-02-10 22:57:19 +08:00
|
|
|
return;
|
2014-07-14 15:37:05 +08:00
|
|
|
case CK_NoOp:
|
2017-02-10 22:57:19 +08:00
|
|
|
if (FnToFnCast) {
|
2017-02-18 17:45:00 +08:00
|
|
|
ReplaceWithNamedCast("static_cast");
|
2017-02-10 22:57:19 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-03-02 23:27:34 +08:00
|
|
|
if (SourceType == DestType) {
|
|
|
|
Diag << "static_cast (if needed, the cast may be redundant)";
|
|
|
|
ReplaceWithCast(("static_cast<" + DestTypeString + ">").str());
|
|
|
|
return;
|
|
|
|
}
|
2014-07-14 15:37:05 +08:00
|
|
|
if (needsConstCast(SourceType, DestType) &&
|
2017-02-18 17:45:00 +08:00
|
|
|
pointedUnqualifiedTypesAreEqual(SourceType, DestType)) {
|
|
|
|
ReplaceWithNamedCast("const_cast");
|
2014-07-14 15:37:05 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-02-18 17:45:00 +08:00
|
|
|
if (DestType->isReferenceType()) {
|
|
|
|
QualType Dest = DestType.getNonReferenceType();
|
|
|
|
QualType Source = SourceType.getNonReferenceType();
|
|
|
|
if (Source == Dest.withConst() ||
|
|
|
|
SourceType.getNonReferenceType() == DestType.getNonReferenceType()) {
|
|
|
|
ReplaceWithNamedCast("const_cast");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2014-07-14 15:37:05 +08:00
|
|
|
}
|
2016-11-08 15:50:19 +08:00
|
|
|
// FALLTHROUGH
|
2014-10-01 20:47:53 +08:00
|
|
|
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())) {
|
2017-02-18 17:45:00 +08:00
|
|
|
ReplaceWithNamedCast("static_cast");
|
2014-07-14 15:37:05 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CK_BitCast:
|
|
|
|
// FIXME: Suggest const_cast<...>(reinterpret_cast<...>(...)) replacement.
|
|
|
|
if (!needsConstCast(SourceType, DestType)) {
|
2017-03-02 23:27:34 +08:00
|
|
|
if (SourceType->isVoidPointerType())
|
|
|
|
ReplaceWithNamedCast("static_cast");
|
|
|
|
else
|
|
|
|
ReplaceWithNamedCast("reinterpret_cast");
|
2014-07-14 15:37:05 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:57:19 +08:00
|
|
|
Diag << "static_cast/const_cast/reinterpret_cast";
|
2014-06-30 06:19:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace readability
|
2015-03-05 21:46:14 +08:00
|
|
|
} // namespace google
|
2014-06-30 06:19:53 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|