2015-03-09 19:48:54 +08:00
|
|
|
//===--- UnusedRAIICheck.cpp - clang-tidy ---------------------------------===//
|
2014-07-23 19:49:46 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-09 19:48:54 +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 {
|
|
|
|
namespace misc {
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void UnusedRAIICheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *E = Result.Nodes.getStmtAs<Expr>("expr");
|
|
|
|
|
|
|
|
// We ignore code expanded from macros to reduce the number of false
|
|
|
|
// positives.
|
|
|
|
if (E->getLocStart().isMacroID())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Don't emit a warning for the last statement in the surrounding compund
|
|
|
|
// statement.
|
|
|
|
const auto *CS = Result.Nodes.getStmtAs<CompoundStmt>("compound");
|
|
|
|
if (E == CS->body_back())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Emit a warning.
|
|
|
|
auto D = diag(E->getLocStart(), "object destroyed immediately after "
|
|
|
|
"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.
|
|
|
|
const auto *BTE = Result.Nodes.getStmtAs<CXXBindTemporaryExpr>("temp");
|
|
|
|
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(
|
|
|
|
Lexer::getLocForEndOfToken(TL->getLocEnd(), 0, *Result.SourceManager,
|
|
|
|
Result.Context->getLangOpts()),
|
|
|
|
Replacement);
|
|
|
|
}
|
|
|
|
|
2015-03-02 20:25:03 +08:00
|
|
|
} // namespace misc
|
2014-07-23 19:49:46 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|