Skip to content

[rfc] Use a dynamically loaded clang to do as much as we can with old clang versions, and experiment with new ones. #335

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
Dec 14, 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
2 changes: 1 addition & 1 deletion bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ version = "0.17.0"
workspace = ".."

[dependencies]
clang-sys = "0.11.1"
clang-sys = "0.12"
clap = "2"
libbindgen = { path = "../libbindgen" }
log = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion libbindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ quasi_codegen = "0.26"
[dependencies]
cexpr = "0.2"
cfg-if = "0.1.0"
clang-sys = { version = "0.11.1", features = ["gte_clang_3_6", "gte_clang_3_7", "gte_clang_3_8", "gte_clang_3_9"] }
clang-sys = { version = "0.12", features = ["runtime", "clang_3_9"] }
lazy_static = "0.2.1"
libc = "0.2"
rustc-serialize = "0.3.19"
Expand Down
39 changes: 6 additions & 33 deletions libbindgen/src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,19 +349,9 @@ impl Cursor {
}

/// Is the referent an inlined function?
#[cfg(not(feature="llvm_stable"))]
pub fn is_inlined_function(&self) -> bool {
unsafe { clang_Cursor_isFunctionInlined(self.x) != 0 }
}

// TODO: Remove this when LLVM 3.9 is released.
//
// This is currently used for CI purposes.

/// Is the referent an inlined function?
#[cfg(feature="llvm_stable")]
pub fn is_inlined_function(&self) -> bool {
false
clang_Cursor_isFunctionInlined::is_loaded() &&
unsafe { clang_Cursor_isFunctionInlined(self.x) != 0 }
}

/// Get the width of this cursor's referent bit field, or `None` if the
Expand Down Expand Up @@ -762,7 +752,6 @@ impl Type {

/// For elaborated types (types which use `class`, `struct`, or `union` to
/// disambiguate types from local bindings), get the underlying type.
#[cfg(not(feature="llvm_stable"))]
pub fn named(&self) -> Type {
unsafe {
Type {
Expand Down Expand Up @@ -1306,28 +1295,13 @@ pub struct EvalResult {
x: CXEvalResult,
}

#[cfg(feature = "llvm_stable")]
impl EvalResult {
/// Create a dummy EvalResult.
pub fn new(_: Cursor) -> Option<Self> {
None
}

/// Not useful in llvm 3.8.
pub fn as_double(&self) -> Option<f64> {
None
}

/// Not useful in llvm 3.8.
pub fn as_int(&self) -> Option<i32> {
None
}
}

#[cfg(not(feature = "llvm_stable"))]
impl EvalResult {
/// Evaluate `cursor` and return the result.
pub fn new(cursor: Cursor) -> Option<Self> {
if !clang_Cursor_Evaluate::is_loaded() {
return None;
}

// Clang has an internal assertion we can trigger if we try to evaluate
// a cursor containing a variadic template type reference. Triggering
// the assertion aborts the process, and we don't want that. Clang
Expand Down Expand Up @@ -1379,7 +1353,6 @@ impl EvalResult {
}
}

#[cfg(not(feature = "llvm_stable"))]
impl Drop for EvalResult {
fn drop(&mut self) {
unsafe { clang_EvalResult_dispose(self.x) };
Expand Down
1 change: 0 additions & 1 deletion libbindgen/src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,6 @@ impl Type {
.expect("Not able to resolve array element?");
TypeKind::Array(inner, ty.num_elements().unwrap())
}
#[cfg(not(feature="llvm_stable"))]
CXType_Elaborated => {
return Self::from_clang_ty(potential_id,
&ty.named(),
Expand Down
9 changes: 9 additions & 0 deletions libbindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ impl<'ctx> Bindings<'ctx> {
span: Option<Span>)
-> Result<Bindings<'ctx>, ()> {
let span = span.unwrap_or(DUMMY_SP);
if !clang_sys::is_loaded() {
// TODO(emilio): Return meaningful error (breaking).
clang_sys::load().expect("Unable to find libclang");
}

// TODO: Make this path fixup configurable?
if let Some(clang) = clang_sys::support::Clang::find(None) {
Expand Down Expand Up @@ -691,6 +695,11 @@ pub struct ClangVersion {

/// Get the major and the minor semvar numbers of Clang's version
pub fn clang_version() -> ClangVersion {
if !clang_sys::is_loaded() {
// TODO(emilio): Return meaningful error (breaking).
clang_sys::load().expect("Unable to find libclang");
}

let raw_v: String = clang::extract_clang_version();
let split_v: Option<Vec<&str>> = raw_v.split_whitespace()
.nth(2)
Expand Down