1 //===--- UndelegatedConstructorCheck.cpp - clang-tidy --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "UndelegatedConstructorCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/Lex/Lexer.h"
12
13 using namespace clang::ast_matchers;
14
15 namespace clang {
16 namespace tidy {
17 namespace bugprone {
18
19 namespace {
AST_MATCHER_P(Stmt,ignoringTemporaryExpr,ast_matchers::internal::Matcher<Stmt>,InnerMatcher)20 AST_MATCHER_P(Stmt, ignoringTemporaryExpr,
21 ast_matchers::internal::Matcher<Stmt>, InnerMatcher) {
22 const Stmt *E = &Node;
23 for (;;) {
24 // Temporaries with non-trivial dtors.
25 if (const auto *EWC = dyn_cast<ExprWithCleanups>(E))
26 E = EWC->getSubExpr();
27 // Temporaries with zero or more than two ctor arguments.
28 else if (const auto *BTE = dyn_cast<CXXBindTemporaryExpr>(E))
29 E = BTE->getSubExpr();
30 // Temporaries with exactly one ctor argument.
31 else if (const auto *FCE = dyn_cast<CXXFunctionalCastExpr>(E))
32 E = FCE->getSubExpr();
33 else
34 break;
35 }
36
37 return InnerMatcher.matches(*E, Finder, Builder);
38 }
39
40 // Finds a node if it's a base of an already bound node.
AST_MATCHER_P(CXXRecordDecl,baseOfBoundNode,std::string,ID)41 AST_MATCHER_P(CXXRecordDecl, baseOfBoundNode, std::string, ID) {
42 return Builder->removeBindings(
43 [&](const ast_matchers::internal::BoundNodesMap &Nodes) {
44 const auto *Derived = Nodes.getNodeAs<CXXRecordDecl>(ID);
45 return Derived != &Node && !Derived->isDerivedFrom(&Node);
46 });
47 }
48 } // namespace
49
registerMatchers(MatchFinder * Finder)50 void UndelegatedConstructorCheck::registerMatchers(MatchFinder *Finder) {
51 // We look for calls to constructors of the same type in constructors. To do
52 // this we have to look through a variety of nodes that occur in the path,
53 // depending on the type's destructor and the number of arguments on the
54 // constructor call, this is handled by ignoringTemporaryExpr. Ignore template
55 // instantiations to reduce the number of duplicated warnings.
56
57 Finder->addMatcher(
58 traverse(
59 ast_type_traits::TK_AsIs,
60 compoundStmt(hasParent(cxxConstructorDecl(
61 ofClass(cxxRecordDecl().bind("parent")))),
62 forEach(ignoringTemporaryExpr(
63 cxxConstructExpr(
64 hasDeclaration(cxxConstructorDecl(ofClass(
65 cxxRecordDecl(baseOfBoundNode("parent"))))))
66 .bind("construct"))),
67 unless(isInTemplateInstantiation()))),
68 this);
69 }
70
check(const MatchFinder::MatchResult & Result)71 void UndelegatedConstructorCheck::check(
72 const MatchFinder::MatchResult &Result) {
73 const auto *E = Result.Nodes.getNodeAs<CXXConstructExpr>("construct");
74 diag(E->getBeginLoc(), "did you intend to call a delegated constructor? "
75 "A temporary object is created here instead");
76 }
77
78 } // namespace bugprone
79 } // namespace tidy
80 } // namespace clang
81