2016-03-30 02:02:26 +08:00
|
|
|
//===--- DanglingHandleCheck.cpp - clang-tidy------------------------------===//
|
|
|
|
//
|
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
|
2016-03-30 02:02:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DanglingHandleCheck.h"
|
2016-06-22 04:11:20 +08:00
|
|
|
#include "../utils/Matchers.h"
|
2016-05-10 23:31:15 +08:00
|
|
|
#include "../utils/OptionsUtils.h"
|
2016-03-30 02:02:26 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
2016-06-22 04:11:20 +08:00
|
|
|
using namespace clang::tidy::matchers;
|
2016-03-30 02:02:26 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2017-11-24 17:52:05 +08:00
|
|
|
namespace bugprone {
|
2016-03-30 02:02:26 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-03-30 20:16:09 +08:00
|
|
|
ast_matchers::internal::BindableMatcher<Stmt>
|
2016-06-15 23:46:10 +08:00
|
|
|
handleFrom(const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle,
|
|
|
|
const ast_matchers::internal::Matcher<Expr> &Arg) {
|
2018-01-08 23:59:08 +08:00
|
|
|
return expr(
|
|
|
|
anyOf(cxxConstructExpr(hasDeclaration(cxxMethodDecl(ofClass(IsAHandle))),
|
|
|
|
hasArgument(0, Arg)),
|
|
|
|
cxxMemberCallExpr(hasType(cxxRecordDecl(IsAHandle)),
|
|
|
|
callee(memberExpr(member(cxxConversionDecl()))),
|
|
|
|
on(Arg))));
|
2016-03-30 02:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ast_matchers::internal::Matcher<Stmt> handleFromTemporaryValue(
|
2016-06-15 23:46:10 +08:00
|
|
|
const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
|
2016-03-30 02:02:26 +08:00
|
|
|
// If a ternary operator returns a temporary value, then both branches hold a
|
|
|
|
// temporary value. If one of them is not a temporary then it must be copied
|
|
|
|
// into one to satisfy the type of the operator.
|
|
|
|
const auto TemporaryTernary =
|
|
|
|
conditionalOperator(hasTrueExpression(cxxBindTemporaryExpr()),
|
|
|
|
hasFalseExpression(cxxBindTemporaryExpr()));
|
|
|
|
|
|
|
|
return handleFrom(IsAHandle, anyOf(cxxBindTemporaryExpr(), TemporaryTernary));
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_matchers::internal::Matcher<RecordDecl> isASequence() {
|
|
|
|
return hasAnyName("::std::deque", "::std::forward_list", "::std::list",
|
|
|
|
"::std::vector");
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_matchers::internal::Matcher<RecordDecl> isASet() {
|
|
|
|
return hasAnyName("::std::set", "::std::multiset", "::std::unordered_set",
|
|
|
|
"::std::unordered_multiset");
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_matchers::internal::Matcher<RecordDecl> isAMap() {
|
|
|
|
return hasAnyName("::std::map", "::std::multimap", "::std::unordered_map",
|
|
|
|
"::std::unordered_multimap");
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:46:10 +08:00
|
|
|
ast_matchers::internal::BindableMatcher<Stmt> makeContainerMatcher(
|
|
|
|
const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
|
2016-03-30 02:02:26 +08:00
|
|
|
// This matcher could be expanded to detect:
|
|
|
|
// - Constructors: eg. vector<string_view>(3, string("A"));
|
|
|
|
// - emplace*(): This requires a different logic to determine that
|
|
|
|
// the conversion will happen inside the container.
|
|
|
|
// - map's insert: This requires detecting that the pair conversion triggers
|
|
|
|
// the bug. A little more complicated than what we have now.
|
|
|
|
return callExpr(
|
2016-03-30 20:16:09 +08:00
|
|
|
hasAnyArgument(
|
|
|
|
ignoringParenImpCasts(handleFromTemporaryValue(IsAHandle))),
|
2016-03-30 02:02:26 +08:00
|
|
|
anyOf(
|
|
|
|
// For sequences: assign, push_back, resize.
|
|
|
|
cxxMemberCallExpr(
|
|
|
|
callee(functionDecl(hasAnyName("assign", "push_back", "resize"))),
|
2017-08-02 21:13:11 +08:00
|
|
|
on(expr(hasType(hasUnqualifiedDesugaredType(
|
|
|
|
recordType(hasDeclaration(recordDecl(isASequence())))))))),
|
2016-03-30 02:02:26 +08:00
|
|
|
// For sequences and sets: insert.
|
2017-08-02 21:13:11 +08:00
|
|
|
cxxMemberCallExpr(callee(functionDecl(hasName("insert"))),
|
|
|
|
on(expr(hasType(hasUnqualifiedDesugaredType(
|
|
|
|
recordType(hasDeclaration(recordDecl(
|
|
|
|
anyOf(isASequence(), isASet()))))))))),
|
2016-03-30 02:02:26 +08:00
|
|
|
// For maps: operator[].
|
|
|
|
cxxOperatorCallExpr(callee(cxxMethodDecl(ofClass(isAMap()))),
|
|
|
|
hasOverloadedOperatorName("[]"))));
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:16:09 +08:00
|
|
|
} // anonymous namespace
|
2016-03-30 02:02:26 +08:00
|
|
|
|
|
|
|
DanglingHandleCheck::DanglingHandleCheck(StringRef Name,
|
|
|
|
ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
2016-05-10 23:31:15 +08:00
|
|
|
HandleClasses(utils::options::parseStringList(Options.get(
|
2016-03-30 02:02:26 +08:00
|
|
|
"HandleClasses",
|
|
|
|
"std::basic_string_view;std::experimental::basic_string_view"))),
|
|
|
|
IsAHandle(cxxRecordDecl(hasAnyName(std::vector<StringRef>(
|
|
|
|
HandleClasses.begin(), HandleClasses.end())))
|
|
|
|
.bind("handle")) {}
|
|
|
|
|
|
|
|
void DanglingHandleCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "HandleClasses",
|
2016-05-10 23:31:15 +08:00
|
|
|
utils::options::serializeStringList(HandleClasses));
|
2016-03-30 02:02:26 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:16:09 +08:00
|
|
|
void DanglingHandleCheck::registerMatchersForVariables(MatchFinder *Finder) {
|
2016-03-30 02:02:26 +08:00
|
|
|
const auto ConvertedHandle = handleFromTemporaryValue(IsAHandle);
|
|
|
|
|
|
|
|
// Find 'Handle foo(ReturnsAValue());'
|
|
|
|
Finder->addMatcher(
|
2017-08-02 21:13:11 +08:00
|
|
|
varDecl(hasType(hasUnqualifiedDesugaredType(
|
|
|
|
recordType(hasDeclaration(cxxRecordDecl(IsAHandle))))),
|
2016-03-30 02:02:26 +08:00
|
|
|
hasInitializer(
|
2016-05-31 23:26:56 +08:00
|
|
|
exprWithCleanups(has(ignoringParenImpCasts(ConvertedHandle)))
|
|
|
|
.bind("bad_stmt"))),
|
2016-03-30 02:02:26 +08:00
|
|
|
this);
|
|
|
|
|
|
|
|
// Find 'Handle foo = ReturnsAValue();'
|
|
|
|
Finder->addMatcher(
|
2019-11-12 23:15:56 +08:00
|
|
|
traverse(ast_type_traits::TK_AsIs,
|
|
|
|
varDecl(hasType(hasUnqualifiedDesugaredType(recordType(
|
|
|
|
hasDeclaration(cxxRecordDecl(IsAHandle))))),
|
|
|
|
unless(parmVarDecl()),
|
|
|
|
hasInitializer(exprWithCleanups(
|
|
|
|
has(ignoringParenImpCasts(handleFrom(
|
2016-05-31 23:26:56 +08:00
|
|
|
IsAHandle, ConvertedHandle))))
|
2019-11-12 23:15:56 +08:00
|
|
|
.bind("bad_stmt")))),
|
2016-03-30 02:02:26 +08:00
|
|
|
this);
|
|
|
|
// Find 'foo = ReturnsAValue(); // foo is Handle'
|
|
|
|
Finder->addMatcher(
|
2019-11-12 23:15:56 +08:00
|
|
|
traverse(ast_type_traits::TK_AsIs,
|
|
|
|
cxxOperatorCallExpr(callee(cxxMethodDecl(ofClass(IsAHandle))),
|
|
|
|
hasOverloadedOperatorName("="),
|
|
|
|
hasArgument(1, ConvertedHandle))
|
|
|
|
.bind("bad_stmt")),
|
2016-03-30 02:02:26 +08:00
|
|
|
this);
|
|
|
|
|
|
|
|
// Container insertions that will dangle.
|
2019-11-12 23:15:56 +08:00
|
|
|
Finder->addMatcher(traverse(ast_type_traits::TK_AsIs,
|
|
|
|
makeContainerMatcher(IsAHandle).bind("bad_stmt")),
|
|
|
|
this);
|
2016-03-30 02:02:26 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:16:09 +08:00
|
|
|
void DanglingHandleCheck::registerMatchersForReturn(MatchFinder *Finder) {
|
2016-03-30 02:02:26 +08:00
|
|
|
// Return a local.
|
|
|
|
Finder->addMatcher(
|
2019-11-12 23:15:56 +08:00
|
|
|
traverse(
|
|
|
|
ast_type_traits::TK_AsIs,
|
|
|
|
returnStmt(
|
|
|
|
// The AST contains two constructor calls:
|
|
|
|
// 1. Value to Handle conversion.
|
|
|
|
// 2. Handle copy construction.
|
|
|
|
// We have to match both.
|
|
|
|
has(ignoringImplicit(handleFrom(
|
|
|
|
IsAHandle,
|
|
|
|
handleFrom(IsAHandle,
|
|
|
|
declRefExpr(to(varDecl(
|
|
|
|
// Is function scope ...
|
|
|
|
hasAutomaticStorageDuration(),
|
|
|
|
// ... and it is a local array or Value.
|
|
|
|
anyOf(hasType(arrayType()),
|
|
|
|
hasType(hasUnqualifiedDesugaredType(
|
|
|
|
recordType(hasDeclaration(recordDecl(
|
|
|
|
unless(IsAHandle)))))))))))))),
|
|
|
|
// Temporary fix for false positives inside lambdas.
|
|
|
|
unless(hasAncestor(lambdaExpr())))
|
|
|
|
.bind("bad_stmt")),
|
2016-03-30 02:02:26 +08:00
|
|
|
this);
|
|
|
|
|
|
|
|
// Return a temporary.
|
|
|
|
Finder->addMatcher(
|
2019-11-12 23:15:56 +08:00
|
|
|
traverse(
|
|
|
|
ast_type_traits::TK_AsIs,
|
|
|
|
returnStmt(has(exprWithCleanups(has(ignoringParenImpCasts(handleFrom(
|
|
|
|
IsAHandle, handleFromTemporaryValue(IsAHandle)))))))
|
|
|
|
.bind("bad_stmt")),
|
2016-03-30 02:02:26 +08:00
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:16:09 +08:00
|
|
|
void DanglingHandleCheck::registerMatchers(MatchFinder *Finder) {
|
2016-03-30 02:02:26 +08:00
|
|
|
registerMatchersForVariables(Finder);
|
|
|
|
registerMatchersForReturn(Finder);
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:16:09 +08:00
|
|
|
void DanglingHandleCheck::check(const MatchFinder::MatchResult &Result) {
|
2016-03-30 02:02:26 +08:00
|
|
|
auto *Handle = Result.Nodes.getNodeAs<CXXRecordDecl>("handle");
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(Result.Nodes.getNodeAs<Stmt>("bad_stmt")->getBeginLoc(),
|
2016-03-30 02:02:26 +08:00
|
|
|
"%0 outlives its value")
|
|
|
|
<< Handle->getQualifiedNameAsString();
|
|
|
|
}
|
|
|
|
|
2017-11-24 17:52:05 +08:00
|
|
|
} // namespace bugprone
|
2016-03-30 02:02:26 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|