Skip to content

Commit 698da73

Browse files
committed
Remove trailing tokens when parsing integer literals
This was the same LLVM bug noted in parse_macro, but it seems that cexpr allowed trailing tokens while saltwater does not.
1 parent fd26731 commit 698da73

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

src/ir/var.rs

+27-28
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,30 @@ fn parse_macro(
389389
}
390390
let parsed_macros = ctx.parsed_macros();
391391

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);
395416
}
396417

397418
// Try without the last token, to workaround a libclang bug in versions
@@ -400,34 +421,12 @@ fn parse_macro(
400421
// See:
401422
// https://bugs.llvm.org//show_bug.cgi?id=9069
402423
// 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)
427426
}
428427

429428
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();
431430

432431
// TODO(emilio): We can try to parse other kinds of literals.
433432
match swcc_expr(swcc_tokens, &HashMap::new()) {

0 commit comments

Comments
 (0)