|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright (c) 2023 Fabian Schiebel. |
| 3 | + * All rights reserved. This program and the accompanying materials are made |
| 4 | + * available under the terms of LICENSE.txt. |
| 5 | + * |
| 6 | + * Contributors: |
| 7 | + * Fabian Schiebel and others |
| 8 | + *****************************************************************************/ |
| 9 | + |
| 10 | +#ifndef PHASAR_DATAFLOW_IFDSIDE_ENTRYPOINTUTILS_H |
| 11 | +#define PHASAR_DATAFLOW_IFDSIDE_ENTRYPOINTUTILS_H |
| 12 | + |
| 13 | +#include "phasar/ControlFlow/CFGBase.h" |
| 14 | +#include "phasar/DB/ProjectIRDBBase.h" |
| 15 | + |
| 16 | +#include "llvm/ADT/STLExtras.h" |
| 17 | + |
| 18 | +#include <functional> |
| 19 | + |
| 20 | +namespace psr { |
| 21 | + |
| 22 | +namespace detail { |
| 23 | +template <typename EntryRange, typename C, typename HandlerFn> |
| 24 | +void forallStartingPoints(const EntryRange &EntryPoints, const CFGBase<C> &CFG, |
| 25 | + HandlerFn Handler) { |
| 26 | + for (const auto &EntryPointFn : EntryPoints) { |
| 27 | + if constexpr (std::is_convertible_v<std::decay_t<decltype(EntryPointFn)>, |
| 28 | + bool>) { |
| 29 | + if (!EntryPointFn) { |
| 30 | + continue; |
| 31 | + } |
| 32 | + } |
| 33 | + for (const auto &SP : CFG.getStartPointsOf(EntryPointFn)) { |
| 34 | + std::invoke(Handler, SP); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +template <typename EntryRange, typename C, typename ICFGorIRDB, |
| 40 | + typename HandlerFn> |
| 41 | +void forallStartingPoints(const EntryRange &EntryPoints, const ICFGorIRDB *ICDB, |
| 42 | + const CFGBase<C> &CFG, HandlerFn Handler) { |
| 43 | + |
| 44 | + if (llvm::hasSingleElement(EntryPoints) && |
| 45 | + *llvm::adl_begin(EntryPoints) == "__ALL__") { |
| 46 | + forallStartingPoints(ICDB->getAllFunctions(), CFG, std::move(Handler)); |
| 47 | + } else { |
| 48 | + forallStartingPoints(llvm::map_range(EntryPoints, |
| 49 | + [ICDB](llvm::StringRef Name) { |
| 50 | + return ICDB->getFunction(Name); |
| 51 | + }), |
| 52 | + CFG, std::move(Handler)); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +} // namespace detail |
| 57 | + |
| 58 | +template <typename EntryRange, typename C, typename DB, typename HandlerFn> |
| 59 | +void forallStartingPoints(const EntryRange &EntryPoints, |
| 60 | + const ProjectIRDBBase<DB> *IRDB, |
| 61 | + const CFGBase<C> &CFG, HandlerFn Handler) { |
| 62 | + return detail::forallStartingPoints(EntryPoints, IRDB, CFG, |
| 63 | + std::move(Handler)); |
| 64 | +} |
| 65 | + |
| 66 | +template <typename EntryRange, typename I, typename HandlerFn> |
| 67 | +void forallStartingPoints(const EntryRange &EntryPoints, const I *ICF, |
| 68 | + HandlerFn Handler) { |
| 69 | + detail::forallStartingPoints(EntryPoints, ICF, *ICF, std::move(Handler)); |
| 70 | +} |
| 71 | + |
| 72 | +template <typename EntryRange, typename C, typename DB, typename SeedsT, |
| 73 | + typename D, typename L> |
| 74 | +void addSeedsForStartingPoints(const EntryRange &EntryPoints, |
| 75 | + const ProjectIRDBBase<DB> *IRDB, |
| 76 | + const CFGBase<C> &CFG, SeedsT &Seeds, |
| 77 | + const D &ZeroValue, const L &BottomValue) { |
| 78 | + forallStartingPoints(EntryPoints, IRDB, CFG, |
| 79 | + [&Seeds, &ZeroValue, &BottomValue](const auto &SP) { |
| 80 | + Seeds.addSeed(SP, ZeroValue, BottomValue); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +template <typename EntryRange, typename I, typename SeedsT, typename D, |
| 85 | + typename L> |
| 86 | +void addSeedsForStartingPoints(const EntryRange &EntryPoints, const I *ICF, |
| 87 | + SeedsT &Seeds, const D &ZeroValue, |
| 88 | + const L &BottomValue) { |
| 89 | + forallStartingPoints(EntryPoints, ICF, |
| 90 | + [&Seeds, &ZeroValue, &BottomValue](const auto &SP) { |
| 91 | + Seeds.addSeed(SP, ZeroValue, BottomValue); |
| 92 | + }); |
| 93 | +} |
| 94 | +} // namespace psr |
| 95 | + |
| 96 | +#endif // PHASAR_DATAFLOW_IFDSIDE_ENTRYPOINTUTILS_H |
0 commit comments