2016-12-21 05:26:07 +08:00
|
|
|
//===--- UseDefaultMemberInitCheck.cpp - clang-tidy------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-12-21 05:26:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "UseDefaultMemberInitCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace modernize {
|
|
|
|
|
|
|
|
static StringRef getValueOfValueInit(const QualType InitType) {
|
|
|
|
switch (InitType->getScalarTypeKind()) {
|
|
|
|
case Type::STK_CPointer:
|
|
|
|
case Type::STK_BlockPointer:
|
|
|
|
case Type::STK_ObjCObjectPointer:
|
|
|
|
case Type::STK_MemberPointer:
|
|
|
|
return "nullptr";
|
|
|
|
|
|
|
|
case Type::STK_Bool:
|
|
|
|
return "false";
|
|
|
|
|
|
|
|
case Type::STK_Integral:
|
|
|
|
switch (InitType->getAs<BuiltinType>()->getKind()) {
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
return "'\\0'";
|
|
|
|
case BuiltinType::WChar_U:
|
|
|
|
case BuiltinType::WChar_S:
|
|
|
|
return "L'\\0'";
|
|
|
|
case BuiltinType::Char16:
|
|
|
|
return "u'\\0'";
|
|
|
|
case BuiltinType::Char32:
|
|
|
|
return "U'\\0'";
|
|
|
|
default:
|
|
|
|
return "0";
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::STK_Floating:
|
|
|
|
switch (InitType->getAs<BuiltinType>()->getKind()) {
|
|
|
|
case BuiltinType::Half:
|
|
|
|
case BuiltinType::Float:
|
|
|
|
return "0.0f";
|
|
|
|
default:
|
|
|
|
return "0.0";
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::STK_FloatingComplex:
|
|
|
|
case Type::STK_IntegralComplex:
|
|
|
|
return getValueOfValueInit(
|
|
|
|
InitType->getAs<ComplexType>()->getElementType());
|
2018-10-16 04:00:03 +08:00
|
|
|
|
2018-10-16 03:59:52 +08:00
|
|
|
case Type::STK_FixedPoint:
|
|
|
|
switch (InitType->getAs<BuiltinType>()->getKind()) {
|
2018-10-16 04:00:03 +08:00
|
|
|
case BuiltinType::ShortAccum:
|
|
|
|
case BuiltinType::SatShortAccum:
|
|
|
|
return "0.0hk";
|
|
|
|
case BuiltinType::Accum:
|
|
|
|
case BuiltinType::SatAccum:
|
|
|
|
return "0.0k";
|
|
|
|
case BuiltinType::LongAccum:
|
|
|
|
case BuiltinType::SatLongAccum:
|
|
|
|
return "0.0lk";
|
|
|
|
case BuiltinType::UShortAccum:
|
|
|
|
case BuiltinType::SatUShortAccum:
|
|
|
|
return "0.0uhk";
|
|
|
|
case BuiltinType::UAccum:
|
|
|
|
case BuiltinType::SatUAccum:
|
|
|
|
return "0.0uk";
|
|
|
|
case BuiltinType::ULongAccum:
|
|
|
|
case BuiltinType::SatULongAccum:
|
|
|
|
return "0.0ulk";
|
|
|
|
case BuiltinType::ShortFract:
|
|
|
|
case BuiltinType::SatShortFract:
|
|
|
|
return "0.0hr";
|
|
|
|
case BuiltinType::Fract:
|
|
|
|
case BuiltinType::SatFract:
|
|
|
|
return "0.0r";
|
|
|
|
case BuiltinType::LongFract:
|
|
|
|
case BuiltinType::SatLongFract:
|
|
|
|
return "0.0lr";
|
|
|
|
case BuiltinType::UShortFract:
|
|
|
|
case BuiltinType::SatUShortFract:
|
|
|
|
return "0.0uhr";
|
|
|
|
case BuiltinType::UFract:
|
|
|
|
case BuiltinType::SatUFract:
|
|
|
|
return "0.0ur";
|
|
|
|
case BuiltinType::ULongFract:
|
|
|
|
case BuiltinType::SatULongFract:
|
|
|
|
return "0.0ulr";
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unhandled fixed point BuiltinType");
|
2018-10-16 03:59:52 +08:00
|
|
|
}
|
2016-12-21 05:26:07 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid scalar type kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isZero(const Expr *E) {
|
|
|
|
switch (E->getStmtClass()) {
|
|
|
|
case Stmt::CXXNullPtrLiteralExprClass:
|
|
|
|
case Stmt::ImplicitValueInitExprClass:
|
|
|
|
return true;
|
|
|
|
case Stmt::InitListExprClass:
|
|
|
|
return cast<InitListExpr>(E)->getNumInits() == 0;
|
|
|
|
case Stmt::CharacterLiteralClass:
|
|
|
|
return !cast<CharacterLiteral>(E)->getValue();
|
|
|
|
case Stmt::CXXBoolLiteralExprClass:
|
|
|
|
return !cast<CXXBoolLiteralExpr>(E)->getValue();
|
|
|
|
case Stmt::IntegerLiteralClass:
|
|
|
|
return !cast<IntegerLiteral>(E)->getValue();
|
|
|
|
case Stmt::FloatingLiteralClass: {
|
|
|
|
llvm::APFloat Value = cast<FloatingLiteral>(E)->getValue();
|
|
|
|
return Value.isZero() && !Value.isNegative();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const Expr *ignoreUnaryPlus(const Expr *E) {
|
|
|
|
auto *UnaryOp = dyn_cast<UnaryOperator>(E);
|
|
|
|
if (UnaryOp && UnaryOp->getOpcode() == UO_Plus)
|
|
|
|
return UnaryOp->getSubExpr();
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const Expr *getInitializer(const Expr *E) {
|
|
|
|
auto *InitList = dyn_cast<InitListExpr>(E);
|
|
|
|
if (InitList && InitList->getNumInits() == 1)
|
|
|
|
return InitList->getInit(0);
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool sameValue(const Expr *E1, const Expr *E2) {
|
|
|
|
E1 = ignoreUnaryPlus(getInitializer(E1->IgnoreParenImpCasts()));
|
|
|
|
E2 = ignoreUnaryPlus(getInitializer(E2->IgnoreParenImpCasts()));
|
|
|
|
|
|
|
|
if (isZero(E1) && isZero(E2))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (E1->getStmtClass() != E2->getStmtClass())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (E1->getStmtClass()) {
|
|
|
|
case Stmt::UnaryOperatorClass:
|
|
|
|
return sameValue(cast<UnaryOperator>(E1)->getSubExpr(),
|
|
|
|
cast<UnaryOperator>(E2)->getSubExpr());
|
|
|
|
case Stmt::CharacterLiteralClass:
|
|
|
|
return cast<CharacterLiteral>(E1)->getValue() ==
|
|
|
|
cast<CharacterLiteral>(E2)->getValue();
|
|
|
|
case Stmt::CXXBoolLiteralExprClass:
|
|
|
|
return cast<CXXBoolLiteralExpr>(E1)->getValue() ==
|
|
|
|
cast<CXXBoolLiteralExpr>(E2)->getValue();
|
|
|
|
case Stmt::IntegerLiteralClass:
|
|
|
|
return cast<IntegerLiteral>(E1)->getValue() ==
|
|
|
|
cast<IntegerLiteral>(E2)->getValue();
|
|
|
|
case Stmt::FloatingLiteralClass:
|
|
|
|
return cast<FloatingLiteral>(E1)->getValue().bitwiseIsEqual(
|
|
|
|
cast<FloatingLiteral>(E2)->getValue());
|
|
|
|
case Stmt::StringLiteralClass:
|
|
|
|
return cast<StringLiteral>(E1)->getString() ==
|
|
|
|
cast<StringLiteral>(E2)->getString();
|
|
|
|
case Stmt::DeclRefExprClass:
|
|
|
|
return cast<DeclRefExpr>(E1)->getDecl() == cast<DeclRefExpr>(E2)->getDecl();
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UseDefaultMemberInitCheck::UseDefaultMemberInitCheck(StringRef Name,
|
|
|
|
ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
2017-05-08 23:13:31 +08:00
|
|
|
UseAssignment(Options.get("UseAssignment", 0) != 0),
|
2017-07-20 22:56:52 +08:00
|
|
|
IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true) != 0) {}
|
2016-12-21 05:26:07 +08:00
|
|
|
|
|
|
|
void UseDefaultMemberInitCheck::storeOptions(
|
|
|
|
ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "UseAssignment", UseAssignment);
|
2017-05-08 23:13:31 +08:00
|
|
|
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
|
2016-12-21 05:26:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
if (!getLangOpts().CPlusPlus11)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto Init =
|
|
|
|
anyOf(stringLiteral(), characterLiteral(), integerLiteral(),
|
|
|
|
unaryOperator(anyOf(hasOperatorName("+"), hasOperatorName("-")),
|
|
|
|
hasUnaryOperand(integerLiteral())),
|
|
|
|
floatLiteral(),
|
|
|
|
unaryOperator(anyOf(hasOperatorName("+"), hasOperatorName("-")),
|
|
|
|
hasUnaryOperand(floatLiteral())),
|
|
|
|
cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(),
|
2017-01-05 01:33:55 +08:00
|
|
|
declRefExpr(to(enumConstantDecl())));
|
2016-12-21 05:26:07 +08:00
|
|
|
|
|
|
|
Finder->addMatcher(
|
|
|
|
cxxConstructorDecl(
|
|
|
|
isDefaultConstructor(), unless(isInstantiated()),
|
2016-12-24 22:30:29 +08:00
|
|
|
forEachConstructorInitializer(
|
2017-08-01 17:54:05 +08:00
|
|
|
cxxCtorInitializer(
|
2018-01-23 23:32:42 +08:00
|
|
|
forField(unless(anyOf(getLangOpts().CPlusPlus2a
|
|
|
|
? unless(anything())
|
|
|
|
: isBitField(),
|
2017-08-01 17:54:05 +08:00
|
|
|
hasInClassInitializer(anything()),
|
|
|
|
hasParent(recordDecl(isUnion()))))),
|
|
|
|
isWritten(), withInitializer(ignoringImplicit(Init)))
|
|
|
|
.bind("default"))),
|
2016-12-21 05:26:07 +08:00
|
|
|
this);
|
|
|
|
|
|
|
|
Finder->addMatcher(
|
|
|
|
cxxConstructorDecl(
|
|
|
|
unless(ast_matchers::isTemplateInstantiation()),
|
|
|
|
forEachConstructorInitializer(
|
2017-08-01 17:54:05 +08:00
|
|
|
cxxCtorInitializer(forField(hasInClassInitializer(anything())),
|
|
|
|
isWritten(),
|
|
|
|
withInitializer(ignoringImplicit(Init)))
|
|
|
|
.bind("existing"))),
|
2016-12-21 05:26:07 +08:00
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UseDefaultMemberInitCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
if (const auto *Default =
|
|
|
|
Result.Nodes.getNodeAs<CXXCtorInitializer>("default"))
|
|
|
|
checkDefaultInit(Result, Default);
|
|
|
|
else if (const auto *Existing =
|
|
|
|
Result.Nodes.getNodeAs<CXXCtorInitializer>("existing"))
|
|
|
|
checkExistingInit(Result, Existing);
|
|
|
|
else
|
|
|
|
llvm_unreachable("Bad Callback. No node provided.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void UseDefaultMemberInitCheck::checkDefaultInit(
|
|
|
|
const MatchFinder::MatchResult &Result, const CXXCtorInitializer *Init) {
|
2017-08-01 17:54:05 +08:00
|
|
|
const FieldDecl *Field = Init->getAnyMember();
|
2016-12-21 05:26:07 +08:00
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation StartLoc = Field->getBeginLoc();
|
2017-05-08 23:13:31 +08:00
|
|
|
if (StartLoc.isMacroID() && IgnoreMacros)
|
|
|
|
return;
|
|
|
|
|
2016-12-21 05:26:07 +08:00
|
|
|
SourceLocation FieldEnd =
|
|
|
|
Lexer::getLocForEndOfToken(Field->getSourceRange().getEnd(), 0,
|
|
|
|
*Result.SourceManager, getLangOpts());
|
|
|
|
SourceLocation LParenEnd = Lexer::getLocForEndOfToken(
|
|
|
|
Init->getLParenLoc(), 0, *Result.SourceManager, getLangOpts());
|
|
|
|
CharSourceRange InitRange =
|
|
|
|
CharSourceRange::getCharRange(LParenEnd, Init->getRParenLoc());
|
|
|
|
|
2019-02-09 03:44:42 +08:00
|
|
|
bool ValueInit = isa<ImplicitValueInitExpr>(Init->getInit());
|
|
|
|
bool CanAssign = UseAssignment && (!ValueInit || !Init->getInit()->getType()->isEnumeralType());
|
|
|
|
|
2016-12-21 05:26:07 +08:00
|
|
|
auto Diag =
|
|
|
|
diag(Field->getLocation(), "use default member initializer for %0")
|
|
|
|
<< Field
|
2019-02-09 03:44:42 +08:00
|
|
|
<< FixItHint::CreateInsertion(FieldEnd, CanAssign ? " = " : "{")
|
2016-12-21 05:26:07 +08:00
|
|
|
<< FixItHint::CreateInsertionFromRange(FieldEnd, InitRange);
|
|
|
|
|
2019-02-09 03:44:42 +08:00
|
|
|
if (CanAssign && ValueInit)
|
2016-12-21 05:26:07 +08:00
|
|
|
Diag << FixItHint::CreateInsertion(
|
|
|
|
FieldEnd, getValueOfValueInit(Init->getInit()->getType()));
|
|
|
|
|
2019-02-09 03:44:42 +08:00
|
|
|
if (!CanAssign)
|
2016-12-21 05:26:07 +08:00
|
|
|
Diag << FixItHint::CreateInsertion(FieldEnd, "}");
|
|
|
|
|
|
|
|
Diag << FixItHint::CreateRemoval(Init->getSourceRange());
|
|
|
|
}
|
|
|
|
|
|
|
|
void UseDefaultMemberInitCheck::checkExistingInit(
|
|
|
|
const MatchFinder::MatchResult &Result, const CXXCtorInitializer *Init) {
|
2019-02-05 05:09:31 +08:00
|
|
|
const FieldDecl *Field = Init->getAnyMember();
|
2016-12-21 05:26:07 +08:00
|
|
|
|
|
|
|
if (!sameValue(Field->getInClassInitializer(), Init->getInit()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
diag(Init->getSourceLocation(), "member initializer for %0 is redundant")
|
|
|
|
<< Field
|
|
|
|
<< FixItHint::CreateRemoval(Init->getSourceRange());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace modernize
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|