Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ jobs:
run: ninja
working-directory: build

- name: Check rules
run: ninja check-rules
working-directory: build

- name: Run unit tests
run: ninja check
working-directory: build
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ tmp
*.BASE.*
*.LOCAL.*
*.REMOTE.*

# Generated by cpp-rule-preprocessor at build time.
rules/*/ir_src.json
17 changes: 11 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ add_custom_target("check"
DEPENDS check-libcc2rs check-unit
)

set(RULES_IR_DIR "${CMAKE_BINARY_DIR}/rules")

file(GLOB rule_subdirs ${PROJECT_SOURCE_DIR}/rules/*)
set(cpp_rules_ir_outputs)
set(rust_rules_ir_outputs)
Expand All @@ -160,20 +162,23 @@ foreach(_rule_dir IN LISTS rule_subdirs)
if(NOT _srcs)
continue()
endif()
set(_out ${_rule_dir}/ir_src.json)
get_filename_component(_rule_name ${_rule_dir} NAME)
set(_out_dir ${RULES_IR_DIR}/${_rule_name})
set(_out ${_out_dir}/ir_src.json)
add_custom_command(
OUTPUT ${_out}
COMMAND $<TARGET_FILE:cpp-rule-preprocessor> --dir ${_rule_dir}
COMMAND ${CMAKE_COMMAND} -E make_directory ${_out_dir}
COMMAND $<TARGET_FILE:cpp-rule-preprocessor> --dir ${_rule_dir} --out ${_out}
DEPENDS ${_srcs} ${PROJECT_SOURCE_DIR}/cpp2rust/cpp_rule_preprocessor.cpp
VERBATIM
)
list(APPEND cpp_rules_ir_outputs ${_out})
if(EXISTS ${_rule_dir}/tgt_unsafe.rs)
list(APPEND rust_rules_ir_outputs ${_rule_dir}/ir_unsafe.json)
list(APPEND rust_rules_ir_outputs ${_out_dir}/ir_unsafe.json)
list(APPEND rust_rules_inputs ${_rule_dir}/tgt_unsafe.rs)
endif()
if(EXISTS ${_rule_dir}/tgt_refcount.rs)
list(APPEND rust_rules_ir_outputs ${_rule_dir}/ir_refcount.json)
list(APPEND rust_rules_ir_outputs ${_out_dir}/ir_refcount.json)
list(APPEND rust_rules_inputs ${_rule_dir}/tgt_refcount.rs)
endif()
endforeach()
Expand All @@ -189,6 +194,7 @@ add_custom_command(
COMMAND ${CMAKE_COMMAND} -E env
CARGO_TARGET_DIR=${CMAKE_BINARY_DIR}/target_preprocessor
cargo +${RUST_NIGHTLY_VERSION} run --release --manifest-path "${PROJECT_SOURCE_DIR}/rule-preprocessor/Cargo.toml"
-- "${RULES_IR_DIR}"
DEPENDS ${rust_rules_inputs} ${rule_preprocessor_sources} "${RUST_STAMP_FILE}"
VERBATIM
)
Expand All @@ -197,10 +203,9 @@ add_custom_target("preprocess-rust-rules" ALL
DEPENDS ${rust_rules_ir_outputs})

add_custom_target("check-rules"
COMMAND find rules "\\(" -name ir_unsafe.json -o -name ir_refcount.json -o -name ir_src.json "\\)" -delete
COMMAND ${CMAKE_COMMAND} -E rm -rf "${RULES_IR_DIR}"
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target preprocess-cpp-rules
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target preprocess-rust-rules
COMMAND git diff --exit-code -- rules/
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)

Expand Down
31 changes: 16 additions & 15 deletions cpp2rust/converter/mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,21 +409,22 @@ TranslationRule::TypeRule *search(clang::QualType qual_type) {
}

void addRulesFromDirectory(const std::filesystem::path &dir, Model model) {
for (const auto &entry : std::filesystem::recursive_directory_iterator(dir)) {
auto &path = entry.path();
if (entry.is_regular_file() &&
(path.extension() == ".cpp" || path.extension() == ".c")) {
auto [expr_rules, type_rules] = TranslationRule::Load(path, model);
if (expr_rules.empty() && type_rules.empty()) {
log() << "No rules found in " << path << '\n';
continue;
}
for (auto &[_, rule] : expr_rules) {
exprs_.emplace(GetExprMapKey(rule.src), std::move(rule));
}
for (auto &[_, rule] : type_rules) {
types_.emplace(GetTypeMapKey(rule.src), std::move(rule));
}
namespace fs = std::filesystem;
for (const auto &entry : fs::directory_iterator(dir)) {
const auto &path = entry.path();
assert(fs::exists(path / "ir_src.json") &&
(fs::exists(path / "ir_unsafe.json") ||
fs::exists(path / "ir_refcount.json")));
auto [expr_rules, type_rules] = TranslationRule::Load(path, model);
if (expr_rules.empty() && type_rules.empty()) {
log() << "No rules found in " << path << '\n';
continue;
}
for (auto &[_, rule] : expr_rules) {
exprs_.emplace(GetExprMapKey(rule.src), std::move(rule));
}
for (auto &[_, rule] : type_rules) {
types_.emplace(GetTypeMapKey(rule.src), std::move(rule));
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions cpp2rust/converter/translation_rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,10 @@ void TypeRule::dump() const {
}
}

std::pair<ExprRules, TypeRules> Load(const std::filesystem::path &path,
std::pair<ExprRules, TypeRules> Load(const std::filesystem::path &dir,
Model model) {
ExprRules exprs;
TypeRules types;
auto dir = path.parent_path();
LoadTgtFromIR(exprs, types, dir / "ir_unsafe.json");

if (model == Model::kRefCount) {
Expand Down
2 changes: 1 addition & 1 deletion cpp2rust/converter/translation_rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ struct TypeRule {
using ExprRules = std::unordered_map<std::string, ExprRule>;
using TypeRules = std::unordered_map<std::string, TypeRule>;

std::pair<ExprRules, TypeRules> Load(const std::filesystem::path &path,
std::pair<ExprRules, TypeRules> Load(const std::filesystem::path &dir,
Model model);
} // namespace cpp2rust::TranslationRule
18 changes: 17 additions & 1 deletion cpp2rust/cpp2rust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,29 @@ static fs::path GetExecutableDir() {
return ".";
}

static bool HasIRFiles(const fs::path &dir) {
std::error_code ec;
for (auto it = fs::recursive_directory_iterator(dir, ec);
!ec && it != fs::recursive_directory_iterator(); it.increment(ec)) {
if (!it->is_directory()) {
continue;
}
const auto &p = it->path();
if (fs::exists(p / "ir_src.json") && (fs::exists(p / "ir_unsafe.json") ||
fs::exists(p / "ir_refcount.json"))) {
return true;
}
}
return false;
}

static bool ResolveRulesDir() {
std::array<fs::path, 3> candidates = {fs::path("./rules"),
fs::path("../rules"),
GetExecutableDir() / "../../rules"};

for (const auto &dir : candidates) {
if (fs::exists(dir) && fs::is_directory(dir)) {
if (fs::exists(dir) && fs::is_directory(dir) && HasIRFiles(dir)) {
RulesDir = fs::canonical(dir).string();
llvm::errs() << "Using rules directory: " << RulesDir << '\n';
return true;
Expand Down
9 changes: 7 additions & 2 deletions cpp2rust/cpp_rule_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,10 +779,15 @@ llvm::cl::OptionCategory cat("cpp-rule-preprocessor options");
llvm::cl::opt<std::string>
SrcDir("dir",
llvm::cl::desc("Path to a rule directory containing src.c and/or "
"src.cpp. ir_src.json is written into this dir."),
"src.cpp."),
llvm::cl::value_desc("rule-dir"), llvm::cl::Required,
llvm::cl::cat(cat));

llvm::cl::opt<std::string>
OutPath("out", llvm::cl::desc("Path of the ir_src.json file to write."),
llvm::cl::value_desc("out.json"), llvm::cl::Required,
llvm::cl::cat(cat));

} // namespace

int main(int argc, char *argv[]) {
Expand All @@ -809,7 +814,7 @@ int main(int argc, char *argv[]) {
}
}

auto out_path = dir / "ir_src.json";
fs::path out_path = OutPath.getValue();
std::error_code ec;
llvm::raw_fd_ostream out(out_path.string(), ec);
if (ec) {
Expand Down
22 changes: 13 additions & 9 deletions rule-preprocessor/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,23 @@ pub struct RulesIR {
}

impl RulesIR {
pub fn write_ir(&self) {
let crate_root = self.crate_root.canonicalize().unwrap();
pub fn write_ir(&self, out_dir: &Path) {
for (rule_path, file_ir) in &self.all_ir {
let rule_path = Path::new(rule_path);
let file_name = rule_path.file_name().unwrap().to_str().unwrap();
let json_name = file_name.replace("tgt_", "ir_").replace(".rs", ".json");
let json_path = rule_path.parent().unwrap().join(json_name);

let rule_name = rule_path.parent().unwrap().file_name().unwrap();
let json_name = rule_path
.file_name()
.unwrap()
.to_str()
.unwrap()
.replace("tgt_", "ir_")
.replace(".rs", ".json");
let json_path = out_dir.join(rule_name).join(json_name);

std::fs::create_dir_all(json_path.parent().unwrap()).unwrap();
let json = serde_json::to_string_pretty(file_ir).unwrap();
std::fs::write(&json_path, format!("{json}\n")).unwrap();

let json_rel = json_path.strip_prefix(&crate_root).unwrap_or(&json_path);
println!("{}", json_rel.display());
println!("{}", json_path.display());
}
}
}
5 changes: 4 additions & 1 deletion rule-preprocessor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ use semantic::SemanticAnalysis;
use syntactic::SyntacticAnalysis;

fn main() {
let out_dir = std::env::args()
.nth(1)
.expect("usage: rule-preprocessor <out-dir>");
SemanticAnalysis::run(SyntacticAnalysis::run(
&std::fs::canonicalize("../rules").unwrap(),
))
.write_ir();
.write_ir(&std::path::PathBuf::from(out_dir));
}
Loading
Loading