blob: 9a7628bfcfd0e7a0462213abc4db80496f6564a0 [file] [log] [blame]
Alexander Kornienko5b982e52015-03-09 11:48:54 +00001//===--- UnusedRAIICheck.cpp - clang-tidy ---------------------------------===//
Benjamin Kramerda3658e2014-07-23 11:49:46 +00002//
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
Alexander Kornienko5b982e52015-03-09 11:48:54 +000010#include "UnusedRAIICheck.h"
Benjamin Kramerda3658e2014-07-23 11:49:46 +000011#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 {
Alexander Kornienko2b3124202015-03-02 12:25:03 +000025namespace misc {
Benjamin Kramerda3658e2014-07-23 11:49:46 +000026
27void UnusedRAIICheck::registerMatchers(MatchFinder *Finder) {
28 // Look for temporaries that are constructed in-place and immediately
29 // destroyed. Look for temporaries created by a functional cast but not for
30 // those returned from a call.
31 auto BindTemp = bindTemporaryExpr(unless(has(callExpr()))).bind("temp");
32 Finder->addMatcher(
Benjamin Kramer6bcbe6e2014-09-03 13:30:28 +000033 exprWithCleanups(unless(isInTemplateInstantiation()),
34 hasParent(compoundStmt().bind("compound")),
35 hasType(recordDecl(hasUserDeclaredDestructor())),
36 anyOf(has(BindTemp), has(functionalCastExpr(
37 has(BindTemp))))).bind("expr"),
Benjamin Kramerda3658e2014-07-23 11:49:46 +000038 this);
39}
40
41void UnusedRAIICheck::check(const MatchFinder::MatchResult &Result) {
42 const auto *E = Result.Nodes.getStmtAs<Expr>("expr");
43
44 // We ignore code expanded from macros to reduce the number of false
45 // positives.
46 if (E->getLocStart().isMacroID())
47 return;
48
49 // Don't emit a warning for the last statement in the surrounding compund
50 // statement.
51 const auto *CS = Result.Nodes.getStmtAs<CompoundStmt>("compound");
52 if (E == CS->body_back())
53 return;
54
55 // Emit a warning.
56 auto D = diag(E->getLocStart(), "object destroyed immediately after "
57 "creation; did you mean to name the object?");
58 const char *Replacement = " give_me_a_name";
59
60 // If this is a default ctor we have to remove the parens or we'll introduce a
61 // most vexing parse.
62 const auto *BTE = Result.Nodes.getStmtAs<CXXBindTemporaryExpr>("temp");
63 if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(BTE->getSubExpr()))
64 if (TOE->getNumArgs() == 0) {
65 D << FixItHint::CreateReplacement(
66 CharSourceRange::getTokenRange(TOE->getParenOrBraceRange()),
67 Replacement);
68 return;
69 }
70
Benjamin Kramer4ff1ffa2014-07-23 11:50:54 +000071 // Otherwise just suggest adding a name. To find the place to insert the name
72 // find the first TypeLoc in the children of E, which always points to the
73 // written type.
Benjamin Kramerc1730e92014-07-24 08:34:42 +000074 auto Matches =
75 match(expr(hasDescendant(typeLoc().bind("t"))), *E, *Result.Context);
76 const auto *TL = selectFirst<TypeLoc>("t", Matches);
Benjamin Kramerda3658e2014-07-23 11:49:46 +000077 D << FixItHint::CreateInsertion(
78 Lexer::getLocForEndOfToken(TL->getLocEnd(), 0, *Result.SourceManager,
79 Result.Context->getLangOpts()),
80 Replacement);
81}
82
Alexander Kornienko2b3124202015-03-02 12:25:03 +000083} // namespace misc
Benjamin Kramerda3658e2014-07-23 11:49:46 +000084} // namespace tidy
85} // namespace clang