blob: e62b31da027be78d422a5ae55cfa61be178417f0 [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 {
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000017namespace {
Alexander Kornienkobdaa6812015-05-12 12:17:20 +000018AST_MATCHER(CXXRecordDecl, hasNonTrivialDestructor) {
Benjamin Kramerda3658e2014-07-23 11:49:46 +000019 // TODO: If the dtor is there but empty we don't want to warn either.
Alexander Kornienkobdaa6812015-05-12 12:17:20 +000020 return Node.hasDefinition() && Node.hasNonTrivialDestructor();
Benjamin Kramerda3658e2014-07-23 11:49:46 +000021}
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000022} // namespace
Benjamin Kramerda3658e2014-07-23 11:49:46 +000023
24namespace tidy {
Alexander Kornienko2b3124202015-03-02 12:25:03 +000025namespace misc {
Benjamin Kramerda3658e2014-07-23 11:49:46 +000026
27void UnusedRAIICheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballman327e97b2015-08-28 19:27:19 +000028 // Only register the matchers for C++; the functionality currently does not
29 // provide any benefit to other languages, despite being benign.
Aaron Ballmanbf891092015-08-31 15:28:57 +000030 if (!getLangOpts().CPlusPlus)
31 return;
32
33 // Look for temporaries that are constructed in-place and immediately
34 // destroyed. Look for temporaries created by a functional cast but not for
35 // those returned from a call.
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000036 auto BindTemp = cxxBindTemporaryExpr(unless(has(callExpr()))).bind("temp");
Aaron Ballmanbf891092015-08-31 15:28:57 +000037 Finder->addMatcher(
38 exprWithCleanups(
39 unless(isInTemplateInstantiation()),
40 hasParent(compoundStmt().bind("compound")),
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000041 hasType(cxxRecordDecl(hasNonTrivialDestructor())),
42 anyOf(has(BindTemp), has(cxxFunctionalCastExpr(has(BindTemp)))))
Aaron Ballmanbf891092015-08-31 15:28:57 +000043 .bind("expr"),
44 this);
Benjamin Kramerda3658e2014-07-23 11:49:46 +000045}
46
47void UnusedRAIICheck::check(const MatchFinder::MatchResult &Result) {
48 const auto *E = Result.Nodes.getStmtAs<Expr>("expr");
49
50 // We ignore code expanded from macros to reduce the number of false
51 // positives.
52 if (E->getLocStart().isMacroID())
53 return;
54
55 // Don't emit a warning for the last statement in the surrounding compund
56 // statement.
57 const auto *CS = Result.Nodes.getStmtAs<CompoundStmt>("compound");
58 if (E == CS->body_back())
59 return;
60
61 // Emit a warning.
62 auto D = diag(E->getLocStart(), "object destroyed immediately after "
63 "creation; did you mean to name the object?");
64 const char *Replacement = " give_me_a_name";
65
66 // If this is a default ctor we have to remove the parens or we'll introduce a
67 // most vexing parse.
68 const auto *BTE = Result.Nodes.getStmtAs<CXXBindTemporaryExpr>("temp");
69 if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(BTE->getSubExpr()))
70 if (TOE->getNumArgs() == 0) {
71 D << FixItHint::CreateReplacement(
72 CharSourceRange::getTokenRange(TOE->getParenOrBraceRange()),
73 Replacement);
74 return;
75 }
76
Benjamin Kramer4ff1ffa2014-07-23 11:50:54 +000077 // Otherwise just suggest adding a name. To find the place to insert the name
78 // find the first TypeLoc in the children of E, which always points to the
79 // written type.
Benjamin Kramerc1730e92014-07-24 08:34:42 +000080 auto Matches =
81 match(expr(hasDescendant(typeLoc().bind("t"))), *E, *Result.Context);
82 const auto *TL = selectFirst<TypeLoc>("t", Matches);
Benjamin Kramerda3658e2014-07-23 11:49:46 +000083 D << FixItHint::CreateInsertion(
84 Lexer::getLocForEndOfToken(TL->getLocEnd(), 0, *Result.SourceManager,
85 Result.Context->getLangOpts()),
86 Replacement);
87}
88
Alexander Kornienko2b3124202015-03-02 12:25:03 +000089} // namespace misc
Benjamin Kramerda3658e2014-07-23 11:49:46 +000090} // namespace tidy
91} // namespace clang