Skip to content

ir: Preserve better whitespace in comments. #1384

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
Sep 13, 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
25 changes: 9 additions & 16 deletions src/ir/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ fn kind(comment: &str) -> Option<Kind> {

fn make_indent(indent: usize) -> String {
const RUST_INDENTATION: usize = 4;

iter::repeat(' ').take(indent * RUST_INDENTATION).collect()
}

Expand All @@ -49,12 +48,11 @@ fn preprocess_single_lines(comment: &str, indent: usize) -> String {
let mut is_first = true;
let lines: Vec<_> = comment
.lines()
.map(|l| l.trim_left_matches('/').trim())
.map(|l| l.trim().trim_left_matches('/'))
.map(|l| {
let indent = if is_first { "" } else { &*indent };
is_first = false;
let maybe_space = if l.is_empty() { "" } else { " " };
format!("{}///{}{}", indent, maybe_space, l)
format!("{}///{}", indent, l)
})
.collect();
lines.join("\n")
Expand All @@ -63,30 +61,24 @@ fn preprocess_single_lines(comment: &str, indent: usize) -> String {
fn preprocess_multi_line(comment: &str, indent: usize) -> String {
let comment = comment
.trim_left_matches('/')
.trim_left_matches('*')
.trim_left_matches('!')
.trim_right_matches('/')
.trim_right_matches('*')
.trim();
.trim_right_matches('*');

let indent = make_indent(indent);
// Strip any potential `*` characters preceding each line.
let mut is_first = true;
let mut lines: Vec<_> = comment.lines()
.map(|line| line.trim().trim_left_matches('*').trim())
.skip_while(|line| line.is_empty()) // Skip the first empty lines.
.map(|line| line.trim().trim_left_matches('*').trim_left_matches('!'))
.skip_while(|line| line.trim().is_empty()) // Skip the first empty lines.
.map(|line| {
let indent = if is_first { "" } else { &*indent };
is_first = false;
let maybe_space = if line.is_empty() { "" } else { " " };
format!("{}///{}{}", indent, maybe_space, line)
format!("{}///{}", indent, line)
})
.collect();

// Remove the trailing line corresponding to the `*/`.
let last_line_is_empty = lines.last().map_or(false, |l| l.is_empty());

if last_line_is_empty {
if lines.last().map_or(false, |l| l.trim().is_empty() || l.trim() == "///") {
lines.pop();
}

Expand All @@ -107,6 +99,7 @@ mod test {
fn processes_single_lines_correctly() {
assert_eq!(preprocess("/// hello", 0), "/// hello");
assert_eq!(preprocess("// hello", 0), "/// hello");
assert_eq!(preprocess("// hello", 0), "/// hello");
}

#[test]
Expand All @@ -118,7 +111,7 @@ mod test {

assert_eq!(
preprocess("/**\nhello\n*world\n*foo\n*/", 0),
"/// hello\n/// world\n/// foo"
"///hello\n///world\n///foo"
);
}
}
2 changes: 1 addition & 1 deletion tests/expectations/tests/constify-enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ pub enum nsCSSPropertyID {
eCSSProperty_b = 1,
eCSSPropertyAlias_aa = 2,
eCSSPropertyAlias_bb = 3,
/// < <div rustbindgen constant></div>
///< <div rustbindgen constant></div>
eCSSProperty_COUNT_unexistingVariantValue = 4,
}
15 changes: 9 additions & 6 deletions tests/expectations/tests/convert-cpp-comment-to-rust.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]

#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

pub type mbedtls_mpi_uint = ::std::os::raw::c_uint;
/// \brief MPI structure
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct mbedtls_mpi {
/// < integer sign
///< integer sign
pub s: ::std::os::raw::c_int,
/// < total # of limbs
///< total # of limbs
pub n: ::std::os::raw::c_ulong,
/// < pointer to limbs
///< pointer to limbs
pub p: *mut mbedtls_mpi_uint,
}
#[test]
Expand Down
11 changes: 8 additions & 3 deletions tests/expectations/tests/enum-doc-bitfield.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* automatically generated by rust-bindgen */

#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

impl B {
/// Document field with three slashes
Expand All @@ -15,11 +20,11 @@ impl B {
pub const VAR_C: B = B(2);
}
impl B {
/// < Document field with following star
///< Document field with following star
pub const VAR_D: B = B(3);
}
impl B {
/// < Document field with following exclamation
///< Document field with following exclamation
pub const VAR_E: B = B(4);
}
impl B {
Expand Down
11 changes: 8 additions & 3 deletions tests/expectations/tests/enum-doc-mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* automatically generated by rust-bindgen */

#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

pub mod B {
/// Document enum
Expand All @@ -11,9 +16,9 @@ pub mod B {
pub const VAR_B: Type = 1;
/// Document field with preceeding exclamation
pub const VAR_C: Type = 2;
/// < Document field with following star
///< Document field with following star
pub const VAR_D: Type = 3;
/// < Document field with following exclamation
///< Document field with following exclamation
pub const VAR_E: Type = 4;
/// Document field with preceeding star, with a loong long multiline
/// comment.
Expand Down
11 changes: 8 additions & 3 deletions tests/expectations/tests/enum-doc-rusty.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* automatically generated by rust-bindgen */

#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

#[repr(u32)]
/// Document enum
Expand All @@ -12,9 +17,9 @@ pub enum B {
VAR_B = 1,
/// Document field with preceeding exclamation
VAR_C = 2,
/// < Document field with following star
///< Document field with following star
VAR_D = 3,
/// < Document field with following exclamation
///< Document field with following exclamation
VAR_E = 4,
/// Document field with preceeding star, with a loong long multiline
/// comment.
Expand Down
11 changes: 8 additions & 3 deletions tests/expectations/tests/enum-doc.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
/* automatically generated by rust-bindgen */

#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

/// Document field with three slashes
pub const B_VAR_A: B = 0;
/// Document field with preceeding star
pub const B_VAR_B: B = 1;
/// Document field with preceeding exclamation
pub const B_VAR_C: B = 2;
/// < Document field with following star
///< Document field with following star
pub const B_VAR_D: B = 3;
/// < Document field with following exclamation
///< Document field with following exclamation
pub const B_VAR_E: B = 4;
/// Document field with preceeding star, with a loong long multiline
/// comment.
Expand Down
19 changes: 12 additions & 7 deletions tests/expectations/tests/layout_align.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* automatically generated by rust-bindgen */

#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down Expand Up @@ -140,15 +145,15 @@ impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {}
#[repr(C)]
#[derive(Debug)]
pub struct rte_kni_fifo {
/// < Next position to be written
///< Next position to be written
pub write: ::std::os::raw::c_uint,
/// < Next position to be read
///< Next position to be read
pub read: ::std::os::raw::c_uint,
/// < Circular buffer length
///< Circular buffer length
pub len: ::std::os::raw::c_uint,
/// < Pointer size - for 32/64 bit OS
///< Pointer size - for 32/64 bit OS
pub elem_size: ::std::os::raw::c_uint,
/// < The buffer contains mbuf pointers
///< The buffer contains mbuf pointers
pub buffer: __IncompleteArrayField<*mut ::std::os::raw::c_void>,
pub __bindgen_align: [u64; 0usize],
}
Expand All @@ -173,7 +178,7 @@ impl Default for rte_kni_fifo {
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct rte_eth_link {
/// < ETH_SPEED_NUM_
///< ETH_SPEED_NUM_
pub link_speed: u32,
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize], u8>,
pub __bindgen_padding_0: [u8; 3usize],
Expand Down
17 changes: 11 additions & 6 deletions tests/expectations/tests/layout_arp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* automatically generated by rust-bindgen */

#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

pub const ETHER_ADDR_LEN: u32 = 6;
pub const ARP_HRD_ETHER: u32 = 1;
Expand All @@ -22,7 +27,7 @@ pub const ARP_OP_INVREPLY: u32 = 9;
#[repr(C, packed)]
#[derive(Debug, Default, Copy, Clone)]
pub struct ether_addr {
/// < Addr bytes in tx order
///< Addr bytes in tx order
pub addr_bytes: [u8; 6usize],
}
#[test]
Expand Down Expand Up @@ -52,13 +57,13 @@ fn bindgen_test_layout_ether_addr() {
#[repr(C, packed)]
#[derive(Debug, Default, Copy, Clone)]
pub struct arp_ipv4 {
/// < sender hardware address
///< sender hardware address
pub arp_sha: ether_addr,
/// < sender IP address
///< sender IP address
pub arp_sip: u32,
/// < target hardware address
///< target hardware address
pub arp_tha: ether_addr,
/// < target IP address
///< target IP address
pub arp_tip: u32,
}
#[test]
Expand Down
38 changes: 24 additions & 14 deletions tests/expectations/tests/layout_array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* automatically generated by rust-bindgen */

#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

pub const RTE_CACHE_LINE_SIZE: u32 = 64;
pub const RTE_MEMPOOL_OPS_NAMESIZE: u32 = 32;
Expand Down Expand Up @@ -46,17 +51,17 @@ pub type rte_mempool_get_count =
#[repr(C)]
#[derive(Copy, Clone)]
pub struct rte_mempool_ops {
/// < Name of mempool ops struct.
///< Name of mempool ops struct.
pub name: [::std::os::raw::c_char; 32usize],
/// < Allocate private data.
///< Allocate private data.
pub alloc: rte_mempool_alloc_t,
/// < Free the external pool.
///< Free the external pool.
pub free: rte_mempool_free_t,
/// < Enqueue an object.
///< Enqueue an object.
pub enqueue: rte_mempool_enqueue_t,
/// < Dequeue an object.
///< Dequeue an object.
pub dequeue: rte_mempool_dequeue_t,
/// < Get qty of available objs.
///< Get qty of available objs.
pub get_count: rte_mempool_get_count,
pub __bindgen_padding_0: [u64; 7usize],
}
Expand Down Expand Up @@ -135,16 +140,19 @@ impl Default for rte_mempool_ops {
}
impl ::std::cmp::PartialEq for rte_mempool_ops {
fn eq(&self, other: &rte_mempool_ops) -> bool {
self.name == other.name && self.alloc == other.alloc && self.free == other.free
&& self.enqueue == other.enqueue && self.dequeue == other.dequeue
self.name == other.name
&& self.alloc == other.alloc
&& self.free == other.free
&& self.enqueue == other.enqueue
&& self.dequeue == other.dequeue
&& self.get_count == other.get_count
}
}
/// The rte_spinlock_t type.
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
pub struct rte_spinlock_t {
/// < lock status 0 = unlocked, 1 = locked
///< lock status 0 = unlocked, 1 = locked
pub locked: ::std::os::raw::c_int,
}
#[test]
Expand Down Expand Up @@ -180,9 +188,9 @@ fn bindgen_test_layout_rte_spinlock_t() {
#[repr(C)]
#[derive(Copy, Clone)]
pub struct rte_mempool_ops_table {
/// < Spinlock for add/delete.
///< Spinlock for add/delete.
pub sl: rte_spinlock_t,
/// < Number of used ops structs in the table.
///< Number of used ops structs in the table.
pub num_ops: u32,
pub __bindgen_padding_0: [u64; 7usize],
/// Storage for all possible ops structs.
Expand Down Expand Up @@ -330,8 +338,10 @@ impl Default for malloc_heap {
}
impl ::std::cmp::PartialEq for malloc_heap {
fn eq(&self, other: &malloc_heap) -> bool {
self.lock == other.lock && self.free_head == other.free_head
&& self.alloc_count == other.alloc_count && self.total_size == other.total_size
self.lock == other.lock
&& self.free_head == other.free_head
&& self.alloc_count == other.alloc_count
&& self.total_size == other.total_size
}
}
#[repr(C)]
Expand Down
Loading