Skip to content

Commit

Permalink
Add kani::stub attribute (#1814)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbembenek-aws authored Oct 31, 2022
1 parent 3396936 commit 33e226b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ impl<'tcx> GotocCtx<'tcx> {
let mut harness = self.default_kanitool_proof();
for attr in other_attributes.iter() {
match attr.0.as_str() {
"stub" => self
.tcx
.sess
.span_warn(attr.1.span, "Attribute `kani::stub` is currently ignored by Kani"),
"unwind" => self.handle_kanitool_unwind(attr.1, &mut harness),
_ => {
self.tcx.sess.span_err(
Expand Down
27 changes: 27 additions & 0 deletions library/kani_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,30 @@ pub fn unwind(attr: TokenStream, item: TokenStream) -> TokenStream {
result.extend(item);
result
}

#[cfg(not(kani))]
#[proc_macro_attribute]
pub fn stub(_attr: TokenStream, item: TokenStream) -> TokenStream {
// When the config is not kani, we should leave the function alone
item
}

/// Specify a function/method stub pair to use for proof harness
///
/// The attribute `#[kani::stub(original, replacement)]` can only be used alongside `#[kani::proof]`.
///
/// # Arguments
/// * `original` - The function or method to replace, specified as a path.
/// * `replacement` - The function or method to use as a replacement, specified as a path.
#[cfg(kani)]
#[proc_macro_attribute]
pub fn stub(attr: TokenStream, item: TokenStream) -> TokenStream {
let mut result = TokenStream::new();

// Translate #[kani::stub(original, replacement)] to #[kanitool::stub(original, replacement)]
let insert_string = "#[kanitool::stub(".to_owned() + &attr.to_string() + ")]";
result.extend(insert_string.parse::<TokenStream>().unwrap());

result.extend(item);
result
}
12 changes: 12 additions & 0 deletions tests/ui/stub-attribute/attribute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT
//
//! Checks that the `kani::stub` attribute is accepted
fn foo() {}

fn bar() {}

#[kani::proof]
#[kani::stub(foo, bar)]
fn main() {}
1 change: 1 addition & 0 deletions tests/ui/stub-attribute/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
warning: Attribute `kani::stub` is currently ignored by Kani

0 comments on commit 33e226b

Please sign in to comment.