Skip to content

clang::Type::num_template_args return Option<u32> fix #135 #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,19 +645,22 @@ impl Type {
}

/// If this type is a class template specialization, return its number of
/// template arguments. Otherwise, return -1.
pub fn num_template_args(&self) -> c_int {
unsafe {
clang_Type_getNumTemplateArguments(self.x)
/// template arguments. Otherwise, return None.
pub fn num_template_args(&self) -> Option<u32> {
let n = unsafe { clang_Type_getNumTemplateArguments(self.x) };
if n >= 0 {
Some(n as u32)
} else {
debug_assert_eq!(n, -1);
None
}
}

/// Get the type of the `i`th template argument for this template
/// specialization.
pub fn template_arg_type(&self, i: c_int) -> Type {
unsafe {
Type { x: clang_Type_getTemplateArgumentAsType(self.x, i) }
}
pub fn template_arg_type(&self, i: u32) -> Type {
let n = i as c_int;
Type { x: unsafe { clang_Type_getTemplateArgumentAsType(self.x, n) } }
}

/// Given that this type is a pointer type, return the type that it points
Expand Down
4 changes: 2 additions & 2 deletions src/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ impl CompInfo {
ci.template_args = match ty.num_template_args() {
// In forward declarations and not specializations, etc, they are in
// the ast, we'll meet them in CXCursor_TemplateTypeParameter
-1 => vec![],
len => {
None => vec![],
Some(len) => {
let mut list = Vec::with_capacity(len as usize);
for i in 0..len {
let arg_type = ty.template_arg_type(i);
Expand Down
2 changes: 1 addition & 1 deletion src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ impl Type {
TypeKind::Function(signature)
// Same here, with template specialisations we can safely assume
// this is a Comp(..)
} else if ty.num_template_args() > 0 {
} else if ty.num_template_args().unwrap_or(0) > 0 {
debug!("Template specialization: {:?}", ty);
let complex =
CompInfo::from_ty(potential_id, ty, location, ctx)
Expand Down