Skip to content

Commit 17494de

Browse files
committed
Return Option<Type> in clang::Type::ret_type fix #141
Also reduce the scope for unsafe code
1 parent 79407f3 commit 17494de

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/clang.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,9 +714,12 @@ impl Type {
714714

715715
/// Given that this type is a function type, get the type of its return
716716
/// value.
717-
pub fn ret_type(&self) -> Type {
718-
unsafe {
719-
Type { x: clang_getResultType(self.x) }
717+
pub fn ret_type(&self) -> Option<Type> {
718+
let rt = Type { x: unsafe { clang_getResultType(self.x) } };
719+
if rt.kind() == CXType_Invalid {
720+
None
721+
} else {
722+
Some(rt)
720723
}
721724
}
722725

src/ir/function.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ impl FunctionSig {
186186
}
187187
}
188188

189-
let ret = try!(Item::from_ty(&ty.ret_type(), None, None, ctx));
189+
let ty_ret_type = try!(ty.ret_type().ok_or(ParseError::Continue));
190+
let ret = try!(Item::from_ty(&ty_ret_type, None, None, ctx));
190191
let abi = get_abi(ty.call_conv());
191192

192193
Ok(Self::new(ret, args, ty.is_variadic(), abi))

src/ir/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ impl Type {
499499
// tests/headers/func_ptr_in_struct.h), so we do a
500500
// guess here trying to see if it has a valid return
501501
// type.
502-
if ty.ret_type().kind() != CXType_Invalid {
502+
if ty.ret_type().is_some() {
503503
let signature =
504504
try!(FunctionSig::from_ty(ty, &location.unwrap_or(cursor), ctx));
505505
TypeKind::Function(signature)

0 commit comments

Comments
 (0)