From 4e2b9d6f0ff3a02ed8ff97ac2ccd7a8e6bee273d Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Tue, 25 Oct 2016 17:12:01 +1100 Subject: Try to read integer literal from expr This and the commit before fixes #101. --- src/ir/var.rs | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/ir/var.rs b/src/ir/var.rs index 8e7f6c42..c7091a5c 100644 --- a/src/ir/var.rs +++ b/src/ir/var.rs @@ -106,21 +106,17 @@ impl ClangSubItemParser for Var { let ty = Item::from_ty(&ty, Some(cursor), None, context) .expect("Unable to resolve constant type?"); - let mut value = None; - // Note: Ty might not be totally resolved yet, see // tests/headers/inner_const.hpp // // That's fine because in that case we know it's not a literal. - if context.safe_resolve_type(ty).map_or(false, |t| t.is_integer(context)) { - // Try to parse a literal token value - cursor.visit(|c, _| { - if c.kind() == CXCursor_IntegerLiteral { - value = parse_int_literal_tokens(&c, context.translation_unit()); - } - CXChildVisit_Continue - }); - } + let value = context.safe_resolve_type(ty).map_or(None, |t| { + if t.is_integer(context) { + get_integer_literal_from_cursor(&cursor, context.translation_unit()) + } else { + None + } + }); let mangling = cursor_mangling(&cursor); @@ -169,3 +165,26 @@ fn parse_int_literal_tokens(cursor: &clang::Cursor, lit.parse().ok() } } + +fn get_integer_literal_from_cursor(cursor: &clang::Cursor, + unit: &clang::TranslationUnit) -> Option { + use clangll::*; + let mut value = None; + cursor.visit(|c, _| { + match c.kind() { + CXCursor_IntegerLiteral => { + value = parse_int_literal_tokens(&c, unit); + } + CXCursor_UnexposedExpr => { + value = get_integer_literal_from_cursor(&c, unit); + } + _ => () + } + if value.is_some() { + CXChildVisit_Break + } else { + CXChildVisit_Continue + } + }); + value +} -- cgit v1.2.3