Skip to content

Commit b64de2e

Browse files
committed
Create unsafe/refcount rules index
1 parent f19aca1 commit b64de2e

3 files changed

Lines changed: 50 additions & 9 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,16 @@ add_custom_command(
300300

301301
add_custom_target("index-cpp-rules" ALL DEPENDS ${cpp_rules_index_outputs})
302302

303+
set(rust_rules_index_outputs ${CMAKE_BINARY_DIR}/rules_index/index_unsafe.json ${CMAKE_BINARY_DIR}/rules_index/index_refcount.json)
304+
add_custom_command(
305+
OUTPUT ${rust_rules_index_outputs}
306+
COMMAND ${PREPROCESSOR_BIN} --merge ${CMAKE_BINARY_DIR}/rules_index ${RULES_IR_DIR}
307+
DEPENDS ${rust_rules_ir_outputs} ${PREPROCESSOR_BIN}
308+
VERBATIM
309+
)
310+
303311
add_custom_target("preprocess-rust-rules" ALL
304-
DEPENDS ${rust_rules_ir_outputs})
312+
DEPENDS ${rust_rules_ir_outputs} ${rust_rules_index_outputs})
305313

306314
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES
307315
"${CMAKE_BINARY_DIR}/libcc2rs-target"

‎rule-preprocessor/src/ir.rs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ impl Model {
2727
Model::Unsafe => "ir_unsafe.json",
2828
}
2929
}
30+
31+
pub fn index_filename(self) -> &'static str {
32+
match self {
33+
Model::Refcount => "index_refcount.json",
34+
Model::Unsafe => "index_unsafe.json",
35+
}
36+
}
3037
}
3138

3239
fn validate_consecutive_keys<'a>(
@@ -299,3 +306,27 @@ impl RulesIR {
299306
}
300307
}
301308
}
309+
310+
pub fn write_index(index_dir: &Path, ir_dir: &Path) {
311+
let mut modules: Vec<_> = std::fs::read_dir(ir_dir)
312+
.unwrap()
313+
.map(|entry| entry.unwrap().path())
314+
.filter(|dir| dir.is_dir())
315+
.collect();
316+
modules.sort();
317+
std::fs::create_dir_all(index_dir).unwrap();
318+
for model in Model::ALL {
319+
let index: BTreeMap<String, serde_json::Value> = modules
320+
.iter()
321+
.filter_map(|dir| {
322+
let ir = std::fs::read_to_string(dir.join(model.ir_filename())).ok()?;
323+
let name = dir.file_name().unwrap().to_string_lossy().to_string();
324+
Some((name, serde_json::from_str(&ir).unwrap()))
325+
})
326+
.collect();
327+
let json_path = index_dir.join(model.index_filename());
328+
let json = serde_json::to_string_pretty(&index).unwrap();
329+
std::fs::write(&json_path, format!("{json}\n")).unwrap();
330+
println!("{}", json_path.display());
331+
}
332+
}

‎rule-preprocessor/src/main.rs‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ use semantic::SemanticAnalysis;
2323
use syntactic::SyntacticAnalysis;
2424

2525
fn main() {
26+
let usage = "usage: rule-preprocessor <out-dir> <rule-dir> | rule-preprocessor --merge <index-dir> <ir-dir>";
2627
let mut args = std::env::args().skip(1);
27-
let out_dir = args
28-
.next()
29-
.expect("usage: rule-preprocessor <out-dir> <rule-dir>");
30-
let in_dir = args
31-
.next()
32-
.expect("usage: rule-preprocessor <out-dir> <rule-dir>");
33-
34-
let out = std::path::PathBuf::from(out_dir);
28+
let first = args.next().expect(usage);
29+
if first == "--merge" {
30+
let index_dir = std::path::PathBuf::from(args.next().expect(usage));
31+
let ir_dir = std::path::PathBuf::from(args.next().expect(usage));
32+
ir::write_index(&index_dir, &ir_dir);
33+
return;
34+
}
35+
let out = std::path::PathBuf::from(first);
36+
let in_dir = args.next().expect(usage);
3537
SemanticAnalysis::run(
3638
SyntacticAnalysis::run(&std::fs::canonicalize(&in_dir).unwrap()),
3739
&out,

0 commit comments

Comments
 (0)