Skip to content

Revise generate function to return Result #4424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
126f2c6
Update pidfd constants and types (Linux 6.9-6.15)
rusty-snake Apr 12, 2025
fdf9b3e
Revise generate function to return Result. Add unit test in ctest-tes…
Owen-CH-Leung Apr 19, 2025
dcb8904
Remove use of backtrace. Fix clippy CI
Owen-CH-Leung Apr 19, 2025
1b24825
Refactor generate_file_impl
Owen-CH-Leung Apr 19, 2025
f92ed6f
netbsd move from e! marcro to c_enum!
devnexen Apr 19, 2025
673af93
Fix an `unnecessary_transmutes` from a recent nightly
tgross35 Apr 25, 2025
d00adf4
Merge pull request #4429 from tgross35/lint-fixes
tgross35 Apr 25, 2025
00e6d6a
Merge pull request #4425 from devnexen/netbsd_e_to_c_enum
tgross35 Apr 25, 2025
8b15e27
chore: lint `libc-test/build.rs`
nyurik Apr 15, 2025
29ab31e
linux: add constant PACKET_IGNORE_OUTGOING
ryancdotorg Mar 11, 2025
4a88460
chore: lint `ctest-test` crate
nyurik Apr 15, 2025
59a0f83
Merge pull request #4319 from ryancdotorg/main
tgross35 Apr 25, 2025
ec7da63
Merge pull request #4412 from nyurik/libc-test-lints
tgross35 Apr 25, 2025
1606561
Fix querying emcc on windows (use emcc.bat)
guusw Jan 23, 2025
8c10293
Merge pull request #4413 from nyurik/ctest-test-lints
tgross35 Apr 25, 2025
a283b9e
chore: apply some clippy lints
nyurik Apr 15, 2025
393e909
Merge pull request #4248 from shards-lang/fix-emcc-main
tgross35 Apr 25, 2025
1b5abd7
Merge pull request #4402 from rusty-snake/pidfd-threaded
tgross35 Apr 25, 2025
172b20a
Merge pull request #4415 from nyurik/lints
tgross35 Apr 25, 2025
74c3cc0
Update Redox SA_ constants.
4lDO2 Apr 21, 2025
79a9c77
Merge pull request #4426 from 4lDO2/redox-signal-defs
tgross35 Apr 25, 2025
0a9e350
Merge branch 'revise_generate_signature' of https://github.com/Owen-C…
Owen-CH-Leung Apr 26, 2025
cfe1679
Move test on TestGenerator to all.rs. Move generate to try_generate. …
Owen-CH-Leung Apr 26, 2025
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
28 changes: 17 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,21 @@ members = [
unused_qualifications = "allow"

[lints.clippy]
missing_safety_doc = "allow"
# Enable pedantic lints - use this manually once in a while, but don't enable by default
# pedantic = { level = "warn", priority = -1 }

# FIXME(clippy): all these are default lints and should probably be fixed
identity_op = "allow"
if_same_then_else = "allow"
non_minimal_cfg = "allow"
precedence = "allow"
redundant_field_names = "allow"
redundant_static_lifetimes = "allow"
unnecessary_cast = "allow"
unused_unit = "allow"
zero_ptr = "allow"
# We are okay with the current state of these lints
explicit_iter_loop = "warn"
identity_op = "allow" # some expressions like `0 | x` are clearer for bit ops
manual_assert = "warn"
map_unwrap_or = "warn"
missing_safety_doc = "allow" # safety? in libc? seriously?
non_minimal_cfg = "allow" # for some reason cfg_if! sometimes trigger this
ptr_as_ptr = "warn"
unnecessary_semicolon = "warn"

# FIXME(clippy): these should be fixed if possible
expl_impl_clone_on_copy = "allow"
uninlined_format_args = "allow"
unnecessary_cast = "allow" # some casts like `as usize` are only needed for some targets
used_underscore_binding = "allow"
49 changes: 27 additions & 22 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{env, str};
// List of cfgs this build script is allowed to set. The list is needed to support check-cfg, as we
// need to know all the possible cfgs that this script will set. If you need to set another cfg
// make sure to add it to this list as well.
const ALLOWED_CFGS: &'static [&'static str] = &[
const ALLOWED_CFGS: &[&str] = &[
"emscripten_old_stat_abi",
"espidf_time32",
"freebsd10",
Expand All @@ -24,7 +24,7 @@ const ALLOWED_CFGS: &'static [&'static str] = &[
];

// Extra values to allow for check-cfg.
const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[
const CHECK_CFG_EXTRA: &[(&str, &[&str])] = &[
(
"target_os",
&[
Expand All @@ -46,7 +46,6 @@ fn main() {
println!("cargo:rerun-if-changed=build.rs");

let (rustc_minor_ver, _is_nightly) = rustc_minor_nightly();
let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
let libc_ci = env::var("LIBC_CI").is_ok();
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
Expand All @@ -66,10 +65,8 @@ fn main() {
vers
} else if libc_ci {
which_freebsd().unwrap_or(12)
} else if rustc_dep_of_std {
12
} else {
12
12 // regardless of CARGO_FEATURE_RUSTC_DEP_OF_STD env var
};

match which_freebsd {
Expand Down Expand Up @@ -163,12 +160,11 @@ fn rustc_version_cmd(is_clippy_driver: bool) -> Output {

let output = cmd.output().expect("Failed to get rustc version");

if !output.status.success() {
panic!(
"failed to run rustc: {}",
String::from_utf8_lossy(output.stderr.as_slice())
);
}
assert!(
output.status.success(),
"failed to run rustc: {}",
String::from_utf8_lossy(output.stderr.as_slice())
);

output
}
Expand All @@ -195,9 +191,11 @@ fn rustc_minor_nightly() -> (u32, bool) {

let mut pieces = version.split('.');

if pieces.next() != Some("rustc 1") {
panic!("Failed to get rustc version");
}
assert_eq!(
pieces.next(),
Some("rustc 1"),
"Failed to get rustc version"
);

let minor = pieces.next();

Expand All @@ -207,9 +205,9 @@ fn rustc_minor_nightly() -> (u32, bool) {
// since a nightly build should either come from CI
// or a git checkout
let nightly_raw = otry!(pieces.next()).split('-').nth(1);
let nightly = nightly_raw
.map(|raw| raw.starts_with("dev") || raw.starts_with("nightly"))
.unwrap_or(false);
let nightly = nightly_raw.map_or(false, |raw| {
raw.starts_with("dev") || raw.starts_with("nightly")
});
let minor = otry!(otry!(minor).parse().ok());

(minor, nightly)
Expand All @@ -235,7 +233,13 @@ fn which_freebsd() -> Option<i32> {
}

fn emcc_version_code() -> Option<u64> {
let output = Command::new("emcc").arg("-dumpversion").output().ok()?;
let emcc = if cfg!(target_os = "windows") {
"emcc.bat"
} else {
"emcc"
};

let output = Command::new(emcc).arg("-dumpversion").output().ok()?;
if !output.status.success() {
return None;
}
Expand All @@ -254,8 +258,9 @@ fn emcc_version_code() -> Option<u64> {
}

fn set_cfg(cfg: &str) {
if !ALLOWED_CFGS.contains(&cfg) {
panic!("trying to set cfg {cfg}, but it is not in ALLOWED_CFGS");
}
assert!(
ALLOWED_CFGS.contains(&cfg),
"trying to set cfg {cfg}, but it is not in ALLOWED_CFGS",
);
println!("cargo:rustc-cfg={cfg}");
}
6 changes: 3 additions & 3 deletions ctest-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ edition = "2021"
ctest = { path = "../ctest" }
cc = "1.0"

[dev-dependencies]
ctest = { path = "../ctest" }

[dependencies]
cfg-if = "1.0.0"
libc = { path = ".." }
Expand Down Expand Up @@ -37,6 +40,3 @@ test = false
unused_qualifications = "allow"

[lints.clippy]
# FIXME(clippy): fix these
match_like_matches_macro = "allow"
eq_op = "allow"
7 changes: 3 additions & 4 deletions ctest-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn main() {
.array_arg(t1_arrays)
.skip_roundtrip(|n| n == "Arr")
.generate("src/t1.rs", "t1gen.rs");

ctest::TestGenerator::new()
.header("t2.h")
.include("src")
Expand Down Expand Up @@ -72,6 +73,7 @@ fn main() {
.array_arg(t1_arrays)
.skip_roundtrip(|n| n == "Arr")
.generate("src/t1.rs", "t1gen_cxx.rs");

ctest::TestGenerator::new()
.header("t2.h")
.language(ctest::Lang::CXX)
Expand Down Expand Up @@ -103,8 +105,5 @@ fn t1_volatile(i: ctest::VolatileItemKind) -> bool {
}

fn t1_arrays(n: &str, i: usize) -> bool {
match n {
"T1r" | "T1s" | "T1t" | "T1v" if i == 0 => true,
_ => false,
}
i == 0 && matches!(n, "T1r" | "T1s" | "T1t" | "T1v")
}
3 changes: 3 additions & 0 deletions ctest-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// src/** is mostly dummy files
#![allow(clippy::style, clippy::correctness)]

pub mod t1;
pub mod t2;
70 changes: 70 additions & 0 deletions ctest-test/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,73 @@ fn t2_cxx() {
panic!();
}
}

#[test]
fn test_missing_out_dir() {
// Save original OUT_DIR
let orig_out_dir = env::var_os("OUT_DIR");
env::remove_var("OUT_DIR");

// Test error handling for OUT_DIR missing
let result = ctest::TestGenerator::new()
.header("t1.h")
.try_generate("src/t1.rs", "out_dir_gen.rs");

// Restore OUT_DIR
if let Some(dir) = orig_out_dir {
env::set_var("OUT_DIR", dir);
}

assert!(result.is_err(), "Expected error when OUT_DIR is missing");
}

#[test]
fn test_invalid_output_path() {
// Test error handling for invalid output path
let err = ctest::TestGenerator::new()
.header("t1.h")
.include("src")
.out_dir("/nonexistent_dir") // Should fail with permission error
.try_generate("src/t1.rs", "out_path_gen.rs");

assert!(err.is_err(), "Expected error with invalid output path");
}

#[test]
fn test_parsing_error() {
// Test parsing error
// Create a temporary file with invalid Rust syntax
let temp_dir = env::temp_dir();
let invalid_file = temp_dir.join("invalid.rs");
std::fs::write(&invalid_file, "fn invalid_syntax {").unwrap();

let err = ctest::TestGenerator::new()
.header("t1.h")
.include("src")
.try_generate(&invalid_file, "parse_gen.rs");

assert!(err.is_err(), "Expected error when parsing invalid syntax");
let _ = std::fs::remove_file(invalid_file);
}

#[test]
fn test_non_existent_header() {
// Test non-existent header
let err = ctest::TestGenerator::new()
.header("nonexistent_header.h")
.include("src")
.try_generate("src/t1.rs", "missing_header_gen.rs");

assert!(err.is_err(), "Expected error with non-existent header");
}

#[test]
fn test_invalid_include_path() {
// Test invalid include path
let err = ctest::TestGenerator::new()
.header("t1.h")
.include("nonexistent_directory")
.try_generate("src/t1.rs", "invalid_include_gen.rs");

assert!(err.is_err(), "Expected error with invalid include path");
}
1 change: 1 addition & 0 deletions ctest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/rust-lang/libc"
rust-version = "1.63.0"

[dependencies]
anyhow = "1.0"
garando_syntax = "0.1"
cc = "1.0.1"
rustc_version = "0.4"
Expand Down
Loading
Loading