aboutsummaryrefslogtreecommitdiff
blob: e9089b77c06a84641045f1695e0418f003d891f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//===--- UnusedRaiiCheck.cpp - clang-tidy ---------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "UnusedRaiiCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/Lex/Lexer.h"

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace bugprone {

namespace {
AST_MATCHER(CXXRecordDecl, hasNonTrivialDestructor) {
  // TODO: If the dtor is there but empty we don't want to warn either.
  return Node.hasDefinition() && Node.hasNonTrivialDestructor();
}
} // namespace

void UnusedRaiiCheck::registerMatchers(MatchFinder *Finder) {
  // Only register the matchers for C++; the functionality currently does not
  // provide any benefit to other languages, despite being benign.
  if (!getLangOpts().CPlusPlus)
    return;

  // Look for temporaries that are constructed in-place and immediately
  // destroyed. Look for temporaries created by a functional cast but not for
  // those returned from a call.
  auto BindTemp =
      cxxBindTemporaryExpr(unless(has(ignoringParenImpCasts(callExpr()))))
          .bind("temp");
  Finder->addMatcher(
      exprWithCleanups(unless(isInTemplateInstantiation()),
                       hasParent(compoundStmt().bind("compound")),
                       hasType(cxxRecordDecl(hasNonTrivialDestructor())),
                       anyOf(has(ignoringParenImpCasts(BindTemp)),
                             has(ignoringParenImpCasts(cxxFunctionalCastExpr(
                                 has(ignoringParenImpCasts(BindTemp)))))))
          .bind("expr"),
      this);
}

void UnusedRaiiCheck::check(const MatchFinder::MatchResult &Result) {
  const auto *E = Result.Nodes.getNodeAs<Expr>("expr");

  // We ignore code expanded from macros to reduce the number of false
  // positives.
  if (E->getBeginLoc().isMacroID())
    return;

  // Don't emit a warning for the last statement in the surrounding compund
  // statement.
  const auto *CS = Result.Nodes.getNodeAs<CompoundStmt>("compound");
  if (E == CS->body_back())
    return;

  // Emit a warning.
  auto D = diag(E->getBeginLoc(), "object destroyed immediately after "
                                  "creation; did you mean to name the object?");
  const char *Replacement = " give_me_a_name";

  // If this is a default ctor we have to remove the parens or we'll introduce a
  // most vexing parse.
  const auto *BTE = Result.Nodes.getNodeAs<CXXBindTemporaryExpr>("temp");
  if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(BTE->getSubExpr()))
    if (TOE->getNumArgs() == 0) {
      D << FixItHint::CreateReplacement(
          CharSourceRange::getTokenRange(TOE->getParenOrBraceRange()),
          Replacement);
      return;
    }

  // Otherwise just suggest adding a name. To find the place to insert the name
  // find the first TypeLoc in the children of E, which always points to the
  // written type.
  auto Matches =
      match(expr(hasDescendant(typeLoc().bind("t"))), *E, *Result.Context);
  const auto *TL = selectFirst<TypeLoc>("t", Matches);
  D << FixItHint::CreateInsertion(
      Lexer::getLocForEndOfToken(TL->getEndLoc(), 0, *Result.SourceManager,
                                 getLangOpts()),
      Replacement);
}

} // namespace bugprone
} // namespace tidy
} // namespace clang