Skip to content

Commit 8e4ff5d

Browse files
randomPoisonthedataking
authored andcommitted
Test for colliding module names
1 parent 7047434 commit 8e4ff5d

5 files changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use std::fs;
2+
use std::path::PathBuf;
3+
use std::process::Command;
4+
5+
use c2rust_rust_tools::RustEdition;
6+
use c2rust_transpile::{ReplaceMode, TranspilerConfig};
7+
use tempfile::TempDir;
8+
9+
fn config(output_dir: PathBuf) -> TranspilerConfig {
10+
TranspilerConfig {
11+
dump_untyped_context: false,
12+
dump_typed_context: false,
13+
pretty_typed_context: false,
14+
dump_function_cfgs: false,
15+
json_function_cfgs: false,
16+
dump_cfg_liveness: false,
17+
dump_structures: false,
18+
verbose: false,
19+
debug_ast_exporter: false,
20+
emit_c_decl_map: false,
21+
incremental_relooper: true,
22+
fail_on_multiple: false,
23+
filter: None,
24+
debug_relooper_labels: false,
25+
cross_checks: false,
26+
cross_check_backend: Default::default(),
27+
cross_check_configs: Default::default(),
28+
prefix_function_names: None,
29+
translate_asm: true,
30+
use_c_loop_info: true,
31+
use_c_multiple_info: true,
32+
simplify_structures: true,
33+
panic_on_translator_failure: false,
34+
emit_modules: true,
35+
fail_on_error: true,
36+
replace_unsupported_decls: ReplaceMode::Extern,
37+
translate_valist: true,
38+
overwrite_existing: true,
39+
reduce_type_annotations: false,
40+
reorganize_definitions: false,
41+
enabled_warnings: Default::default(),
42+
emit_no_std: false,
43+
output_dir: Some(output_dir),
44+
translate_const_macros: Default::default(),
45+
translate_fn_macros: Default::default(),
46+
disable_rustfmt: false,
47+
disable_refactoring: false,
48+
preserve_unused_functions: false,
49+
log_level: log::LevelFilter::Warn,
50+
edition: RustEdition::Edition2021,
51+
deny_unsafe_op_in_unsafe_fn: false,
52+
postprocess: false,
53+
emit_build_files: true,
54+
binaries: vec!["main".to_owned()],
55+
thin_binaries: true,
56+
c2rust_dir: Some(
57+
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
58+
.parent()
59+
.unwrap()
60+
.to_path_buf(),
61+
),
62+
}
63+
}
64+
65+
#[test]
66+
fn sibling_file_and_directory_modules_do_not_collide() {
67+
let td = TempDir::new().unwrap();
68+
let src = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
69+
.join("tests/module_paths/sibling_file_and_directory");
70+
let output_dir = td.path().join("translated");
71+
72+
let (_compile_commands_dir, compile_commands_path) =
73+
c2rust_transpile::create_temp_compile_commands(&[
74+
src.join("main.c"),
75+
src.join("hash.c"),
76+
src.join("hash/sha1.c"),
77+
]);
78+
79+
c2rust_transpile::transpile(config(output_dir.clone()), &compile_commands_path, &["-w"]);
80+
81+
let lib_rs = fs::read_to_string(output_dir.join("lib.rs")).unwrap();
82+
assert!(
83+
lib_rs.contains("#[path = \"src/hash.rs\"]\npub mod c2rust_src_hash;"),
84+
"generated module tree should include the sibling hash.rs module via an \
85+
explicit path module instead of shadowing it with the hash/ directory:\n{lib_rs}"
86+
);
87+
assert!(
88+
lib_rs.contains("pub mod sha1;"),
89+
"generated lib.rs should include the nested hash/sha1.rs module:\n{lib_rs}"
90+
);
91+
92+
let status = Command::new("cargo")
93+
.arg("build")
94+
.env("CARGO_TARGET_DIR", td.path().join("target"))
95+
.current_dir(&output_dir)
96+
.status()
97+
.unwrap();
98+
assert!(status.success(), "generated crate should build");
99+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "hash/sha1.h"
2+
3+
int hash_value(int x) {
4+
return sha1_mix(x) + 1;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int sha1_mix(int x) {
2+
return x * 3;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int sha1_mix(int x);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int hash_value(int);
2+
3+
int main(void) {
4+
return hash_value(13) == 40 ? 0 : 1;
5+
}

0 commit comments

Comments
 (0)