blob: 41327b65bb522003c4cafe7975b016d351f56cf6 [file] [log] [blame]
Benjamin Kramerda3658e2014-07-23 11:49:46 +00001//===--- UnusedRAII.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 "UnusedRAII.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/Lex/Lexer.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace ast_matchers {
18AST_MATCHER(CXXRecordDecl, hasUserDeclaredDestructor) {
19 // TODO: If the dtor is there but empty we don't want to warn either.
Benjamin Kramerc1730e92014-07-24 08:34:42 +000020 return Node.hasDefinition() && Node.hasUserDeclaredDestructor();
Benjamin Kramerda3658e2014-07-23 11:49:46 +000021}
22} // namespace ast_matchers
23
24namespace tidy {
25
26void UnusedRAIICheck::registerMatchers(MatchFinder *Finder) {
27 // Look for temporaries that are constructed in-place and immediately
28 // destroyed. Look for temporaries created by a functional cast but not for
29 // those returned from a call.
30 auto BindTemp = bindTemporaryExpr(unless(has(callExpr()))).bind("temp");
31 Finder->addMatcher(
Benjamin Kramer6bcbe6e2014-09-03 13:30:28 +000032 exprWithCleanups(unless(isInTemplateInstantiation()),
33 hasParent(compoundStmt().bind("compound")),
34 hasType(recordDecl(hasUserDeclaredDestructor())),
35 anyOf(has(BindTemp), has(functionalCastExpr(
36 has(BindTemp))))).bind("expr"),
Benjamin Kramerda3658e2014-07-23 11:49:46 +000037 this);
38}
39
40void UnusedRAIICheck::check(const MatchFinder::MatchResult &Result) {
41 const auto *E = Result.Nodes.getStmtAs<Expr>("expr");
42
43 // We ignore code expanded from macros to reduce the number of false
44 // positives.
45 if (E->getLocStart().isMacroID())
46 return;
47
48 // Don't emit a warning for the last statement in the surrounding compund
49 // statement.
50 const auto *CS = Result.Nodes.getStmtAs<CompoundStmt>("compound");
51 if (E == CS->body_back())
52 return;
53
54 // Emit a warning.
55 auto D = diag(E->getLocStart(), "object destroyed immediately after "
56 "creation; did you mean to name the object?");
57 const char *Replacement = " give_me_a_name";
58
59 // If this is a default ctor we have to remove the parens or we'll introduce a
60 // most vexing parse.
61 const auto *BTE = Result.Nodes.getStmtAs<CXXBindTemporaryExpr>("temp");
62 if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(BTE->getSubExpr()))
63 if (TOE->getNumArgs() == 0) {
64 D << FixItHint::CreateReplacement(
65 CharSourceRange::getTokenRange(TOE->getParenOrBraceRange()),
66 Replacement);
67 return;
68 }
69
Benjamin Kramer4ff1ffa2014-07-23 11:50:54 +000070 // Otherwise just suggest adding a name. To find the place to insert the name
71 // find the first TypeLoc in the children of E, which always points to the
72 // written type.
Benjamin Kramerc1730e92014-07-24 08:34:42 +000073 auto Matches =
74 match(expr(hasDescendant(typeLoc().bind("t"))), *E, *Result.Context);
75 const auto *TL = selectFirst<TypeLoc>("t", Matches);
Benjamin Kramerda3658e2014-07-23 11:49:46 +000076 D << FixItHint::CreateInsertion(
77 Lexer::getLocForEndOfToken(TL->getLocEnd(), 0, *Result.SourceManager,
78 Result.Context->getLangOpts()),
79 Replacement);
80}
81
82} // namespace tidy
83} // namespace clang