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