summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXidorn Quan <me@upsuper.org>2016-10-25 17:12:01 +1100
committerXidorn Quan <me@upsuper.org>2016-10-25 23:45:31 +1100
commit4e2b9d6f0ff3a02ed8ff97ac2ccd7a8e6bee273d (patch)
tree0e7bae5798d7b4f775d03bb42b1c17998931aa82
parentfaa2ff06abf9f9913aa311e8ee739c761237669d (diff)
Try to read integer literal from expr
This and the commit before fixes #101.
-rw-r--r--src/ir/var.rs41
1 files 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<i64> {
+ 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
+}