Skip to content

Commit 631871c

Browse files
Eijebongemilio
authored andcommitted
Update quote and proc-macro.
I give up on the doc comments. This is a rebase of rust-lang#1334 keeping the formatting of the comments and using TokenStream::from_str instead because one can hope. Fixes rust-lang#1407.
1 parent 33912cf commit 631871c

File tree

100 files changed

+1311
-1602
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1311
-1602
lines changed

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ clap = "2"
5151
clang-sys = { version = "0.26", features = ["runtime", "clang_6_0"] }
5252
lazy_static = "1"
5353
peeking_take_while = "0.1.2"
54-
quote = { version = "0.5", default-features = false }
54+
quote = { version = "0.6", default-features = false }
5555
regex = "1.0"
5656
which = "1.0.2"
5757
# New validation in 0.3.6 breaks bindgen-integration:
5858
# https://github.com/alexcrichton/proc-macro2/commit/489c642.
59-
proc-macro2 = { version = "0.3.2, < 0.3.6", default-features = false }
59+
proc-macro2 = { version = "0.4", default-features = false }
6060

6161
[dependencies.env_logger]
6262
optional = true

src/codegen/helpers.rs

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,54 @@
22
33
use ir::context::BindgenContext;
44
use ir::layout::Layout;
5-
use quote;
6-
use proc_macro2::{Term, Span};
5+
use proc_macro2::{self, Ident, Span};
6+
use quote::TokenStreamExt;
77

88
pub mod attributes {
9-
use quote;
10-
use proc_macro2::{Term, Span};
9+
use proc_macro2::{self, Ident, Span};
1110

12-
pub fn repr(which: &str) -> quote::Tokens {
13-
let which = Term::new(which, Span::call_site());
11+
pub fn repr(which: &str) -> proc_macro2::TokenStream {
12+
let which = Ident::new(which, Span::call_site());
1413
quote! {
1514
#[repr( #which )]
1615
}
1716
}
1817

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

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

33-
pub fn inline() -> quote::Tokens {
32+
pub fn inline() -> proc_macro2::TokenStream {
3433
quote! {
3534
#[inline]
3635
}
3736
}
3837

39-
pub fn must_use() -> quote::Tokens {
38+
pub fn must_use() -> proc_macro2::TokenStream {
4039
quote! {
4140
#[must_use]
4241
}
4342
}
4443

45-
pub fn doc(comment: String) -> quote::Tokens {
46-
// Doc comments are already preprocessed into nice `///` formats by the
47-
// time they get here. Just make sure that we have newlines around it so
48-
// that nothing else gets wrapped into the comment.
49-
let mut tokens = quote! {};
50-
tokens.append(Term::new("\n", Span::call_site()));
51-
tokens.append(Term::new(&comment, Span::call_site()));
52-
tokens.append(Term::new("\n", Span::call_site()));
53-
tokens
44+
pub fn doc(comment: String) -> proc_macro2::TokenStream {
45+
use std::str::FromStr;
46+
47+
// NOTE(emilio): By this point comments are already preprocessed and in
48+
// `///` form. Quote turns them into `#[doc]` comments, but oh well.
49+
proc_macro2::TokenStream::from_str(&comment).unwrap()
5450
}
5551

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

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

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

83-
let ty_name = Term::new(ty_name, Span::call_site());
79+
let ty_name = Ident::new(ty_name, Span::call_site());
8480

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

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

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

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

109105
if ctx.options().enable_cxx_namespaces {
@@ -130,10 +126,9 @@ pub mod ast_ty {
130126
use ir::function::FunctionSig;
131127
use ir::layout::Layout;
132128
use ir::ty::FloatKind;
133-
use quote;
134129
use proc_macro2;
135130

136-
pub fn raw_type(ctx: &BindgenContext, name: &str) -> quote::Tokens {
131+
pub fn raw_type(ctx: &BindgenContext, name: &str) -> proc_macro2::TokenStream {
137132
let ident = ctx.rust_ident_raw(name);
138133
match ctx.options().ctypes_prefix {
139134
Some(ref prefix) => {
@@ -152,7 +147,7 @@ pub mod ast_ty {
152147
ctx: &BindgenContext,
153148
fk: FloatKind,
154149
layout: Option<Layout>,
155-
) -> quote::Tokens {
150+
) -> proc_macro2::TokenStream {
156151
// TODO: we probably should take the type layout into account more
157152
// often?
158153
//
@@ -192,25 +187,25 @@ pub mod ast_ty {
192187
}
193188
}
194189

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

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

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

213-
pub fn cstr_expr(mut string: String) -> quote::Tokens {
208+
pub fn cstr_expr(mut string: String) -> proc_macro2::TokenStream {
214209
string.push('\0');
215210
let b = proc_macro2::Literal::byte_string(&string.as_bytes());
216211
quote! {
@@ -221,7 +216,7 @@ pub mod ast_ty {
221216
pub fn float_expr(
222217
ctx: &BindgenContext,
223218
f: f64,
224-
) -> Result<quote::Tokens, ()> {
219+
) -> Result<proc_macro2::TokenStream, ()> {
225220
if f.is_finite() {
226221
let val = proc_macro2::Literal::f64_unsuffixed(f);
227222

@@ -255,7 +250,7 @@ pub mod ast_ty {
255250
pub fn arguments_from_signature(
256251
signature: &FunctionSig,
257252
ctx: &BindgenContext,
258-
) -> Vec<quote::Tokens> {
253+
) -> Vec<proc_macro2::TokenStream> {
259254
let mut unnamed_arguments = 0;
260255
signature
261256
.argument_types()

src/codegen/impl_debug.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use ir::context::BindgenContext;
33
use ir::derive::CanTriviallyDeriveDebug;
44
use ir::item::{HasTypeParamInArray, IsOpaque, Item, ItemCanonicalName};
55
use ir::ty::{RUST_DERIVE_IN_ARRAY_LIMIT, TypeKind};
6-
use quote;
6+
use proc_macro2;
77

88
pub fn gen_debug_impl(
99
ctx: &BindgenContext,
1010
fields: &[Field],
1111
item: &Item,
1212
kind: CompKind,
13-
) -> quote::Tokens {
13+
) -> proc_macro2::TokenStream {
1414
let struct_name = item.canonical_name(ctx);
1515
let mut format_string = format!("{} {{{{ ", struct_name);
1616
let mut tokens = vec![];
@@ -63,7 +63,7 @@ pub trait ImplDebug<'a> {
6363
&self,
6464
ctx: &BindgenContext,
6565
extra: Self::Extra,
66-
) -> Option<(String, Vec<quote::Tokens>)>;
66+
) -> Option<(String, Vec<proc_macro2::TokenStream>)>;
6767
}
6868

6969
impl<'a> ImplDebug<'a> for FieldData {
@@ -73,7 +73,7 @@ impl<'a> ImplDebug<'a> for FieldData {
7373
&self,
7474
ctx: &BindgenContext,
7575
_: Self::Extra,
76-
) -> Option<(String, Vec<quote::Tokens>)> {
76+
) -> Option<(String, Vec<proc_macro2::TokenStream>)> {
7777
if let Some(name) = self.name() {
7878
ctx.resolve_item(self.ty()).impl_debug(ctx, name)
7979
} else {
@@ -89,7 +89,7 @@ impl<'a> ImplDebug<'a> for BitfieldUnit {
8989
&self,
9090
ctx: &BindgenContext,
9191
_: Self::Extra,
92-
) -> Option<(String, Vec<quote::Tokens>)> {
92+
) -> Option<(String, Vec<proc_macro2::TokenStream>)> {
9393
let mut format_string = String::new();
9494
let mut tokens = vec![];
9595
for (i, bitfield) in self.bitfields().iter().enumerate() {
@@ -118,7 +118,7 @@ impl<'a> ImplDebug<'a> for Item {
118118
&self,
119119
ctx: &BindgenContext,
120120
name: &str,
121-
) -> Option<(String, Vec<quote::Tokens>)> {
121+
) -> Option<(String, Vec<proc_macro2::TokenStream>)> {
122122
let name_ident = ctx.rust_ident(name);
123123

124124
// We don't know if blacklisted items `impl Debug` or not, so we can't
@@ -136,8 +136,8 @@ impl<'a> ImplDebug<'a> for Item {
136136

137137
fn debug_print(
138138
name: &str,
139-
name_ident: quote::Tokens,
140-
) -> Option<(String, Vec<quote::Tokens>)> {
139+
name_ident: proc_macro2::TokenStream,
140+
) -> Option<(String, Vec<proc_macro2::TokenStream>)> {
141141
Some((
142142
format!("{}: {{:?}}", name),
143143
vec![quote! {

src/codegen/impl_partialeq.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use ir::comp::{CompInfo, CompKind, Field, FieldMethods};
33
use ir::context::BindgenContext;
44
use ir::item::{IsOpaque, Item};
55
use ir::ty::{TypeKind, RUST_DERIVE_IN_ARRAY_LIMIT};
6-
use quote;
76
use proc_macro2;
87

98
/// Generate a manual implementation of `PartialEq` trait for the
@@ -12,8 +11,8 @@ pub fn gen_partialeq_impl(
1211
ctx: &BindgenContext,
1312
comp_info: &CompInfo,
1413
item: &Item,
15-
ty_for_impl: &quote::Tokens,
16-
) -> Option<quote::Tokens> {
14+
ty_for_impl: &proc_macro2::TokenStream,
15+
) -> Option<proc_macro2::TokenStream> {
1716
let mut tokens = vec![];
1817

1918
if item.is_opaque(ctx, &()) {
@@ -71,8 +70,8 @@ pub fn gen_partialeq_impl(
7170
})
7271
}
7372

74-
fn gen_field(ctx: &BindgenContext, ty_item: &Item, name: &str) -> quote::Tokens {
75-
fn quote_equals(name_ident: proc_macro2::Term) -> quote::Tokens {
73+
fn gen_field(ctx: &BindgenContext, ty_item: &Item, name: &str) -> proc_macro2::TokenStream {
74+
fn quote_equals(name_ident: proc_macro2::Ident) -> proc_macro2::TokenStream {
7675
quote! { self.#name_ident == other.#name_ident }
7776
}
7877

0 commit comments

Comments
 (0)