Skip to content

Commit

Permalink
Auto merge of #916 - fitzgen:check-if-num-template-args-is-loaded, r=…
Browse files Browse the repository at this point in the history
…fitzgen

Check if `clang_Type_getNumTemplateArguments` is loaded before use

Older clang don't have it, and while we can't pass our whole test suite with those older clangs, we can still generate simple C bindings, so it makes sense not to panic for C++ only things.
  • Loading branch information
bors-servo authored Aug 16, 2017
2 parents 74834c7 + b6c7592 commit 333dc1d
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,13 @@ impl Type {
/// Get the number of template arguments this type has, or `None` if it is
/// not some kind of template.
pub fn num_template_args(&self) -> Option<u32> {
// If an old libclang is loaded, we have no hope of answering this
// question correctly. However, that's no reason to panic when
// generating bindings for simple C headers with an old libclang.
if !clang_Type_getNumTemplateArguments::is_loaded() {
return None
}

let n = unsafe { clang_Type_getNumTemplateArguments(self.x) };
if n >= 0 {
Some(n as u32)
Expand Down Expand Up @@ -1594,14 +1601,19 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult {

print_indent(depth,
format!(" {}spelling = \"{}\"", prefix, ty.spelling()));
let num_template_args =
unsafe { clang_Type_getNumTemplateArguments(ty.x) };

let num_template_args = if clang_Type_getNumTemplateArguments::is_loaded() {
unsafe { clang_Type_getNumTemplateArguments(ty.x) }
} else {
-1
};
if num_template_args >= 0 {
print_indent(depth,
format!(" {}number-of-template-args = {}",
prefix,
num_template_args));
}

if let Some(num) = ty.num_elements() {
print_indent(depth,
format!(" {}number-of-elements = {}", prefix, num));
Expand Down

0 comments on commit 333dc1d

Please sign in to comment.