Skip to content

Commit c940bd8

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

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/clang.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,26 @@ impl Cursor {
387387

388388
/// Given that this cursor's referent is a function, return cursors to its
389389
/// parameters.
390-
pub fn args(&self) -> Vec<Cursor> {
390+
pub fn args(&self) -> Option<Vec<Cursor>> {
391+
// XXX: We might want to use and keep num_args
392+
// match self.kind() {
393+
// CXCursor_FunctionDecl |
394+
// CXCursor_CXXMethod => {
391395
unsafe {
392-
let num = self.num_args().expect("expected value, got none") as u32;
393-
let mut args = vec![];
394-
for i in 0..num {
395-
args.push(Cursor {
396-
x: clang_Cursor_getArgument(self.x, i as c_uint),
397-
});
396+
let w = clang_Cursor_getNumArguments(self.x);
397+
if w == -1 {
398+
None
399+
} else {
400+
let num = w as u32;
401+
402+
let mut args = vec![];
403+
for i in 0..num {
404+
args.push(Cursor {
405+
x: clang_Cursor_getArgument(self.x, i as c_uint),
406+
});
407+
}
408+
Some(args)
398409
}
399-
args
400410
}
401411
}
402412

src/ir/function.rs

Lines changed: 1 addition & 1 deletion
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)