Skip to content

Commit 7b18191

Browse files
committed
analyze: refactor has_test_attr and move into util
1 parent 2f00607 commit 7b18191

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

c2rust-analyze/src/main.rs

+4-26
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ use crate::equiv::{GlobalEquivSet, LocalEquivSet};
2424
use crate::labeled_ty::LabeledTyCtxt;
2525
use crate::log::init_logger;
2626
use crate::panic_detail::PanicDetail;
27-
use crate::util::Callee;
27+
use crate::util::{Callee, TestAttr};
2828
use context::AdtMetadataTable;
29-
use rustc_ast::ast::AttrKind;
3029
use rustc_hir::def::DefKind;
3130
use rustc_hir::def_id::{DefId, LocalDefId};
3231
use rustc_index::vec::IndexVec;
@@ -36,7 +35,6 @@ use rustc_middle::mir::{
3635
Rvalue, StatementKind,
3736
};
3837
use rustc_middle::ty::{Ty, TyCtxt, TyKind, WithOptConstParam};
39-
use rustc_span::symbol::Symbol;
4038
use rustc_span::Span;
4139
use std::collections::{HashMap, HashSet};
4240
use std::env;
@@ -500,7 +498,7 @@ fn run(tcx: TyCtxt) {
500498
// For testing, putting #[c2rust_analyze_test::fixed_signature] on a function makes all
501499
// pointers in its signature FIXED.
502500
for &ldid in &all_fn_ldids {
503-
if !has_test_attr(tcx, ldid, "fixed_signature") {
501+
if !util::has_test_attr(tcx, ldid, TestAttr::FixedSignature) {
504502
continue;
505503
}
506504
let lsig = match gacx.fn_sigs.get(&ldid.to_def_id()) {
@@ -512,7 +510,7 @@ fn run(tcx: TyCtxt) {
512510

513511
// For testing, putting #[c2rust_analyze_test::fail_analysis] on a function marks it as failed.
514512
for &ldid in &all_fn_ldids {
515-
if !has_test_attr(tcx, ldid, "fail_analysis") {
513+
if !util::has_test_attr(tcx, ldid, TestAttr::FailAnalysis) {
516514
continue;
517515
}
518516
gacx.mark_fn_failed(
@@ -593,7 +591,7 @@ fn run(tcx: TyCtxt) {
593591
continue;
594592
}
595593

596-
if has_test_attr(tcx, ldid, "skip_rewrite") {
594+
if util::has_test_attr(tcx, ldid, TestAttr::SkipRewrite) {
597595
continue;
598596
}
599597

@@ -967,26 +965,6 @@ fn for_each_callee(tcx: TyCtxt, ldid: LocalDefId, f: impl FnMut(LocalDefId)) {
967965
CalleeVisitor { tcx, mir, f }.visit_body(mir);
968966
}
969967

970-
fn has_test_attr(tcx: TyCtxt, ldid: LocalDefId, name: &str) -> bool {
971-
let tool_sym = Symbol::intern("c2rust_analyze_test");
972-
let name_sym = Symbol::intern(name);
973-
974-
for attr in tcx.get_attrs_unchecked(ldid.to_def_id()) {
975-
let path = match attr.kind {
976-
AttrKind::Normal(ref item, _) => &item.path,
977-
AttrKind::DocComment(..) => continue,
978-
};
979-
let (a, b) = match &path.segments[..] {
980-
&[ref a, ref b] => (a, b),
981-
_ => continue,
982-
};
983-
if a.ident.name == tool_sym && b.ident.name == name_sym {
984-
return true;
985-
}
986-
}
987-
false
988-
}
989-
990968
struct AnalysisCallbacks;
991969

992970
impl rustc_driver::Callbacks for AnalysisCallbacks {

c2rust-analyze/src/util.rs

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use crate::labeled_ty::LabeledTy;
22
use crate::trivial::IsTrivial;
3+
use rustc_ast::ast::AttrKind;
34
use rustc_const_eval::interpret::Scalar;
45
use rustc_hir::def::DefKind;
5-
use rustc_hir::def_id::DefId;
6+
use rustc_hir::def_id::{DefId, LocalDefId};
67
use rustc_middle::mir::{
78
BasicBlock, BasicBlockData, Constant, Field, Local, Location, Mutability, Operand, Place,
89
PlaceElem, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind,
910
};
1011
use rustc_middle::ty::{
1112
self, AdtDef, DefIdTree, EarlyBinder, Subst, SubstsRef, Ty, TyCtxt, TyKind, UintTy,
1213
};
14+
use rustc_span::symbol::Symbol;
1315
use std::fmt::Debug;
1416

1517
#[derive(Debug)]
@@ -353,3 +355,40 @@ pub fn is_null_const(constant: Constant) -> bool {
353355

354356
pub trait PhantomLifetime<'a> {}
355357
impl<'a, T: ?Sized> PhantomLifetime<'a> for T {}
358+
359+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
360+
pub enum TestAttr {
361+
FixedSignature,
362+
FailAnalysis,
363+
SkipRewrite,
364+
}
365+
366+
impl TestAttr {
367+
pub fn name(self) -> &'static str {
368+
match self {
369+
TestAttr::FixedSignature => "fixed_signature",
370+
TestAttr::FailAnalysis => "fail_analysis",
371+
TestAttr::SkipRewrite => "skip_rewrite",
372+
}
373+
}
374+
}
375+
376+
pub fn has_test_attr(tcx: TyCtxt, ldid: LocalDefId, attr: TestAttr) -> bool {
377+
let tool_sym = Symbol::intern("c2rust_analyze_test");
378+
let name_sym = Symbol::intern(attr.name());
379+
380+
for attr in tcx.get_attrs_unchecked(ldid.to_def_id()) {
381+
let path = match attr.kind {
382+
AttrKind::Normal(ref item, _) => &item.path,
383+
AttrKind::DocComment(..) => continue,
384+
};
385+
let (a, b) = match &path.segments[..] {
386+
&[ref a, ref b] => (a, b),
387+
_ => continue,
388+
};
389+
if a.ident.name == tool_sym && b.ident.name == name_sym {
390+
return true;
391+
}
392+
}
393+
false
394+
}

0 commit comments

Comments
 (0)