clang-tools  3.8.0
UnusedRAIICheck.cpp
Go to the documentation of this file.
1 //===--- UnusedRAIICheck.cpp - clang-tidy ---------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "UnusedRAIICheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/Lex/Lexer.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace {
18 AST_MATCHER(CXXRecordDecl, hasNonTrivialDestructor) {
19  // TODO: If the dtor is there but empty we don't want to warn either.
20  return Node.hasDefinition() && Node.hasNonTrivialDestructor();
21 }
22 } // namespace
23 
24 namespace tidy {
25 namespace misc {
26 
27 void UnusedRAIICheck::registerMatchers(MatchFinder *Finder) {
28  // Only register the matchers for C++; the functionality currently does not
29  // provide any benefit to other languages, despite being benign.
30  if (!getLangOpts().CPlusPlus)
31  return;
32 
33  // Look for temporaries that are constructed in-place and immediately
34  // destroyed. Look for temporaries created by a functional cast but not for
35  // those returned from a call.
36  auto BindTemp = cxxBindTemporaryExpr(unless(has(callExpr()))).bind("temp");
37  Finder->addMatcher(
38  exprWithCleanups(
39  unless(isInTemplateInstantiation()),
40  hasParent(compoundStmt().bind("compound")),
41  hasType(cxxRecordDecl(hasNonTrivialDestructor())),
42  anyOf(has(BindTemp), has(cxxFunctionalCastExpr(has(BindTemp)))))
43  .bind("expr"),
44  this);
45 }
46 
47 void UnusedRAIICheck::check(const MatchFinder::MatchResult &Result) {
48  const auto *E = Result.Nodes.getStmtAs<Expr>("expr");
49 
50  // We ignore code expanded from macros to reduce the number of false
51  // positives.
52  if (E->getLocStart().isMacroID())
53  return;
54 
55  // Don't emit a warning for the last statement in the surrounding compund
56  // statement.
57  const auto *CS = Result.Nodes.getStmtAs<CompoundStmt>("compound");
58  if (E == CS->body_back())
59  return;
60 
61  // Emit a warning.
62  auto D = diag(E->getLocStart(), "object destroyed immediately after "
63  "creation; did you mean to name the object?");
64  const char *Replacement = " give_me_a_name";
65 
66  // If this is a default ctor we have to remove the parens or we'll introduce a
67  // most vexing parse.
68  const auto *BTE = Result.Nodes.getStmtAs<CXXBindTemporaryExpr>("temp");
69  if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(BTE->getSubExpr()))
70  if (TOE->getNumArgs() == 0) {
71  D << FixItHint::CreateReplacement(
72  CharSourceRange::getTokenRange(TOE->getParenOrBraceRange()),
73  Replacement);
74  return;
75  }
76 
77  // Otherwise just suggest adding a name. To find the place to insert the name
78  // find the first TypeLoc in the children of E, which always points to the
79  // written type.
80  auto Matches =
81  match(expr(hasDescendant(typeLoc().bind("t"))), *E, *Result.Context);
82  const auto *TL = selectFirst<TypeLoc>("t", Matches);
83  D << FixItHint::CreateInsertion(
84  Lexer::getLocForEndOfToken(TL->getLocEnd(), 0, *Result.SourceManager,
85  Result.Context->getLangOpts()),
86  Replacement);
87 }
88 
89 } // namespace misc
90 } // namespace tidy
91 } // namespace clang
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt)
const NamedDecl * Result
Definition: USRFinder.cpp:121