Skip to content

Commit

Permalink
Introducing a pass to save ptr types
Browse files Browse the repository at this point in the history
  • Loading branch information
mxHuber committed Jun 18, 2024
1 parent 2bfffa8 commit 2a91b6d
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
37 changes: 37 additions & 0 deletions include/phasar/PhasarLLVM/Passes/OpaquePtrTyPass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/******************************************************************************
* Copyright (c) 2018 Philipp Schubert.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of LICENSE.txt.
*
* Contributors:
* Philipp Schubert and others
*****************************************************************************/

#ifndef PHASAR_PHASARPASS_OPAQUEPTRTYPASS_H_
#define PHASAR_PHASARPASS_OPAQUEPTRTYPASS_H_

#include "llvm/Pass.h"

namespace llvm {
class Module;
} // namespace llvm

namespace psr {

class OpaquePtrTyPass : public llvm::ModulePass {
public:
static inline char ID = 12; // NOLINT FIXME: make const when LLVM supports it

explicit OpaquePtrTyPass();
OpaquePtrTyPass(const OpaquePtrTyPass &) = delete;
OpaquePtrTyPass &operator=(const OpaquePtrTyPass &) = delete;
~OpaquePtrTyPass() override = default;

[[nodiscard]] llvm::StringRef getPassName() const override;

bool runOnModule(llvm::Module &M) override;
};

} // namespace psr

#endif
68 changes: 68 additions & 0 deletions lib/PhasarLLVM/Passes/OpaquePtrTyPass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/******************************************************************************
* Copyright (c) 2024 Philipp Schubert.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of LICENSE.txt.
*
* Contributors:
* Maximilian Leo Huber and others
*****************************************************************************/

#include "phasar/PhasarLLVM/Passes/OpaquePtrTyPass.h"

#include "phasar/Utils/Logger.h"
#include "phasar/Utils/NlohmannLogging.h"

#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/FileSystem.h"

#include <map>

namespace psr {

OpaquePtrTyPass::OpaquePtrTyPass() : llvm::ModulePass(ID) {}

llvm::StringRef OpaquePtrTyPass::getPassName() const {
return "OpaquePtrTyPass";
}

bool OpaquePtrTyPass::runOnModule(llvm::Module &M) {
llvm::outs() << "OpaquePtrTyPass::runOnModule()\n";

std::map<std::string, std::string> PtrTypes;

// get pointer types
for (const auto &Func : M.getFunctionList()) {
if (Func.isDeclaration()) {
continue;
}

PtrTypes[Func.getName().str()] =
Func.getFunctionType()->getStructName().str();
}

// save pointer types to json file
nlohmann::json Json(PtrTypes);
std::string PathToJson = "./OpaquePtrTyPassJsons/";
std::string FileName = PathToJson + "PointerTypes.json";

llvm::sys::fs::create_directories(PathToJson);
std::error_code EC;
llvm::raw_fd_ostream FileStream(llvm::StringRef(FileName), EC);

if (EC) {
PHASAR_LOG_LEVEL(ERROR, EC.message());
return false;
}

FileStream << Json;

llvm::outs() << "Json with pointer types saved to: " << PathToJson << "\n";

return true;
}
static llvm::RegisterPass<OpaquePtrTyPass>
Phasar("opaque-pointer-type-pass", "PhASAR Opaque Pointer Type Pass", false,
false);

} // namespace psr

0 comments on commit 2a91b6d

Please sign in to comment.