clang-tools  10.0.0git
UnusedRaiiCheck.cpp
Go to the documentation of this file.
1 //===--- UnusedRaiiCheck.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 "UnusedRaiiCheck.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 {
20 AST_MATCHER(CXXRecordDecl, hasNonTrivialDestructor) {
21  // TODO: If the dtor is there but empty we don't want to warn either.
22  return Node.hasDefinition() && Node.hasNonTrivialDestructor();
23 }
24 } // namespace
25 
26 void UnusedRaiiCheck::registerMatchers(MatchFinder *Finder) {
27  // Only register the matchers for C++; the functionality currently does not
28  // provide any benefit to other languages, despite being benign.
29  if (!getLangOpts().CPlusPlus)
30  return;
31 
32  // Look for temporaries that are constructed in-place and immediately
33  // destroyed. Look for temporaries created by a functional cast but not for
34  // those returned from a call.
35  auto BindTemp =
36  cxxBindTemporaryExpr(unless(has(ignoringParenImpCasts(callExpr()))))
37  .bind("temp");
38  Finder->addMatcher(
39  exprWithCleanups(unless(isInTemplateInstantiation()),
40  hasParent(compoundStmt().bind("compound")),
41  hasType(cxxRecordDecl(hasNonTrivialDestructor())),
42  anyOf(has(ignoringParenImpCasts(BindTemp)),
43  has(ignoringParenImpCasts(cxxFunctionalCastExpr(
44  has(ignoringParenImpCasts(BindTemp)))))))
45  .bind("expr"),
46  this);
47 }
48 
49 void UnusedRaiiCheck::check(const MatchFinder::MatchResult &Result) {
50  const auto *E = Result.Nodes.getNodeAs<Expr>("expr");
51 
52  // We ignore code expanded from macros to reduce the number of false
53  // positives.
54  if (E->getBeginLoc().isMacroID())
55  return;
56 
57  // Don't emit a warning for the last statement in the surrounding compund
58  // statement.
59  const auto *CS = Result.Nodes.getNodeAs<CompoundStmt>("compound");
60  if (E == CS->body_back())
61  return;
62 
63  // Emit a warning.
64  auto D = diag(E->getBeginLoc(), "object destroyed immediately after "
65  "creation; did you mean to name the object?");
66  const char *Replacement = " give_me_a_name";
67 
68  // If this is a default ctor we have to remove the parens or we'll introduce a
69  // most vexing parse.
70  const auto *BTE = Result.Nodes.getNodeAs<CXXBindTemporaryExpr>("temp");
71  if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(BTE->getSubExpr()))
72  if (TOE->getNumArgs() == 0) {
73  D << FixItHint::CreateReplacement(
74  CharSourceRange::getTokenRange(TOE->getParenOrBraceRange()),
75  Replacement);
76  return;
77  }
78 
79  // Otherwise just suggest adding a name. To find the place to insert the name
80  // find the first TypeLoc in the children of E, which always points to the
81  // written type.
82  auto Matches =
83  match(expr(hasDescendant(typeLoc().bind("t"))), *E, *Result.Context);
84  const auto *TL = selectFirst<TypeLoc>("t", Matches);
85  D << FixItHint::CreateInsertion(
86  Lexer::getLocForEndOfToken(TL->getEndLoc(), 0, *Result.SourceManager,
87  getLangOpts()),
88  Replacement);
89 }
90 
91 } // namespace bugprone
92 } // namespace tidy
93 } // namespace clang
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:94
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
Definition: SourceCode.cpp:227
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const Expr * E