Skip to content

Commit b89880c

Browse files
author
J. Cliff Dyer
committed
Wrap enum_val_signed in an Option.
Also reorganize calling function to avoid duplicate checking of cursor type. Fixes rust-lang#127
1 parent eaa674e commit b89880c

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/clang.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,14 @@ impl Cursor {
352352
/// Returns `LLONG_MIN` if the cursor's referent is not an enum variant,
353353
/// which is also a valid enum value, so callers should check the cursor
354354
/// kind before calling this method (see issue #127).
355-
pub fn enum_val_signed(&self) -> i64 {
356-
unsafe { clang_getEnumConstantDeclValue(self.x) as i64 }
355+
pub fn enum_val_signed(&self) -> Option<i64> {
356+
unsafe {
357+
if self.kind() == CXCursor_EnumConstantDecl {
358+
Some(clang_getEnumConstantDeclValue(self.x) as i64)
359+
} else {
360+
None
361+
}
362+
}
357363
}
358364

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

src/ir/enum_ty.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,20 @@ impl Enum {
7171
};
7272

7373
declaration.visit(|cursor| {
74-
if cursor.kind() == CXCursor_EnumConstantDecl {
74+
let val = if is_signed {
75+
cursor.enum_val_signed().map(|val| EnumVariantValue::Signed(val))
76+
} else {
77+
if cursor.kind() == CXCursor_EnumConstantDecl {
78+
Some(EnumVariantValue::Unsigned(cursor.enum_val_unsigned()))
79+
} else {
80+
None
81+
}
82+
};
83+
if let Some(val) = val {
7584
let name = cursor.spelling();
7685
let comment = cursor.raw_comment();
77-
let val = if is_signed {
78-
EnumVariantValue::Signed(cursor.enum_val_signed())
79-
} else {
80-
EnumVariantValue::Unsigned(cursor.enum_val_unsigned())
81-
};
8286
variants.push(EnumVariant::new(name, comment, val));
83-
}
87+
};
8488
CXChildVisit_Continue
8589
});
8690

0 commit comments

Comments
 (0)