Skip to content

Commit e8b32d0

Browse files
committed
Avoid parsing tokens twice
1 parent ffd7be6 commit e8b32d0

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/ir/var.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ impl ClangSubItemParser for Var {
193193
use clang_sys::*;
194194
match cursor.kind() {
195195
CXCursor_MacroDefinition => {
196+
let tokens: Vec<_> = cursor.tokens().iter().collect();
197+
196198
if let Some(callbacks) = ctx.parse_callbacks() {
197199
match callbacks.will_parse_macro(&cursor.spelling()) {
198200
MacroParsingBehavior::Ignore => {
@@ -201,13 +203,12 @@ impl ClangSubItemParser for Var {
201203
MacroParsingBehavior::Default => {}
202204
}
203205

204-
let tokens: Vec<_> = cursor.tokens().iter().collect();
205206
handle_function_macro(&tokens, |left, right| {
206207
callbacks.func_macro(left, right)
207208
})?;
208209
}
209210

210-
let value = parse_macro(ctx, &cursor.cexpr_tokens());
211+
let value = parse_macro(ctx, &tokens);
211212

212213
let (id, value) = match value {
213214
Some(v) => v,
@@ -374,7 +375,7 @@ impl ClangSubItemParser for Var {
374375
/// Try and parse a macro using all the macros parsed until now.
375376
fn parse_macro(
376377
ctx: &BindgenContext,
377-
cexpr_tokens: &[cexpr::token::Token],
378+
tokens: &[clang::ClangToken],
378379
) -> Option<(Vec<u8>, cexpr::expr::EvalResult)> {
379380
use cexpr::expr;
380381

@@ -385,12 +386,15 @@ fn parse_macro(
385386
// See:
386387
// https://bugs.llvm.org//show_bug.cgi?id=9069
387388
// https://reviews.llvm.org/D26446
388-
let cexpr_tokens = match (cexpr_tokens.len(), crate::clang_version().parsed)
389-
{
390-
(len, Some((v, _))) if len > 0 && v < 4 => &cexpr_tokens[..len - 1],
391-
_ => &cexpr_tokens[..],
389+
let tokens = match (tokens.len(), crate::clang_version().parsed) {
390+
(len, Some((v, _))) if len > 0 && v < 4 => &tokens[..len - 1],
391+
_ => &tokens[..],
392392
};
393393

394+
let cexpr_tokens: Vec<_> = tokens
395+
.iter()
396+
.filter_map(|token| token.as_cexpr_token())
397+
.collect();
394398
match parser.macro_definition(&cexpr_tokens) {
395399
Ok((_, (id, val))) => Some((id.into(), val)),
396400
_ => None,

0 commit comments

Comments
 (0)