Skip to content

Commit 81f0e9c

Browse files
authored
Merge pull request #2032 from topecongiro/trailing-whitespaces-in-macro-def
Remove trailing whitespaces in macro def
2 parents 69ab2b5 + 00f8610 commit 81f0e9c

File tree

4 files changed

+79
-14
lines changed

4 files changed

+79
-14
lines changed

src/comment.rs

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ impl FindUncommented for str {
464464
return Some(i - pat.len());
465465
}
466466
Some(c) => match kind {
467-
FullCodeCharKind::Normal if b == c => {}
467+
FullCodeCharKind::Normal | FullCodeCharKind::InString if b == c => {}
468468
_ => {
469469
needle_iter = pat.chars();
470470
}
@@ -487,7 +487,7 @@ impl FindUncommented for str {
487487
pub fn find_comment_end(s: &str) -> Option<usize> {
488488
let mut iter = CharClasses::new(s.char_indices());
489489
for (kind, (i, _c)) in &mut iter {
490-
if kind == FullCodeCharKind::Normal {
490+
if kind == FullCodeCharKind::Normal || kind == FullCodeCharKind::InString {
491491
return Some(i);
492492
}
493493
}
@@ -505,6 +505,35 @@ pub fn contains_comment(text: &str) -> bool {
505505
CharClasses::new(text.chars()).any(|(kind, _)| kind.is_comment())
506506
}
507507

508+
/// Remove trailing spaces from the specified snippet. We do not remove spaces
509+
/// inside strings or comments.
510+
pub fn remove_trailing_white_spaces(text: &str) -> String {
511+
let mut buffer = String::with_capacity(text.len());
512+
let mut space_buffer = String::with_capacity(128);
513+
for (char_kind, c) in CharClasses::new(text.chars()) {
514+
match c {
515+
'\n' => {
516+
if char_kind == FullCodeCharKind::InString {
517+
buffer.push_str(&space_buffer);
518+
}
519+
space_buffer.clear();
520+
buffer.push('\n');
521+
}
522+
_ if c.is_whitespace() => {
523+
space_buffer.push(c);
524+
}
525+
_ => {
526+
if !space_buffer.is_empty() {
527+
buffer.push_str(&space_buffer);
528+
space_buffer.clear();
529+
}
530+
buffer.push(c);
531+
}
532+
}
533+
}
534+
buffer
535+
}
536+
508537
struct CharClasses<T>
509538
where
510539
T: Iterator,
@@ -568,15 +597,17 @@ enum FullCodeCharKind {
568597
InComment,
569598
/// Last character of a comment, '\n' for a line comment, '/' for a block comment.
570599
EndComment,
600+
/// Inside a string.
601+
InString,
571602
}
572603

573604
impl FullCodeCharKind {
574605
fn is_comment(&self) -> bool {
575606
match *self {
576-
FullCodeCharKind::Normal => false,
577607
FullCodeCharKind::StartComment |
578608
FullCodeCharKind::InComment |
579609
FullCodeCharKind::EndComment => true,
610+
_ => false,
580611
}
581612
}
582613

@@ -612,21 +643,34 @@ where
612643
fn next(&mut self) -> Option<(FullCodeCharKind, T::Item)> {
613644
let item = try_opt!(self.base.next());
614645
let chr = item.get_char();
646+
let mut char_kind = FullCodeCharKind::Normal;
615647
self.status = match self.status {
616648
CharClassesStatus::LitString => match chr {
617649
'"' => CharClassesStatus::Normal,
618-
'\\' => CharClassesStatus::LitStringEscape,
619-
_ => CharClassesStatus::LitString,
650+
'\\' => {
651+
char_kind = FullCodeCharKind::InString;
652+
CharClassesStatus::LitStringEscape
653+
}
654+
_ => {
655+
char_kind = FullCodeCharKind::InString;
656+
CharClassesStatus::LitString
657+
}
620658
},
621-
CharClassesStatus::LitStringEscape => CharClassesStatus::LitString,
659+
CharClassesStatus::LitStringEscape => {
660+
char_kind = FullCodeCharKind::InString;
661+
CharClassesStatus::LitString
662+
}
622663
CharClassesStatus::LitChar => match chr {
623664
'\\' => CharClassesStatus::LitCharEscape,
624665
'\'' => CharClassesStatus::Normal,
625666
_ => CharClassesStatus::LitChar,
626667
},
627668
CharClassesStatus::LitCharEscape => CharClassesStatus::LitChar,
628669
CharClassesStatus::Normal => match chr {
629-
'"' => CharClassesStatus::LitString,
670+
'"' => {
671+
char_kind = FullCodeCharKind::InString;
672+
CharClassesStatus::LitString
673+
}
630674
'\'' => CharClassesStatus::LitChar,
631675
'/' => match self.base.peek() {
632676
Some(next) if next.get_char() == '*' => {
@@ -680,7 +724,7 @@ where
680724
}
681725
},
682726
};
683-
Some((FullCodeCharKind::Normal, item))
727+
Some((char_kind, item))
684728
}
685729
}
686730

@@ -707,9 +751,12 @@ impl<'a> Iterator for UngroupedCommentCodeSlices<'a> {
707751
fn next(&mut self) -> Option<Self::Item> {
708752
let (kind, (start_idx, _)) = try_opt!(self.iter.next());
709753
match kind {
710-
FullCodeCharKind::Normal => {
754+
FullCodeCharKind::Normal | FullCodeCharKind::InString => {
711755
// Consume all the Normal code
712-
while let Some(&(FullCodeCharKind::Normal, (_, _))) = self.iter.peek() {
756+
while let Some(&(char_kind, _)) = self.iter.peek() {
757+
if char_kind.is_comment() {
758+
break;
759+
}
713760
let _ = self.iter.next();
714761
}
715762
}
@@ -1032,7 +1079,7 @@ mod test {
10321079
fn uncommented(text: &str) -> String {
10331080
CharClasses::new(text.chars())
10341081
.filter_map(|(s, c)| match s {
1035-
FullCodeCharKind::Normal => Some(c),
1082+
FullCodeCharKind::Normal | FullCodeCharKind::InString => Some(c),
10361083
_ => None,
10371084
})
10381085
.collect()

src/visitor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use syntax::parse::ParseSess;
1919
use expr::rewrite_literal;
2020
use spanned::Spanned;
2121
use codemap::{LineRangeUtils, SpanUtils};
22-
use comment::{contains_comment, recover_missing_comment_in_span, CodeCharKind, CommentCodeSlices,
23-
FindUncommented};
22+
use comment::{contains_comment, recover_missing_comment_in_span, remove_trailing_white_spaces,
23+
CodeCharKind, CommentCodeSlices, FindUncommented};
2424
use comment::rewrite_comment;
2525
use config::{BraceStyle, Config};
2626
use items::{format_impl, format_struct, format_struct_struct, format_trait,
@@ -459,7 +459,7 @@ impl<'a> FmtVisitor<'a> {
459459
}
460460
ast::ItemKind::MacroDef(..) => {
461461
// FIXME(#1539): macros 2.0
462-
let mac_snippet = Some(self.snippet(item.span));
462+
let mac_snippet = Some(remove_trailing_white_spaces(&self.snippet(item.span)));
463463
self.push_rewrite(item.span, mac_snippet);
464464
}
465465
}

tests/source/macros.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,12 @@ fn __bindgen_test_layout_HandleWithDtor_open0_int_close0_instantiation() {
189189
);
190190
assert_eq ! ( :: std :: mem :: align_of :: < HandleWithDtor < :: std :: os :: raw :: c_int > > ( ) , 8usize , concat ! ( "Alignment of template specialization: " , stringify ! ( HandleWithDtor < :: std :: os :: raw :: c_int > ) ) );
191191
}
192+
193+
// #878
194+
macro_rules! try_opt {
195+
($expr:expr) => (match $expr {
196+
Some(val) => val,
197+
198+
None => { return None; }
199+
})
200+
}

tests/target/macros.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,12 @@ fn __bindgen_test_layout_HandleWithDtor_open0_int_close0_instantiation() {
240240
)
241241
);
242242
}
243+
244+
// #878
245+
macro_rules! try_opt {
246+
($expr:expr) => (match $expr {
247+
Some(val) => val,
248+
249+
None => { return None; }
250+
})
251+
}

0 commit comments

Comments
 (0)