Skip to content

Commit cdc870f

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 d5cd806 commit cdc870f

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

src/ir/var.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,30 @@ fn parse_macro(
368368
}
369369
let parsed_macros = ctx.parsed_macros();
370370

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);
374395
}
375396

376397
// Try without the last token, to workaround a libclang bug in versions
@@ -379,34 +400,12 @@ fn parse_macro(
379400
// See:
380401
// https://bugs.llvm.org//show_bug.cgi?id=9069
381402
// 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)
406405
}
407406

408407
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();
410409

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

0 commit comments

Comments
 (0)