mirror of
https://github.com/lifting-bits/remill
synced 2026-06-21 13:56:07 +00:00
bc:util: Use std::filesystem::path in place of std::string in some apis.
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
// clang-format on
|
||||
|
||||
#include <array>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
@@ -159,8 +160,9 @@ std::unique_ptr<llvm::Module> LoadModuleFromFile(llvm::LLVMContext *context,
|
||||
// code that we want to lift.
|
||||
std::unique_ptr<llvm::Module> LoadArchSemantics(const Arch *arch);
|
||||
// `sem_dirs` is forwarded to `FindSemanticsBitcodeFile`.
|
||||
std::unique_ptr<llvm::Module> LoadArchSemantics(const Arch *arch,
|
||||
const std::vector<std::string> &sem_dirs);
|
||||
std::unique_ptr<llvm::Module>
|
||||
LoadArchSemantics(const Arch *arch,
|
||||
const std::vector<std::filesystem::path> &sem_dirs);
|
||||
|
||||
// Store an LLVM module into a file.
|
||||
bool StoreModuleToFile(llvm::Module *module, std::string_view file_name,
|
||||
@@ -172,13 +174,14 @@ bool StoreModuleIRToFile(llvm::Module *module, std::string_view file_name,
|
||||
|
||||
// Find a semantics bitcode file for the architecture `arch`.
|
||||
// Default compile-time created list of directories is searched.
|
||||
std::optional<std::string> FindSemanticsBitcodeFile(std::string_view arch);
|
||||
std::optional<std::filesystem::path> FindSemanticsBitcodeFile(std::string_view arch);
|
||||
// List of directories to search is provided as second argument - default compile time
|
||||
// created list is used as fallback only if `fallback_to_defaults` is set.
|
||||
// A "shallow" search happens, searching for file `arch` + ".bc".
|
||||
std::optional<std::string> FindSemanticsBitcodeFile(std::string_view arch,
|
||||
const std::vector< std::string > &dirs,
|
||||
bool fallback_to_defaults=true);
|
||||
std::optional<std::filesystem::path>
|
||||
FindSemanticsBitcodeFile(std::string_view arch,
|
||||
const std::vector<std::filesystem::path> &dirs,
|
||||
bool fallback_to_defaults=true);
|
||||
|
||||
// Return a pointer to the Nth argument (N=0 is the first argument).
|
||||
llvm::Argument *NthArgument(llvm::Function *func, size_t index);
|
||||
|
||||
+18
-14
@@ -379,8 +379,10 @@ std::unique_ptr<llvm::Module> LoadArchSemantics(const Arch *arch) {
|
||||
return LoadArchSemantics(arch, {});
|
||||
}
|
||||
|
||||
std::unique_ptr<llvm::Module> LoadArchSemantics(const Arch *arch,
|
||||
const std::vector<std::string> &sem_dirs) {
|
||||
std::unique_ptr<llvm::Module>
|
||||
LoadArchSemantics(const Arch *arch,
|
||||
const std::vector<std::filesystem::path> &sem_dirs)
|
||||
{
|
||||
auto arch_name = GetArchName(arch->arch_name);
|
||||
// If `sem_dirs` does not contain the dir, fallback to compiled in paths.
|
||||
auto path = FindSemanticsBitcodeFile(arch_name, sem_dirs, true);
|
||||
@@ -566,9 +568,11 @@ namespace {
|
||||
#define S(x) _S(x)
|
||||
#define MAJOR_MINOR S(LLVM_VERSION_MAJOR) "." S(LLVM_VERSION_MINOR)
|
||||
|
||||
const std::vector< std::string > &DefaultSemanticsSearchPaths()
|
||||
using paths_t = std::vector<std::filesystem::path>;
|
||||
|
||||
const paths_t &DefaultSemanticsSearchPaths()
|
||||
{
|
||||
static const std::vector< std::string > paths =
|
||||
static const paths_t paths =
|
||||
{
|
||||
REMILL_BUILD_SEMANTICS_DIR_X86,
|
||||
REMILL_BUILD_SEMANTICS_DIR_AARCH32,
|
||||
@@ -583,29 +587,29 @@ const std::vector< std::string > &DefaultSemanticsSearchPaths()
|
||||
return paths;
|
||||
}
|
||||
|
||||
using maybe_path_t = std::optional< std::filesystem::path >;
|
||||
using maybe_path_t = std::optional<std::filesystem::path>;
|
||||
|
||||
maybe_path_t IsSemanticsBitcodeFile(
|
||||
std::string_view dir,
|
||||
std::filesystem::path dir,
|
||||
std::string_view arch)
|
||||
{
|
||||
auto path = std::filesystem::path(dir) / (std::string(arch) + ".bc");
|
||||
auto path = dir / (std::string(arch) + ".bc");
|
||||
return (std::filesystem::exists(path)) ? std::make_optional(std::move(path)) : std::nullopt;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::optional<std::string> _FindSemanticsBitcodeFile(std::string_view arch,
|
||||
const std::vector< std::string > &dirs) {
|
||||
maybe_path_t _FindSemanticsBitcodeFile(std::string_view arch,
|
||||
const paths_t &dirs) {
|
||||
for (const auto &dir : dirs)
|
||||
if (auto sem_path = IsSemanticsBitcodeFile(dir, arch))
|
||||
return sem_path->string();
|
||||
return sem_path;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<std::string> FindSemanticsBitcodeFile(std::string_view arch,
|
||||
const std::vector< std::string > &dirs,
|
||||
bool fallback_to_defaults) {
|
||||
maybe_path_t FindSemanticsBitcodeFile(std::string_view arch,
|
||||
const paths_t &dirs,
|
||||
bool fallback_to_defaults) {
|
||||
if (auto path = _FindSemanticsBitcodeFile(arch, dirs)) {
|
||||
return path;
|
||||
}
|
||||
@@ -616,7 +620,7 @@ std::optional<std::string> FindSemanticsBitcodeFile(std::string_view arch,
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<std::string> FindSemanticsBitcodeFile(std::string_view arch) {
|
||||
maybe_path_t FindSemanticsBitcodeFile(std::string_view arch) {
|
||||
return _FindSemanticsBitcodeFile(arch, DefaultSemanticsSearchPaths());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user