1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
//===--- DanglingHandleCheck.cpp - clang-tidy------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "DanglingHandleCheck.h"
#include "../utils/Matchers.h"
#include "../utils/OptionsUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
using namespace clang::tidy::matchers;
namespace clang::tidy::bugprone {
namespace {
ast_matchers::internal::BindableMatcher<Stmt>
handleFrom(const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle,
const ast_matchers::internal::Matcher<Expr> &Arg) {
return expr(
anyOf(cxxConstructExpr(hasDeclaration(cxxMethodDecl(ofClass(IsAHandle))),
hasArgument(0, Arg)),
cxxMemberCallExpr(hasType(hasUnqualifiedDesugaredType(recordType(
hasDeclaration(cxxRecordDecl(IsAHandle))))),
callee(memberExpr(member(cxxConversionDecl()))),
on(Arg))));
}
ast_matchers::internal::Matcher<Stmt> handleFromTemporaryValue(
const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
// 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");
}
ast_matchers::internal::BindableMatcher<Stmt> makeContainerMatcher(
const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
// 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(
hasAnyArgument(
ignoringParenImpCasts(handleFromTemporaryValue(IsAHandle))),
anyOf(
// For sequences: assign, push_back, resize.
cxxMemberCallExpr(
callee(functionDecl(hasAnyName("assign", "push_back", "resize"))),
on(expr(hasType(hasUnqualifiedDesugaredType(
recordType(hasDeclaration(recordDecl(isASequence())))))))),
// For sequences and sets: insert.
cxxMemberCallExpr(callee(functionDecl(hasName("insert"))),
on(expr(hasType(hasUnqualifiedDesugaredType(
recordType(hasDeclaration(recordDecl(
anyOf(isASequence(), isASet()))))))))),
// For maps: operator[].
cxxOperatorCallExpr(callee(cxxMethodDecl(ofClass(isAMap()))),
hasOverloadedOperatorName("[]"))));
}
} // anonymous namespace
DanglingHandleCheck::DanglingHandleCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HandleClasses(utils::options::parseStringList(Options.get(
"HandleClasses",
"std::basic_string_view;std::experimental::basic_string_view"))),
IsAHandle(cxxRecordDecl(hasAnyName(HandleClasses)).bind("handle")) {}
void DanglingHandleCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "HandleClasses",
utils::options::serializeStringList(HandleClasses));
}
void DanglingHandleCheck::registerMatchersForVariables(MatchFinder *Finder) {
const auto ConvertedHandle = handleFromTemporaryValue(IsAHandle);
// Find 'Handle foo(ReturnsAValue());'
Finder->addMatcher(
varDecl(hasType(hasUnqualifiedDesugaredType(
recordType(hasDeclaration(cxxRecordDecl(IsAHandle))))),
hasInitializer(
exprWithCleanups(has(ignoringParenImpCasts(ConvertedHandle)))
.bind("bad_stmt"))),
this);
// Find 'Handle foo = ReturnsAValue();'
Finder->addMatcher(
traverse(TK_AsIs,
varDecl(hasType(hasUnqualifiedDesugaredType(recordType(
hasDeclaration(cxxRecordDecl(IsAHandle))))),
unless(parmVarDecl()),
hasInitializer(exprWithCleanups(
has(ignoringParenImpCasts(handleFrom(
IsAHandle, ConvertedHandle))))
.bind("bad_stmt")))),
this);
// Find 'foo = ReturnsAValue(); // foo is Handle'
Finder->addMatcher(
traverse(TK_AsIs,
cxxOperatorCallExpr(callee(cxxMethodDecl(ofClass(IsAHandle))),
hasOverloadedOperatorName("="),
hasArgument(1, ConvertedHandle))
.bind("bad_stmt")),
this);
// Container insertions that will dangle.
Finder->addMatcher(
traverse(TK_AsIs, makeContainerMatcher(IsAHandle).bind("bad_stmt")),
this);
}
void DanglingHandleCheck::registerMatchersForReturn(MatchFinder *Finder) {
// Return a local.
Finder->addMatcher(
traverse(
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")),
this);
// Return a temporary.
Finder->addMatcher(
traverse(
TK_AsIs,
returnStmt(has(exprWithCleanups(has(ignoringParenImpCasts(handleFrom(
IsAHandle, handleFromTemporaryValue(IsAHandle)))))))
.bind("bad_stmt")),
this);
}
void DanglingHandleCheck::registerMatchers(MatchFinder *Finder) {
registerMatchersForVariables(Finder);
registerMatchersForReturn(Finder);
}
void DanglingHandleCheck::check(const MatchFinder::MatchResult &Result) {
auto *Handle = Result.Nodes.getNodeAs<CXXRecordDecl>("handle");
diag(Result.Nodes.getNodeAs<Stmt>("bad_stmt")->getBeginLoc(),
"%0 outlives its value")
<< Handle->getQualifiedNameAsString();
}
} // namespace clang::tidy::bugprone
|