Alexander Kornienko | 5b982e5 | 2015-03-09 11:48:54 +0000 | [diff] [blame^] | 1 | //===--- UnusedRAIICheck.cpp - clang-tidy ---------------------------------===// |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 5b982e5 | 2015-03-09 11:48:54 +0000 | [diff] [blame^] | 10 | #include "UnusedRAIICheck.h" |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/Lex/Lexer.h" |
| 13 | |
| 14 | using namespace clang::ast_matchers; |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace ast_matchers { |
| 18 | AST_MATCHER(CXXRecordDecl, hasUserDeclaredDestructor) { |
| 19 | // TODO: If the dtor is there but empty we don't want to warn either. |
Benjamin Kramer | c1730e9 | 2014-07-24 08:34:42 +0000 | [diff] [blame] | 20 | return Node.hasDefinition() && Node.hasUserDeclaredDestructor(); |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 21 | } |
| 22 | } // namespace ast_matchers |
| 23 | |
| 24 | namespace tidy { |
Alexander Kornienko | 2b312420 | 2015-03-02 12:25:03 +0000 | [diff] [blame] | 25 | namespace misc { |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 26 | |
| 27 | void 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 Kramer | 6bcbe6e | 2014-09-03 13:30:28 +0000 | [diff] [blame] | 33 | exprWithCleanups(unless(isInTemplateInstantiation()), |
| 34 | hasParent(compoundStmt().bind("compound")), |
| 35 | hasType(recordDecl(hasUserDeclaredDestructor())), |
| 36 | anyOf(has(BindTemp), has(functionalCastExpr( |
| 37 | has(BindTemp))))).bind("expr"), |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 38 | this); |
| 39 | } |
| 40 | |
| 41 | void 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 Kramer | 4ff1ffa | 2014-07-23 11:50:54 +0000 | [diff] [blame] | 71 | // 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 Kramer | c1730e9 | 2014-07-24 08:34:42 +0000 | [diff] [blame] | 74 | auto Matches = |
| 75 | match(expr(hasDescendant(typeLoc().bind("t"))), *E, *Result.Context); |
| 76 | const auto *TL = selectFirst<TypeLoc>("t", Matches); |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 77 | D << FixItHint::CreateInsertion( |
| 78 | Lexer::getLocForEndOfToken(TL->getLocEnd(), 0, *Result.SourceManager, |
| 79 | Result.Context->getLangOpts()), |
| 80 | Replacement); |
| 81 | } |
| 82 | |
Alexander Kornienko | 2b312420 | 2015-03-02 12:25:03 +0000 | [diff] [blame] | 83 | } // namespace misc |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 84 | } // namespace tidy |
| 85 | } // namespace clang |