From 841350f469e4f4970ecc38d4c32a7412d1781f26 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Apr 2026 21:04:39 +0100 Subject: [PATCH 1/3] Speed-up unit tests --- tests/lit/lit/formats/Cpp2RustTest.py | 35 +++++++++++++++++++-------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/tests/lit/lit/formats/Cpp2RustTest.py b/tests/lit/lit/formats/Cpp2RustTest.py index 292eedcb..e309556d 100644 --- a/tests/lit/lit/formats/Cpp2RustTest.py +++ b/tests/lit/lit/formats/Cpp2RustTest.py @@ -7,21 +7,29 @@ import difflib, os, re, shutil, random import tomli -def read_rust_version(): - toolchain_path = os.path.join(os.path.dirname(__file__), - '../../../../libcc2rs/rust-toolchain.toml') - with open(toolchain_path, 'rb') as f: - return tomli.load(f)['toolchain']['channel'] - class Cpp2RustTest(TestFormat): def __init__(self): self.regex_xfail = re.compile(r"//\s*XFAIL:\s*(.*)") self.regex_panic = re.compile(r"//\s*panic\s*(?::\s*(.*))?$", re.MULTILINE) self.regex_nocompile = re.compile(r"//\s*no-compile\s*(?::\s*(.*))?$", re.MULTILINE) self.regex_nondet_result = re.compile(r"//\s*nondet-result\s*(?::\s*(.*))?$", re.MULTILINE) - self.rust_version = read_rust_version() + self.rust_version = self.readRustVersion() os.environ['RUSTFLAGS'] = '-Awarnings -A dangerous-implicit-autorefs' + def readRustVersion(self): + toolchain_path = os.path.join(os.path.dirname(__file__), + '../../../../libcc2rs/rust-toolchain.toml') + with open(toolchain_path, 'rb') as f: + return tomli.load(f)['toolchain']['channel'] + + def sharedTargetDir(self): + return os.path.abspath(os.path.join( + os.path.dirname(__file__), + '../../../../build/tmp/cargo-target')) + + def cargoEnv(self): + return dict(os.environ, CARGO_TARGET_DIR=os.path.abspath(self.sharedTargetDir())) + def updateExpected(self, generated, expected_path): os.makedirs(os.path.dirname(expected_path), exist_ok=True) with open(expected_path, 'w') as f: @@ -120,24 +128,31 @@ def fail(str, code = fail_code): fromfile='expected', tofile='generated')) return fail('different output\n' + diff) + pkg_name = "test_" + re.sub(r'[^a-zA-Z0-9_]', '_', os.path.basename(tmp_dir)) + # Check if we can compile the rust file with open(tmp_dir + "/rust-toolchain.toml", 'w') as f: f.write(f'[toolchain]\nchannel = "{self.rust_version}"\n') with open(tmp_dir + "/Cargo.toml", 'w') as f: f.write(f""" [package] -name = "test" +name = "{pkg_name}" version = "0.1.0" edition = "2021" rust-version = "{self.rust_version}" +[[bin]] +name = "{pkg_name}" +path = "src/main.rs" + [dependencies] libc = "0.2.169" libcc2rs = {{ path = "../../../libcc2rs" }} +rules = {{ path = "../../../rules" }} """) cmd = ['cargo', 'build', '--release', '--quiet'] - _, err, returncode = lit.util.executeCommand(cmd, tmp_dir) + _, err, returncode = lit.util.executeCommand(cmd, tmp_dir, env=self.cargoEnv()) if should_not_compile: if returncode != 0: shutil.rmtree(tmp_dir, True) @@ -146,7 +161,7 @@ def fail(str, code = fail_code): if returncode != 0: return fail('cargo failed\n' + err) - rust_bin = tmp_dir + "/target/release/test" + rust_bin = os.path.join(self.sharedTargetDir(), "release", pkg_name) if not skip_run: if should_panic: From 98b057ab408e6d64ee0e41481bb6e0358216b08a Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Apr 2026 22:20:35 +0100 Subject: [PATCH 2/3] Remove unused Cargo.toml --- tests/unit/Cargo.toml | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 tests/unit/Cargo.toml diff --git a/tests/unit/Cargo.toml b/tests/unit/Cargo.toml deleted file mode 100644 index a8618fe4..00000000 --- a/tests/unit/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "rs" -version = "0.1.0" -edition = "2021" - -[dependencies] -libc = "0.2.153" -libcc2rs = { path = "../../../libcc2rs" } - -# Any project that uses rust-analyzer with the rustc_private -# crates must set [package.metadata.rust-analyzer] rustc_private=true to use it. - -[package.metadata.rust-analyzer] -rustc_private=true From b4552a25344f723f1e3910caee1abc11af13c774 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 17 Apr 2026 09:37:25 +0100 Subject: [PATCH 3/3] Switch to free functions --- tests/lit/lit/formats/Cpp2RustTest.py | 34 +++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/lit/lit/formats/Cpp2RustTest.py b/tests/lit/lit/formats/Cpp2RustTest.py index e309556d..1f0d54da 100644 --- a/tests/lit/lit/formats/Cpp2RustTest.py +++ b/tests/lit/lit/formats/Cpp2RustTest.py @@ -7,29 +7,29 @@ import difflib, os, re, shutil, random import tomli +def read_rust_version(): + toolchain_path = os.path.join(os.path.dirname(__file__), + '../../../../libcc2rs/rust-toolchain.toml') + with open(toolchain_path, 'rb') as f: + return tomli.load(f)['toolchain']['channel'] + +def shared_target_dir(): + return os.path.abspath(os.path.join( + os.path.dirname(__file__), + '../../../../build/tmp/cargo-target')) + +def cargo_env(): + return dict(os.environ, CARGO_TARGET_DIR=os.path.abspath(shared_target_dir())) + class Cpp2RustTest(TestFormat): def __init__(self): self.regex_xfail = re.compile(r"//\s*XFAIL:\s*(.*)") self.regex_panic = re.compile(r"//\s*panic\s*(?::\s*(.*))?$", re.MULTILINE) self.regex_nocompile = re.compile(r"//\s*no-compile\s*(?::\s*(.*))?$", re.MULTILINE) self.regex_nondet_result = re.compile(r"//\s*nondet-result\s*(?::\s*(.*))?$", re.MULTILINE) - self.rust_version = self.readRustVersion() + self.rust_version = read_rust_version() os.environ['RUSTFLAGS'] = '-Awarnings -A dangerous-implicit-autorefs' - def readRustVersion(self): - toolchain_path = os.path.join(os.path.dirname(__file__), - '../../../../libcc2rs/rust-toolchain.toml') - with open(toolchain_path, 'rb') as f: - return tomli.load(f)['toolchain']['channel'] - - def sharedTargetDir(self): - return os.path.abspath(os.path.join( - os.path.dirname(__file__), - '../../../../build/tmp/cargo-target')) - - def cargoEnv(self): - return dict(os.environ, CARGO_TARGET_DIR=os.path.abspath(self.sharedTargetDir())) - def updateExpected(self, generated, expected_path): os.makedirs(os.path.dirname(expected_path), exist_ok=True) with open(expected_path, 'w') as f: @@ -152,7 +152,7 @@ def fail(str, code = fail_code): """) cmd = ['cargo', 'build', '--release', '--quiet'] - _, err, returncode = lit.util.executeCommand(cmd, tmp_dir, env=self.cargoEnv()) + _, err, returncode = lit.util.executeCommand(cmd, tmp_dir, env=cargo_env()) if should_not_compile: if returncode != 0: shutil.rmtree(tmp_dir, True) @@ -161,7 +161,7 @@ def fail(str, code = fail_code): if returncode != 0: return fail('cargo failed\n' + err) - rust_bin = os.path.join(self.sharedTargetDir(), "release", pkg_name) + rust_bin = os.path.join(shared_target_dir(), "release", pkg_name) if not skip_run: if should_panic: