Skip to content

Commit 333dc1d

Browse files
author
bors-servo
authored
Auto merge of rust-lang#916 - fitzgen:check-if-num-template-args-is-loaded, r=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.
2 parents 74834c7 + b6c7592 commit 333dc1d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/clang.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,13 @@ impl Type {
833833
/// Get the number of template arguments this type has, or `None` if it is
834834
/// not some kind of template.
835835
pub fn num_template_args(&self) -> Option<u32> {
836+
// If an old libclang is loaded, we have no hope of answering this
837+
// question correctly. However, that's no reason to panic when
838+
// generating bindings for simple C headers with an old libclang.
839+
if !clang_Type_getNumTemplateArguments::is_loaded() {
840+
return None
841+
}
842+
836843
let n = unsafe { clang_Type_getNumTemplateArguments(self.x) };
837844
if n >= 0 {
838845
Some(n as u32)
@@ -1594,14 +1601,19 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult {
15941601

15951602
print_indent(depth,
15961603
format!(" {}spelling = \"{}\"", prefix, ty.spelling()));
1597-
let num_template_args =
1598-
unsafe { clang_Type_getNumTemplateArguments(ty.x) };
1604+
1605+
let num_template_args = if clang_Type_getNumTemplateArguments::is_loaded() {
1606+
unsafe { clang_Type_getNumTemplateArguments(ty.x) }
1607+
} else {
1608+
-1
1609+
};
15991610
if num_template_args >= 0 {
16001611
print_indent(depth,
16011612
format!(" {}number-of-template-args = {}",
16021613
prefix,
16031614
num_template_args));
16041615
}
1616+
16051617
if let Some(num) = ty.num_elements() {
16061618
print_indent(depth,
16071619
format!(" {}number-of-elements = {}", prefix, num));

0 commit comments

Comments
 (0)