clang-tools  10.0.0
UndelegatedConstructorCheck.cpp
Go to the documentation of this file.
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 
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 {
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.
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 
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  // Only register the matchers for C++11; the functionality currently does not
58  // provide any benefit to other languages, despite being benign.
59  if (!getLangOpts().CPlusPlus11)
60  return;
61 
62  Finder->addMatcher(
63  compoundStmt(
64  hasParent(
65  cxxConstructorDecl(ofClass(cxxRecordDecl().bind("parent")))),
66  forEach(ignoringTemporaryExpr(
67  cxxConstructExpr(hasDeclaration(cxxConstructorDecl(ofClass(
68  cxxRecordDecl(baseOfBoundNode("parent"))))))
69  .bind("construct"))),
70  unless(isInTemplateInstantiation())),
71  this);
72 }
73 
74 void UndelegatedConstructorCheck::check(
75  const MatchFinder::MatchResult &Result) {
76  const auto *E = Result.Nodes.getNodeAs<CXXConstructExpr>("construct");
77  diag(E->getBeginLoc(), "did you intend to call a delegated constructor? "
78  "A temporary object is created here instead");
79 }
80 
81 } // namespace bugprone
82 } // namespace tidy
83 } // namespace clang
CodeCompletionBuilder Builder
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const Expr * E
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)