Skip to content

Commit b47f405

Browse files
author
bors-servo
authored
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
2 parents 3acb314 + fda0548 commit b47f405

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/clang.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,15 @@ impl Cursor {
349349

350350
/// Get the signed constant value for this cursor's enum variant referent.
351351
///
352-
/// Returns `LLONG_MIN` if the cursor's referent is not an enum variant,
353-
/// which is also a valid enum value, so callers should check the cursor
354-
/// kind before calling this method (see issue #127).
355-
pub fn enum_val_signed(&self) -> i64 {
356-
unsafe { clang_getEnumConstantDeclValue(self.x) as i64 }
352+
/// Returns None if the cursor's referent is not an enum variant.
353+
pub fn enum_val_signed(&self) -> Option<i64> {
354+
unsafe {
355+
if self.kind() == CXCursor_EnumConstantDecl {
356+
Some(clang_getEnumConstantDeclValue(self.x) as i64)
357+
} else {
358+
None
359+
}
360+
}
357361
}
358362

359363
/// Get the unsigned constant value for this cursor's enum variant referent.

src/ir/enum_ty.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,19 @@ impl Enum {
7272

7373
declaration.visit(|cursor| {
7474
if cursor.kind() == CXCursor_EnumConstantDecl {
75-
let name = cursor.spelling();
76-
let comment = cursor.raw_comment();
77-
let val = if is_signed {
78-
EnumVariantValue::Signed(cursor.enum_val_signed())
75+
let value = if is_signed {
76+
cursor.enum_val_signed().map(EnumVariantValue::Signed)
7977
} else {
80-
EnumVariantValue::Unsigned(cursor.enum_val_unsigned())
78+
Some(EnumVariantValue::Unsigned(cursor.enum_val_unsigned()))
8179
};
82-
variants.push(EnumVariant::new(name, comment, val));
80+
if let Some(val) = value {
81+
let name = cursor.spelling();
82+
let comment = cursor.raw_comment();
83+
variants.push(EnumVariant::new(name, comment, val));
84+
}
8385
}
8486
CXChildVisit_Continue
8587
});
86-
8788
Ok(Enum::new(repr, variants))
8889
}
8990
}

0 commit comments

Comments
 (0)