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 { |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 17 | namespace tidy { |
| 18 | namespace misc { |
| 19 | |
Alexander Kornienko | 50d7f461 | 2015-06-17 13:11:37 +0000 | [diff] [blame] | 20 | namespace { |
Alexander Kornienko | bdaa681 | 2015-05-12 12:17:20 +0000 | [diff] [blame] | 21 | AST_MATCHER(CXXRecordDecl, hasNonTrivialDestructor) { |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 22 | // TODO: If the dtor is there but empty we don't want to warn either. |
Alexander Kornienko | bdaa681 | 2015-05-12 12:17:20 +0000 | [diff] [blame] | 23 | return Node.hasDefinition() && Node.hasNonTrivialDestructor(); |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 24 | } |
Alexander Kornienko | 50d7f461 | 2015-06-17 13:11:37 +0000 | [diff] [blame] | 25 | } // namespace |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 26 | |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 27 | void UnusedRAIICheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | 327e97b | 2015-08-28 19:27:19 +0000 | [diff] [blame] | 28 | // Only register the matchers for C++; the functionality currently does not |
| 29 | // provide any benefit to other languages, despite being benign. |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 30 | 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. |
Piotr Padlewski | e93a73f | 2016-05-31 15:26:56 +0000 | [diff] [blame] | 36 | auto BindTemp = |
| 37 | cxxBindTemporaryExpr(unless(has(ignoringParenImpCasts(callExpr())))) |
| 38 | .bind("temp"); |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 39 | Finder->addMatcher( |
Piotr Padlewski | e93a73f | 2016-05-31 15:26:56 +0000 | [diff] [blame] | 40 | exprWithCleanups(unless(isInTemplateInstantiation()), |
| 41 | hasParent(compoundStmt().bind("compound")), |
| 42 | hasType(cxxRecordDecl(hasNonTrivialDestructor())), |
| 43 | anyOf(has(ignoringParenImpCasts(BindTemp)), |
| 44 | has(ignoringParenImpCasts(cxxFunctionalCastExpr( |
| 45 | has(ignoringParenImpCasts(BindTemp))))))) |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 46 | .bind("expr"), |
| 47 | this); |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void UnusedRAIICheck::check(const MatchFinder::MatchResult &Result) { |
Alexander Kornienko | 9f58fe0 | 2016-12-13 16:19:19 +0000 | [diff] [blame] | 51 | const auto *E = Result.Nodes.getNodeAs<Expr>("expr"); |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 52 | |
| 53 | // We ignore code expanded from macros to reduce the number of false |
| 54 | // positives. |
| 55 | if (E->getLocStart().isMacroID()) |
| 56 | return; |
| 57 | |
| 58 | // Don't emit a warning for the last statement in the surrounding compund |
| 59 | // statement. |
Alexander Kornienko | 9f58fe0 | 2016-12-13 16:19:19 +0000 | [diff] [blame] | 60 | const auto *CS = Result.Nodes.getNodeAs<CompoundStmt>("compound"); |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 61 | if (E == CS->body_back()) |
| 62 | return; |
| 63 | |
| 64 | // Emit a warning. |
| 65 | auto D = diag(E->getLocStart(), "object destroyed immediately after " |
| 66 | "creation; did you mean to name the object?"); |
| 67 | const char *Replacement = " give_me_a_name"; |
| 68 | |
| 69 | // If this is a default ctor we have to remove the parens or we'll introduce a |
| 70 | // most vexing parse. |
Alexander Kornienko | 9f58fe0 | 2016-12-13 16:19:19 +0000 | [diff] [blame] | 71 | const auto *BTE = Result.Nodes.getNodeAs<CXXBindTemporaryExpr>("temp"); |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 72 | if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(BTE->getSubExpr())) |
| 73 | if (TOE->getNumArgs() == 0) { |
| 74 | D << FixItHint::CreateReplacement( |
| 75 | CharSourceRange::getTokenRange(TOE->getParenOrBraceRange()), |
| 76 | Replacement); |
| 77 | return; |
| 78 | } |
| 79 | |
Benjamin Kramer | 4ff1ffa | 2014-07-23 11:50:54 +0000 | [diff] [blame] | 80 | // Otherwise just suggest adding a name. To find the place to insert the name |
| 81 | // find the first TypeLoc in the children of E, which always points to the |
| 82 | // written type. |
Benjamin Kramer | c1730e9 | 2014-07-24 08:34:42 +0000 | [diff] [blame] | 83 | auto Matches = |
| 84 | match(expr(hasDescendant(typeLoc().bind("t"))), *E, *Result.Context); |
| 85 | const auto *TL = selectFirst<TypeLoc>("t", Matches); |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 86 | D << FixItHint::CreateInsertion( |
| 87 | Lexer::getLocForEndOfToken(TL->getLocEnd(), 0, *Result.SourceManager, |
Gabor Horvath | afad84c | 2016-09-24 02:13:45 +0000 | [diff] [blame] | 88 | getLangOpts()), |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 89 | Replacement); |
| 90 | } |
| 91 | |
Alexander Kornienko | 2b312420 | 2015-03-02 12:25:03 +0000 | [diff] [blame] | 92 | } // namespace misc |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 93 | } // namespace tidy |
| 94 | } // namespace clang |