|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright (c) 2024 Philipp Schubert. |
| 3 | + * All rights reserved. This program and the accompanying materials are made |
| 4 | + * available under the terms of LICENSE.txt. |
| 5 | + * |
| 6 | + * Contributors: |
| 7 | + * Maximilian Leo Huber and others |
| 8 | + *****************************************************************************/ |
| 9 | + |
| 10 | +#include "phasar/PhasarLLVM/Passes/OpaquePtrTyPass.h" |
| 11 | + |
| 12 | +#include "phasar/Utils/Logger.h" |
| 13 | +#include "phasar/Utils/NlohmannLogging.h" |
| 14 | + |
| 15 | +#include "llvm/ADT/StringRef.h" |
| 16 | +#include "llvm/IR/Module.h" |
| 17 | +#include "llvm/Support/FileSystem.h" |
| 18 | + |
| 19 | +#include <map> |
| 20 | + |
| 21 | +namespace psr { |
| 22 | + |
| 23 | +OpaquePtrTyPass::OpaquePtrTyPass() : llvm::ModulePass(ID) {} |
| 24 | + |
| 25 | +llvm::StringRef OpaquePtrTyPass::getPassName() const { |
| 26 | + return "OpaquePtrTyPass"; |
| 27 | +} |
| 28 | + |
| 29 | +bool OpaquePtrTyPass::runOnModule(llvm::Module &M) { |
| 30 | + llvm::outs() << "OpaquePtrTyPass::runOnModule()\n"; |
| 31 | + |
| 32 | + std::map<std::string, std::string> PtrTypes; |
| 33 | + |
| 34 | + // get pointer types |
| 35 | + for (const auto &Func : M.getFunctionList()) { |
| 36 | + if (Func.isDeclaration()) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + PtrTypes[Func.getName().str()] = |
| 41 | + Func.getFunctionType()->getStructName().str(); |
| 42 | + } |
| 43 | + |
| 44 | + // save pointer types to json file |
| 45 | + nlohmann::json Json(PtrTypes); |
| 46 | + std::string PathToJson = "./OpaquePtrTyPassJsons/"; |
| 47 | + std::string FileName = PathToJson + "PointerTypes.json"; |
| 48 | + |
| 49 | + llvm::sys::fs::create_directories(PathToJson); |
| 50 | + std::error_code EC; |
| 51 | + llvm::raw_fd_ostream FileStream(llvm::StringRef(FileName), EC); |
| 52 | + |
| 53 | + if (EC) { |
| 54 | + PHASAR_LOG_LEVEL(ERROR, EC.message()); |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + FileStream << Json; |
| 59 | + |
| 60 | + llvm::outs() << "Json with pointer types saved to: " << PathToJson << "\n"; |
| 61 | + |
| 62 | + return true; |
| 63 | +} |
| 64 | +static llvm::RegisterPass<OpaquePtrTyPass> |
| 65 | + Phasar("opaque-pointer-type-pass", "PhASAR Opaque Pointer Type Pass", false, |
| 66 | + false); |
| 67 | + |
| 68 | +} // namespace psr |
0 commit comments