blob: 7f06a49f5f8979a141ed310be761421c5c8a1501 [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")),
37 hasDescendant(typeLoc().bind("typeloc")),
38 hasType(recordDecl(hasUserDeclaredDestructor())),
39 anyOf(has(BindTemp), has(functionalCastExpr(has(BindTemp)))))
40 .bind("expr"),
41 this);
42}
43
44void UnusedRAIICheck::check(const MatchFinder::MatchResult &Result) {
45 const auto *E = Result.Nodes.getStmtAs<Expr>("expr");
46
47 // We ignore code expanded from macros to reduce the number of false
48 // positives.
49 if (E->getLocStart().isMacroID())
50 return;
51
52 // Don't emit a warning for the last statement in the surrounding compund
53 // statement.
54 const auto *CS = Result.Nodes.getStmtAs<CompoundStmt>("compound");
55 if (E == CS->body_back())
56 return;
57
58 // Emit a warning.
59 auto D = diag(E->getLocStart(), "object destroyed immediately after "
60 "creation; did you mean to name the object?");
61 const char *Replacement = " give_me_a_name";
62
63 // If this is a default ctor we have to remove the parens or we'll introduce a
64 // most vexing parse.
65 const auto *BTE = Result.Nodes.getStmtAs<CXXBindTemporaryExpr>("temp");
66 if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(BTE->getSubExpr()))
67 if (TOE->getNumArgs() == 0) {
68 D << FixItHint::CreateReplacement(
69 CharSourceRange::getTokenRange(TOE->getParenOrBraceRange()),
70 Replacement);
71 return;
72 }
73
74 // Otherwise just suggest adding a name.
75 const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("typeloc");
76 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