summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2016-11-06 15:56:23 -0600
committerGitHub <noreply@github.com>2016-11-06 15:56:23 -0600
commitb47f405c0d1ee4235f380d787e603687c8cd57e4 (patch)
treec857a0e2e428465828899bc1b58632eaae5fe506
parent3acb3140d975de95a2258f86113e03555949c32c (diff)
parentfda05481d98a86254168a850d8be4d23a9c7c51a (diff)
Auto merge of #220 - jcdyer:enum_value_signed, r=emilio
Wrap enum_val_signed in an Option. Also reorganize calling code to avoid duplicate checking of cursor type. Fixes #127
-rwxr-xr-xsrc/clang.rs14
-rw-r--r--src/ir/enum_ty.rs15
2 files changed, 17 insertions, 12 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..1bcd5b14 100644
--- a/src/ir/enum_ty.rs
+++ b/src/ir/enum_ty.rs
@@ -72,18 +72,19 @@ impl Enum {
declaration.visit(|cursor| {
if cursor.kind() == CXCursor_EnumConstantDecl {
- let name = cursor.spelling();
- let comment = cursor.raw_comment();
- let val = if is_signed {
- EnumVariantValue::Signed(cursor.enum_val_signed())
+ let value = if is_signed {
+ cursor.enum_val_signed().map(EnumVariantValue::Signed)
} else {
- EnumVariantValue::Unsigned(cursor.enum_val_unsigned())
+ Some(EnumVariantValue::Unsigned(cursor.enum_val_unsigned()))
};
- variants.push(EnumVariant::new(name, comment, val));
+ if let Some(val) = value {
+ let name = cursor.spelling();
+ let comment = cursor.raw_comment();
+ variants.push(EnumVariant::new(name, comment, val));
+ }
}
CXChildVisit_Continue
});
-
Ok(Enum::new(repr, variants))
}
}