Skip to content

Commit bac92b7

Browse files
committed
Use cargo locate-project (rust-lang#904)
* use message-format plain, update error handling
1 parent 4e5c70b commit bac92b7

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

src/cargo-kani/src/args_toml.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,28 @@
44
use anyhow::{bail, Result};
55
use std::collections::BTreeMap;
66
use std::ffi::OsString;
7+
use std::path::PathBuf;
8+
use std::process::Command;
79
use toml::value::Table;
810
use toml::Value;
911

1012
/// Produces a set of arguments to pass to ourself (cargo-kani) from a Cargo.toml project file
1113
pub fn config_toml_to_args() -> Result<Vec<OsString>> {
12-
// TODO: `cargo locate-project` maybe?
13-
let file = std::fs::read_to_string("Cargo.toml");
14-
if let Ok(file) = file {
15-
toml_to_args(&file)
16-
} else {
17-
// Suppress the error if we can't find it, for now.
18-
Ok(vec![])
14+
let file = std::fs::read_to_string(cargo_locate_project()?)?;
15+
toml_to_args(&file)
16+
}
17+
18+
/// `locate-project` produces a response like: `/full/path/to/src/cargo-kani/Cargo.toml`
19+
fn cargo_locate_project() -> Result<PathBuf> {
20+
let cmd =
21+
Command::new("cargo").args(["locate-project", "--message-format", "plain"]).output()?;
22+
if !cmd.status.success() {
23+
let err = std::str::from_utf8(&cmd.stderr)?;
24+
bail!("{}", err);
1925
}
26+
let path = std::str::from_utf8(&cmd.stdout)?;
27+
// A trim is essential: remove the trailing newline
28+
Ok(path.trim().into())
2029
}
2130

2231
/// Parse a config toml string and extract the cargo-kani arguments we should try injecting

src/cargo-kani/src/call_cbmc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ impl KaniSession {
109109
args.push("--nan-check".into());
110110
args.push("--pointer-overflow-check".into());
111111
args.push("--undefined-shift-check".into());
112+
// With PR #647 we use Rust's `-C overflow-checks=on` instead of:
113+
// --unsigned-overflow-check
114+
// --signed-overflow-check
115+
// So these options are deliberately skipped to avoid erroneously re-checking operations.
112116
}
113117
if self.args.checks.unwinding_on() {
114118
args.push("--unwinding-assertions".into());

src/cargo-kani/src/call_goto_cc.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ impl KaniSession {
2525

2626
// Special case hack for handling the "c-ffi" abs-type
2727
if self.args.use_abs && self.args.abs_type == AbstractionType::CFfi {
28-
let mut vec = self.kani_c_stubs.clone();
29-
vec.push("vec");
30-
vec.push("vec.c");
31-
let mut hashset = self.kani_c_stubs.clone();
32-
hashset.push("hashset");
33-
hashset.push("hashset.c");
28+
let vec = self.kani_c_stubs.join("vec/vec.c");
29+
let hashset = self.kani_c_stubs.join("hashset/hashset.c");
3430

3531
args.push(vec.into_os_string());
3632
args.push(hashset.into_os_string());

src/cargo-kani/src/util.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use std::process::Command;
88

99
/// Replace an extension with another one, in a new PathBuf. (See tests for examples)
1010
pub fn alter_extension(path: &Path, ext: &str) -> PathBuf {
11-
let mut result = path.to_owned();
12-
result.set_extension(ext);
13-
result
11+
path.with_extension(ext)
1412
}
1513

1614
/// Add an extension to an existing file path (amazingly Rust doesn't support this well)

0 commit comments

Comments
 (0)