Skip to content

Commit 2496a23

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 dc85e44 commit 2496a23

File tree

97 files changed

+1302
-1611
lines changed

Some content is hidden

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

97 files changed

+1302
-1611
lines changed

Cargo.lock

Lines changed: 2 additions & 20 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.24", 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 = "2.0"
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: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,48 @@
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 doc(comment: String) -> quote::Tokens {
40-
// Doc comments are already preprocessed into nice `///` formats by the
41-
// time they get here. Just make sure that we have newlines around it so
42-
// that nothing else gets wrapped into the comment.
43-
let mut tokens = quote! {};
44-
tokens.append(Term::new("\n", Span::call_site()));
45-
tokens.append(Term::new(&comment, Span::call_site()));
46-
tokens.append(Term::new("\n", Span::call_site()));
47-
tokens
38+
pub fn doc(comment: String) -> proc_macro2::TokenStream {
39+
use std::str::FromStr;
40+
41+
// NOTE(emilio): By this point comments are already preprocessed and in
42+
// `///` form. Quote turns them into `#[doc]` comments, but oh well.
43+
proc_macro2::TokenStream::from_str(&comment).unwrap()
4844
}
4945

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

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

6561
// FIXME(emilio, #412): We fall back to byte alignment, but there are
@@ -74,7 +70,7 @@ pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
7470
}
7571
};
7672

77-
let ty_name = Term::new(ty_name, Span::call_site());
73+
let ty_name = Ident::new(ty_name, Span::call_site());
7874

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

@@ -90,14 +86,14 @@ pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
9086
}
9187

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

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

10399
if ctx.options().enable_cxx_namespaces {
@@ -124,10 +120,9 @@ pub mod ast_ty {
124120
use ir::function::FunctionSig;
125121
use ir::layout::Layout;
126122
use ir::ty::FloatKind;
127-
use quote;
128123
use proc_macro2;
129124

130-
pub fn raw_type(ctx: &BindgenContext, name: &str) -> quote::Tokens {
125+
pub fn raw_type(ctx: &BindgenContext, name: &str) -> proc_macro2::TokenStream {
131126
let ident = ctx.rust_ident_raw(name);
132127
match ctx.options().ctypes_prefix {
133128
Some(ref prefix) => {
@@ -146,7 +141,7 @@ pub mod ast_ty {
146141
ctx: &BindgenContext,
147142
fk: FloatKind,
148143
layout: Option<Layout>,
149-
) -> quote::Tokens {
144+
) -> proc_macro2::TokenStream {
150145
// TODO: we probably should take the type layout into account more
151146
// often?
152147
//
@@ -186,25 +181,25 @@ pub mod ast_ty {
186181
}
187182
}
188183

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

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

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

207-
pub fn cstr_expr(mut string: String) -> quote::Tokens {
202+
pub fn cstr_expr(mut string: String) -> proc_macro2::TokenStream {
208203
string.push('\0');
209204
let b = proc_macro2::Literal::byte_string(&string.as_bytes());
210205
quote! {
@@ -215,7 +210,7 @@ pub mod ast_ty {
215210
pub fn float_expr(
216211
ctx: &BindgenContext,
217212
f: f64,
218-
) -> Result<quote::Tokens, ()> {
213+
) -> Result<proc_macro2::TokenStream, ()> {
219214
if f.is_finite() {
220215
let val = proc_macro2::Literal::f64_unsuffixed(f);
221216

@@ -249,7 +244,7 @@ pub mod ast_ty {
249244
pub fn arguments_from_signature(
250245
signature: &FunctionSig,
251246
ctx: &BindgenContext,
252-
) -> Vec<quote::Tokens> {
247+
) -> Vec<proc_macro2::TokenStream> {
253248
let mut unnamed_arguments = 0;
254249
signature
255250
.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)