Skip to content

Commit d00b54c

Browse files
author
bors-servo
authored
Auto merge of #1384 - emilio:whitespace-comment, r=fitzgen
ir: Preserve better whitespace in comments. Fixes #1341.
2 parents 83751c8 + 7ad57a4 commit d00b54c

17 files changed

+429
-364
lines changed

src/ir/comment.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ fn kind(comment: &str) -> Option<Kind> {
3535

3636
fn make_indent(indent: usize) -> String {
3737
const RUST_INDENTATION: usize = 4;
38-
3938
iter::repeat(' ').take(indent * RUST_INDENTATION).collect()
4039
}
4140

@@ -49,12 +48,11 @@ fn preprocess_single_lines(comment: &str, indent: usize) -> String {
4948
let mut is_first = true;
5049
let lines: Vec<_> = comment
5150
.lines()
52-
.map(|l| l.trim_left_matches('/').trim())
51+
.map(|l| l.trim().trim_left_matches('/'))
5352
.map(|l| {
5453
let indent = if is_first { "" } else { &*indent };
5554
is_first = false;
56-
let maybe_space = if l.is_empty() { "" } else { " " };
57-
format!("{}///{}{}", indent, maybe_space, l)
55+
format!("{}///{}", indent, l)
5856
})
5957
.collect();
6058
lines.join("\n")
@@ -63,30 +61,24 @@ fn preprocess_single_lines(comment: &str, indent: usize) -> String {
6361
fn preprocess_multi_line(comment: &str, indent: usize) -> String {
6462
let comment = comment
6563
.trim_left_matches('/')
66-
.trim_left_matches('*')
67-
.trim_left_matches('!')
6864
.trim_right_matches('/')
69-
.trim_right_matches('*')
70-
.trim();
65+
.trim_right_matches('*');
7166

7267
let indent = make_indent(indent);
7368
// Strip any potential `*` characters preceding each line.
7469
let mut is_first = true;
7570
let mut lines: Vec<_> = comment.lines()
76-
.map(|line| line.trim().trim_left_matches('*').trim())
77-
.skip_while(|line| line.is_empty()) // Skip the first empty lines.
71+
.map(|line| line.trim().trim_left_matches('*').trim_left_matches('!'))
72+
.skip_while(|line| line.trim().is_empty()) // Skip the first empty lines.
7873
.map(|line| {
7974
let indent = if is_first { "" } else { &*indent };
8075
is_first = false;
81-
let maybe_space = if line.is_empty() { "" } else { " " };
82-
format!("{}///{}{}", indent, maybe_space, line)
76+
format!("{}///{}", indent, line)
8377
})
8478
.collect();
8579

8680
// Remove the trailing line corresponding to the `*/`.
87-
let last_line_is_empty = lines.last().map_or(false, |l| l.is_empty());
88-
89-
if last_line_is_empty {
81+
if lines.last().map_or(false, |l| l.trim().is_empty() || l.trim() == "///") {
9082
lines.pop();
9183
}
9284

@@ -107,6 +99,7 @@ mod test {
10799
fn processes_single_lines_correctly() {
108100
assert_eq!(preprocess("/// hello", 0), "/// hello");
109101
assert_eq!(preprocess("// hello", 0), "/// hello");
102+
assert_eq!(preprocess("// hello", 0), "/// hello");
110103
}
111104

112105
#[test]
@@ -118,7 +111,7 @@ mod test {
118111

119112
assert_eq!(
120113
preprocess("/**\nhello\n*world\n*foo\n*/", 0),
121-
"/// hello\n/// world\n/// foo"
114+
"///hello\n///world\n///foo"
122115
);
123116
}
124117
}

tests/expectations/tests/constify-enum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ pub enum nsCSSPropertyID {
1919
eCSSProperty_b = 1,
2020
eCSSPropertyAlias_aa = 2,
2121
eCSSPropertyAlias_bb = 3,
22-
/// < <div rustbindgen constant></div>
22+
///< <div rustbindgen constant></div>
2323
eCSSProperty_COUNT_unexistingVariantValue = 4,
2424
}

tests/expectations/tests/convert-cpp-comment-to-rust.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
/* automatically generated by rust-bindgen */
22

3-
4-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5-
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
69

710
pub type mbedtls_mpi_uint = ::std::os::raw::c_uint;
811
/// \brief MPI structure
912
#[repr(C)]
1013
#[derive(Debug, Copy, Clone)]
1114
pub struct mbedtls_mpi {
12-
/// < integer sign
15+
///< integer sign
1316
pub s: ::std::os::raw::c_int,
14-
/// < total # of limbs
17+
///< total # of limbs
1518
pub n: ::std::os::raw::c_ulong,
16-
/// < pointer to limbs
19+
///< pointer to limbs
1720
pub p: *mut mbedtls_mpi_uint,
1821
}
1922
#[test]

tests/expectations/tests/enum-doc-bitfield.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
impl B {
611
/// Document field with three slashes
@@ -15,11 +20,11 @@ impl B {
1520
pub const VAR_C: B = B(2);
1621
}
1722
impl B {
18-
/// < Document field with following star
23+
///< Document field with following star
1924
pub const VAR_D: B = B(3);
2025
}
2126
impl B {
22-
/// < Document field with following exclamation
27+
///< Document field with following exclamation
2328
pub const VAR_E: B = B(4);
2429
}
2530
impl B {

tests/expectations/tests/enum-doc-mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
pub mod B {
611
/// Document enum
@@ -11,9 +16,9 @@ pub mod B {
1116
pub const VAR_B: Type = 1;
1217
/// Document field with preceeding exclamation
1318
pub const VAR_C: Type = 2;
14-
/// < Document field with following star
19+
///< Document field with following star
1520
pub const VAR_D: Type = 3;
16-
/// < Document field with following exclamation
21+
///< Document field with following exclamation
1722
pub const VAR_E: Type = 4;
1823
/// Document field with preceeding star, with a loong long multiline
1924
/// comment.

tests/expectations/tests/enum-doc-rusty.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
#[repr(u32)]
611
/// Document enum
@@ -12,9 +17,9 @@ pub enum B {
1217
VAR_B = 1,
1318
/// Document field with preceeding exclamation
1419
VAR_C = 2,
15-
/// < Document field with following star
20+
///< Document field with following star
1621
VAR_D = 3,
17-
/// < Document field with following exclamation
22+
///< Document field with following exclamation
1823
VAR_E = 4,
1924
/// Document field with preceeding star, with a loong long multiline
2025
/// comment.

tests/expectations/tests/enum-doc.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
/// Document field with three slashes
611
pub const B_VAR_A: B = 0;
712
/// Document field with preceeding star
813
pub const B_VAR_B: B = 1;
914
/// Document field with preceeding exclamation
1015
pub const B_VAR_C: B = 2;
11-
/// < Document field with following star
16+
///< Document field with following star
1217
pub const B_VAR_D: B = 3;
13-
/// < Document field with following exclamation
18+
///< Document field with following exclamation
1419
pub const B_VAR_E: B = 4;
1520
/// Document field with preceeding star, with a loong long multiline
1621
/// comment.

tests/expectations/tests/layout_align.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
#[repr(C)]
611
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
@@ -140,15 +145,15 @@ impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {}
140145
#[repr(C)]
141146
#[derive(Debug)]
142147
pub struct rte_kni_fifo {
143-
/// < Next position to be written
148+
///< Next position to be written
144149
pub write: ::std::os::raw::c_uint,
145-
/// < Next position to be read
150+
///< Next position to be read
146151
pub read: ::std::os::raw::c_uint,
147-
/// < Circular buffer length
152+
///< Circular buffer length
148153
pub len: ::std::os::raw::c_uint,
149-
/// < Pointer size - for 32/64 bit OS
154+
///< Pointer size - for 32/64 bit OS
150155
pub elem_size: ::std::os::raw::c_uint,
151-
/// < The buffer contains mbuf pointers
156+
///< The buffer contains mbuf pointers
152157
pub buffer: __IncompleteArrayField<*mut ::std::os::raw::c_void>,
153158
pub __bindgen_align: [u64; 0usize],
154159
}
@@ -173,7 +178,7 @@ impl Default for rte_kni_fifo {
173178
#[repr(C)]
174179
#[derive(Debug, Default, Copy, Clone)]
175180
pub struct rte_eth_link {
176-
/// < ETH_SPEED_NUM_
181+
///< ETH_SPEED_NUM_
177182
pub link_speed: u32,
178183
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize], u8>,
179184
pub __bindgen_padding_0: [u8; 3usize],

tests/expectations/tests/layout_arp.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
pub const ETHER_ADDR_LEN: u32 = 6;
611
pub const ARP_HRD_ETHER: u32 = 1;
@@ -22,7 +27,7 @@ pub const ARP_OP_INVREPLY: u32 = 9;
2227
#[repr(C, packed)]
2328
#[derive(Debug, Default, Copy, Clone)]
2429
pub struct ether_addr {
25-
/// < Addr bytes in tx order
30+
///< Addr bytes in tx order
2631
pub addr_bytes: [u8; 6usize],
2732
}
2833
#[test]
@@ -52,13 +57,13 @@ fn bindgen_test_layout_ether_addr() {
5257
#[repr(C, packed)]
5358
#[derive(Debug, Default, Copy, Clone)]
5459
pub struct arp_ipv4 {
55-
/// < sender hardware address
60+
///< sender hardware address
5661
pub arp_sha: ether_addr,
57-
/// < sender IP address
62+
///< sender IP address
5863
pub arp_sip: u32,
59-
/// < target hardware address
64+
///< target hardware address
6065
pub arp_tha: ether_addr,
61-
/// < target IP address
66+
///< target IP address
6267
pub arp_tip: u32,
6368
}
6469
#[test]

tests/expectations/tests/layout_array.rs

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
pub const RTE_CACHE_LINE_SIZE: u32 = 64;
611
pub const RTE_MEMPOOL_OPS_NAMESIZE: u32 = 32;
@@ -46,17 +51,17 @@ pub type rte_mempool_get_count =
4651
#[repr(C)]
4752
#[derive(Copy, Clone)]
4853
pub struct rte_mempool_ops {
49-
/// < Name of mempool ops struct.
54+
///< Name of mempool ops struct.
5055
pub name: [::std::os::raw::c_char; 32usize],
51-
/// < Allocate private data.
56+
///< Allocate private data.
5257
pub alloc: rte_mempool_alloc_t,
53-
/// < Free the external pool.
58+
///< Free the external pool.
5459
pub free: rte_mempool_free_t,
55-
/// < Enqueue an object.
60+
///< Enqueue an object.
5661
pub enqueue: rte_mempool_enqueue_t,
57-
/// < Dequeue an object.
62+
///< Dequeue an object.
5863
pub dequeue: rte_mempool_dequeue_t,
59-
/// < Get qty of available objs.
64+
///< Get qty of available objs.
6065
pub get_count: rte_mempool_get_count,
6166
pub __bindgen_padding_0: [u64; 7usize],
6267
}
@@ -135,16 +140,19 @@ impl Default for rte_mempool_ops {
135140
}
136141
impl ::std::cmp::PartialEq for rte_mempool_ops {
137142
fn eq(&self, other: &rte_mempool_ops) -> bool {
138-
self.name == other.name && self.alloc == other.alloc && self.free == other.free
139-
&& self.enqueue == other.enqueue && self.dequeue == other.dequeue
143+
self.name == other.name
144+
&& self.alloc == other.alloc
145+
&& self.free == other.free
146+
&& self.enqueue == other.enqueue
147+
&& self.dequeue == other.dequeue
140148
&& self.get_count == other.get_count
141149
}
142150
}
143151
/// The rte_spinlock_t type.
144152
#[repr(C)]
145153
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
146154
pub struct rte_spinlock_t {
147-
/// < lock status 0 = unlocked, 1 = locked
155+
///< lock status 0 = unlocked, 1 = locked
148156
pub locked: ::std::os::raw::c_int,
149157
}
150158
#[test]
@@ -180,9 +188,9 @@ fn bindgen_test_layout_rte_spinlock_t() {
180188
#[repr(C)]
181189
#[derive(Copy, Clone)]
182190
pub struct rte_mempool_ops_table {
183-
/// < Spinlock for add/delete.
191+
///< Spinlock for add/delete.
184192
pub sl: rte_spinlock_t,
185-
/// < Number of used ops structs in the table.
193+
///< Number of used ops structs in the table.
186194
pub num_ops: u32,
187195
pub __bindgen_padding_0: [u64; 7usize],
188196
/// Storage for all possible ops structs.
@@ -330,8 +338,10 @@ impl Default for malloc_heap {
330338
}
331339
impl ::std::cmp::PartialEq for malloc_heap {
332340
fn eq(&self, other: &malloc_heap) -> bool {
333-
self.lock == other.lock && self.free_head == other.free_head
334-
&& self.alloc_count == other.alloc_count && self.total_size == other.total_size
341+
self.lock == other.lock
342+
&& self.free_head == other.free_head
343+
&& self.alloc_count == other.alloc_count
344+
&& self.total_size == other.total_size
335345
}
336346
}
337347
#[repr(C)]

0 commit comments

Comments
 (0)