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 RangeSelector.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-17/lib/clang/17 -D CLANG_REPOSITORY_STRING="++20230510111145+7df43bdb42ae-1~exp1~20230510111303.1288" -D _DEBUG -D _GLIBCXX_ASSERTIONS -D _GNU_SOURCE -D _LIBCPP_ENABLE_ASSERTIONS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/clang/lib/Tooling/Transformer -I /build/source/clang/lib/Tooling/Transformer -I /build/source/clang/include -I tools/clang/include -I include -I /build/source/llvm/include -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-17/lib/clang/17/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 1683717183 -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-2023-05-10-133810-16478-1 -x c++ /build/source/clang/lib/Tooling/Transformer/RangeSelector.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include "clang/Tooling/Transformer/RangeSelector.h" |
| 10 | #include "clang/AST/Expr.h" |
| 11 | #include "clang/AST/TypeLoc.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | #include "clang/Basic/SourceLocation.h" |
| 14 | #include "clang/Lex/Lexer.h" |
| 15 | #include "clang/Tooling/Transformer/SourceCode.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | #include "llvm/Support/Errc.h" |
| 18 | #include "llvm/Support/Error.h" |
| 19 | #include <string> |
| 20 | #include <utility> |
| 21 | #include <vector> |
| 22 | |
| 23 | using namespace clang; |
| 24 | using namespace transformer; |
| 25 | |
| 26 | using ast_matchers::MatchFinder; |
| 27 | using llvm::Error; |
| 28 | using llvm::StringError; |
| 29 | |
| 30 | using MatchResult = MatchFinder::MatchResult; |
| 31 | |
| 32 | static Error invalidArgumentError(Twine Message) { |
| 33 | return llvm::make_error<StringError>(llvm::errc::invalid_argument, Message); |
| 34 | } |
| 35 | |
| 36 | static Error typeError(StringRef ID, const ASTNodeKind &Kind) { |
| 37 | return invalidArgumentError("mismatched type (node id=" + ID + |
| 38 | " kind=" + Kind.asStringRef() + ")"); |
| 39 | } |
| 40 | |
| 41 | static Error typeError(StringRef ID, const ASTNodeKind &Kind, |
| 42 | Twine ExpectedType) { |
| 43 | return invalidArgumentError("mismatched type: expected one of " + |
| 44 | ExpectedType + " (node id=" + ID + |
| 45 | " kind=" + Kind.asStringRef() + ")"); |
| 46 | } |
| 47 | |
| 48 | static Error missingPropertyError(StringRef ID, Twine Description, |
| 49 | StringRef Property) { |
| 50 | return invalidArgumentError(Description + " requires property '" + Property + |
| 51 | "' (node id=" + ID + ")"); |
| 52 | } |
| 53 | |
| 54 | static Expected<DynTypedNode> getNode(const ast_matchers::BoundNodes &Nodes, |
| 55 | StringRef ID) { |
| 56 | auto &NodesMap = Nodes.getMap(); |
| 57 | auto It = NodesMap.find(ID); |
| 58 | if (It == NodesMap.end()) |
| 59 | return invalidArgumentError("ID not bound: " + ID); |
| 60 | return It->second; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | static SourceLocation findPreviousTokenStart(SourceLocation Start, |
| 65 | const SourceManager &SM, |
| 66 | const LangOptions &LangOpts) { |
| 67 | if (Start.isInvalid() || Start.isMacroID()) |
| 68 | return SourceLocation(); |
| 69 | |
| 70 | SourceLocation BeforeStart = Start.getLocWithOffset(-1); |
| 71 | if (BeforeStart.isInvalid() || BeforeStart.isMacroID()) |
| 72 | return SourceLocation(); |
| 73 | |
| 74 | return Lexer::GetBeginningOfToken(BeforeStart, SM, LangOpts); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | static SourceLocation findPreviousTokenKind(SourceLocation Start, |
| 80 | const SourceManager &SM, |
| 81 | const LangOptions &LangOpts, |
| 82 | tok::TokenKind TK) { |
| 83 | while (true) { |
| 84 | SourceLocation L = findPreviousTokenStart(Start, SM, LangOpts); |
| 85 | if (L.isInvalid() || L.isMacroID()) |
| 86 | return SourceLocation(); |
| 87 | |
| 88 | Token T; |
| 89 | if (Lexer::getRawToken(L, T, SM, LangOpts, true)) |
| 90 | return SourceLocation(); |
| 91 | |
| 92 | if (T.is(TK)) |
| 93 | return T.getLocation(); |
| 94 | |
| 95 | Start = L; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static SourceLocation findOpenParen(const CallExpr &E, const SourceManager &SM, |
| 100 | const LangOptions &LangOpts) { |
| 101 | SourceLocation EndLoc = |
| 102 | E.getNumArgs() == 0 ? E.getRParenLoc() : E.getArg(0)->getBeginLoc(); |
| 103 | return findPreviousTokenKind(EndLoc, SM, LangOpts, tok::TokenKind::l_paren); |
| 104 | } |
| 105 | |
| 106 | RangeSelector transformer::before(RangeSelector Selector) { |
| 107 | return [Selector](const MatchResult &Result) -> Expected<CharSourceRange> { |
| 108 | Expected<CharSourceRange> SelectedRange = Selector(Result); |
| 109 | if (!SelectedRange) |
| 110 | return SelectedRange.takeError(); |
| 111 | return CharSourceRange::getCharRange(SelectedRange->getBegin()); |
| 112 | }; |
| 113 | } |
| 114 | |
| 115 | RangeSelector transformer::after(RangeSelector Selector) { |
| 116 | return [Selector](const MatchResult &Result) -> Expected<CharSourceRange> { |
| 117 | Expected<CharSourceRange> SelectedRange = Selector(Result); |
| 118 | if (!SelectedRange) |
| 119 | return SelectedRange.takeError(); |
| 120 | SourceLocation End = SelectedRange->getEnd(); |
| 121 | if (SelectedRange->isTokenRange()) { |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | CharSourceRange Range = Lexer::makeFileCharRange( |
| 129 | CharSourceRange::getTokenRange(SelectedRange->getEnd()), |
| 130 | *Result.SourceManager, Result.Context->getLangOpts()); |
| 131 | if (Range.isInvalid()) |
| 132 | return invalidArgumentError( |
| 133 | "after: can't resolve sub-range to valid source range"); |
| 134 | End = Range.getEnd(); |
| 135 | } |
| 136 | |
| 137 | return CharSourceRange::getCharRange(End); |
| 138 | }; |
| 139 | } |
| 140 | |
| 141 | RangeSelector transformer::node(std::string ID) { |
| 142 | return [ID](const MatchResult &Result) -> Expected<CharSourceRange> { |
| 143 | Expected<DynTypedNode> Node = getNode(Result.Nodes, ID); |
| 144 | if (!Node) |
| 145 | return Node.takeError(); |
| 146 | return (Node->get<Decl>() != nullptr || |
| 147 | (Node->get<Stmt>() != nullptr && Node->get<Expr>() == nullptr)) |
| 148 | ? tooling::getExtendedRange(*Node, tok::TokenKind::semi, |
| 149 | *Result.Context) |
| 150 | : CharSourceRange::getTokenRange(Node->getSourceRange()); |
| 151 | }; |
| 152 | } |
| 153 | |
| 154 | RangeSelector transformer::statement(std::string ID) { |
| 155 | return [ID](const MatchResult &Result) -> Expected<CharSourceRange> { |
| 156 | Expected<DynTypedNode> Node = getNode(Result.Nodes, ID); |
| 157 | if (!Node) |
| 158 | return Node.takeError(); |
| 159 | return tooling::getExtendedRange(*Node, tok::TokenKind::semi, |
| 160 | *Result.Context); |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | RangeSelector transformer::enclose(RangeSelector Begin, RangeSelector End) { |
| 165 | return [Begin, End](const MatchResult &Result) -> Expected<CharSourceRange> { |
| 2 | | Calling copy constructor for 'function<llvm::Expected<clang::CharSourceRange> (const clang::ast_matchers::MatchFinder::MatchResult &)>' | |
|
| 14 | | Returning from copy constructor for 'function<llvm::Expected<clang::CharSourceRange> (const clang::ast_matchers::MatchFinder::MatchResult &)>' | |
|
| |
| 166 | Expected<CharSourceRange> BeginRange = Begin(Result); |
| 167 | if (!BeginRange) |
| 168 | return BeginRange.takeError(); |
| 169 | Expected<CharSourceRange> EndRange = End(Result); |
| 170 | if (!EndRange) |
| 171 | return EndRange.takeError(); |
| 172 | SourceLocation B = BeginRange->getBegin(); |
| 173 | SourceLocation E = EndRange->getEnd(); |
| 174 | |
| 175 | |
| 176 | if (Result.SourceManager->isBeforeInTranslationUnit(E, B)) { |
| 177 | return invalidArgumentError("Bad range: out of order"); |
| 178 | } |
| 179 | return CharSourceRange(SourceRange(B, E), EndRange->isTokenRange()); |
| 180 | }; |
| 181 | } |
| 182 | |
| 183 | RangeSelector transformer::encloseNodes(std::string BeginID, |
| 184 | std::string EndID) { |
| 185 | return transformer::enclose(node(std::move(BeginID)), node(std::move(EndID))); |
| |
| 186 | } |
| 187 | |
| 188 | RangeSelector transformer::member(std::string ID) { |
| 189 | return [ID](const MatchResult &Result) -> Expected<CharSourceRange> { |
| 190 | Expected<DynTypedNode> Node = getNode(Result.Nodes, ID); |
| 191 | if (!Node) |
| 192 | return Node.takeError(); |
| 193 | if (auto *M = Node->get<clang::MemberExpr>()) |
| 194 | return CharSourceRange::getTokenRange( |
| 195 | M->getMemberNameInfo().getSourceRange()); |
| 196 | return typeError(ID, Node->getNodeKind(), "MemberExpr"); |
| 197 | }; |
| 198 | } |
| 199 | |
| 200 | RangeSelector transformer::name(std::string ID) { |
| 201 | return [ID](const MatchResult &Result) -> Expected<CharSourceRange> { |
| 202 | Expected<DynTypedNode> N = getNode(Result.Nodes, ID); |
| 203 | if (!N) |
| 204 | return N.takeError(); |
| 205 | auto &Node = *N; |
| 206 | if (const auto *D = Node.get<NamedDecl>()) { |
| 207 | if (!D->getDeclName().isIdentifier()) |
| 208 | return missingPropertyError(ID, "name", "identifier"); |
| 209 | SourceLocation L = D->getLocation(); |
| 210 | auto R = CharSourceRange::getTokenRange(L, L); |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | if (tooling::getText(R, *Result.Context) != D->getName()) |
| 217 | return CharSourceRange(); |
| 218 | return R; |
| 219 | } |
| 220 | if (const auto *E = Node.get<DeclRefExpr>()) { |
| 221 | if (!E->getNameInfo().getName().isIdentifier()) |
| 222 | return missingPropertyError(ID, "name", "identifier"); |
| 223 | SourceLocation L = E->getLocation(); |
| 224 | return CharSourceRange::getTokenRange(L, L); |
| 225 | } |
| 226 | if (const auto *I = Node.get<CXXCtorInitializer>()) { |
| 227 | if (!I->isMemberInitializer() && I->isWritten()) |
| 228 | return missingPropertyError(ID, "name", "explicit member initializer"); |
| 229 | SourceLocation L = I->getMemberLocation(); |
| 230 | return CharSourceRange::getTokenRange(L, L); |
| 231 | } |
| 232 | if (const auto *T = Node.get<TypeLoc>()) { |
| 233 | TypeLoc Loc = *T; |
| 234 | auto ET = Loc.getAs<ElaboratedTypeLoc>(); |
| 235 | if (!ET.isNull()) { |
| 236 | Loc = ET.getNamedTypeLoc(); |
| 237 | } |
| 238 | return CharSourceRange::getTokenRange(Loc.getSourceRange()); |
| 239 | } |
| 240 | return typeError(ID, Node.getNodeKind(), |
| 241 | "DeclRefExpr, NamedDecl, CXXCtorInitializer, TypeLoc"); |
| 242 | }; |
| 243 | } |
| 244 | |
| 245 | namespace { |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | template <typename T, CharSourceRange (*Func)(const MatchResult &, const T &)> |
| 253 | class RelativeSelector { |
| 254 | std::string ID; |
| 255 | |
| 256 | public: |
| 257 | RelativeSelector(std::string ID) : ID(std::move(ID)) {} |
| 258 | |
| 259 | Expected<CharSourceRange> operator()(const MatchResult &Result) { |
| 260 | Expected<DynTypedNode> N = getNode(Result.Nodes, ID); |
| 261 | if (!N) |
| 262 | return N.takeError(); |
| 263 | if (const auto *Arg = N->get<T>()) |
| 264 | return Func(Result, *Arg); |
| 265 | return typeError(ID, N->getNodeKind()); |
| 266 | } |
| 267 | }; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | namespace { |
| 275 | |
| 276 | CharSourceRange getStatementsRange(const MatchResult &, |
| 277 | const CompoundStmt &CS) { |
| 278 | return CharSourceRange::getCharRange(CS.getLBracLoc().getLocWithOffset(1), |
| 279 | CS.getRBracLoc()); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | RangeSelector transformer::statements(std::string ID) { |
| 284 | return RelativeSelector<CompoundStmt, getStatementsRange>(std::move(ID)); |
| 285 | } |
| 286 | |
| 287 | namespace { |
| 288 | |
| 289 | CharSourceRange getCallArgumentsRange(const MatchResult &Result, |
| 290 | const CallExpr &CE) { |
| 291 | return CharSourceRange::getCharRange( |
| 292 | findOpenParen(CE, *Result.SourceManager, Result.Context->getLangOpts()) |
| 293 | .getLocWithOffset(1), |
| 294 | CE.getRParenLoc()); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | RangeSelector transformer::callArgs(std::string ID) { |
| 299 | return RelativeSelector<CallExpr, getCallArgumentsRange>(std::move(ID)); |
| 300 | } |
| 301 | |
| 302 | namespace { |
| 303 | |
| 304 | |
| 305 | CharSourceRange getElementsRange(const MatchResult &, |
| 306 | const InitListExpr &E) { |
| 307 | return CharSourceRange::getCharRange(E.getLBraceLoc().getLocWithOffset(1), |
| 308 | E.getRBraceLoc()); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | RangeSelector transformer::initListElements(std::string ID) { |
| 313 | return RelativeSelector<InitListExpr, getElementsRange>(std::move(ID)); |
| 314 | } |
| 315 | |
| 316 | namespace { |
| 317 | |
| 318 | CharSourceRange getElseRange(const MatchResult &Result, const IfStmt &S) { |
| 319 | return tooling::maybeExtendRange( |
| 320 | CharSourceRange::getTokenRange(S.getElseLoc(), S.getEndLoc()), |
| 321 | tok::TokenKind::semi, *Result.Context); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | RangeSelector transformer::elseBranch(std::string ID) { |
| 326 | return RelativeSelector<IfStmt, getElseRange>(std::move(ID)); |
| 327 | } |
| 328 | |
| 329 | RangeSelector transformer::expansion(RangeSelector S) { |
| 330 | return [S](const MatchResult &Result) -> Expected<CharSourceRange> { |
| 331 | Expected<CharSourceRange> SRange = S(Result); |
| 332 | if (!SRange) |
| 333 | return SRange.takeError(); |
| 334 | return Result.SourceManager->getExpansionRange(*SRange); |
| 335 | }; |
| 336 | } |
| 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 | #ifndef _GLIBCXX_STD_FUNCTION_H |
| 31 | #define _GLIBCXX_STD_FUNCTION_H 1 |
| 32 | |
| 33 | #pragma GCC system_header |
| 34 | |
| 35 | #if __cplusplus < 201103L |
| 36 | # include <bits/c++0x_warning.h> |
| 37 | #else |
| 38 | |
| 39 | #if __cpp_rtti |
| 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 | |
| 47 | namespace std _GLIBCXX_VISIBILITY(default) |
| 48 | { |
| 49 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 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 | |
| 66 | |
| 67 | |
| 68 | |
| 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 | |
| 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 | |
| 135 | static _Functor* |
| 136 | _M_get_pointer(const _Any_data& __source) |
| 137 | { |
| 138 | if _GLIBCXX17_CONSTEXPR (__stored_locally) |
| 139 | { |
| 140 | const _Functor& __f = __source._M_access<_Functor>(); |
| 141 | return const_cast<_Functor*>(std::__addressof(__f)); |
| 142 | } |
| 143 | else |
| 144 | return __source._M_access<_Functor*>(); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 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 | |
| 156 | |
| 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 | |
| 165 | |
| 166 | static void |
| 167 | _M_destroy(_Any_data& __victim, true_type) |
| 168 | { |
| 169 | __victim._M_access<_Functor>().~_Functor(); |
| 170 | } |
| 171 | |
| 172 | |
| 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) |
| 7 | | Control jumps to 'case __clone_functor:' at line 195 | |
|
| 185 | { |
| 186 | #if __cpp_rtti |
| 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()); |
| 8 | | Calling '_Base_manager::_M_clone' | |
|
| 10 | | Returned allocated memory | |
|
| 197 | break; |
| 11 | | Execution continues on line 203 | |
|
| 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()); } |
| 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)); } |
| 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) |
| 5 | | Control jumps to the 'default' case at line 282 | |
|
| 272 | { |
| 273 | #if __cpp_rtti |
| 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); |
| 6 | | Calling '_Base_manager::_M_manager' | |
|
| 12 | | Returned allocated memory | |
|
| 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 | |
| 298 | |
| 299 | |
| 300 | |
| 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 | |
| 314 | |
| 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 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | function() noexcept |
| 331 | : _Function_base() { } |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | function(nullptr_t) noexcept |
| 338 | : _Function_base() { } |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | function(const function& __x); |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | function(function&& __x) noexcept : _Function_base() |
| 358 | { |
| 359 | __x.swap(*this); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 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 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | function& |
| 396 | operator=(const function& __x) |
| 397 | { |
| 398 | function(__x).swap(*this); |
| 399 | return *this; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | function& |
| 414 | operator=(function&& __x) noexcept |
| 415 | { |
| 416 | function(std::move(__x)).swap(*this); |
| 417 | return *this; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 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 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 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 | |
| 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 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 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 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | explicit operator bool() const noexcept |
| 499 | { return !_M_empty(); } |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | _Res operator()(_ArgTypes... __args) const; |
| 512 | |
| 513 | #if __cpp_rtti |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | const type_info& target_type() const noexcept; |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 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_guides >= 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 | |
| 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); |
| 4 | | Calling '_Function_handler::_M_manager' | |
|
| 13 | | Returned allocated memory | |
|
| 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)) |
| 608 | { |
| 609 | _My_handler::_M_init_functor(_M_functor, std::move(__f)); |
| 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_rtti |
| 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 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 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 | |
| 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 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 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 | |
| 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 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 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 __cplusplus >= 201703L |
| 724 | namespace __detail::__variant |
| 725 | { |
| 726 | template<typename> struct _Never_valueless_alt; |
| 727 | |
| 728 | |
| 729 | |
| 730 | template<typename _Signature> |
| 731 | struct _Never_valueless_alt<std::function<_Signature>> |
| 732 | : std::true_type |
| 733 | { }; |
| 734 | } |
| 735 | #endif // C++17 |
| 736 | |
| 737 | _GLIBCXX_END_NAMESPACE_VERSION |
| 738 | } |
| 739 | |
| 740 | #endif // C++11 |
| 741 | #endif // _GLIBCXX_STD_FUNCTION_H |