@@ -389,9 +389,30 @@ fn parse_macro(
389
389
}
390
390
let parsed_macros = ctx. parsed_macros ( ) ;
391
391
392
- // TODO: remove this clone (will need changes in saltwater)
393
- if let Some ( literal) = swcc_expr ( swcc_tokens. clone ( ) , & parsed_macros) {
394
- return Some ( ( ident_str, literal) ) ;
392
+ swcc_expr ( swcc_tokens. collect ( ) , & parsed_macros)
393
+ . map ( |literal| ( ident_str, literal) )
394
+ }
395
+
396
+ fn swcc_expr (
397
+ mut tokens : Vec < saltwater:: Locatable < saltwater:: Token > > ,
398
+ definitions : & HashMap < InternedStr , saltwater:: Definition > ,
399
+ ) -> Option < Literal > {
400
+ use saltwater:: { Locatable , PreProcessor } ;
401
+
402
+ let parse = |tokens : Vec < Locatable < _ > > | {
403
+ let mut tokens = tokens. into_iter ( ) . peekable ( ) ;
404
+ let location = tokens. peek ( ) ?. location ;
405
+ PreProcessor :: cpp_expr ( definitions, tokens, location)
406
+ . ok ( ) ?
407
+ . const_fold ( )
408
+ . ok ( ) ?
409
+ . into_literal ( )
410
+ . ok ( )
411
+ } ;
412
+
413
+ // TODO: remove this clone (requires changes in saltwater)
414
+ if let Some ( literal) = parse ( tokens. clone ( ) ) {
415
+ return Some ( literal) ;
395
416
}
396
417
397
418
// Try without the last token, to workaround a libclang bug in versions
@@ -400,34 +421,12 @@ fn parse_macro(
400
421
// See:
401
422
// https://bugs.llvm.org//show_bug.cgi?id=9069
402
423
// https://reviews.llvm.org/D26446
403
- let tokens = tokens[ 1 ..tokens. len ( ) - 1 ]
404
- . iter ( )
405
- . filter_map ( ClangToken :: as_swcc_token) ;
406
- if let Some ( literal) = swcc_expr ( tokens, & parsed_macros) {
407
- Some ( ( ident_str, literal) )
408
- } else {
409
- None
410
- }
411
- }
412
-
413
- fn swcc_expr (
414
- swcc_tokens : impl Iterator < Item = saltwater:: Locatable < saltwater:: Token > > ,
415
- definitions : & HashMap < InternedStr , saltwater:: Definition > ,
416
- ) -> Option < Literal > {
417
- use saltwater:: PreProcessor ;
418
-
419
- let mut swcc_tokens = swcc_tokens. peekable ( ) ;
420
- let location = swcc_tokens. peek ( ) ?. location ;
421
- PreProcessor :: cpp_expr ( definitions, swcc_tokens, location)
422
- . ok ( ) ?
423
- . const_fold ( )
424
- . ok ( ) ?
425
- . into_literal ( )
426
- . ok ( )
424
+ tokens. pop ( ) ;
425
+ parse ( tokens)
427
426
}
428
427
429
428
fn parse_int_literal_tokens ( cursor : & clang:: Cursor ) -> Option < i64 > {
430
- let swcc_tokens = cursor. swcc_tokens ( ) . into_iter ( ) ;
429
+ let swcc_tokens = cursor. swcc_tokens ( ) ;
431
430
432
431
// TODO(emilio): We can try to parse other kinds of literals.
433
432
match swcc_expr ( swcc_tokens, & HashMap :: new ( ) ) {
0 commit comments