Skip to content

Commit 5ebbdaa

Browse files
committed
Merge branch 'tgt-index' into ast-matcher
2 parents 6efa4d9 + b64de2e commit 5ebbdaa

4 files changed

Lines changed: 85 additions & 101 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ set(CPP2RUST_CXX_STANDARDS 11 17 20 23)
230230
set(c_rules_index_parts)
231231
set(cpp_rules_index_parts)
232232
set(rules_index_parts_dir ${CMAKE_BINARY_DIR}/rules_index/parts)
233-
set(rust_rules_srcs)
234-
set(rust_rules_index_outputs ${CMAKE_BINARY_DIR}/rules_index/index_unsafe.json ${CMAKE_BINARY_DIR}/rules_index/index_refcount.json)
233+
set(RULES_IR_DIR "${CMAKE_BINARY_DIR}/rules")
234+
set(rust_rules_ir_outputs)
235235
foreach(_rule_dir IN LISTS rule_subdirs)
236236
file(GLOB _cpp_srcs ${_rule_dir}/src.c ${_rule_dir}/src.cpp)
237237
if(NOT _cpp_srcs)
@@ -261,25 +261,35 @@ foreach(_rule_dir IN LISTS rule_subdirs)
261261
list(APPEND cpp_rules_index_parts ${_part})
262262
endforeach()
263263

264-
foreach(_model IN ITEMS unsafe refcount)
265-
if(EXISTS ${_rule_dir}/tgt_${_model}.rs)
266-
list(APPEND rust_rules_srcs ${_rule_dir}/tgt_${_model}.rs)
267-
endif()
268-
endforeach()
264+
set(_out_dir ${RULES_IR_DIR}/${_rule_name})
265+
set(_rust_srcs)
266+
set(_rust_out)
267+
if(EXISTS ${_rule_dir}/tgt_unsafe.rs)
268+
list(APPEND _rust_out ${_out_dir}/ir_unsafe.json)
269+
list(APPEND _rust_srcs ${_rule_dir}/tgt_unsafe.rs)
270+
endif()
271+
if(EXISTS ${_rule_dir}/tgt_refcount.rs)
272+
list(APPEND _rust_out ${_out_dir}/ir_refcount.json)
273+
list(APPEND _rust_srcs ${_rule_dir}/tgt_refcount.rs)
274+
endif()
275+
if(NOT _rust_srcs)
276+
continue()
277+
endif()
278+
add_custom_command(
279+
OUTPUT ${_rust_out}
280+
COMMAND ${CMAKE_COMMAND} -E make_directory ${_out_dir}
281+
COMMAND ${CARGO_ENV_RULE_PREPROCESSOR}
282+
RULE_PREPROCESSOR_ARTIFACTS=${PREPROCESSOR_ARTIFACTS}
283+
RUSTUP_TOOLCHAIN=${RUST_NIGHTLY_VERSION}
284+
LD_LIBRARY_PATH=${NIGHTLY_SYSROOT}/lib
285+
DYLD_LIBRARY_PATH=${NIGHTLY_SYSROOT}/lib
286+
${PREPROCESSOR_BIN} ${_out_dir} ${_rule_dir}
287+
DEPENDS ${_rust_srcs} ${PREPROCESSOR_BIN} ${PREPROCESSOR_ARTIFACTS} ${RULES_LIB}
288+
VERBATIM
289+
)
290+
list(APPEND rust_rules_ir_outputs ${_rust_out})
269291
endforeach()
270292

271-
add_custom_command(
272-
OUTPUT ${rust_rules_index_outputs}
273-
COMMAND ${CARGO_ENV_RULE_PREPROCESSOR}
274-
RULE_PREPROCESSOR_ARTIFACTS=${PREPROCESSOR_ARTIFACTS}
275-
RUSTUP_TOOLCHAIN=${RUST_NIGHTLY_VERSION}
276-
LD_LIBRARY_PATH=${NIGHTLY_SYSROOT}/lib
277-
DYLD_LIBRARY_PATH=${NIGHTLY_SYSROOT}/lib
278-
${PREPROCESSOR_BIN} ${CMAKE_BINARY_DIR}/rules_index ${PROJECT_SOURCE_DIR}/rules
279-
DEPENDS ${rust_rules_srcs} ${PREPROCESSOR_BIN} ${PREPROCESSOR_ARTIFACTS} ${RULES_LIB}
280-
VERBATIM
281-
)
282-
283293
add_custom_command(
284294
OUTPUT ${CMAKE_BINARY_DIR}/rules_index/c.json
285295
COMMAND $<TARGET_FILE:cpp-rule-indexer> -o ${CMAKE_BINARY_DIR}/rules_index/c.json ${c_rules_index_parts}
@@ -296,8 +306,16 @@ add_custom_command(
296306

297307
add_custom_target("index-cpp-rules" ALL DEPENDS ${CMAKE_BINARY_DIR}/rules_index/c.json ${CMAKE_BINARY_DIR}/rules_index/cpp.json)
298308

309+
set(rust_rules_index_outputs ${CMAKE_BINARY_DIR}/rules_index/index_unsafe.json ${CMAKE_BINARY_DIR}/rules_index/index_refcount.json)
310+
add_custom_command(
311+
OUTPUT ${rust_rules_index_outputs}
312+
COMMAND ${PREPROCESSOR_BIN} --merge ${CMAKE_BINARY_DIR}/rules_index ${RULES_IR_DIR}
313+
DEPENDS ${rust_rules_ir_outputs} ${PREPROCESSOR_BIN}
314+
VERBATIM
315+
)
316+
299317
add_custom_target("preprocess-rust-rules" ALL
300-
DEPENDS ${rust_rules_index_outputs})
318+
DEPENDS ${rust_rules_ir_outputs} ${rust_rules_index_outputs})
301319

302320
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES
303321
"${CMAKE_BINARY_DIR}/libcc2rs-target"

‎rule-preprocessor/src/ir.rs‎

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ impl Model {
2121
}
2222
}
2323

24-
pub fn index_filename(self) -> &'static str {
24+
pub fn ir_filename(self) -> &'static str {
2525
match self {
26-
Model::Refcount => "index_refcount.json",
27-
Model::Unsafe => "index_unsafe.json",
26+
Model::Refcount => "ir_refcount.json",
27+
Model::Unsafe => "ir_unsafe.json",
2828
}
2929
}
3030
}
@@ -280,37 +280,22 @@ impl<'a> IntoIterator for &'a RulesIR {
280280
}
281281

282282
impl RulesIR {
283-
pub fn name(&self) -> String {
284-
self.dir.file_name().unwrap().to_string_lossy().to_string()
283+
pub fn write_ir(&self, out_dir: &Path) {
284+
for (model, file_ir) in self {
285+
let json_path = out_dir.join(model.ir_filename());
286+
let json = serde_json::to_string_pretty(file_ir).unwrap();
287+
std::fs::write(&json_path, format!("{json}\n")).unwrap();
288+
println!("{}", json_path.display());
289+
}
285290
}
286291

287-
pub fn get_mut(&mut self, file: &Path) -> Option<&mut FileIr> {
288-
if file == self.dir.join(Model::Refcount.src_filename()) {
292+
pub fn get_mut(&mut self, file: &str) -> Option<&mut FileIr> {
293+
if file.ends_with(Model::Refcount.src_filename()) {
289294
self.refcount_ir.as_mut()
290-
} else if file == self.dir.join(Model::Unsafe.src_filename()) {
295+
} else if file.ends_with(Model::Unsafe.src_filename()) {
291296
self.unsafe_ir.as_mut()
292297
} else {
293298
None
294299
}
295300
}
296301
}
297-
298-
pub fn write_index(rules: &[RulesIR], index_dir: &Path) {
299-
std::fs::create_dir_all(index_dir).unwrap();
300-
for model in Model::ALL {
301-
let index: BTreeMap<String, &FileIr> = rules
302-
.iter()
303-
.filter_map(|ir| {
304-
let file_ir = match model {
305-
Model::Refcount => ir.refcount_ir.as_ref(),
306-
Model::Unsafe => ir.unsafe_ir.as_ref(),
307-
}?;
308-
Some((ir.name(), file_ir))
309-
})
310-
.collect();
311-
let json_path = index_dir.join(model.index_filename());
312-
let json = serde_json::to_string_pretty(&index).unwrap();
313-
std::fs::write(&json_path, format!("{json}\n")).unwrap();
314-
println!("{}", json_path.display());
315-
}
316-
}

‎rule-preprocessor/src/main.rs‎

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,18 @@ use semantic::SemanticAnalysis;
2323
use syntactic::SyntacticAnalysis;
2424

2525
fn main() {
26-
let usage = "usage: rule-preprocessor <index-dir> <rules-dir>";
2726
let mut args = std::env::args().skip(1);
28-
let index_dir = std::path::PathBuf::from(args.next().expect(usage));
29-
let rules_dir = std::fs::canonicalize(args.next().expect(usage)).unwrap();
30-
31-
let mut rule_dirs: Vec<_> = std::fs::read_dir(&rules_dir)
32-
.unwrap()
33-
.map(|entry| entry.unwrap().path())
34-
.filter(|dir| dir.join("src.c").exists() || dir.join("src.cpp").exists())
35-
.collect();
36-
rule_dirs.sort();
37-
38-
let rules = rule_dirs
39-
.iter()
40-
.map(|dir| {
41-
let ir = SyntacticAnalysis::run(dir);
42-
assert!(
43-
ir.refcount_ir.is_some() || ir.unsafe_ir.is_some(),
44-
"{}: no tgt_unsafe.rs or tgt_refcount.rs",
45-
dir.display()
46-
);
47-
ir
48-
})
49-
.collect();
50-
51-
ir::write_index(&SemanticAnalysis::run(rules, &index_dir), &index_dir);
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);
35+
SemanticAnalysis::run(
36+
SyntacticAnalysis::run(&std::fs::canonicalize(&in_dir).unwrap()),
37+
&out,
38+
)
39+
.write_ir(&out);
5240
}

‎rule-preprocessor/src/semantic.rs‎

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::path::{Path, PathBuf};
88
pub struct SemanticAnalysis;
99

1010
impl SemanticAnalysis {
11-
pub fn run(ir: Vec<RulesIR>, out_dir: &Path) -> Vec<RulesIR> {
11+
pub fn run(ir: RulesIR, out_dir: &Path) -> RulesIR {
1212
let crate_root = write_crate_root(&ir, out_dir);
1313
let args = build_rustc_args(&crate_root);
1414
let mut resolver = MethodResolver { ir };
@@ -26,22 +26,18 @@ impl SemanticAnalysis {
2626
}
2727
}
2828

29-
fn write_crate_root(rules: &[RulesIR], out_dir: &Path) -> PathBuf {
29+
fn write_crate_root(ir: &RulesIR, out_dir: &Path) -> PathBuf {
3030
let mut buf = String::from("#![allow(warnings)]\n");
31-
for ir in rules {
32-
for (model, _) in ir {
33-
let filename = model.src_filename();
34-
let stem = filename.strip_suffix(".rs").unwrap();
35-
let source = ir.dir.join(filename);
36-
buf.push_str(&format!(
37-
"#[path = r#\"{}\"#]\npub mod {}_{stem};\n",
38-
source.display(),
39-
ir.name()
40-
));
41-
}
31+
for (model, _) in ir {
32+
let filename = model.src_filename();
33+
let stem = filename.strip_suffix(".rs").unwrap();
34+
let source = ir.dir.join(filename);
35+
buf.push_str(&format!(
36+
"#[path = r#\"{}\"#]\npub mod rule_{stem};\n",
37+
source.display()
38+
));
4239
}
4340

44-
std::fs::create_dir_all(out_dir).unwrap();
4541
let path = out_dir.join("crate_root.rs");
4642
std::fs::write(&path, buf).unwrap();
4743
path
@@ -204,13 +200,12 @@ impl<'tcx> FnDecl<'tcx> {
204200
}
205201

206202
struct MethodResolver {
207-
ir: Vec<RulesIR>,
203+
ir: RulesIR,
208204
}
209205

210206
impl MethodResolver {
211207
fn resolve_rule<'tcx>(&mut self, tcx: rustc_middle::ty::TyCtxt<'tcx>, f: &FnDecl<'tcx>) {
212-
let source_file = Path::new(&f.source_file);
213-
let Some(file_ir) = self.ir.iter_mut().find_map(|ir| ir.get_mut(source_file)) else {
208+
let Some(file_ir) = self.ir.get_mut(&f.source_file) else {
214209
return;
215210
};
216211
match file_ir.get_mut(&f.name) {
@@ -224,17 +219,15 @@ impl MethodResolver {
224219
}
225220

226221
fn assert_no_unknowns(&self) {
227-
for ir in &self.ir {
228-
for (model, file_ir) in ir {
229-
for (rule_name, rule) in file_ir {
230-
let RuleIr::Fn(fn_ir) = rule else { continue };
231-
assert!(
232-
!fn_ir.has_unknowns(),
233-
"unresolved access=\"unknown\" in {} ({})",
234-
rule_name,
235-
ir.dir.join(model.src_filename()).display()
236-
);
237-
}
222+
for (model, file_ir) in &self.ir {
223+
for (rule_name, rule) in file_ir {
224+
let RuleIr::Fn(fn_ir) = rule else { continue };
225+
assert!(
226+
!fn_ir.has_unknowns(),
227+
"unresolved access=\"unknown\" in {} ({})",
228+
rule_name,
229+
self.ir.dir.join(model.src_filename()).display()
230+
);
238231
}
239232
}
240233
}

0 commit comments

Comments
 (0)