Skip to content

Commit e65cd9b

Browse files
author
Rémy HUBSCHER
committed
Attempt to fix #130 — clang::Cursor::args should return an Option<Vec<Cursor>>
1 parent 3761ada commit e65cd9b

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

libbindgen/src/clang.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -420,16 +420,26 @@ impl Cursor {
420420

421421
/// Given that this cursor's referent is a function, return cursors to its
422422
/// parameters.
423-
pub fn args(&self) -> Vec<Cursor> {
423+
pub fn args(&self) -> Option<Vec<Cursor>> {
424+
// XXX: We might want to use and keep num_args
425+
// match self.kind() {
426+
// CXCursor_FunctionDecl |
427+
// CXCursor_CXXMethod => {
424428
unsafe {
425-
let num = self.num_args().expect("expected value, got none") as u32;
426-
let mut args = vec![];
427-
for i in 0..num {
428-
args.push(Cursor {
429-
x: clang_Cursor_getArgument(self.x, i as c_uint),
430-
});
429+
let w = clang_Cursor_getNumArguments(self.x);
430+
if w == -1 {
431+
None
432+
} else {
433+
let num = w as u32;
434+
435+
let mut args = vec![];
436+
for i in 0..num {
437+
args.push(Cursor {
438+
x: clang_Cursor_getArgument(self.x, i as c_uint),
439+
});
440+
}
441+
Some(args)
431442
}
432-
args
433443
}
434444
}
435445

libbindgen/src/ir/function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl FunctionSig {
150150
CXCursor_CXXMethod => {
151151
// For CXCursor_FunctionDecl, cursor.args() is the reliable way
152152
// to get parameter names and types.
153-
cursor.args()
153+
cursor.args().expect("It cannot be None because we are in a method/function")
154154
.iter()
155155
.map(|arg| {
156156
let arg_ty = arg.cur_type();

0 commit comments

Comments
 (0)