blob: f1d904251087b0a4a2123ff319f4a3d5780881ab [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.
20 return Node.hasUserDeclaredDestructor();
21}
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(
32 exprWithCleanups(
33 unless(hasAncestor(decl(
34 anyOf(recordDecl(ast_matchers::isTemplateInstantiation()),
35 functionDecl(ast_matchers::isTemplateInstantiation()))))),
36 hasParent(compoundStmt().bind("compound")),
Benjamin Kramerda3658e2014-07-23 11:49:46 +000037 hasType(recordDecl(hasUserDeclaredDestructor())),
38 anyOf(has(BindTemp), has(functionalCastExpr(has(BindTemp)))))
39 .bind("expr"),
40 this);
41}
42
43void UnusedRAIICheck::check(const MatchFinder::MatchResult &Result) {
44 const auto *E = Result.Nodes.getStmtAs<Expr>("expr");
45
46 // We ignore code expanded from macros to reduce the number of false
47 // positives.
48 if (E->getLocStart().isMacroID())
49 return;
50
51 // Don't emit a warning for the last statement in the surrounding compund
52 // statement.
53 const auto *CS = Result.Nodes.getStmtAs<CompoundStmt>("compound");
54 if (E == CS->body_back())
55 return;
56
57 // Emit a warning.
58 auto D = diag(E->getLocStart(), "object destroyed immediately after "
59 "creation; did you mean to name the object?");
60 const char *Replacement = " give_me_a_name";
61
62 // If this is a default ctor we have to remove the parens or we'll introduce a
63 // most vexing parse.
64 const auto *BTE = Result.Nodes.getStmtAs<CXXBindTemporaryExpr>("temp");
65 if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(BTE->getSubExpr()))
66 if (TOE->getNumArgs() == 0) {
67 D << FixItHint::CreateReplacement(
68 CharSourceRange::getTokenRange(TOE->getParenOrBraceRange()),
69 Replacement);
70 return;
71 }
72
Benjamin Kramer4ff1ffa2014-07-23 11:50:54 +000073 // Otherwise just suggest adding a name. To find the place to insert the name
74 // find the first TypeLoc in the children of E, which always points to the
75 // written type.
76 const auto *TL =
77 selectFirst<TypeLoc>("t", match(expr(hasDescendant(typeLoc().bind("t"))),
78 *E, *Result.Context));
Benjamin Kramerda3658e2014-07-23 11:49:46 +000079 D << FixItHint::CreateInsertion(
80 Lexer::getLocForEndOfToken(TL->getLocEnd(), 0, *Result.SourceManager,
81 Result.Context->getLangOpts()),
82 Replacement);
83}
84
85} // namespace tidy
86} // namespace clang