Bug Summary

File:build/source/clang/include/clang/Analysis/FlowSensitive/CFGMatchSwitch.h
Warning:line 71, column 12
Potential memory leak

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name UncheckedOptionalAccessModel.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-16/lib/clang/16 -I tools/clang/lib/Analysis/FlowSensitive/Models -I /build/source/clang/lib/Analysis/FlowSensitive/Models -I /build/source/clang/include -I tools/clang/include -I include -I /build/source/llvm/include -D CLANG_REPOSITORY_STRING="++20221204100757+8d6589cba7cd-1~exp1~20221204220919.942" -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/source/= -fcoverage-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/source/= -source-date-epoch 1670191760 -O2 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-12-05-012027-15999-1 -x c++ /build/source/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp

/build/source/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp

1//===-- UncheckedOptionalAccessModel.cpp ------------------------*- C++ -*-===//
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// This file defines a dataflow analysis that detects unsafe uses of optional
10// values.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclCXX.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/Stmt.h"
20#include "clang/ASTMatchers/ASTMatchers.h"
21#include "clang/Analysis/CFG.h"
22#include "clang/Analysis/FlowSensitive/CFGMatchSwitch.h"
23#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
24#include "clang/Analysis/FlowSensitive/NoopLattice.h"
25#include "clang/Analysis/FlowSensitive/Value.h"
26#include "clang/Basic/SourceLocation.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/Support/Casting.h"
29#include <cassert>
30#include <memory>
31#include <utility>
32#include <vector>
33
34namespace clang {
35namespace dataflow {
36namespace {
37
38using namespace ::clang::ast_matchers;
39using LatticeTransferState = TransferState<NoopLattice>;
40
41DeclarationMatcher optionalClass() {
42 return classTemplateSpecializationDecl(
43 anyOf(hasName("std::optional"), hasName("std::__optional_storage_base"),
44 hasName("__optional_destruct_base"), hasName("absl::optional"),
45 hasName("base::Optional")),
46 hasTemplateArgument(0, refersToType(type().bind("T"))));
47}
48
49auto optionalOrAliasType() {
50 return hasUnqualifiedDesugaredType(
51 recordType(hasDeclaration(optionalClass())));
52}
53
54/// Matches any of the spellings of the optional types and sugar, aliases, etc.
55auto hasOptionalType() { return hasType(optionalOrAliasType()); }
56
57auto isOptionalMemberCallWithName(
58 llvm::StringRef MemberName,
59 llvm::Optional<StatementMatcher> Ignorable = std::nullopt) {
60 auto Exception = unless(Ignorable ? expr(anyOf(*Ignorable, cxxThisExpr()))
61 : cxxThisExpr());
62 return cxxMemberCallExpr(
63 on(expr(Exception)),
64 callee(cxxMethodDecl(hasName(MemberName), ofClass(optionalClass()))));
65}
66
67auto isOptionalOperatorCallWithName(
68 llvm::StringRef operator_name,
69 llvm::Optional<StatementMatcher> Ignorable = std::nullopt) {
70 return cxxOperatorCallExpr(
71 hasOverloadedOperatorName(operator_name),
72 callee(cxxMethodDecl(ofClass(optionalClass()))),
73 Ignorable ? callExpr(unless(hasArgument(0, *Ignorable))) : callExpr());
74}
75
76auto isMakeOptionalCall() {
77 return callExpr(
78 callee(functionDecl(hasAnyName(
79 "std::make_optional", "base::make_optional", "absl::make_optional"))),
80 hasOptionalType());
81}
82
83auto hasNulloptType() {
84 return hasType(namedDecl(
85 hasAnyName("std::nullopt_t", "absl::nullopt_t", "base::nullopt_t")));
86}
87
88auto inPlaceClass() {
89 return recordDecl(
90 hasAnyName("std::in_place_t", "absl::in_place_t", "base::in_place_t"));
91}
92
93auto isOptionalNulloptConstructor() {
94 return cxxConstructExpr(hasOptionalType(), argumentCountIs(1),
95 hasArgument(0, hasNulloptType()));
96}
97
98auto isOptionalInPlaceConstructor() {
99 return cxxConstructExpr(hasOptionalType(),
100 hasArgument(0, hasType(inPlaceClass())));
101}
102
103auto isOptionalValueOrConversionConstructor() {
104 return cxxConstructExpr(
105 hasOptionalType(),
106 unless(hasDeclaration(
107 cxxConstructorDecl(anyOf(isCopyConstructor(), isMoveConstructor())))),
108 argumentCountIs(1), hasArgument(0, unless(hasNulloptType())));
109}
110
111auto isOptionalValueOrConversionAssignment() {
112 return cxxOperatorCallExpr(
113 hasOverloadedOperatorName("="),
114 callee(cxxMethodDecl(ofClass(optionalClass()))),
115 unless(hasDeclaration(cxxMethodDecl(
116 anyOf(isCopyAssignmentOperator(), isMoveAssignmentOperator())))),
117 argumentCountIs(2), hasArgument(1, unless(hasNulloptType())));
118}
119
120auto isOptionalNulloptAssignment() {
121 return cxxOperatorCallExpr(hasOverloadedOperatorName("="),
122 callee(cxxMethodDecl(ofClass(optionalClass()))),
123 argumentCountIs(2),
124 hasArgument(1, hasNulloptType()));
125}
126
127auto isStdSwapCall() {
128 return callExpr(callee(functionDecl(hasName("std::swap"))),
129 argumentCountIs(2), hasArgument(0, hasOptionalType()),
130 hasArgument(1, hasOptionalType()));
131}
132
133constexpr llvm::StringLiteral ValueOrCallID = "ValueOrCall";
134
135auto isValueOrStringEmptyCall() {
136 // `opt.value_or("").empty()`
137 return cxxMemberCallExpr(
138 callee(cxxMethodDecl(hasName("empty"))),
139 onImplicitObjectArgument(ignoringImplicit(
140 cxxMemberCallExpr(on(expr(unless(cxxThisExpr()))),
141 callee(cxxMethodDecl(hasName("value_or"),
142 ofClass(optionalClass()))),
143 hasArgument(0, stringLiteral(hasSize(0))))
144 .bind(ValueOrCallID))));
145}
146
147auto isValueOrNotEqX() {
148 auto ComparesToSame = [](ast_matchers::internal::Matcher<Stmt> Arg) {
149 return hasOperands(
150 ignoringImplicit(
151 cxxMemberCallExpr(on(expr(unless(cxxThisExpr()))),
152 callee(cxxMethodDecl(hasName("value_or"),
153 ofClass(optionalClass()))),
154 hasArgument(0, Arg))
155 .bind(ValueOrCallID)),
156 ignoringImplicit(Arg));
157 };
158
159 // `opt.value_or(X) != X`, for X is `nullptr`, `""`, or `0`. Ideally, we'd
160 // support this pattern for any expression, but the AST does not have a
161 // generic expression comparison facility, so we specialize to common cases
162 // seen in practice. FIXME: define a matcher that compares values across
163 // nodes, which would let us generalize this to any `X`.
164 return binaryOperation(hasOperatorName("!="),
165 anyOf(ComparesToSame(cxxNullPtrLiteralExpr()),
166 ComparesToSame(stringLiteral(hasSize(0))),
167 ComparesToSame(integerLiteral(equals(0)))));
168}
169
170auto isCallReturningOptional() {
171 return callExpr(hasType(qualType(anyOf(
172 optionalOrAliasType(), referenceType(pointee(optionalOrAliasType()))))));
173}
174
175/// Sets `HasValueVal` as the symbolic value that represents the "has_value"
176/// property of the optional value `OptionalVal`.
177void setHasValue(Value &OptionalVal, BoolValue &HasValueVal) {
178 OptionalVal.setProperty("has_value", HasValueVal);
179}
180
181/// Creates a symbolic value for an `optional` value using `HasValueVal` as the
182/// symbolic value of its "has_value" property.
183StructValue &createOptionalValue(Environment &Env, BoolValue &HasValueVal) {
184 auto OptionalVal = std::make_unique<StructValue>();
185 setHasValue(*OptionalVal, HasValueVal);
186 return Env.takeOwnership(std::move(OptionalVal));
187}
188
189/// Returns the symbolic value that represents the "has_value" property of the
190/// optional value `OptionalVal`. Returns null if `OptionalVal` is null.
191BoolValue *getHasValue(Environment &Env, Value *OptionalVal) {
192 if (OptionalVal != nullptr) {
193 auto *HasValueVal =
194 cast_or_null<BoolValue>(OptionalVal->getProperty("has_value"));
195 if (HasValueVal == nullptr) {
196 HasValueVal = &Env.makeAtomicBoolValue();
197 OptionalVal->setProperty("has_value", *HasValueVal);
198 }
199 return HasValueVal;
200 }
201 return nullptr;
202}
203
204/// If `Type` is a reference type, returns the type of its pointee. Otherwise,
205/// returns `Type` itself.
206QualType stripReference(QualType Type) {
207 return Type->isReferenceType() ? Type->getPointeeType() : Type;
208}
209
210/// Returns true if and only if `Type` is an optional type.
211bool isOptionalType(QualType Type) {
212 if (!Type->isRecordType())
213 return false;
214 // FIXME: Optimize this by avoiding the `getQualifiedNameAsString` call.
215 auto TypeName = Type->getAsCXXRecordDecl()->getQualifiedNameAsString();
216 return TypeName == "std::optional" || TypeName == "absl::optional" ||
217 TypeName == "base::Optional";
218}
219
220/// Returns the number of optional wrappers in `Type`.
221///
222/// For example, if `Type` is `optional<optional<int>>`, the result of this
223/// function will be 2.
224int countOptionalWrappers(const ASTContext &ASTCtx, QualType Type) {
225 if (!isOptionalType(Type))
226 return 0;
227 return 1 + countOptionalWrappers(
228 ASTCtx,
229 cast<ClassTemplateSpecializationDecl>(Type->getAsRecordDecl())
230 ->getTemplateArgs()
231 .get(0)
232 .getAsType()
233 .getDesugaredType(ASTCtx));
234}
235
236/// Tries to initialize the `optional`'s value (that is, contents), and return
237/// its location. Returns nullptr if the value can't be represented.
238StorageLocation *maybeInitializeOptionalValueMember(QualType Q,
239 Value &OptionalVal,
240 Environment &Env) {
241 // The "value" property represents a synthetic field. As such, it needs
242 // `StorageLocation`, like normal fields (and other variables). So, we model
243 // it with a `ReferenceValue`, since that includes a storage location. Once
244 // the property is set, it will be shared by all environments that access the
245 // `Value` representing the optional (here, `OptionalVal`).
246 if (auto *ValueProp = OptionalVal.getProperty("value")) {
247 auto *ValueRef = clang::cast<ReferenceValue>(ValueProp);
248 auto &ValueLoc = ValueRef->getReferentLoc();
249 if (Env.getValue(ValueLoc) == nullptr) {
250 // The property was previously set, but the value has been lost. This can
251 // happen, for example, because of an environment merge (where the two
252 // environments mapped the property to different values, which resulted in
253 // them both being discarded), or when two blocks in the CFG, with neither
254 // a dominator of the other, visit the same optional value, or even when a
255 // block is revisited during testing to collect per-statement state.
256 // FIXME: This situation means that the optional contents are not shared
257 // between branches and the like. Practically, this lack of sharing
258 // reduces the precision of the model when the contents are relevant to
259 // the check, like another optional or a boolean that influences control
260 // flow.
261 auto *ValueVal = Env.createValue(ValueLoc.getType());
262 if (ValueVal == nullptr)
263 return nullptr;
264 Env.setValue(ValueLoc, *ValueVal);
265 }
266 return &ValueLoc;
267 }
268
269 auto Ty = stripReference(Q);
270 auto *ValueVal = Env.createValue(Ty);
271 if (ValueVal == nullptr)
272 return nullptr;
273 auto &ValueLoc = Env.createStorageLocation(Ty);
274 Env.setValue(ValueLoc, *ValueVal);
275 auto ValueRef = std::make_unique<ReferenceValue>(ValueLoc);
276 OptionalVal.setProperty("value", Env.takeOwnership(std::move(ValueRef)));
277 return &ValueLoc;
278}
279
280void initializeOptionalReference(const Expr *OptionalExpr,
281 const MatchFinder::MatchResult &,
282 LatticeTransferState &State) {
283 if (auto *OptionalVal =
284 State.Env.getValue(*OptionalExpr, SkipPast::Reference)) {
285 if (OptionalVal->getProperty("has_value") == nullptr) {
286 setHasValue(*OptionalVal, State.Env.makeAtomicBoolValue());
287 }
288 }
289}
290
291/// Returns true if and only if `OptionalVal` is initialized and known to be
292/// empty in `Env.
293bool isEmptyOptional(const Value &OptionalVal, const Environment &Env) {
294 auto *HasValueVal =
295 cast_or_null<BoolValue>(OptionalVal.getProperty("has_value"));
296 return HasValueVal != nullptr &&
297 Env.flowConditionImplies(Env.makeNot(*HasValueVal));
298}
299
300/// Returns true if and only if `OptionalVal` is initialized and known to be
301/// non-empty in `Env.
302bool isNonEmptyOptional(const Value &OptionalVal, const Environment &Env) {
303 auto *HasValueVal =
304 cast_or_null<BoolValue>(OptionalVal.getProperty("has_value"));
305 return HasValueVal != nullptr && Env.flowConditionImplies(*HasValueVal);
306}
307
308void transferUnwrapCall(const Expr *UnwrapExpr, const Expr *ObjectExpr,
309 LatticeTransferState &State) {
310 if (auto *OptionalVal =
311 State.Env.getValue(*ObjectExpr, SkipPast::ReferenceThenPointer)) {
312 if (State.Env.getStorageLocation(*UnwrapExpr, SkipPast::None) == nullptr)
313 if (auto *Loc = maybeInitializeOptionalValueMember(
314 UnwrapExpr->getType(), *OptionalVal, State.Env))
315 State.Env.setStorageLocation(*UnwrapExpr, *Loc);
316 }
317}
318
319void transferMakeOptionalCall(const CallExpr *E,
320 const MatchFinder::MatchResult &,
321 LatticeTransferState &State) {
322 auto &Loc = State.Env.createStorageLocation(*E);
323 State.Env.setStorageLocation(*E, Loc);
324 State.Env.setValue(
325 Loc, createOptionalValue(State.Env, State.Env.getBoolLiteralValue(true)));
326}
327
328void transferOptionalHasValueCall(const CXXMemberCallExpr *CallExpr,
329 const MatchFinder::MatchResult &,
330 LatticeTransferState &State) {
331 if (auto *HasValueVal = getHasValue(
332 State.Env, State.Env.getValue(*CallExpr->getImplicitObjectArgument(),
333 SkipPast::ReferenceThenPointer))) {
334 auto &CallExprLoc = State.Env.createStorageLocation(*CallExpr);
335 State.Env.setValue(CallExprLoc, *HasValueVal);
336 State.Env.setStorageLocation(*CallExpr, CallExprLoc);
337 }
338}
339
340/// `ModelPred` builds a logical formula relating the predicate in
341/// `ValueOrPredExpr` to the optional's `has_value` property.
342void transferValueOrImpl(const clang::Expr *ValueOrPredExpr,
343 const MatchFinder::MatchResult &Result,
344 LatticeTransferState &State,
345 BoolValue &(*ModelPred)(Environment &Env,
346 BoolValue &ExprVal,
347 BoolValue &HasValueVal)) {
348 auto &Env = State.Env;
349
350 const auto *ObjectArgumentExpr =
351 Result.Nodes.getNodeAs<clang::CXXMemberCallExpr>(ValueOrCallID)
352 ->getImplicitObjectArgument();
353
354 auto *HasValueVal = getHasValue(
355 State.Env,
356 State.Env.getValue(*ObjectArgumentExpr, SkipPast::ReferenceThenPointer));
357 if (HasValueVal == nullptr)
358 return;
359
360 auto *ExprValue = cast_or_null<BoolValue>(
361 State.Env.getValue(*ValueOrPredExpr, SkipPast::None));
362 if (ExprValue == nullptr) {
363 auto &ExprLoc = State.Env.createStorageLocation(*ValueOrPredExpr);
364 ExprValue = &State.Env.makeAtomicBoolValue();
365 State.Env.setValue(ExprLoc, *ExprValue);
366 State.Env.setStorageLocation(*ValueOrPredExpr, ExprLoc);
367 }
368
369 Env.addToFlowCondition(ModelPred(Env, *ExprValue, *HasValueVal));
370}
371
372void transferValueOrStringEmptyCall(const clang::Expr *ComparisonExpr,
373 const MatchFinder::MatchResult &Result,
374 LatticeTransferState &State) {
375 return transferValueOrImpl(ComparisonExpr, Result, State,
376 [](Environment &Env, BoolValue &ExprVal,
377 BoolValue &HasValueVal) -> BoolValue & {
378 // If the result is *not* empty, then we know the
379 // optional must have been holding a value. If
380 // `ExprVal` is true, though, we don't learn
381 // anything definite about `has_value`, so we
382 // don't add any corresponding implications to
383 // the flow condition.
384 return Env.makeImplication(Env.makeNot(ExprVal),
385 HasValueVal);
386 });
387}
388
389void transferValueOrNotEqX(const Expr *ComparisonExpr,
390 const MatchFinder::MatchResult &Result,
391 LatticeTransferState &State) {
392 transferValueOrImpl(ComparisonExpr, Result, State,
393 [](Environment &Env, BoolValue &ExprVal,
394 BoolValue &HasValueVal) -> BoolValue & {
395 // We know that if `(opt.value_or(X) != X)` then
396 // `opt.hasValue()`, even without knowing further
397 // details about the contents of `opt`.
398 return Env.makeImplication(ExprVal, HasValueVal);
399 });
400}
401
402void transferCallReturningOptional(const CallExpr *E,
403 const MatchFinder::MatchResult &Result,
404 LatticeTransferState &State) {
405 if (State.Env.getStorageLocation(*E, SkipPast::None) != nullptr)
406 return;
407
408 auto &Loc = State.Env.createStorageLocation(*E);
409 State.Env.setStorageLocation(*E, Loc);
410 State.Env.setValue(
411 Loc, createOptionalValue(State.Env, State.Env.makeAtomicBoolValue()));
412}
413
414void assignOptionalValue(const Expr &E, LatticeTransferState &State,
415 BoolValue &HasValueVal) {
416 if (auto *OptionalLoc =
417 State.Env.getStorageLocation(E, SkipPast::ReferenceThenPointer)) {
418 State.Env.setValue(*OptionalLoc,
419 createOptionalValue(State.Env, HasValueVal));
420 }
421}
422
423/// Returns a symbolic value for the "has_value" property of an `optional<T>`
424/// value that is constructed/assigned from a value of type `U` or `optional<U>`
425/// where `T` is constructible from `U`.
426BoolValue &value_orConversionHasValue(const FunctionDecl &F, const Expr &E,
427 const MatchFinder::MatchResult &MatchRes,
428 LatticeTransferState &State) {
429 assert(F.getTemplateSpecializationArgs()->size() > 0)(static_cast <bool> (F.getTemplateSpecializationArgs()->
size() > 0) ? void (0) : __assert_fail ("F.getTemplateSpecializationArgs()->size() > 0"
, "clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp"
, 429, __extension__ __PRETTY_FUNCTION__))
;
430
431 const int TemplateParamOptionalWrappersCount = countOptionalWrappers(
432 *MatchRes.Context,
433 stripReference(F.getTemplateSpecializationArgs()->get(0).getAsType()));
434 const int ArgTypeOptionalWrappersCount =
435 countOptionalWrappers(*MatchRes.Context, stripReference(E.getType()));
436
437 // Check if this is a constructor/assignment call for `optional<T>` with
438 // argument of type `U` such that `T` is constructible from `U`.
439 if (TemplateParamOptionalWrappersCount == ArgTypeOptionalWrappersCount)
440 return State.Env.getBoolLiteralValue(true);
441
442 // This is a constructor/assignment call for `optional<T>` with argument of
443 // type `optional<U>` such that `T` is constructible from `U`.
444 if (auto *HasValueVal =
445 getHasValue(State.Env, State.Env.getValue(E, SkipPast::Reference)))
446 return *HasValueVal;
447 return State.Env.makeAtomicBoolValue();
448}
449
450void transferValueOrConversionConstructor(
451 const CXXConstructExpr *E, const MatchFinder::MatchResult &MatchRes,
452 LatticeTransferState &State) {
453 assert(E->getNumArgs() > 0)(static_cast <bool> (E->getNumArgs() > 0) ? void (
0) : __assert_fail ("E->getNumArgs() > 0", "clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp"
, 453, __extension__ __PRETTY_FUNCTION__))
;
454
455 assignOptionalValue(*E, State,
456 value_orConversionHasValue(*E->getConstructor(),
457 *E->getArg(0), MatchRes,
458 State));
459}
460
461void transferAssignment(const CXXOperatorCallExpr *E, BoolValue &HasValueVal,
462 LatticeTransferState &State) {
463 assert(E->getNumArgs() > 0)(static_cast <bool> (E->getNumArgs() > 0) ? void (
0) : __assert_fail ("E->getNumArgs() > 0", "clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp"
, 463, __extension__ __PRETTY_FUNCTION__))
;
464
465 auto *OptionalLoc =
466 State.Env.getStorageLocation(*E->getArg(0), SkipPast::Reference);
467 if (OptionalLoc == nullptr)
468 return;
469
470 State.Env.setValue(*OptionalLoc, createOptionalValue(State.Env, HasValueVal));
471
472 // Assign a storage location for the whole expression.
473 State.Env.setStorageLocation(*E, *OptionalLoc);
474}
475
476void transferValueOrConversionAssignment(
477 const CXXOperatorCallExpr *E, const MatchFinder::MatchResult &MatchRes,
478 LatticeTransferState &State) {
479 assert(E->getNumArgs() > 1)(static_cast <bool> (E->getNumArgs() > 1) ? void (
0) : __assert_fail ("E->getNumArgs() > 1", "clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp"
, 479, __extension__ __PRETTY_FUNCTION__))
;
480 transferAssignment(E,
481 value_orConversionHasValue(*E->getDirectCallee(),
482 *E->getArg(1), MatchRes, State),
483 State);
484}
485
486void transferNulloptAssignment(const CXXOperatorCallExpr *E,
487 const MatchFinder::MatchResult &,
488 LatticeTransferState &State) {
489 transferAssignment(E, State.Env.getBoolLiteralValue(false), State);
490}
491
492void transferSwap(const StorageLocation &OptionalLoc1,
493 const StorageLocation &OptionalLoc2,
494 LatticeTransferState &State) {
495 auto *OptionalVal1 = State.Env.getValue(OptionalLoc1);
496 assert(OptionalVal1 != nullptr)(static_cast <bool> (OptionalVal1 != nullptr) ? void (0
) : __assert_fail ("OptionalVal1 != nullptr", "clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp"
, 496, __extension__ __PRETTY_FUNCTION__))
;
497
498 auto *OptionalVal2 = State.Env.getValue(OptionalLoc2);
499 assert(OptionalVal2 != nullptr)(static_cast <bool> (OptionalVal2 != nullptr) ? void (0
) : __assert_fail ("OptionalVal2 != nullptr", "clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp"
, 499, __extension__ __PRETTY_FUNCTION__))
;
500
501 State.Env.setValue(OptionalLoc1, *OptionalVal2);
502 State.Env.setValue(OptionalLoc2, *OptionalVal1);
503}
504
505void transferSwapCall(const CXXMemberCallExpr *E,
506 const MatchFinder::MatchResult &,
507 LatticeTransferState &State) {
508 assert(E->getNumArgs() == 1)(static_cast <bool> (E->getNumArgs() == 1) ? void (0
) : __assert_fail ("E->getNumArgs() == 1", "clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp"
, 508, __extension__ __PRETTY_FUNCTION__))
;
509
510 auto *OptionalLoc1 = State.Env.getStorageLocation(
511 *E->getImplicitObjectArgument(), SkipPast::ReferenceThenPointer);
512 assert(OptionalLoc1 != nullptr)(static_cast <bool> (OptionalLoc1 != nullptr) ? void (0
) : __assert_fail ("OptionalLoc1 != nullptr", "clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp"
, 512, __extension__ __PRETTY_FUNCTION__))
;
513
514 auto *OptionalLoc2 =
515 State.Env.getStorageLocation(*E->getArg(0), SkipPast::Reference);
516 assert(OptionalLoc2 != nullptr)(static_cast <bool> (OptionalLoc2 != nullptr) ? void (0
) : __assert_fail ("OptionalLoc2 != nullptr", "clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp"
, 516, __extension__ __PRETTY_FUNCTION__))
;
517
518 transferSwap(*OptionalLoc1, *OptionalLoc2, State);
519}
520
521void transferStdSwapCall(const CallExpr *E, const MatchFinder::MatchResult &,
522 LatticeTransferState &State) {
523 assert(E->getNumArgs() == 2)(static_cast <bool> (E->getNumArgs() == 2) ? void (0
) : __assert_fail ("E->getNumArgs() == 2", "clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp"
, 523, __extension__ __PRETTY_FUNCTION__))
;
524
525 auto *OptionalLoc1 =
526 State.Env.getStorageLocation(*E->getArg(0), SkipPast::Reference);
527 assert(OptionalLoc1 != nullptr)(static_cast <bool> (OptionalLoc1 != nullptr) ? void (0
) : __assert_fail ("OptionalLoc1 != nullptr", "clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp"
, 527, __extension__ __PRETTY_FUNCTION__))
;
528
529 auto *OptionalLoc2 =
530 State.Env.getStorageLocation(*E->getArg(1), SkipPast::Reference);
531 assert(OptionalLoc2 != nullptr)(static_cast <bool> (OptionalLoc2 != nullptr) ? void (0
) : __assert_fail ("OptionalLoc2 != nullptr", "clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp"
, 531, __extension__ __PRETTY_FUNCTION__))
;
532
533 transferSwap(*OptionalLoc1, *OptionalLoc2, State);
534}
535
536llvm::Optional<StatementMatcher>
537ignorableOptional(const UncheckedOptionalAccessModelOptions &Options) {
538 if (Options.IgnoreSmartPointerDereference)
539 return memberExpr(hasObjectExpression(ignoringParenImpCasts(
540 cxxOperatorCallExpr(anyOf(hasOverloadedOperatorName("->"),
541 hasOverloadedOperatorName("*")),
542 unless(hasArgument(0, expr(hasOptionalType())))))));
543 return std::nullopt;
544}
545
546StatementMatcher
547valueCall(llvm::Optional<StatementMatcher> &IgnorableOptional) {
548 return isOptionalMemberCallWithName("value", IgnorableOptional);
549}
550
551StatementMatcher
552valueOperatorCall(llvm::Optional<StatementMatcher> &IgnorableOptional) {
553 return expr(anyOf(isOptionalOperatorCallWithName("*", IgnorableOptional),
554 isOptionalOperatorCallWithName("->", IgnorableOptional)));
555}
556
557auto buildTransferMatchSwitch(
558 const UncheckedOptionalAccessModelOptions &Options) {
559 // FIXME: Evaluate the efficiency of matchers. If using matchers results in a
560 // lot of duplicated work (e.g. string comparisons), consider providing APIs
561 // that avoid it through memoization.
562 auto IgnorableOptional = ignorableOptional(Options);
563 return CFGMatchSwitchBuilder<LatticeTransferState>()
564 // Attach a symbolic "has_value" state to optional values that we see for
565 // the first time.
566 .CaseOfCFGStmt<Expr>(
567 expr(anyOf(declRefExpr(), memberExpr()), hasOptionalType()),
568 initializeOptionalReference)
569
570 // make_optional
571 .CaseOfCFGStmt<CallExpr>(isMakeOptionalCall(), transferMakeOptionalCall)
572
573 // optional::optional
574 .CaseOfCFGStmt<CXXConstructExpr>(
575 isOptionalInPlaceConstructor(),
576 [](const CXXConstructExpr *E, const MatchFinder::MatchResult &,
577 LatticeTransferState &State) {
578 assignOptionalValue(*E, State, State.Env.getBoolLiteralValue(true));
579 })
580 .CaseOfCFGStmt<CXXConstructExpr>(
581 isOptionalNulloptConstructor(),
582 [](const CXXConstructExpr *E, const MatchFinder::MatchResult &,
583 LatticeTransferState &State) {
584 assignOptionalValue(*E, State,
585 State.Env.getBoolLiteralValue(false));
586 })
587 .CaseOfCFGStmt<CXXConstructExpr>(isOptionalValueOrConversionConstructor(),
588 transferValueOrConversionConstructor)
589
590 // optional::operator=
591 .CaseOfCFGStmt<CXXOperatorCallExpr>(
592 isOptionalValueOrConversionAssignment(),
593 transferValueOrConversionAssignment)
594 .CaseOfCFGStmt<CXXOperatorCallExpr>(isOptionalNulloptAssignment(),
595 transferNulloptAssignment)
596
597 // optional::value
598 .CaseOfCFGStmt<CXXMemberCallExpr>(
599 valueCall(IgnorableOptional),
600 [](const CXXMemberCallExpr *E, const MatchFinder::MatchResult &,
601 LatticeTransferState &State) {
602 transferUnwrapCall(E, E->getImplicitObjectArgument(), State);
603 })
604
605 // optional::operator*, optional::operator->
606 .CaseOfCFGStmt<CallExpr>(valueOperatorCall(IgnorableOptional),
607 [](const CallExpr *E,
608 const MatchFinder::MatchResult &,
609 LatticeTransferState &State) {
610 transferUnwrapCall(E, E->getArg(0), State);
611 })
612
613 // optional::has_value
614 .CaseOfCFGStmt<CXXMemberCallExpr>(
615 isOptionalMemberCallWithName("has_value"),
616 transferOptionalHasValueCall)
617
618 // optional::operator bool
619 .CaseOfCFGStmt<CXXMemberCallExpr>(
620 isOptionalMemberCallWithName("operator bool"),
621 transferOptionalHasValueCall)
622
623 // optional::emplace
624 .CaseOfCFGStmt<CXXMemberCallExpr>(
625 isOptionalMemberCallWithName("emplace"),
626 [](const CXXMemberCallExpr *E, const MatchFinder::MatchResult &,
627 LatticeTransferState &State) {
628 assignOptionalValue(*E->getImplicitObjectArgument(), State,
629 State.Env.getBoolLiteralValue(true));
630 })
631
632 // optional::reset
633 .CaseOfCFGStmt<CXXMemberCallExpr>(
634 isOptionalMemberCallWithName("reset"),
635 [](const CXXMemberCallExpr *E, const MatchFinder::MatchResult &,
636 LatticeTransferState &State) {
637 assignOptionalValue(*E->getImplicitObjectArgument(), State,
638 State.Env.getBoolLiteralValue(false));
639 })
640
641 // optional::swap
642 .CaseOfCFGStmt<CXXMemberCallExpr>(isOptionalMemberCallWithName("swap"),
643 transferSwapCall)
644
645 // std::swap
646 .CaseOfCFGStmt<CallExpr>(isStdSwapCall(), transferStdSwapCall)
647
648 // opt.value_or("").empty()
649 .CaseOfCFGStmt<Expr>(isValueOrStringEmptyCall(),
650 transferValueOrStringEmptyCall)
651
652 // opt.value_or(X) != X
653 .CaseOfCFGStmt<Expr>(isValueOrNotEqX(), transferValueOrNotEqX)
654
655 // returns optional
656 .CaseOfCFGStmt<CallExpr>(isCallReturningOptional(),
657 transferCallReturningOptional)
658
659 .Build();
660}
661
662std::vector<SourceLocation> diagnoseUnwrapCall(const Expr *UnwrapExpr,
663 const Expr *ObjectExpr,
664 const Environment &Env) {
665 if (auto *OptionalVal =
666 Env.getValue(*ObjectExpr, SkipPast::ReferenceThenPointer)) {
667 auto *Prop = OptionalVal->getProperty("has_value");
668 if (auto *HasValueVal = cast_or_null<BoolValue>(Prop)) {
669 if (Env.flowConditionImplies(*HasValueVal))
670 return {};
671 }
672 }
673
674 // Record that this unwrap is *not* provably safe.
675 // FIXME: include either the name of the optional (if applicable) or a source
676 // range of the access for easier interpretation of the result.
677 return {ObjectExpr->getBeginLoc()};
678}
679
680auto buildDiagnoseMatchSwitch(
681 const UncheckedOptionalAccessModelOptions &Options) {
682 // FIXME: Evaluate the efficiency of matchers. If using matchers results in a
683 // lot of duplicated work (e.g. string comparisons), consider providing APIs
684 // that avoid it through memoization.
685 auto IgnorableOptional = ignorableOptional(Options);
686 return CFGMatchSwitchBuilder<const Environment, std::vector<SourceLocation>>()
2
Calling 'CFGMatchSwitchBuilder::Build'
687 // optional::value
688 .CaseOfCFGStmt<CXXMemberCallExpr>(
689 valueCall(IgnorableOptional),
690 [](const CXXMemberCallExpr *E, const MatchFinder::MatchResult &,
691 const Environment &Env) {
692 return diagnoseUnwrapCall(E, E->getImplicitObjectArgument(), Env);
693 })
694
695 // optional::operator*, optional::operator->
696 .CaseOfCFGStmt<CallExpr>(
697 valueOperatorCall(IgnorableOptional),
698 [](const CallExpr *E, const MatchFinder::MatchResult &,
699 const Environment &Env) {
700 return diagnoseUnwrapCall(E, E->getArg(0), Env);
701 })
702 .Build();
703}
704
705} // namespace
706
707ast_matchers::DeclarationMatcher
708UncheckedOptionalAccessModel::optionalClassDecl() {
709 return optionalClass();
710}
711
712UncheckedOptionalAccessModel::UncheckedOptionalAccessModel(
713 ASTContext &Ctx, UncheckedOptionalAccessModelOptions Options)
714 : DataflowAnalysis<UncheckedOptionalAccessModel, NoopLattice>(Ctx),
715 TransferMatchSwitch(buildTransferMatchSwitch(Options)) {}
716
717void UncheckedOptionalAccessModel::transfer(const CFGElement *Elt,
718 NoopLattice &L, Environment &Env) {
719 LatticeTransferState State(L, Env);
720 TransferMatchSwitch(*Elt, getASTContext(), State);
721}
722
723ComparisonResult UncheckedOptionalAccessModel::compare(
724 QualType Type, const Value &Val1, const Environment &Env1,
725 const Value &Val2, const Environment &Env2) {
726 if (!isOptionalType(Type))
727 return ComparisonResult::Unknown;
728 return isNonEmptyOptional(Val1, Env1) == isNonEmptyOptional(Val2, Env2)
729 ? ComparisonResult::Same
730 : ComparisonResult::Different;
731}
732
733bool UncheckedOptionalAccessModel::merge(QualType Type, const Value &Val1,
734 const Environment &Env1,
735 const Value &Val2,
736 const Environment &Env2,
737 Value &MergedVal,
738 Environment &MergedEnv) {
739 if (!isOptionalType(Type))
740 return true;
741
742 auto &HasValueVal = MergedEnv.makeAtomicBoolValue();
743 if (isNonEmptyOptional(Val1, Env1) && isNonEmptyOptional(Val2, Env2))
744 MergedEnv.addToFlowCondition(HasValueVal);
745 else if (isEmptyOptional(Val1, Env1) && isEmptyOptional(Val2, Env2))
746 MergedEnv.addToFlowCondition(MergedEnv.makeNot(HasValueVal));
747 setHasValue(MergedVal, HasValueVal);
748 return true;
749}
750
751UncheckedOptionalAccessDiagnoser::UncheckedOptionalAccessDiagnoser(
752 UncheckedOptionalAccessModelOptions Options)
753 : DiagnoseMatchSwitch(buildDiagnoseMatchSwitch(Options)) {}
1
Calling 'buildDiagnoseMatchSwitch'
754
755std::vector<SourceLocation> UncheckedOptionalAccessDiagnoser::diagnose(
756 ASTContext &Ctx, const CFGElement *Elt, const Environment &Env) {
757 return DiagnoseMatchSwitch(*Elt, Ctx, Env);
758}
759
760} // namespace dataflow
761} // namespace clang

/build/source/clang/include/clang/Analysis/FlowSensitive/CFGMatchSwitch.h

1//===---- CFGMatchSwitch.h --------------------------------------*- C++ -*-===//
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// This file defines the `CFGMatchSwitch` abstraction for building a "switch"
10// statement for control flow graph elements. Each case of the switch is
11// defined by an ASTMatcher which is applied on the AST node contained in the
12// input `CFGElement`.
13//
14// Currently, the `CFGMatchSwitch` only handles `CFGElement`s of
15// `Kind::Statement` and `Kind::Initializer`.
16//
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CFGMATCHSWITCH_H_
20#define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CFGMATCHSWITCH_H_
21
22#include "clang/AST/ASTContext.h"
23#include "clang/AST/Stmt.h"
24#include "clang/Analysis/CFG.h"
25#include "clang/Analysis/FlowSensitive/MatchSwitch.h"
26#include <functional>
27#include <utility>
28
29namespace clang {
30namespace dataflow {
31
32template <typename State, typename Result = void>
33using CFGMatchSwitch =
34 std::function<Result(const CFGElement &, ASTContext &, State &)>;
35
36/// Collects cases of a "match switch": a collection of matchers paired with
37/// callbacks, which together define a switch that can be applied to an AST node
38/// contained in a CFG element.
39template <typename State, typename Result = void> class CFGMatchSwitchBuilder {
40public:
41 /// Registers an action `A` for `CFGStmt`s that will be triggered by the match
42 /// of the pattern `M` against the `Stmt` contained in the input `CFGStmt`.
43 ///
44 /// Requirements:
45 ///
46 /// `NodeT` should be derived from `Stmt`.
47 template <typename NodeT>
48 CFGMatchSwitchBuilder &&
49 CaseOfCFGStmt(MatchSwitchMatcher<Stmt> M,
50 MatchSwitchAction<NodeT, State, Result> A) && {
51 std::move(StmtBuilder).template CaseOf<NodeT>(M, A);
52 return std::move(*this);
53 }
54
55 /// Registers an action `A` for `CFGInitializer`s that will be triggered by
56 /// the match of the pattern `M` against the `CXXCtorInitializer` contained in
57 /// the input `CFGInitializer`.
58 ///
59 /// Requirements:
60 ///
61 /// `NodeT` should be derived from `CXXCtorInitializer`.
62 template <typename NodeT>
63 CFGMatchSwitchBuilder &&
64 CaseOfCFGInit(MatchSwitchMatcher<CXXCtorInitializer> M,
65 MatchSwitchAction<NodeT, State, Result> A) && {
66 std::move(InitBuilder).template CaseOf<NodeT>(M, A);
67 return std::move(*this);
68 }
69
70 CFGMatchSwitch<State, Result> Build() && {
71 return [StmtMS = std::move(StmtBuilder).Build(),
13
Potential memory leak
72 InitMS = std::move(InitBuilder).Build()](const CFGElement &Element,
3
Calling 'ASTMatchSwitchBuilder::Build'
12
Returned allocated memory
73 ASTContext &Context,
74 State &S) -> Result {
75 switch (Element.getKind()) {
76 case CFGElement::Initializer:
77 return InitMS(*Element.castAs<CFGInitializer>().getInitializer(),
78 Context, S);
79 case CFGElement::Statement:
80 case CFGElement::Constructor:
81 case CFGElement::CXXRecordTypedCall:
82 return StmtMS(*Element.castAs<CFGStmt>().getStmt(), Context, S);
83 default:
84 // FIXME: Handle other kinds of CFGElement.
85 return Result();
86 }
87 };
88 }
89
90private:
91 ASTMatchSwitchBuilder<Stmt, State, Result> StmtBuilder;
92 ASTMatchSwitchBuilder<CXXCtorInitializer, State, Result> InitBuilder;
93};
94
95} // namespace dataflow
96} // namespace clang
97
98#endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CFGMATCHSWITCH_H_

/build/source/clang/include/clang/Analysis/FlowSensitive/MatchSwitch.h

1//===---- MatchSwitch.h -----------------------------------------*- C++ -*-===//
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// This file defines the `MatchSwitch` abstraction for building a "switch"
10// statement, where each case of the switch is defined by an AST matcher. The
11// cases are considered in order, like pattern matching in functional
12// languages.
13//
14// Currently, the design is catered towards simplifying the implementation of
15// `DataflowAnalysis` transfer functions. Based on experience here, this
16// library may be generalized and moved to ASTMatchers.
17//
18//===----------------------------------------------------------------------===//
19//
20// FIXME: Rename to ASTMatchSwitch.h and update documentation when all usages of
21// `MatchSwitch` are updated to `ASTMatchSwitch<Stmt>`
22
23#ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_MATCHSWITCH_H_
24#define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_MATCHSWITCH_H_
25
26#include "clang/AST/ASTContext.h"
27#include "clang/AST/Stmt.h"
28#include "clang/ASTMatchers/ASTMatchFinder.h"
29#include "clang/ASTMatchers/ASTMatchers.h"
30#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
31#include "llvm/ADT/StringRef.h"
32#include <functional>
33#include <string>
34#include <type_traits>
35#include <utility>
36#include <vector>
37
38namespace clang {
39namespace dataflow {
40
41/// A common form of state shared between the cases of a transfer function.
42template <typename LatticeT> struct TransferState {
43 TransferState(LatticeT &Lattice, Environment &Env)
44 : Lattice(Lattice), Env(Env) {}
45
46 /// Current lattice element.
47 LatticeT &Lattice;
48 Environment &Env;
49};
50
51template <typename T>
52using MatchSwitchMatcher = ast_matchers::internal::Matcher<T>;
53
54template <typename T, typename State, typename Result = void>
55using MatchSwitchAction = std::function<Result(
56 const T *, const ast_matchers::MatchFinder::MatchResult &, State &)>;
57
58template <typename BaseT, typename State, typename Result = void>
59using ASTMatchSwitch =
60 std::function<Result(const BaseT &, ASTContext &, State &)>;
61
62// FIXME: Remove this alias when all usages of `MatchSwitch` are updated to
63// `ASTMatchSwitch<Stmt>`.
64template <typename State, typename Result = void>
65using MatchSwitch = ASTMatchSwitch<Stmt, State, Result>;
66
67/// Collects cases of a "match switch": a collection of matchers paired with
68/// callbacks, which together define a switch that can be applied to a node
69/// whose type derives from `BaseT`. This structure can simplify the definition
70/// of `transfer` functions that rely on pattern-matching.
71///
72/// For example, consider an analysis that handles particular function calls. It
73/// can define the `ASTMatchSwitch` once, in the constructor of the analysis,
74/// and then reuse it each time that `transfer` is called, with a fresh state
75/// value.
76///
77/// \code
78/// ASTMatchSwitch<Stmt, TransferState<MyLattice> BuildSwitch() {
79/// return ASTMatchSwitchBuilder<TransferState<MyLattice>>()
80/// .CaseOf(callExpr(callee(functionDecl(hasName("foo")))), TransferFooCall)
81/// .CaseOf(callExpr(argumentCountIs(2),
82/// callee(functionDecl(hasName("bar")))),
83/// TransferBarCall)
84/// .Build();
85/// }
86/// \endcode
87template <typename BaseT, typename State, typename Result = void>
88class ASTMatchSwitchBuilder {
89public:
90 /// Registers an action that will be triggered by the match of a pattern
91 /// against the input statement.
92 ///
93 /// Requirements:
94 ///
95 /// `NodeT` should be derived from `BaseT`.
96 template <typename NodeT>
97 ASTMatchSwitchBuilder &&CaseOf(MatchSwitchMatcher<BaseT> M,
98 MatchSwitchAction<NodeT, State, Result> A) && {
99 static_assert(std::is_base_of<BaseT, NodeT>::value,
100 "NodeT must be derived from BaseT.");
101 Matchers.push_back(std::move(M));
102 Actions.push_back(
103 [A = std::move(A)](const BaseT *Node,
104 const ast_matchers::MatchFinder::MatchResult &R,
105 State &S) { return A(cast<NodeT>(Node), R, S); });
106 return std::move(*this);
107 }
108
109 ASTMatchSwitch<BaseT, State, Result> Build() && {
110 return [Matcher = BuildMatcher(), Actions = std::move(Actions)](
4
Calling constructor for 'function<std::vector<clang::SourceLocation> (const clang::CXXCtorInitializer &, clang::ASTContext &, const clang::dataflow::Environment &)>'
11
Returning from constructor for 'function<std::vector<clang::SourceLocation> (const clang::CXXCtorInitializer &, clang::ASTContext &, const clang::dataflow::Environment &)>'
111 const BaseT &Node, ASTContext &Context, State &S) -> Result {
112 auto Results = ast_matchers::matchDynamic(Matcher, Node, Context);
113 if (Results.empty()) {
114 return Result();
115 }
116 // Look through the map for the first binding of the form "TagN..." use
117 // that to select the action.
118 for (const auto &Element : Results[0].getMap()) {
119 llvm::StringRef ID(Element.first);
120 size_t Index = 0;
121 if (ID.consume_front("Tag") && !ID.getAsInteger(10, Index) &&
122 Index < Actions.size()) {
123 return Actions[Index](
124 &Node,
125 ast_matchers::MatchFinder::MatchResult(Results[0], &Context), S);
126 }
127 }
128 return Result();
129 };
130 }
131
132private:
133 ast_matchers::internal::DynTypedMatcher BuildMatcher() {
134 using ast_matchers::anything;
135 using ast_matchers::stmt;
136 using ast_matchers::unless;
137 using ast_matchers::internal::DynTypedMatcher;
138 if (Matchers.empty())
139 return stmt(unless(anything()));
140 for (int I = 0, N = Matchers.size(); I < N; ++I) {
141 std::string Tag = ("Tag" + llvm::Twine(I)).str();
142 // Many matchers are not bindable, so ensure that tryBind will work.
143 Matchers[I].setAllowBind(true);
144 auto M = *Matchers[I].tryBind(Tag);
145 // Each anyOf explicitly controls the traversal kind. The anyOf itself is
146 // set to `TK_AsIs` to ensure no nodes are skipped, thereby deferring to
147 // the kind of the branches. Then, each branch is either left as is, if
148 // the kind is already set, or explicitly set to `TK_AsIs`. We choose this
149 // setting because it is the default interpretation of matchers.
150 Matchers[I] =
151 !M.getTraversalKind() ? M.withTraversalKind(TK_AsIs) : std::move(M);
152 }
153 // The matcher type on the cases ensures that `Expr` kind is compatible with
154 // all of the matchers.
155 return DynTypedMatcher::constructVariadic(
156 DynTypedMatcher::VO_AnyOf, ASTNodeKind::getFromNodeKind<BaseT>(),
157 std::move(Matchers));
158 }
159
160 std::vector<ast_matchers::internal::DynTypedMatcher> Matchers;
161 std::vector<MatchSwitchAction<BaseT, State, Result>> Actions;
162};
163
164// FIXME: Remove this alias when all usages of `MatchSwitchBuilder` are updated
165// to `ASTMatchSwitchBuilder<Stmt>`.
166template <typename State, typename Result = void>
167using MatchSwitchBuilder = ASTMatchSwitchBuilder<Stmt, State, Result>;
168
169} // namespace dataflow
170} // namespace clang
171#endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_MATCHSWITCH_H_

/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/std_function.h

1// Implementation of std::function -*- C++ -*-
2
3// Copyright (C) 2004-2020 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/bits/std_function.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{functional}
28 */
29
30#ifndef _GLIBCXX_STD_FUNCTION_H1
31#define _GLIBCXX_STD_FUNCTION_H1 1
32
33#pragma GCC system_header
34
35#if __cplusplus201703L < 201103L
36# include <bits/c++0x_warning.h>
37#else
38
39#if __cpp_rtti199711L
40# include <typeinfo>
41#endif
42#include <bits/stl_function.h>
43#include <bits/invoke.h>
44#include <bits/refwrap.h>
45#include <bits/functexcept.h>
46
47namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
48{
49_GLIBCXX_BEGIN_NAMESPACE_VERSION
50
51 /**
52 * @brief Exception class thrown when class template function's
53 * operator() is called with an empty target.
54 * @ingroup exceptions
55 */
56 class bad_function_call : public std::exception
57 {
58 public:
59 virtual ~bad_function_call() noexcept;
60
61 const char* what() const noexcept;
62 };
63
64 /**
65 * Trait identifying "location-invariant" types, meaning that the
66 * address of the object (or any of its members) will not escape.
67 * Trivially copyable types are location-invariant and users can
68 * specialize this trait for other types.
69 */
70 template<typename _Tp>
71 struct __is_location_invariant
72 : is_trivially_copyable<_Tp>::type
73 { };
74
75 class _Undefined_class;
76
77 union _Nocopy_types
78 {
79 void* _M_object;
80 const void* _M_const_object;
81 void (*_M_function_pointer)();
82 void (_Undefined_class::*_M_member_pointer)();
83 };
84
85 union [[gnu::may_alias]] _Any_data
86 {
87 void* _M_access() { return &_M_pod_data[0]; }
88 const void* _M_access() const { return &_M_pod_data[0]; }
89
90 template<typename _Tp>
91 _Tp&
92 _M_access()
93 { return *static_cast<_Tp*>(_M_access()); }
94
95 template<typename _Tp>
96 const _Tp&
97 _M_access() const
98 { return *static_cast<const _Tp*>(_M_access()); }
99
100 _Nocopy_types _M_unused;
101 char _M_pod_data[sizeof(_Nocopy_types)];
102 };
103
104 enum _Manager_operation
105 {
106 __get_type_info,
107 __get_functor_ptr,
108 __clone_functor,
109 __destroy_functor
110 };
111
112 template<typename _Signature>
113 class function;
114
115 /// Base class of all polymorphic function object wrappers.
116 class _Function_base
117 {
118 public:
119 static const size_t _M_max_size = sizeof(_Nocopy_types);
120 static const size_t _M_max_align = __alignof__(_Nocopy_types);
121
122 template<typename _Functor>
123 class _Base_manager
124 {
125 protected:
126 static const bool __stored_locally =
127 (__is_location_invariant<_Functor>::value
128 && sizeof(_Functor) <= _M_max_size
129 && __alignof__(_Functor) <= _M_max_align
130 && (_M_max_align % __alignof__(_Functor) == 0));
131
132 typedef integral_constant<bool, __stored_locally> _Local_storage;
133
134 // Retrieve a pointer to the function object
135 static _Functor*
136 _M_get_pointer(const _Any_data& __source)
137 {
138 if _GLIBCXX17_CONSTEXPRconstexpr (__stored_locally)
139 {
140 const _Functor& __f = __source._M_access<_Functor>();
141 return const_cast<_Functor*>(std::__addressof(__f));
142 }
143 else // have stored a pointer
144 return __source._M_access<_Functor*>();
145 }
146
147 // Clone a location-invariant function object that fits within
148 // an _Any_data structure.
149 static void
150 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
151 {
152 ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
153 }
154
155 // Clone a function object that is not location-invariant or
156 // that cannot fit into an _Any_data structure.
157 static void
158 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
159 {
160 __dest._M_access<_Functor*>() =
161 new _Functor(*__source._M_access<const _Functor*>());
162 }
163
164 // Destroying a location-invariant object may still require
165 // destruction.
166 static void
167 _M_destroy(_Any_data& __victim, true_type)
168 {
169 __victim._M_access<_Functor>().~_Functor();
170 }
171
172 // Destroying an object located on the heap.
173 static void
174 _M_destroy(_Any_data& __victim, false_type)
175 {
176 delete __victim._M_access<_Functor*>();
177 }
178
179 public:
180 static bool
181 _M_manager(_Any_data& __dest, const _Any_data& __source,
182 _Manager_operation __op)
183 {
184 switch (__op)
185 {
186#if __cpp_rtti199711L
187 case __get_type_info:
188 __dest._M_access<const type_info*>() = &typeid(_Functor);
189 break;
190#endif
191 case __get_functor_ptr:
192 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
193 break;
194
195 case __clone_functor:
196 _M_clone(__dest, __source, _Local_storage());
197 break;
198
199 case __destroy_functor:
200 _M_destroy(__dest, _Local_storage());
201 break;
202 }
203 return false;
204 }
205
206 static void
207 _M_init_functor(_Any_data& __functor, _Functor&& __f)
208 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
7
Calling '_Base_manager::_M_init_functor'
9
Returned allocated memory
209
210 template<typename _Signature>
211 static bool
212 _M_not_empty_function(const function<_Signature>& __f)
213 { return static_cast<bool>(__f); }
214
215 template<typename _Tp>
216 static bool
217 _M_not_empty_function(_Tp* __fp)
218 { return __fp != nullptr; }
219
220 template<typename _Class, typename _Tp>
221 static bool
222 _M_not_empty_function(_Tp _Class::* __mp)
223 { return __mp != nullptr; }
224
225 template<typename _Tp>
226 static bool
227 _M_not_empty_function(const _Tp&)
228 { return true; }
229
230 private:
231 static void
232 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
233 { ::new (__functor._M_access()) _Functor(std::move(__f)); }
234
235 static void
236 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
237 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
8
Memory is allocated
238 };
239
240 _Function_base() : _M_manager(nullptr) { }
241
242 ~_Function_base()
243 {
244 if (_M_manager)
245 _M_manager(_M_functor, _M_functor, __destroy_functor);
246 }
247
248 bool _M_empty() const { return !_M_manager; }
249
250 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
251 _Manager_operation);
252
253 _Any_data _M_functor;
254 _Manager_type _M_manager;
255 };
256
257 template<typename _Signature, typename _Functor>
258 class _Function_handler;
259
260 template<typename _Res, typename _Functor, typename... _ArgTypes>
261 class _Function_handler<_Res(_ArgTypes...), _Functor>
262 : public _Function_base::_Base_manager<_Functor>
263 {
264 typedef _Function_base::_Base_manager<_Functor> _Base;
265
266 public:
267 static bool
268 _M_manager(_Any_data& __dest, const _Any_data& __source,
269 _Manager_operation __op)
270 {
271 switch (__op)
272 {
273#if __cpp_rtti199711L
274 case __get_type_info:
275 __dest._M_access<const type_info*>() = &typeid(_Functor);
276 break;
277#endif
278 case __get_functor_ptr:
279 __dest._M_access<_Functor*>() = _Base::_M_get_pointer(__source);
280 break;
281
282 default:
283 _Base::_M_manager(__dest, __source, __op);
284 }
285 return false;
286 }
287
288 static _Res
289 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
290 {
291 return std::__invoke_r<_Res>(*_Base::_M_get_pointer(__functor),
292 std::forward<_ArgTypes>(__args)...);
293 }
294 };
295
296 /**
297 * @brief Primary class template for std::function.
298 * @ingroup functors
299 *
300 * Polymorphic function wrapper.
301 */
302 template<typename _Res, typename... _ArgTypes>
303 class function<_Res(_ArgTypes...)>
304 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
305 private _Function_base
306 {
307 template<typename _Func,
308 typename _Res2 = __invoke_result<_Func&, _ArgTypes...>>
309 struct _Callable
310 : __is_invocable_impl<_Res2, _Res>::type
311 { };
312
313 // Used so the return type convertibility checks aren't done when
314 // performing overload resolution for copy construction/assignment.
315 template<typename _Tp>
316 struct _Callable<function, _Tp> : false_type { };
317
318 template<typename _Cond, typename _Tp>
319 using _Requires = typename enable_if<_Cond::value, _Tp>::type;
320
321 public:
322 typedef _Res result_type;
323
324 // [3.7.2.1] construct/copy/destroy
325
326 /**
327 * @brief Default construct creates an empty function call wrapper.
328 * @post @c !(bool)*this
329 */
330 function() noexcept
331 : _Function_base() { }
332
333 /**
334 * @brief Creates an empty function call wrapper.
335 * @post @c !(bool)*this
336 */
337 function(nullptr_t) noexcept
338 : _Function_base() { }
339
340 /**
341 * @brief %Function copy constructor.
342 * @param __x A %function object with identical call signature.
343 * @post @c bool(*this) == bool(__x)
344 *
345 * The newly-created %function contains a copy of the target of @a
346 * __x (if it has one).
347 */
348 function(const function& __x);
349
350 /**
351 * @brief %Function move constructor.
352 * @param __x A %function object rvalue with identical call signature.
353 *
354 * The newly-created %function contains the target of @a __x
355 * (if it has one).
356 */
357 function(function&& __x) noexcept : _Function_base()
358 {
359 __x.swap(*this);
360 }
361
362 /**
363 * @brief Builds a %function that targets a copy of the incoming
364 * function object.
365 * @param __f A %function object that is callable with parameters of
366 * type @c T1, @c T2, ..., @c TN and returns a value convertible
367 * to @c Res.
368 *
369 * The newly-created %function object will target a copy of
370 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
371 * object will contain a reference to the function object @c
372 * __f.get(). If @a __f is a NULL function pointer or NULL
373 * pointer-to-member, the newly-created object will be empty.
374 *
375 * If @a __f is a non-NULL function pointer or an object of type @c
376 * reference_wrapper<F>, this function will not throw.
377 */
378 template<typename _Functor,
379 typename = _Requires<__not_<is_same<_Functor, function>>, void>,
380 typename = _Requires<_Callable<_Functor>, void>>
381 function(_Functor);
382
383 /**
384 * @brief %Function assignment operator.
385 * @param __x A %function with identical call signature.
386 * @post @c (bool)*this == (bool)x
387 * @returns @c *this
388 *
389 * The target of @a __x is copied to @c *this. If @a __x has no
390 * target, then @c *this will be empty.
391 *
392 * If @a __x targets a function pointer or a reference to a function
393 * object, then this operation will not throw an %exception.
394 */
395 function&
396 operator=(const function& __x)
397 {
398 function(__x).swap(*this);
399 return *this;
400 }
401
402 /**
403 * @brief %Function move-assignment operator.
404 * @param __x A %function rvalue with identical call signature.
405 * @returns @c *this
406 *
407 * The target of @a __x is moved to @c *this. If @a __x has no
408 * target, then @c *this will be empty.
409 *
410 * If @a __x targets a function pointer or a reference to a function
411 * object, then this operation will not throw an %exception.
412 */
413 function&
414 operator=(function&& __x) noexcept
415 {
416 function(std::move(__x)).swap(*this);
417 return *this;
418 }
419
420 /**
421 * @brief %Function assignment to zero.
422 * @post @c !(bool)*this
423 * @returns @c *this
424 *
425 * The target of @c *this is deallocated, leaving it empty.
426 */
427 function&
428 operator=(nullptr_t) noexcept
429 {
430 if (_M_manager)
431 {
432 _M_manager(_M_functor, _M_functor, __destroy_functor);
433 _M_manager = nullptr;
434 _M_invoker = nullptr;
435 }
436 return *this;
437 }
438
439 /**
440 * @brief %Function assignment to a new target.
441 * @param __f A %function object that is callable with parameters of
442 * type @c T1, @c T2, ..., @c TN and returns a value convertible
443 * to @c Res.
444 * @return @c *this
445 *
446 * This %function object wrapper will target a copy of @a
447 * __f. If @a __f is @c reference_wrapper<F>, then this function
448 * object will contain a reference to the function object @c
449 * __f.get(). If @a __f is a NULL function pointer or NULL
450 * pointer-to-member, @c this object will be empty.
451 *
452 * If @a __f is a non-NULL function pointer or an object of type @c
453 * reference_wrapper<F>, this function will not throw.
454 */
455 template<typename _Functor>
456 _Requires<_Callable<typename decay<_Functor>::type>, function&>
457 operator=(_Functor&& __f)
458 {
459 function(std::forward<_Functor>(__f)).swap(*this);
460 return *this;
461 }
462
463 /// @overload
464 template<typename _Functor>
465 function&
466 operator=(reference_wrapper<_Functor> __f) noexcept
467 {
468 function(__f).swap(*this);
469 return *this;
470 }
471
472 // [3.7.2.2] function modifiers
473
474 /**
475 * @brief Swap the targets of two %function objects.
476 * @param __x A %function with identical call signature.
477 *
478 * Swap the targets of @c this function object and @a __f. This
479 * function will not throw an %exception.
480 */
481 void swap(function& __x) noexcept
482 {
483 std::swap(_M_functor, __x._M_functor);
484 std::swap(_M_manager, __x._M_manager);
485 std::swap(_M_invoker, __x._M_invoker);
486 }
487
488 // [3.7.2.3] function capacity
489
490 /**
491 * @brief Determine if the %function wrapper has a target.
492 *
493 * @return @c true when this %function object contains a target,
494 * or @c false when it is empty.
495 *
496 * This function will not throw an %exception.
497 */
498 explicit operator bool() const noexcept
499 { return !_M_empty(); }
500
501 // [3.7.2.4] function invocation
502
503 /**
504 * @brief Invokes the function targeted by @c *this.
505 * @returns the result of the target.
506 * @throws bad_function_call when @c !(bool)*this
507 *
508 * The function call operator invokes the target function object
509 * stored by @c this.
510 */
511 _Res operator()(_ArgTypes... __args) const;
512
513#if __cpp_rtti199711L
514 // [3.7.2.5] function target access
515 /**
516 * @brief Determine the type of the target of this function object
517 * wrapper.
518 *
519 * @returns the type identifier of the target function object, or
520 * @c typeid(void) if @c !(bool)*this.
521 *
522 * This function will not throw an %exception.
523 */
524 const type_info& target_type() const noexcept;
525
526 /**
527 * @brief Access the stored target function object.
528 *
529 * @return Returns a pointer to the stored target function object,
530 * if @c typeid(_Functor).equals(target_type()); otherwise, a NULL
531 * pointer.
532 *
533 * This function does not throw exceptions.
534 *
535 * @{
536 */
537 template<typename _Functor> _Functor* target() noexcept;
538
539 template<typename _Functor> const _Functor* target() const noexcept;
540 // @}
541#endif
542
543 private:
544 using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
545 _Invoker_type _M_invoker;
546 };
547
548#if __cpp_deduction_guides201703L >= 201606
549 template<typename>
550 struct __function_guide_helper
551 { };
552
553 template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
554 struct __function_guide_helper<
555 _Res (_Tp::*) (_Args...) noexcept(_Nx)
556 >
557 { using type = _Res(_Args...); };
558
559 template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
560 struct __function_guide_helper<
561 _Res (_Tp::*) (_Args...) & noexcept(_Nx)
562 >
563 { using type = _Res(_Args...); };
564
565 template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
566 struct __function_guide_helper<
567 _Res (_Tp::*) (_Args...) const noexcept(_Nx)
568 >
569 { using type = _Res(_Args...); };
570
571 template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
572 struct __function_guide_helper<
573 _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
574 >
575 { using type = _Res(_Args...); };
576
577 template<typename _Res, typename... _ArgTypes>
578 function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
579
580 template<typename _Functor, typename _Signature = typename
581 __function_guide_helper<decltype(&_Functor::operator())>::type>
582 function(_Functor) -> function<_Signature>;
583#endif
584
585 // Out-of-line member definitions.
586 template<typename _Res, typename... _ArgTypes>
587 function<_Res(_ArgTypes...)>::
588 function(const function& __x)
589 : _Function_base()
590 {
591 if (static_cast<bool>(__x))
592 {
593 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
594 _M_invoker = __x._M_invoker;
595 _M_manager = __x._M_manager;
596 }
597 }
598
599 template<typename _Res, typename... _ArgTypes>
600 template<typename _Functor, typename, typename>
601 function<_Res(_ArgTypes...)>::
602 function(_Functor __f)
603 : _Function_base()
604 {
605 typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler;
606
607 if (_My_handler::_M_not_empty_function(__f))
5
Taking true branch
608 {
609 _My_handler::_M_init_functor(_M_functor, std::move(__f));
6
Calling '_Base_manager::_M_init_functor'
10
Returned allocated memory
610 _M_invoker = &_My_handler::_M_invoke;
611 _M_manager = &_My_handler::_M_manager;
612 }
613 }
614
615 template<typename _Res, typename... _ArgTypes>
616 _Res
617 function<_Res(_ArgTypes...)>::
618 operator()(_ArgTypes... __args) const
619 {
620 if (_M_empty())
621 __throw_bad_function_call();
622 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
623 }
624
625#if __cpp_rtti199711L
626 template<typename _Res, typename... _ArgTypes>
627 const type_info&
628 function<_Res(_ArgTypes...)>::
629 target_type() const noexcept
630 {
631 if (_M_manager)
632 {
633 _Any_data __typeinfo_result;
634 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
635 return *__typeinfo_result._M_access<const type_info*>();
636 }
637 else
638 return typeid(void);
639 }
640
641 template<typename _Res, typename... _ArgTypes>
642 template<typename _Functor>
643 _Functor*
644 function<_Res(_ArgTypes...)>::
645 target() noexcept
646 {
647 const function* __const_this = this;
648 const _Functor* __func = __const_this->template target<_Functor>();
649 return const_cast<_Functor*>(__func);
650 }
651
652 template<typename _Res, typename... _ArgTypes>
653 template<typename _Functor>
654 const _Functor*
655 function<_Res(_ArgTypes...)>::
656 target() const noexcept
657 {
658 if (typeid(_Functor) == target_type() && _M_manager)
659 {
660 _Any_data __ptr;
661 _M_manager(__ptr, _M_functor, __get_functor_ptr);
662 return __ptr._M_access<const _Functor*>();
663 }
664 else
665 return nullptr;
666 }
667#endif
668
669 // [20.7.15.2.6] null pointer comparisons
670
671 /**
672 * @brief Compares a polymorphic function object wrapper against 0
673 * (the NULL pointer).
674 * @returns @c true if the wrapper has no target, @c false otherwise
675 *
676 * This function will not throw an %exception.
677 */
678 template<typename _Res, typename... _Args>
679 inline bool
680 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
681 { return !static_cast<bool>(__f); }
682
683#if __cpp_impl_three_way_comparison < 201907L
684 /// @overload
685 template<typename _Res, typename... _Args>
686 inline bool
687 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
688 { return !static_cast<bool>(__f); }
689
690 /**
691 * @brief Compares a polymorphic function object wrapper against 0
692 * (the NULL pointer).
693 * @returns @c false if the wrapper has no target, @c true otherwise
694 *
695 * This function will not throw an %exception.
696 */
697 template<typename _Res, typename... _Args>
698 inline bool
699 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
700 { return static_cast<bool>(__f); }
701
702 /// @overload
703 template<typename _Res, typename... _Args>
704 inline bool
705 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
706 { return static_cast<bool>(__f); }
707#endif
708
709 // [20.7.15.2.7] specialized algorithms
710
711 /**
712 * @brief Swap the targets of two polymorphic function object wrappers.
713 *
714 * This function will not throw an %exception.
715 */
716 // _GLIBCXX_RESOLVE_LIB_DEFECTS
717 // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
718 template<typename _Res, typename... _Args>
719 inline void
720 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
721 { __x.swap(__y); }
722
723#if __cplusplus201703L >= 201703L
724 namespace __detail::__variant
725 {
726 template<typename> struct _Never_valueless_alt; // see <variant>
727
728 // Provide the strong exception-safety guarantee when emplacing a
729 // function into a variant.
730 template<typename _Signature>
731 struct _Never_valueless_alt<std::function<_Signature>>
732 : std::true_type
733 { };
734 } // namespace __detail::__variant
735#endif // C++17
736
737_GLIBCXX_END_NAMESPACE_VERSION
738} // namespace std
739
740#endif // C++11
741#endif // _GLIBCXX_STD_FUNCTION_H