clang-tools  14.0.0git
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  Finder->addMatcher(
58  traverse(
59  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 
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
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
UndelegatedConstructorCheck.h
clang::ast_matchers
Definition: AbseilMatcher.h:14
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:36
clang::clangd::check
bool check(llvm::StringRef File, llvm::function_ref< bool(const Position &)> ShouldCheckLine, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:259
ID
static char ID
Definition: Logger.cpp:74
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::bugprone::AST_MATCHER_P
AST_MATCHER_P(FunctionDecl, parameterCountGE, unsigned, N)
Matches functions that have at least the specified amount of parameters.
Definition: EasilySwappableParametersCheck.cpp:1877