@@ -368,9 +368,30 @@ fn parse_macro(
368
368
}
369
369
let parsed_macros = ctx. parsed_macros ( ) ;
370
370
371
- // TODO: remove this clone (will need changes in saltwater)
372
- if let Some ( literal) = swcc_expr ( swcc_tokens. clone ( ) , & parsed_macros) {
373
- return Some ( ( ident_str, literal) ) ;
371
+ swcc_expr ( swcc_tokens. collect ( ) , & parsed_macros)
372
+ . map ( |literal| ( ident_str, literal) )
373
+ }
374
+
375
+ fn swcc_expr (
376
+ mut tokens : Vec < saltwater:: Locatable < saltwater:: Token > > ,
377
+ definitions : & HashMap < InternedStr , saltwater:: Definition > ,
378
+ ) -> Option < Literal > {
379
+ use saltwater:: { Locatable , PreProcessor } ;
380
+
381
+ let parse = |tokens : Vec < Locatable < _ > > | {
382
+ let mut tokens = tokens. into_iter ( ) . peekable ( ) ;
383
+ let location = tokens. peek ( ) ?. location ;
384
+ PreProcessor :: cpp_expr ( definitions, tokens, location)
385
+ . ok ( ) ?
386
+ . const_fold ( )
387
+ . ok ( ) ?
388
+ . into_literal ( )
389
+ . ok ( )
390
+ } ;
391
+
392
+ // TODO: remove this clone (requires changes in saltwater)
393
+ if let Some ( literal) = parse ( tokens. clone ( ) ) {
394
+ return Some ( literal) ;
374
395
}
375
396
376
397
// Try without the last token, to workaround a libclang bug in versions
@@ -379,34 +400,12 @@ fn parse_macro(
379
400
// See:
380
401
// https://bugs.llvm.org//show_bug.cgi?id=9069
381
402
// https://reviews.llvm.org/D26446
382
- let tokens = tokens[ 1 ..tokens. len ( ) - 1 ]
383
- . iter ( )
384
- . filter_map ( ClangToken :: as_swcc_token) ;
385
- if let Some ( literal) = swcc_expr ( tokens, & parsed_macros) {
386
- Some ( ( ident_str, literal) )
387
- } else {
388
- None
389
- }
390
- }
391
-
392
- fn swcc_expr (
393
- swcc_tokens : impl Iterator < Item = saltwater:: Locatable < saltwater:: Token > > ,
394
- definitions : & HashMap < InternedStr , saltwater:: Definition > ,
395
- ) -> Option < Literal > {
396
- use saltwater:: PreProcessor ;
397
-
398
- let mut swcc_tokens = swcc_tokens. peekable ( ) ;
399
- let location = swcc_tokens. peek ( ) ?. location ;
400
- PreProcessor :: cpp_expr ( definitions, swcc_tokens, location)
401
- . ok ( ) ?
402
- . const_fold ( )
403
- . ok ( ) ?
404
- . into_literal ( )
405
- . ok ( )
403
+ tokens. pop ( ) ;
404
+ parse ( tokens)
406
405
}
407
406
408
407
fn parse_int_literal_tokens ( cursor : & clang:: Cursor ) -> Option < i64 > {
409
- let swcc_tokens = cursor. swcc_tokens ( ) . into_iter ( ) ;
408
+ let swcc_tokens = cursor. swcc_tokens ( ) ;
410
409
411
410
// TODO(emilio): We can try to parse other kinds of literals.
412
411
match swcc_expr ( swcc_tokens, & HashMap :: new ( ) ) {
0 commit comments