1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use crate::error::{Location, ParseError};
use crate::testcommand::TestOption;
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::settings::{Configurable, Flags, SetError};
pub enum IsaSpec {
None(Flags),
Some(Vec<Box<dyn TargetIsa>>),
}
impl IsaSpec {
pub fn unique_isa(&self) -> Option<&dyn TargetIsa> {
if let Self::Some(ref isa_vec) = *self {
if isa_vec.len() == 1 {
return Some(&*isa_vec[0]);
}
}
None
}
}
pub enum ParseOptionError {
Generic(ParseError),
UnknownFlag {
loc: Location,
name: String,
},
UnknownValue {
loc: Location,
name: String,
value: String,
},
}
impl From<ParseOptionError> for ParseError {
fn from(err: ParseOptionError) -> Self {
match err {
ParseOptionError::Generic(err) => err,
ParseOptionError::UnknownFlag { loc, name } => Self {
location: loc,
message: format!("unknown setting '{}'", name),
is_warning: false,
},
ParseOptionError::UnknownValue { loc, name, value } => Self {
location: loc,
message: format!("unknown setting '{}={}'", name, value),
is_warning: false,
},
}
}
}
macro_rules! option_err {
( $loc:expr, $fmt:expr, $( $arg:expr ),+ ) => {
Err($crate::ParseOptionError::Generic($crate::ParseError {
location: $loc.clone(),
message: format!( $fmt, $( $arg ),+ ),
is_warning: false,
}))
};
}
pub fn parse_options<'a, I>(
iter: I,
config: &mut dyn Configurable,
loc: Location,
) -> Result<(), ParseOptionError>
where
I: Iterator<Item = &'a str>,
{
for opt in iter.map(TestOption::new) {
match opt {
TestOption::Flag(name) => match config.enable(name) {
Ok(_) => {}
Err(SetError::BadName(name)) => {
return Err(ParseOptionError::UnknownFlag { loc, name })
}
Err(_) => return option_err!(loc, "not a boolean flag: '{}'", opt),
},
TestOption::Value(name, value) => match config.set(name, value) {
Ok(_) => {}
Err(SetError::BadName(name)) => {
return Err(ParseOptionError::UnknownValue {
loc,
name,
value: value.to_string(),
})
}
Err(SetError::BadType) => {
return option_err!(loc, "invalid setting type: '{}'", opt)
}
Err(SetError::BadValue(expected)) => {
return option_err!(
loc,
"invalid setting value for '{}', expected {}",
opt,
expected
);
}
},
}
}
Ok(())
}