| 1 |
//===--- DanglingHandleCheck.cpp - clang-tidy------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "DanglingHandleCheck.h" |
| 10 |
#include "../utils/Matchers.h" |
| 11 |
#include "../utils/OptionsUtils.h" |
| 12 |
#include "clang/AST/ASTContext.h" |
| 13 |
#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 14 |
|
| 15 |
using namespace clang::ast_matchers; |
| 16 |
using namespace clang::tidy::matchers; |
| 17 |
|
| 18 |
namespace clang::tidy::bugprone { |
| 19 |
|
| 20 |
namespace { |
| 21 |
|
| 22 |
ast_matchers::internal::BindableMatcher<Stmt> |
| 23 |
handleFrom(const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle, |
| 24 |
const ast_matchers::internal::Matcher<Expr> &Arg) { |
| 25 |
return expr( |
| 26 |
anyOf(cxxConstructExpr(hasDeclaration(cxxMethodDecl(ofClass(IsAHandle))), |
| 27 |
hasArgument(0, Arg)), |
| 28 |
cxxMemberCallExpr(hasType(cxxRecordDecl(IsAHandle)), |
| 29 |
callee(memberExpr(member(cxxConversionDecl()))), |
| 30 |
on(Arg)))); |
| 31 |
} |
| 32 |
|
| 33 |
ast_matchers::internal::Matcher<Stmt> handleFromTemporaryValue( |
| 34 |
const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) { |
| 35 |
// If a ternary operator returns a temporary value, then both branches hold a |
| 36 |
// temporary value. If one of them is not a temporary then it must be copied |
| 37 |
// into one to satisfy the type of the operator. |
| 38 |
const auto TemporaryTernary = |
| 39 |
conditionalOperator(hasTrueExpression(cxxBindTemporaryExpr()), |
| 40 |
hasFalseExpression(cxxBindTemporaryExpr())); |
| 41 |
|
| 42 |
return handleFrom(IsAHandle, anyOf(cxxBindTemporaryExpr(), TemporaryTernary)); |
| 43 |
} |
| 44 |
|
| 45 |
ast_matchers::internal::Matcher<RecordDecl> isASequence() { |
| 46 |
return hasAnyName("::std::deque", "::std::forward_list", "::std::list", |
| 47 |
"::std::vector"); |
| 48 |
} |
| 49 |
|
| 50 |
ast_matchers::internal::Matcher<RecordDecl> isASet() { |
| 51 |
return hasAnyName("::std::set", "::std::multiset", "::std::unordered_set", |
| 52 |
"::std::unordered_multiset"); |
| 53 |
} |
| 54 |
|
| 55 |
ast_matchers::internal::Matcher<RecordDecl> isAMap() { |
| 56 |
return hasAnyName("::std::map", "::std::multimap", "::std::unordered_map", |
| 57 |
"::std::unordered_multimap"); |
| 58 |
} |
| 59 |
|
| 60 |
ast_matchers::internal::BindableMatcher<Stmt> makeContainerMatcher( |
| 61 |
const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) { |
| 62 |
// This matcher could be expanded to detect: |
| 63 |
// - Constructors: eg. vector<string_view>(3, string("A")); |
| 64 |
// - emplace*(): This requires a different logic to determine that |
| 65 |
// the conversion will happen inside the container. |
| 66 |
// - map's insert: This requires detecting that the pair conversion triggers |
| 67 |
// the bug. A little more complicated than what we have now. |
| 68 |
return callExpr( |
| 69 |
hasAnyArgument( |
| 70 |
ignoringParenImpCasts(handleFromTemporaryValue(IsAHandle))), |
| 71 |
anyOf( |
| 72 |
// For sequences: assign, push_back, resize. |
| 73 |
cxxMemberCallExpr( |
| 74 |
callee(functionDecl(hasAnyName("assign", "push_back", "resize"))), |
| 75 |
on(expr(hasType(hasUnqualifiedDesugaredType( |
| 76 |
recordType(hasDeclaration(recordDecl(isASequence())))))))), |
| 77 |
// For sequences and sets: insert. |
| 78 |
cxxMemberCallExpr(callee(functionDecl(hasName("insert"))), |
| 79 |
on(expr(hasType(hasUnqualifiedDesugaredType( |
| 80 |
recordType(hasDeclaration(recordDecl( |
| 81 |
anyOf(isASequence(), isASet()))))))))), |
| 82 |
// For maps: operator[]. |
| 83 |
cxxOperatorCallExpr(callee(cxxMethodDecl(ofClass(isAMap()))), |
| 84 |
hasOverloadedOperatorName("[]")))); |
| 85 |
} |
| 86 |
|
| 87 |
} // anonymous namespace |
| 88 |
|
| 89 |
DanglingHandleCheck::DanglingHandleCheck(StringRef Name, |
| 90 |
ClangTidyContext *Context) |
| 91 |
: ClangTidyCheck(Name, Context), |
| 92 |
HandleClasses(utils::options::parseStringList(Options.get( |
| 93 |
"HandleClasses", |
| 94 |
"std::basic_string_view;std::experimental::basic_string_view"))), |
| 95 |
IsAHandle(cxxRecordDecl(hasAnyName(HandleClasses)).bind("handle")) {} |
| 96 |
|
| 97 |
void DanglingHandleCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 98 |
Options.store(Opts, "HandleClasses", |
| 99 |
utils::options::serializeStringList(HandleClasses)); |
| 100 |
} |
| 101 |
|
| 102 |
void DanglingHandleCheck::registerMatchersForVariables(MatchFinder *Finder) { |
| 103 |
const auto ConvertedHandle = handleFromTemporaryValue(IsAHandle); |
| 104 |
|
| 105 |
// Find 'Handle foo(ReturnsAValue());' |
| 106 |
Finder->addMatcher( |
| 107 |
varDecl(hasType(hasUnqualifiedDesugaredType( |
| 108 |
recordType(hasDeclaration(cxxRecordDecl(IsAHandle))))), |
| 109 |
hasInitializer( |
| 110 |
exprWithCleanups(has(ignoringParenImpCasts(ConvertedHandle))) |
| 111 |
.bind("bad_stmt"))), |
| 112 |
this); |
| 113 |
|
| 114 |
// Find 'Handle foo = ReturnsAValue();' |
| 115 |
Finder->addMatcher( |
| 116 |
traverse(TK_AsIs, |
| 117 |
varDecl(hasType(hasUnqualifiedDesugaredType(recordType( |
| 118 |
hasDeclaration(cxxRecordDecl(IsAHandle))))), |
| 119 |
unless(parmVarDecl()), |
| 120 |
hasInitializer(exprWithCleanups( |
| 121 |
has(ignoringParenImpCasts(handleFrom( |
| 122 |
IsAHandle, ConvertedHandle)))) |
| 123 |
.bind("bad_stmt")))), |
| 124 |
this); |
| 125 |
// Find 'foo = ReturnsAValue(); // foo is Handle' |
| 126 |
Finder->addMatcher( |
| 127 |
traverse(TK_AsIs, |
| 128 |
cxxOperatorCallExpr(callee(cxxMethodDecl(ofClass(IsAHandle))), |
| 129 |
hasOverloadedOperatorName("="), |
| 130 |
hasArgument(1, ConvertedHandle)) |
| 131 |
.bind("bad_stmt")), |
| 132 |
this); |
| 133 |
|
| 134 |
// Container insertions that will dangle. |
| 135 |
Finder->addMatcher( |
| 136 |
traverse(TK_AsIs, makeContainerMatcher(IsAHandle).bind("bad_stmt")), |
| 137 |
this); |
| 138 |
} |
| 139 |
|
| 140 |
void DanglingHandleCheck::registerMatchersForReturn(MatchFinder *Finder) { |
| 141 |
// Return a local. |
| 142 |
Finder->addMatcher( |
| 143 |
traverse( |
| 144 |
TK_AsIs, |
| 145 |
returnStmt( |
| 146 |
// The AST contains two constructor calls: |
| 147 |
// 1. Value to Handle conversion. |
| 148 |
// 2. Handle copy construction. |
| 149 |
// We have to match both. |
| 150 |
has(ignoringImplicit(handleFrom( |
| 151 |
IsAHandle, |
| 152 |
handleFrom(IsAHandle, |
| 153 |
declRefExpr(to(varDecl( |
| 154 |
// Is function scope ... |
| 155 |
hasAutomaticStorageDuration(), |
| 156 |
// ... and it is a local array or Value. |
| 157 |
anyOf(hasType(arrayType()), |
| 158 |
hasType(hasUnqualifiedDesugaredType( |
| 159 |
recordType(hasDeclaration(recordDecl( |
| 160 |
unless(IsAHandle)))))))))))))), |
| 161 |
// Temporary fix for false positives inside lambdas. |
| 162 |
unless(hasAncestor(lambdaExpr()))) |
| 163 |
.bind("bad_stmt")), |
| 164 |
this); |
| 165 |
|
| 166 |
// Return a temporary. |
| 167 |
Finder->addMatcher( |
| 168 |
traverse( |
| 169 |
TK_AsIs, |
| 170 |
returnStmt(has(exprWithCleanups(has(ignoringParenImpCasts(handleFrom( |
| 171 |
IsAHandle, handleFromTemporaryValue(IsAHandle))))))) |
| 172 |
.bind("bad_stmt")), |
| 173 |
this); |
| 174 |
} |
| 175 |
|
| 176 |
void DanglingHandleCheck::registerMatchers(MatchFinder *Finder) { |
| 177 |
registerMatchersForVariables(Finder); |
| 178 |
registerMatchersForReturn(Finder); |
| 179 |
} |
| 180 |
|
| 181 |
void DanglingHandleCheck::check(const MatchFinder::MatchResult &Result) { |
| 182 |
auto *Handle = Result.Nodes.getNodeAs<CXXRecordDecl>("handle"); |
| 183 |
diag(Result.Nodes.getNodeAs<Stmt>("bad_stmt")->getBeginLoc(), |
| 184 |
"%0 outlives its value") |
| 185 |
<< Handle->getQualifiedNameAsString(); |
| 186 |
} |
| 187 |
|
| 188 |
} // namespace clang::tidy::bugprone |