2018-03-15 16:27:42 +08:00
|
|
|
//===--- UnusedRaiiCheck.cpp - clang-tidy ---------------------------------===//
|
2014-07-23 19:49:46 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-07-23 19:49:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-03-15 16:27:42 +08:00
|
|
|
#include "UnusedRaiiCheck.h"
|
2014-07-23 19:49:46 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
2016-05-03 02:00:29 +08:00
|
|
|
namespace tidy {
|
2018-03-15 16:27:42 +08:00
|
|
|
namespace bugprone {
|
2016-05-03 02:00:29 +08:00
|
|
|
|
2015-06-17 21:11:37 +08:00
|
|
|
namespace {
|
2015-05-12 20:17:20 +08:00
|
|
|
AST_MATCHER(CXXRecordDecl, hasNonTrivialDestructor) {
|
2014-07-23 19:49:46 +08:00
|
|
|
// TODO: If the dtor is there but empty we don't want to warn either.
|
2015-05-12 20:17:20 +08:00
|
|
|
return Node.hasDefinition() && Node.hasNonTrivialDestructor();
|
2014-07-23 19:49:46 +08:00
|
|
|
}
|
2015-06-17 21:11:37 +08:00
|
|
|
} // namespace
|
2014-07-23 19:49:46 +08:00
|
|
|
|
2018-03-15 16:27:42 +08:00
|
|
|
void UnusedRaiiCheck::registerMatchers(MatchFinder *Finder) {
|
2015-08-29 03:27:19 +08:00
|
|
|
// Only register the matchers for C++; the functionality currently does not
|
|
|
|
// provide any benefit to other languages, despite being benign.
|
2015-08-31 23:28:57 +08:00
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Look for temporaries that are constructed in-place and immediately
|
|
|
|
// destroyed. Look for temporaries created by a functional cast but not for
|
|
|
|
// those returned from a call.
|
2016-05-31 23:26:56 +08:00
|
|
|
auto BindTemp =
|
|
|
|
cxxBindTemporaryExpr(unless(has(ignoringParenImpCasts(callExpr()))))
|
|
|
|
.bind("temp");
|
2015-08-31 23:28:57 +08:00
|
|
|
Finder->addMatcher(
|
2016-05-31 23:26:56 +08:00
|
|
|
exprWithCleanups(unless(isInTemplateInstantiation()),
|
|
|
|
hasParent(compoundStmt().bind("compound")),
|
|
|
|
hasType(cxxRecordDecl(hasNonTrivialDestructor())),
|
|
|
|
anyOf(has(ignoringParenImpCasts(BindTemp)),
|
|
|
|
has(ignoringParenImpCasts(cxxFunctionalCastExpr(
|
|
|
|
has(ignoringParenImpCasts(BindTemp)))))))
|
2015-08-31 23:28:57 +08:00
|
|
|
.bind("expr"),
|
|
|
|
this);
|
2014-07-23 19:49:46 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:27:42 +08:00
|
|
|
void UnusedRaiiCheck::check(const MatchFinder::MatchResult &Result) {
|
2016-12-14 00:19:19 +08:00
|
|
|
const auto *E = Result.Nodes.getNodeAs<Expr>("expr");
|
2014-07-23 19:49:46 +08:00
|
|
|
|
|
|
|
// We ignore code expanded from macros to reduce the number of false
|
|
|
|
// positives.
|
2018-08-10 06:42:26 +08:00
|
|
|
if (E->getBeginLoc().isMacroID())
|
2014-07-23 19:49:46 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Don't emit a warning for the last statement in the surrounding compund
|
|
|
|
// statement.
|
2016-12-14 00:19:19 +08:00
|
|
|
const auto *CS = Result.Nodes.getNodeAs<CompoundStmt>("compound");
|
2014-07-23 19:49:46 +08:00
|
|
|
if (E == CS->body_back())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Emit a warning.
|
2018-08-10 06:42:26 +08:00
|
|
|
auto D = diag(E->getBeginLoc(), "object destroyed immediately after "
|
2014-07-23 19:49:46 +08:00
|
|
|
"creation; did you mean to name the object?");
|
|
|
|
const char *Replacement = " give_me_a_name";
|
|
|
|
|
|
|
|
// If this is a default ctor we have to remove the parens or we'll introduce a
|
|
|
|
// most vexing parse.
|
2016-12-14 00:19:19 +08:00
|
|
|
const auto *BTE = Result.Nodes.getNodeAs<CXXBindTemporaryExpr>("temp");
|
2014-07-23 19:49:46 +08:00
|
|
|
if (const auto *TOE = dyn_cast<CXXTemporaryObjectExpr>(BTE->getSubExpr()))
|
|
|
|
if (TOE->getNumArgs() == 0) {
|
|
|
|
D << FixItHint::CreateReplacement(
|
|
|
|
CharSourceRange::getTokenRange(TOE->getParenOrBraceRange()),
|
|
|
|
Replacement);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-23 19:50:54 +08:00
|
|
|
// Otherwise just suggest adding a name. To find the place to insert the name
|
|
|
|
// find the first TypeLoc in the children of E, which always points to the
|
|
|
|
// written type.
|
2014-07-24 16:34:42 +08:00
|
|
|
auto Matches =
|
|
|
|
match(expr(hasDescendant(typeLoc().bind("t"))), *E, *Result.Context);
|
|
|
|
const auto *TL = selectFirst<TypeLoc>("t", Matches);
|
2014-07-23 19:49:46 +08:00
|
|
|
D << FixItHint::CreateInsertion(
|
2018-08-10 06:43:02 +08:00
|
|
|
Lexer::getLocForEndOfToken(TL->getEndLoc(), 0, *Result.SourceManager,
|
2016-09-24 10:13:45 +08:00
|
|
|
getLangOpts()),
|
2014-07-23 19:49:46 +08:00
|
|
|
Replacement);
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:27:42 +08:00
|
|
|
} // namespace bugprone
|
2014-07-23 19:49:46 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|