@@ -4,7 +4,7 @@ use regex::Regex;
4
4
5
5
use crate :: rustc_stderr:: Level ;
6
6
7
- use color_eyre:: eyre:: Result ;
7
+ use color_eyre:: eyre:: { bail , ensure , eyre , Result } ;
8
8
9
9
#[ cfg( test) ]
10
10
mod tests;
@@ -66,43 +66,6 @@ impl Condition {
66
66
}
67
67
}
68
68
69
- macro_rules! checked {
70
- ( $path: expr, $l: expr) => {
71
- let path = $path;
72
- let l = $l;
73
- #[ allow( unused_macros) ]
74
- macro_rules! exit {
75
- ( $fmt: expr $$( , $args: expr) * ) => { {
76
- eprint!( "{}:{l}: " , path. display( ) ) ;
77
- eprintln!( $fmt, $$( $args, ) * ) ;
78
- #[ cfg( not( test) ) ]
79
- std:: process:: exit( 1 ) ;
80
- #[ cfg( test) ]
81
- panic!( ) ;
82
- } } ;
83
- }
84
- #[ allow( unused_macros) ]
85
- macro_rules! check {
86
- ( $cond: expr, $fmt: expr $$( , $args: expr) * ) => { {
87
- if !$cond {
88
- exit!( $fmt $$( , $args) * ) ;
89
- }
90
- } } ;
91
- }
92
- #[ allow( unused_macros) ]
93
- macro_rules! unwrap {
94
- ( $cond: expr, $fmt: expr $$( , $args: expr) * ) => { {
95
- match $cond {
96
- Some ( val) => val,
97
- None => {
98
- exit!( $fmt $$( , $args) * ) ;
99
- }
100
- }
101
- } } ;
102
- }
103
- } ;
104
- }
105
-
106
69
impl Comments {
107
70
pub ( crate ) fn parse_file ( path : & Path ) -> Result < Self > {
108
71
let content = std:: fs:: read_to_string ( path) ?;
@@ -121,24 +84,36 @@ impl Comments {
121
84
let mut fallthrough_to = None ;
122
85
for ( l, line) in content. lines ( ) . enumerate ( ) {
123
86
let l = l + 1 ; // enumerate starts at 0, but line numbers start at 1
124
- if let Some ( ( _, command) ) = line. split_once ( "//@" ) {
125
- let command = command. trim ( ) ;
126
- if let Some ( ( command, args) ) = command. split_once ( ':' ) {
127
- this. parse_command_with_args ( command, args, path, l) ;
128
- } else if let Some ( ( command, _comments) ) = command. split_once ( ' ' ) {
129
- this. parse_command ( command, path, l)
130
- } else {
131
- this. parse_command ( command, path, l)
132
- }
133
- } else if let Some ( ( _, pattern) ) = line. split_once ( "//~" ) {
134
- this. parse_pattern ( pattern, & mut fallthrough_to, path, l)
135
- } else if let Some ( ( _, pattern) ) = line. split_once ( "//[" ) {
136
- this. parse_revisioned_pattern ( pattern, & mut fallthrough_to, path, l)
87
+ this. parse_checked_line ( l, & mut fallthrough_to, line) . map_err ( |err| {
88
+ err. wrap_err ( format ! ( "{}:{l}: failed to parse annotation" , path. display( ) ) )
89
+ } ) ?;
90
+ }
91
+ Ok ( this)
92
+ }
93
+
94
+ fn parse_checked_line (
95
+ & mut self ,
96
+ l : usize ,
97
+ fallthrough_to : & mut Option < usize > ,
98
+ line : & str ,
99
+ ) -> Result < ( ) > {
100
+ if let Some ( ( _, command) ) = line. split_once ( "//@" ) {
101
+ let command = command. trim ( ) ;
102
+ if let Some ( ( command, args) ) = command. split_once ( ':' ) {
103
+ self . parse_command_with_args ( command, args, l)
104
+ } else if let Some ( ( command, _comments) ) = command. split_once ( ' ' ) {
105
+ self . parse_command ( command)
137
106
} else {
138
- fallthrough_to = None ;
107
+ self . parse_command ( command )
139
108
}
109
+ } else if let Some ( ( _, pattern) ) = line. split_once ( "//~" ) {
110
+ self . parse_pattern ( pattern, fallthrough_to, l)
111
+ } else if let Some ( ( _, pattern) ) = line. split_once ( "//[" ) {
112
+ self . parse_revisioned_pattern ( pattern, fallthrough_to, l)
113
+ } else {
114
+ * fallthrough_to = None ;
115
+ Ok ( ( ) )
140
116
}
141
- Ok ( this)
142
117
}
143
118
144
119
/// Parse comments in `content`.
@@ -214,91 +189,86 @@ impl Comments {
214
189
Ok ( this)
215
190
}
216
191
217
- fn parse_command_with_args ( & mut self , command : & str , args : & str , path : & Path , l : usize ) {
218
- checked ! ( path, l) ;
192
+ fn parse_command_with_args ( & mut self , command : & str , args : & str , l : usize ) -> Result < ( ) > {
219
193
match command {
220
194
"revisions" => {
221
- check ! ( self . revisions. is_none( ) , "cannot specifiy revisions twice" ) ;
195
+ ensure ! ( self . revisions. is_none( ) , "cannot specifiy revisions twice" ) ;
222
196
self . revisions = Some ( args. split_whitespace ( ) . map ( |s| s. to_string ( ) ) . collect ( ) ) ;
223
197
}
224
198
"compile-flags" => {
225
199
self . compile_flags . extend ( args. split_whitespace ( ) . map ( |s| s. to_string ( ) ) ) ;
226
200
}
227
201
"rustc-env" =>
228
202
for env in args. split_whitespace ( ) {
229
- let ( k, v) = unwrap ! (
230
- env. split_once( '=' ) ,
231
- "environment variables must be key/value pairs separated by a `=`"
232
- ) ;
203
+ let ( k, v) = env. split_once ( '=' ) . ok_or_else ( || {
204
+ eyre ! ( "environment variables must be key/value pairs separated by a `=`" )
205
+ } ) ?;
233
206
self . env_vars . push ( ( k. to_string ( ) , v. to_string ( ) ) ) ;
234
207
} ,
235
208
"normalize-stderr-test" => {
236
- let ( from, to) =
237
- unwrap ! ( args. split_once( "->" ) , "normalize-stderr-test needs a `->`" ) ;
209
+ let ( from, to) = args
210
+ . split_once ( "->" )
211
+ . ok_or_else ( || eyre ! ( "normalize-stderr-test needs a `->`" ) ) ?;
238
212
let from = from. trim ( ) . trim_matches ( '"' ) ;
239
213
let to = to. trim ( ) . trim_matches ( '"' ) ;
240
- let from = unwrap ! ( Regex :: new( from) . ok( ) , "invalid regex" ) ;
214
+ let from = Regex :: new ( from) . ok ( ) . ok_or_else ( || eyre ! ( "invalid regex" ) ) ? ;
241
215
self . normalize_stderr . push ( ( from, to. to_string ( ) ) ) ;
242
216
}
243
217
"error-pattern" => {
244
- check ! (
218
+ ensure ! (
245
219
self . error_pattern. is_none( ) ,
246
220
"cannot specifiy error_pattern twice, previous: {:?}" ,
247
221
self . error_pattern
248
222
) ;
249
223
self . error_pattern = Some ( ( args. trim ( ) . to_string ( ) , l) ) ;
250
224
}
251
225
// Maybe the user just left a comment explaining a command without arguments
252
- _ => self . parse_command ( command, path , l ) ,
226
+ _ => self . parse_command ( command) ? ,
253
227
}
228
+ Ok ( ( ) )
254
229
}
255
230
256
- fn parse_command ( & mut self , command : & str , path : & Path , l : usize ) {
257
- checked ! ( path, l) ;
258
-
231
+ fn parse_command ( & mut self , command : & str ) -> Result < ( ) > {
259
232
if let Some ( s) = command. strip_prefix ( "ignore-" ) {
260
233
self . ignore . push ( Condition :: parse ( s) ) ;
261
- return ;
234
+ return Ok ( ( ) ) ;
262
235
}
263
236
264
237
if let Some ( s) = command. strip_prefix ( "only-" ) {
265
238
self . only . push ( Condition :: parse ( s) ) ;
266
- return ;
239
+ return Ok ( ( ) ) ;
267
240
}
268
241
269
242
if command. starts_with ( "stderr-per-bitwidth" ) {
270
- check ! ( !self . stderr_per_bitwidth, "cannot specifiy stderr-per-bitwidth twice" ) ;
243
+ ensure ! ( !self . stderr_per_bitwidth, "cannot specifiy stderr-per-bitwidth twice" ) ;
271
244
self . stderr_per_bitwidth = true ;
272
- return ;
245
+ return Ok ( ( ) ) ;
273
246
}
274
247
275
- exit ! ( "unknown command {command}" ) ;
248
+ bail ! ( "unknown command {command}" ) ;
276
249
}
277
250
278
251
fn parse_pattern (
279
252
& mut self ,
280
253
pattern : & str ,
281
254
fallthrough_to : & mut Option < usize > ,
282
- path : & Path ,
283
255
l : usize ,
284
- ) {
285
- self . parse_pattern_inner ( pattern, fallthrough_to, None , path , l)
256
+ ) -> Result < ( ) > {
257
+ self . parse_pattern_inner ( pattern, fallthrough_to, None , l)
286
258
}
287
259
288
260
fn parse_revisioned_pattern (
289
261
& mut self ,
290
262
pattern : & str ,
291
263
fallthrough_to : & mut Option < usize > ,
292
- path : & Path ,
293
264
l : usize ,
294
- ) {
295
- checked ! ( path, l) ;
265
+ ) -> Result < ( ) > {
296
266
let ( revision, pattern) =
297
- unwrap ! ( pattern. split_once( ']' ) , "`//[` without corresponding `]`" ) ;
267
+ pattern. split_once ( ']' ) . ok_or_else ( || eyre ! ( "`//[` without corresponding `]`" ) ) ? ;
298
268
if let Some ( pattern) = pattern. strip_prefix ( '~' ) {
299
- self . parse_pattern_inner ( pattern, fallthrough_to, Some ( revision. to_owned ( ) ) , path , l)
269
+ self . parse_pattern_inner ( pattern, fallthrough_to, Some ( revision. to_owned ( ) ) , l)
300
270
} else {
301
- exit ! ( "revisioned pattern must have `~` following the `]`" ) ;
271
+ bail ! ( "revisioned pattern must have `~` following the `]`" ) ;
302
272
}
303
273
}
304
274
@@ -308,21 +278,25 @@ impl Comments {
308
278
pattern : & str ,
309
279
fallthrough_to : & mut Option < usize > ,
310
280
revision : Option < String > ,
311
- path : & Path ,
312
281
l : usize ,
313
- ) {
314
- checked ! ( path, l) ;
282
+ ) -> Result < ( ) > {
315
283
// FIXME: check that the error happens on the marked line
316
284
317
- let ( match_line, pattern) = match unwrap ! ( pattern. chars( ) . next( ) , "no pattern specified" ) {
318
- '|' =>
319
- ( * unwrap ! ( fallthrough_to, "`//~|` pattern without preceding line" ) , & pattern[ 1 ..] ) ,
320
- '^' => {
321
- let offset = pattern. chars ( ) . take_while ( |& c| c == '^' ) . count ( ) ;
322
- ( l - offset, & pattern[ offset..] )
323
- }
324
- _ => ( l, pattern) ,
325
- } ;
285
+ let ( match_line, pattern) =
286
+ match pattern. chars ( ) . next ( ) . ok_or_else ( || eyre ! ( "no pattern specified" ) ) ? {
287
+ '|' =>
288
+ (
289
+ * fallthrough_to
290
+ . as_mut ( )
291
+ . ok_or_else ( || eyre ! ( "`//~|` pattern without preceding line" ) ) ?,
292
+ & pattern[ 1 ..] ,
293
+ ) ,
294
+ '^' => {
295
+ let offset = pattern. chars ( ) . take_while ( |& c| c == '^' ) . count ( ) ;
296
+ ( l - offset, & pattern[ offset..] )
297
+ }
298
+ _ => ( l, pattern) ,
299
+ } ;
326
300
327
301
let ( level, pattern) = match pattern. trim_start ( ) . split_once ( |c| matches ! ( c, ':' | ' ' ) ) {
328
302
None => ( None , pattern) ,
@@ -335,7 +309,7 @@ impl Comments {
335
309
336
310
let matched = pattern. trim ( ) . to_string ( ) ;
337
311
338
- check ! ( !matched. is_empty( ) , "no pattern specified" ) ;
312
+ ensure ! ( !matched. is_empty( ) , "no pattern specified" ) ;
339
313
340
314
* fallthrough_to = Some ( match_line) ;
341
315
@@ -346,5 +320,7 @@ impl Comments {
346
320
definition_line : l,
347
321
line : match_line,
348
322
} ) ;
323
+
324
+ Ok ( ( ) )
349
325
}
350
326
}
0 commit comments