|
| 1 | +/** |
| 2 | + * File: CGC2DemoMD.h |
| 3 | + * License: Part of the MetaCG project. Licensed under BSD 3 clause license. See LICENSE.txt file at |
| 4 | + * https://github.com/tudasc/metacg/LICENSE.txt |
| 5 | + */ |
| 6 | +#ifndef METACG_CGCOLLECTOR2_CAGEDEMOMD_h |
| 7 | +#define METACG_CGCOLLECTOR2_CAGEDEMOMD_h |
| 8 | + |
| 9 | +#include "metacg/metadata/MetaData.h" |
| 10 | + |
| 11 | +namespace clang { |
| 12 | +class FunctionDecl; |
| 13 | +} |
| 14 | + |
| 15 | +class CaGeDemoMD : public metacg::MetaData::Registrar<CaGeDemoMD> { |
| 16 | + public: |
| 17 | + static constexpr const char* key = "CaCeDemoMetadata"; |
| 18 | + |
| 19 | + CaGeDemoMD() = default; |
| 20 | + /** |
| 21 | + * Specify how to create your Metadata from a json input |
| 22 | + */ |
| 23 | + explicit CaGeDemoMD(const nlohmann::json&, metacg::StrToNodeMapping&) { |
| 24 | + assert(false && "This metadata should not be created via json"); |
| 25 | + } |
| 26 | + |
| 27 | + explicit CaGeDemoMD(const void* const f) : address(reinterpret_cast<uintptr_t>(f)) {} |
| 28 | + explicit CaGeDemoMD(const uintptr_t p) : address(p) {} |
| 29 | + |
| 30 | + /** |
| 31 | + * Specify how to serialize your metadata into a json output |
| 32 | + * Return an empty json object to signal that this metadata will not be serialized |
| 33 | + * @return empty json object |
| 34 | + */ |
| 35 | + nlohmann::json toJson(metacg::NodeToStrMapping&) const final { |
| 36 | + // We just store the address |
| 37 | + return {{"Address", reinterpret_cast<uintptr_t>(address)}}; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Allows to query the metadata-key from a baseclass-pointer via virtual dispatch |
| 42 | + * @return your metadata key |
| 43 | + */ |
| 44 | + [[nodiscard]] const char* getKey() const final { return key; } |
| 45 | + |
| 46 | + /** |
| 47 | + * If your metadata stores ids of other callgraph-nodes, they might be invalidated |
| 48 | + * This can happen if the graph containing your metadata is merged with another graph. |
| 49 | + * Your metadata might be copied into the new graph |
| 50 | + * In this case you need to remap the stored node-ids to be valid in the new merged graph context |
| 51 | + * |
| 52 | + * @param g A map how to rename the old node-id to a new node-id, which is valid in the merged graph |
| 53 | + */ |
| 54 | + void applyMapping([[maybe_unused]] const metacg::GraphMapping& g) final {} |
| 55 | + |
| 56 | + /** |
| 57 | + * How to merge your metadata with itself |
| 58 | + * This can happen if the graph containing your metadata is merged with another graph. |
| 59 | + * Update the state of your this metadata object accordingly |
| 60 | + * @param toMerge the other Metadata to merge with |
| 61 | + * @param mergeAction contains whether the merge will replace the other metadata |
| 62 | + * @param g A map how to rename the old node-id to a new node-id, which is valid in the merged graph |
| 63 | + */ |
| 64 | + void merge(const MetaData& toMerge, std::optional<metacg::MergeAction> mergeAction, |
| 65 | + const metacg::GraphMapping& g) final { |
| 66 | + if (std::strcmp(toMerge.getKey(), getKey()) != 0) { |
| 67 | + metacg::MCGLogger::instance().getErrConsole()->error( |
| 68 | + "The MetaData which was tried to merge with ASTNodeMetadata was of a different MetaData type"); |
| 69 | + abort(); |
| 70 | + } |
| 71 | + assert(false && "This metadata can not be exported and therefore is not mergeable"); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Specify how to clone your metadata |
| 76 | + * @return a cloned version of your metadata |
| 77 | + */ |
| 78 | + [[nodiscard]] std::unique_ptr<MetaData> clone() const final { return std::make_unique<CaGeDemoMD>(); } |
| 79 | + |
| 80 | + uintptr_t getAdressValue() { return reinterpret_cast<uintptr_t>(address); } |
| 81 | + |
| 82 | + private: |
| 83 | + uintptr_t address = 0; |
| 84 | +}; |
| 85 | +#endif |
0 commit comments