Skip to content

Commit 2a91b6d

Browse files
committed
Introducing a pass to save ptr types
1 parent 2bfffa8 commit 2a91b6d

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/******************************************************************************
2+
* Copyright (c) 2018 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+
* Philipp Schubert and others
8+
*****************************************************************************/
9+
10+
#ifndef PHASAR_PHASARPASS_OPAQUEPTRTYPASS_H_
11+
#define PHASAR_PHASARPASS_OPAQUEPTRTYPASS_H_
12+
13+
#include "llvm/Pass.h"
14+
15+
namespace llvm {
16+
class Module;
17+
} // namespace llvm
18+
19+
namespace psr {
20+
21+
class OpaquePtrTyPass : public llvm::ModulePass {
22+
public:
23+
static inline char ID = 12; // NOLINT FIXME: make const when LLVM supports it
24+
25+
explicit OpaquePtrTyPass();
26+
OpaquePtrTyPass(const OpaquePtrTyPass &) = delete;
27+
OpaquePtrTyPass &operator=(const OpaquePtrTyPass &) = delete;
28+
~OpaquePtrTyPass() override = default;
29+
30+
[[nodiscard]] llvm::StringRef getPassName() const override;
31+
32+
bool runOnModule(llvm::Module &M) override;
33+
};
34+
35+
} // namespace psr
36+
37+
#endif
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Comments
 (0)