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