-
Notifications
You must be signed in to change notification settings - Fork 258
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
analyze: add function attrs for testing #942
Changes from all commits
ba566ac
334efc8
6435494
9adc180
557084b
efca777
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,15 +1,17 @@ | ||||||
use crate::labeled_ty::LabeledTy; | ||||||
use crate::trivial::IsTrivial; | ||||||
use rustc_ast::ast::AttrKind; | ||||||
use rustc_const_eval::interpret::Scalar; | ||||||
use rustc_hir::def::DefKind; | ||||||
use rustc_hir::def_id::DefId; | ||||||
use rustc_hir::def_id::{DefId, LocalDefId}; | ||||||
use rustc_middle::mir::{ | ||||||
BasicBlock, BasicBlockData, Constant, Field, Local, Location, Mutability, Operand, Place, | ||||||
PlaceElem, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, | ||||||
}; | ||||||
use rustc_middle::ty::{ | ||||||
self, AdtDef, DefIdTree, EarlyBinder, Subst, SubstsRef, Ty, TyCtxt, TyKind, UintTy, | ||||||
}; | ||||||
use rustc_span::symbol::Symbol; | ||||||
use std::fmt::Debug; | ||||||
|
||||||
#[derive(Debug)] | ||||||
|
@@ -353,3 +355,44 @@ pub fn is_null_const(constant: Constant) -> bool { | |||||
|
||||||
pub trait PhantomLifetime<'a> {} | ||||||
impl<'a, T: ?Sized> PhantomLifetime<'a> for T {} | ||||||
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] | ||||||
pub enum TestAttr { | ||||||
/// `#[c2rust_analyze_test::fixed_signature]`: Mark all pointers in the function signature as | ||||||
/// [`FIXED`](crate::context::FlagSet::FIXED). | ||||||
FixedSignature, | ||||||
/// `#[c2rust_analyze_test::fail_analysis]`: Force an analysis failure for the function. | ||||||
FailAnalysis, | ||||||
/// `#[c2rust_analyze_test::skip_rewrite]`: Skip generating rewrites for the function. | ||||||
SkipRewrite, | ||||||
} | ||||||
|
||||||
impl TestAttr { | ||||||
pub fn name(self) -> &'static str { | ||||||
match self { | ||||||
TestAttr::FixedSignature => "fixed_signature", | ||||||
TestAttr::FailAnalysis => "fail_analysis", | ||||||
TestAttr::SkipRewrite => "skip_rewrite", | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
pub fn has_test_attr(tcx: TyCtxt, ldid: LocalDefId, attr: TestAttr) -> bool { | ||||||
let tool_sym = Symbol::intern("c2rust_analyze_test"); | ||||||
kkysen marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
let name_sym = Symbol::intern(attr.name()); | ||||||
|
||||||
for attr in tcx.get_attrs_unchecked(ldid.to_def_id()) { | ||||||
kkysen marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
let path = match attr.kind { | ||||||
AttrKind::Normal(ref item, _) => &item.path, | ||||||
AttrKind::DocComment(..) => continue, | ||||||
}; | ||||||
let (a, b) = match &path.segments[..] { | ||||||
&[ref a, ref b] => (a, b), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mostly avoid using the "match ergonomics" feature - it makes it less clear whether the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't that clear from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO |
||||||
_ => continue, | ||||||
}; | ||||||
if a.ident.name == tool_sym && b.ident.name == name_sym { | ||||||
return true; | ||||||
} | ||||||
} | ||||||
false | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ define_tests! { | |
offset2, | ||
ptrptr1, | ||
statics, | ||
test_attrs, | ||
trivial, | ||
type_annotation_rewrite, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#![feature(register_tool)] | ||
#![register_tool(c2rust_analyze_test)] | ||
|
||
// CHECK-LABEL: type assignment for "f": | ||
// CHECK: _0 ({{.*}}: *mut i32): *mut i32 | ||
// CHECK: _1 ({{.*}}: x): *mut i32 | ||
#[c2rust_analyze_test::fixed_signature] | ||
fn f(x: *mut i32) -> *mut i32 { | ||
x | ||
} | ||
|
||
// CHECK-LABEL: type assignment for "g": | ||
// CHECK: _0 ({{.*}}: *mut i32): &i32 | ||
// CHECK: _1 ({{.*}}: x): &i32 | ||
#[c2rust_analyze_test::skip_rewrite] | ||
fn g(x: *mut i32) -> *mut i32 { | ||
x | ||
} | ||
|
||
// CHECK: analysis of DefId({{.*}} ~ test_attrs[{{.*}}]::h) failed: [unknown]: explicit fail_analysis for testing | ||
#[c2rust_analyze_test::fail_analysis] | ||
fn h(x: *mut i32) -> *mut i32 { | ||
x | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spernsteiner, I vaguely remember you answering this before, but I can't find the comment. Why do we
continue
here instead of panicking? When would the lookup fail?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure. I tried changing the
continue
to apanic!()
just now and nothing broke in the tests (though only a few of those usefixed_signature
).