From 15d1881e071278cc65acd202284943a6f5ce4543 Mon Sep 17 00:00:00 2001 From: "J. Cliff Dyer" Date: Sun, 6 Nov 2016 11:56:46 -0500 Subject: [PATCH 1/2] Wrap enum_val_signed in an Option. Also reorganize calling function to avoid duplicate checking of cursor type. Fixes #127 --- src/clang.rs | 14 +++++++++----- src/ir/enum_ty.rs | 16 ++++++++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/clang.rs b/src/clang.rs index 5a2421f3f9..0bfd78ef0e 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 { + 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 e78184e7c5..fb7b2018ab 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 From fda05481d98a86254168a850d8be4d23a9c7c51a Mon Sep 17 00:00:00 2001 From: "J. Cliff Dyer" Date: Sun, 6 Nov 2016 16:51:25 -0500 Subject: [PATCH 2/2] Restore outer conditional. --- src/ir/enum_ty.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/ir/enum_ty.rs b/src/ir/enum_ty.rs index fb7b2018ab..1bcd5b149c 100644 --- a/src/ir/enum_ty.rs +++ b/src/ir/enum_ty.rs @@ -71,23 +71,20 @@ impl Enum { }; declaration.visit(|cursor| { - 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())) + if cursor.kind() == CXCursor_EnumConstantDecl { + let value = if is_signed { + cursor.enum_val_signed().map(EnumVariantValue::Signed) } else { - None + Some(EnumVariantValue::Unsigned(cursor.enum_val_unsigned())) + }; + if let Some(val) = value { + let name = cursor.spelling(); + let comment = cursor.raw_comment(); + variants.push(EnumVariant::new(name, comment, val)); } - }; - if let Some(val) = val { - let name = cursor.spelling(); - let comment = cursor.raw_comment(); - variants.push(EnumVariant::new(name, comment, val)); } CXChildVisit_Continue }); - Ok(Enum::new(repr, variants)) } }