2016-10-21 00:08:03 +08:00
|
|
|
//===--- RedundantMemberInitCheck.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-10-21 00:08:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "RedundantMemberInitCheck.h"
|
2016-11-08 15:50:19 +08:00
|
|
|
#include "../utils/Matchers.h"
|
2016-10-21 00:08:03 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
using namespace clang::tidy::matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace readability {
|
|
|
|
|
2019-11-19 23:57:16 +08:00
|
|
|
void RedundantMemberInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "IgnoreBaseInCopyConstructors",
|
|
|
|
IgnoreBaseInCopyConstructors);
|
|
|
|
}
|
|
|
|
|
2016-10-21 00:08:03 +08:00
|
|
|
void RedundantMemberInitCheck::registerMatchers(MatchFinder *Finder) {
|
2016-12-28 06:14:40 +08:00
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
2016-10-21 00:08:03 +08:00
|
|
|
auto Construct =
|
|
|
|
cxxConstructExpr(
|
|
|
|
hasDeclaration(cxxConstructorDecl(hasParent(
|
|
|
|
cxxRecordDecl(unless(isTriviallyDefaultConstructible()))))))
|
|
|
|
.bind("construct");
|
|
|
|
|
|
|
|
Finder->addMatcher(
|
|
|
|
cxxConstructorDecl(
|
|
|
|
unless(isDelegatingConstructor()),
|
|
|
|
ofClass(unless(
|
|
|
|
anyOf(isUnion(), ast_matchers::isTemplateInstantiation()))),
|
|
|
|
forEachConstructorInitializer(
|
2019-11-19 23:57:16 +08:00
|
|
|
cxxCtorInitializer(
|
|
|
|
isWritten(), withInitializer(ignoringImplicit(Construct)),
|
|
|
|
unless(forField(hasType(isConstQualified()))),
|
|
|
|
unless(forField(hasParent(recordDecl(isUnion())))))
|
|
|
|
.bind("init")))
|
|
|
|
.bind("constructor"),
|
2016-10-21 00:08:03 +08:00
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantMemberInitCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *Init = Result.Nodes.getNodeAs<CXXCtorInitializer>("init");
|
|
|
|
const auto *Construct = Result.Nodes.getNodeAs<CXXConstructExpr>("construct");
|
2019-11-19 23:57:16 +08:00
|
|
|
const auto *ConstructorDecl =
|
|
|
|
Result.Nodes.getNodeAs<CXXConstructorDecl>("constructor");
|
|
|
|
|
|
|
|
if (IgnoreBaseInCopyConstructors && ConstructorDecl->isCopyConstructor() &&
|
|
|
|
Init->isBaseInitializer())
|
|
|
|
return;
|
2016-10-21 00:08:03 +08:00
|
|
|
|
|
|
|
if (Construct->getNumArgs() == 0 ||
|
|
|
|
Construct->getArg(0)->isDefaultArgument()) {
|
|
|
|
if (Init->isAnyMemberInitializer()) {
|
|
|
|
diag(Init->getSourceLocation(), "initializer for member %0 is redundant")
|
2017-08-01 17:54:05 +08:00
|
|
|
<< Init->getAnyMember()
|
2016-10-21 00:08:03 +08:00
|
|
|
<< FixItHint::CreateRemoval(Init->getSourceRange());
|
|
|
|
} else {
|
|
|
|
diag(Init->getSourceLocation(),
|
|
|
|
"initializer for base class %0 is redundant")
|
2016-11-16 01:49:00 +08:00
|
|
|
<< Construct->getType()
|
2016-10-21 00:08:03 +08:00
|
|
|
<< FixItHint::CreateRemoval(Init->getSourceRange());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace readability
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|