2014-07-31 17:58:52 +08:00
|
|
|
//===--- UndelegatedConstructor.cpp - clang-tidy --------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "UndelegatedConstructor.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
2015-06-17 21:11:37 +08:00
|
|
|
namespace {
|
|
|
|
AST_MATCHER_P(Stmt, ignoringTemporaryExpr,
|
|
|
|
ast_matchers::internal::Matcher<Stmt>, InnerMatcher) {
|
2014-07-31 17:58:52 +08:00
|
|
|
const Stmt *E = &Node;
|
|
|
|
for (;;) {
|
|
|
|
// Temporaries with non-trivial dtors.
|
|
|
|
if (const auto *EWC = dyn_cast<ExprWithCleanups>(E))
|
|
|
|
E = EWC->getSubExpr();
|
|
|
|
// Temporaries with zero or more than two ctor arguments.
|
|
|
|
else if (const auto *BTE = dyn_cast<CXXBindTemporaryExpr>(E))
|
|
|
|
E = BTE->getSubExpr();
|
|
|
|
// Temporaries with exactly one ctor argument.
|
|
|
|
else if (const auto *FCE = dyn_cast<CXXFunctionalCastExpr>(E))
|
|
|
|
E = FCE->getSubExpr();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return InnerMatcher.matches(*E, Finder, Builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finds a node if it's a base of an already bound node.
|
|
|
|
AST_MATCHER_P(CXXRecordDecl, baseOfBoundNode, std::string, ID) {
|
2015-06-17 21:11:37 +08:00
|
|
|
return Builder->removeBindings(
|
|
|
|
[&](const ast_matchers::internal::BoundNodesMap &Nodes) {
|
|
|
|
const auto *Derived = Nodes.getNodeAs<CXXRecordDecl>(ID);
|
|
|
|
return Derived != &Node && !Derived->isDerivedFrom(&Node);
|
|
|
|
});
|
2014-07-31 17:58:52 +08:00
|
|
|
}
|
2015-06-17 21:11:37 +08:00
|
|
|
} // namespace
|
2014-07-31 17:58:52 +08:00
|
|
|
|
|
|
|
namespace tidy {
|
2015-03-02 20:25:03 +08:00
|
|
|
namespace misc {
|
2014-07-31 17:58:52 +08:00
|
|
|
|
|
|
|
void UndelegatedConstructorCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
// We look for calls to constructors of the same type in constructors. To do
|
|
|
|
// this we have to look through a variety of nodes that occur in the path,
|
|
|
|
// depending on the type's destructor and the number of arguments on the
|
|
|
|
// constructor call, this is handled by ignoringTemporaryExpr. Ignore template
|
|
|
|
// instantiations to reduce the number of duplicated warnings.
|
2015-08-29 03:27:19 +08:00
|
|
|
//
|
|
|
|
// Only register the matchers for C++11; the functionality currently does not
|
|
|
|
// provide any benefit to other languages, despite being benign.
|
2015-08-31 23:28:57 +08:00
|
|
|
if (!getLangOpts().CPlusPlus11)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Finder->addMatcher(
|
|
|
|
compoundStmt(
|
2015-09-17 21:31:25 +08:00
|
|
|
hasParent(
|
|
|
|
cxxConstructorDecl(ofClass(cxxRecordDecl().bind("parent")))),
|
2015-08-31 23:28:57 +08:00
|
|
|
forEach(ignoringTemporaryExpr(
|
2015-09-17 21:31:25 +08:00
|
|
|
cxxConstructExpr(hasDeclaration(cxxConstructorDecl(ofClass(
|
|
|
|
cxxRecordDecl(baseOfBoundNode("parent"))))))
|
2015-08-31 23:28:57 +08:00
|
|
|
.bind("construct"))),
|
|
|
|
unless(isInTemplateInstantiation())),
|
|
|
|
this);
|
2014-07-31 17:58:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UndelegatedConstructorCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *E = Result.Nodes.getStmtAs<CXXConstructExpr>("construct");
|
|
|
|
diag(E->getLocStart(), "did you intend to call a delegated constructor? "
|
|
|
|
"A temporary object is created here instead");
|
|
|
|
}
|
|
|
|
|
2015-03-02 20:25:03 +08:00
|
|
|
} // namespace misc
|
2014-07-31 17:58:52 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|