diff options
author | J. Cliff Dyer <cdyer@edx.org> | 2016-11-06 11:56:46 -0500 |
---|---|---|
committer | J. Cliff Dyer <cdyer@edx.org> | 2016-11-06 12:36:37 -0500 |
commit | 15d1881e071278cc65acd202284943a6f5ce4543 (patch) | |
tree | a15ee9c5dcfd1d93ae6876755ccd7edf8f8fe810 | |
parent | 58cf53e1e5eb6a28c6a092faaad9a057ba0a2c1c (diff) |
Wrap enum_val_signed in an Option.
Also reorganize calling function to avoid duplicate checking of cursor
type.
Fixes #127
-rwxr-xr-x | src/clang.rs | 14 | ||||
-rw-r--r-- | src/ir/enum_ty.rs | 16 |
2 files changed, 19 insertions, 11 deletions
diff --git a/src/clang.rs b/src/clang.rs index 5a2421f3..0bfd78ef 100755 --- a/src/clang.rs +++ b/src/clang.rs @@ -349,11 +349,15 @@ impl Cursor { /// Get the signed constant value for this cursor's enum variant referent. /// - /// Returns `LLONG_MIN` if the cursor's referent is not an enum variant, - /// which is also a valid enum value, so callers should check the cursor - /// kind before calling this method (see issue #127). - pub fn enum_val_signed(&self) -> i64 { - unsafe { clang_getEnumConstantDeclValue(self.x) as i64 } + /// Returns None if the cursor's referent is not an enum variant. + pub fn enum_val_signed(&self) -> Option<i64> { + unsafe { + if self.kind() == CXCursor_EnumConstantDecl { + Some(clang_getEnumConstantDeclValue(self.x) as i64) + } else { + None + } + } } /// Get the unsigned constant value for this cursor's enum variant referent. diff --git a/src/ir/enum_ty.rs b/src/ir/enum_ty.rs index e78184e7..fb7b2018 100644 --- a/src/ir/enum_ty.rs +++ b/src/ir/enum_ty.rs @@ -71,14 +71,18 @@ impl Enum { }; declaration.visit(|cursor| { - if cursor.kind() == CXCursor_EnumConstantDecl { + let val = if is_signed { + cursor.enum_val_signed().map(EnumVariantValue::Signed) + } else { + if cursor.kind() == CXCursor_EnumConstantDecl { + Some(EnumVariantValue::Unsigned(cursor.enum_val_unsigned())) + } else { + None + } + }; + if let Some(val) = val { let name = cursor.spelling(); let comment = cursor.raw_comment(); - let val = if is_signed { - EnumVariantValue::Signed(cursor.enum_val_signed()) - } else { - EnumVariantValue::Unsigned(cursor.enum_val_unsigned()) - }; variants.push(EnumVariant::new(name, comment, val)); } CXChildVisit_Continue |