Skip to content

Update which, quote and proc_macro. #1409

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 6 commits into from
Nov 30, 2018
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
61 changes: 50 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "README.md"
repository = "https://github.com/rust-lang/rust-bindgen"
documentation = "https://docs.rs/bindgen"
homepage = "https://rust-lang.github.io/rust-bindgen/"
version = "0.43.2"
version = "0.44.0"
build = "build.rs"

include = [
Expand Down Expand Up @@ -51,12 +51,12 @@ clap = "2"
clang-sys = { version = "0.26", features = ["runtime", "clang_6_0"] }
lazy_static = "1"
peeking_take_while = "0.1.2"
quote = { version = "0.5", default-features = false }
quote = { version = "0.6", default-features = false }
regex = "1.0"
which = "1.0.2"
which = "2.0"
# New validation in 0.3.6 breaks bindgen-integration:
# https://github.com/alexcrichton/proc-macro2/commit/489c642.
proc-macro2 = { version = "0.3.2, < 0.3.6", default-features = false }
proc-macro2 = { version = "0.4", default-features = false }

[dependencies.env_logger]
optional = true
Expand Down
2 changes: 1 addition & 1 deletion bindgen-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build = "build.rs"

[build-dependencies]
bindgen = { path = ".." }
gcc = "0.3"
cc = "1.0"

[features]
testing_only_libclang_5 = ["bindgen/testing_only_libclang_5"]
Expand Down
4 changes: 2 additions & 2 deletions bindgen-integration/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extern crate bindgen;
extern crate gcc;
extern crate cc;

use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks};
use bindgen::Builder;
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Drop for MacroCallback {
}

fn main() {
gcc::Build::new()
cc::Build::new()
.cpp(true)
.file("cpp/Test.cc")
.compile("libtest.a");
Expand Down
67 changes: 31 additions & 36 deletions src/codegen/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,54 @@

use ir::context::BindgenContext;
use ir::layout::Layout;
use quote;
use proc_macro2::{Term, Span};
use proc_macro2::{self, Ident, Span};
use quote::TokenStreamExt;

pub mod attributes {
use quote;
use proc_macro2::{Term, Span};
use proc_macro2::{self, Ident, Span};

pub fn repr(which: &str) -> quote::Tokens {
let which = Term::new(which, Span::call_site());
pub fn repr(which: &str) -> proc_macro2::TokenStream {
let which = Ident::new(which, Span::call_site());
quote! {
#[repr( #which )]
}
}

pub fn repr_list(which_ones: &[&str]) -> quote::Tokens {
let which_ones = which_ones.iter().cloned().map(|one| Term::new(one, Span::call_site()));
pub fn repr_list(which_ones: &[&str]) -> proc_macro2::TokenStream {
let which_ones = which_ones.iter().cloned().map(|one| Ident::new(one, Span::call_site()));
quote! {
#[repr( #( #which_ones ),* )]
}
}

pub fn derives(which_ones: &[&str]) -> quote::Tokens {
let which_ones = which_ones.iter().cloned().map(|one| Term::new(one, Span::call_site()));
pub fn derives(which_ones: &[&str]) -> proc_macro2::TokenStream {
let which_ones = which_ones.iter().cloned().map(|one| Ident::new(one, Span::call_site()));
quote! {
#[derive( #( #which_ones ),* )]
}
}

pub fn inline() -> quote::Tokens {
pub fn inline() -> proc_macro2::TokenStream {
quote! {
#[inline]
}
}

pub fn must_use() -> quote::Tokens {
pub fn must_use() -> proc_macro2::TokenStream {
quote! {
#[must_use]
}
}

pub fn doc(comment: String) -> quote::Tokens {
// Doc comments are already preprocessed into nice `///` formats by the
// time they get here. Just make sure that we have newlines around it so
// that nothing else gets wrapped into the comment.
let mut tokens = quote! {};
tokens.append(Term::new("\n", Span::call_site()));
tokens.append(Term::new(&comment, Span::call_site()));
tokens.append(Term::new("\n", Span::call_site()));
tokens
pub fn doc(comment: String) -> proc_macro2::TokenStream {
use std::str::FromStr;

// NOTE(emilio): By this point comments are already preprocessed and in
// `///` form. Quote turns them into `#[doc]` comments, but oh well.
proc_macro2::TokenStream::from_str(&comment).unwrap()
}

pub fn link_name(name: &str) -> quote::Tokens {
pub fn link_name(name: &str) -> proc_macro2::TokenStream {
// LLVM mangles the name by default but it's already mangled.
// Prefixing the name with \u{1} should tell LLVM to not mangle it.
let name = format!("\u{1}{}", name);
Expand All @@ -65,7 +61,7 @@ pub mod attributes {

/// Generates a proper type for a field or type with a given `Layout`, that is,
/// a type with the correct size and alignment restrictions.
pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
pub fn blob(ctx: &BindgenContext, layout: Layout) -> proc_macro2::TokenStream {
let opaque = layout.opaque();

// FIXME(emilio, #412): We fall back to byte alignment, but there are
Expand All @@ -80,7 +76,7 @@ pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
}
};

let ty_name = Term::new(ty_name, Span::call_site());
let ty_name = Ident::new(ty_name, Span::call_site());

let data_len = opaque.array_size(ctx).unwrap_or(layout.size);

Expand All @@ -96,14 +92,14 @@ pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
}

/// Integer type of the same size as the given `Layout`.
pub fn integer_type(ctx: &BindgenContext, layout: Layout) -> Option<quote::Tokens> {
pub fn integer_type(ctx: &BindgenContext, layout: Layout) -> Option<proc_macro2::TokenStream> {
let name = Layout::known_type_for_size(ctx, layout.size)?;
let name = Term::new(name, Span::call_site());
let name = Ident::new(name, Span::call_site());
Some(quote! { #name })
}

/// Generates a bitfield allocation unit type for a type with the given `Layout`.
pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> proc_macro2::TokenStream {
let mut tokens = quote! {};

if ctx.options().enable_cxx_namespaces {
Expand All @@ -130,10 +126,9 @@ pub mod ast_ty {
use ir::function::FunctionSig;
use ir::layout::Layout;
use ir::ty::FloatKind;
use quote;
use proc_macro2;

pub fn raw_type(ctx: &BindgenContext, name: &str) -> quote::Tokens {
pub fn raw_type(ctx: &BindgenContext, name: &str) -> proc_macro2::TokenStream {
let ident = ctx.rust_ident_raw(name);
match ctx.options().ctypes_prefix {
Some(ref prefix) => {
Expand All @@ -152,7 +147,7 @@ pub mod ast_ty {
ctx: &BindgenContext,
fk: FloatKind,
layout: Option<Layout>,
) -> quote::Tokens {
) -> proc_macro2::TokenStream {
// TODO: we probably should take the type layout into account more
// often?
//
Expand Down Expand Up @@ -192,25 +187,25 @@ pub mod ast_ty {
}
}

pub fn int_expr(val: i64) -> quote::Tokens {
pub fn int_expr(val: i64) -> proc_macro2::TokenStream {
// Don't use quote! { #val } because that adds the type suffix.
let val = proc_macro2::Literal::i64_unsuffixed(val);
quote!(#val)
}

pub fn uint_expr(val: u64) -> quote::Tokens {
pub fn uint_expr(val: u64) -> proc_macro2::TokenStream {
// Don't use quote! { #val } because that adds the type suffix.
let val = proc_macro2::Literal::u64_unsuffixed(val);
quote!(#val)
}

pub fn byte_array_expr(bytes: &[u8]) -> quote::Tokens {
pub fn byte_array_expr(bytes: &[u8]) -> proc_macro2::TokenStream {
let mut bytes: Vec<_> = bytes.iter().cloned().collect();
bytes.push(0);
quote! { [ #(#bytes),* ] }
}

pub fn cstr_expr(mut string: String) -> quote::Tokens {
pub fn cstr_expr(mut string: String) -> proc_macro2::TokenStream {
string.push('\0');
let b = proc_macro2::Literal::byte_string(&string.as_bytes());
quote! {
Expand All @@ -221,7 +216,7 @@ pub mod ast_ty {
pub fn float_expr(
ctx: &BindgenContext,
f: f64,
) -> Result<quote::Tokens, ()> {
) -> Result<proc_macro2::TokenStream, ()> {
if f.is_finite() {
let val = proc_macro2::Literal::f64_unsuffixed(f);

Expand Down Expand Up @@ -255,7 +250,7 @@ pub mod ast_ty {
pub fn arguments_from_signature(
signature: &FunctionSig,
ctx: &BindgenContext,
) -> Vec<quote::Tokens> {
) -> Vec<proc_macro2::TokenStream> {
let mut unnamed_arguments = 0;
signature
.argument_types()
Expand Down
Loading