• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // High level formatting functions.
2 
3 use std::collections::HashMap;
4 use std::io::{self, Write};
5 use std::time::{Duration, Instant};
6 
7 use rustc_ast::ast;
8 use rustc_span::Span;
9 
10 use self::newline_style::apply_newline_style;
11 use crate::comment::{CharClasses, FullCodeCharKind};
12 use crate::config::{Config, FileName, Verbosity};
13 use crate::formatting::generated::is_generated_file;
14 use crate::modules::Module;
15 use crate::parse::parser::{DirectoryOwnership, Parser, ParserError};
16 use crate::parse::session::ParseSess;
17 use crate::utils::{contains_skip, count_newlines};
18 use crate::visitor::FmtVisitor;
19 use crate::{modules, source_file, ErrorKind, FormatReport, Input, Session};
20 
21 mod generated;
22 mod newline_style;
23 
24 // A map of the files of a crate, with their new content
25 pub(crate) type SourceFile = Vec<FileRecord>;
26 pub(crate) type FileRecord = (FileName, String);
27 
28 impl<'b, T: Write + 'b> Session<'b, T> {
format_input_inner( &mut self, input: Input, is_macro_def: bool, ) -> Result<FormatReport, ErrorKind>29     pub(crate) fn format_input_inner(
30         &mut self,
31         input: Input,
32         is_macro_def: bool,
33     ) -> Result<FormatReport, ErrorKind> {
34         if !self.config.version_meets_requirement() {
35             return Err(ErrorKind::VersionMismatch);
36         }
37 
38         rustc_span::create_session_if_not_set_then(self.config.edition().into(), |_| {
39             if self.config.disable_all_formatting() {
40                 // When the input is from stdin, echo back the input.
41                 return match input {
42                     Input::Text(ref buf) => echo_back_stdin(buf),
43                     _ => Ok(FormatReport::new()),
44                 };
45             }
46 
47             let config = &self.config.clone();
48             let format_result = format_project(input, config, self, is_macro_def);
49 
50             format_result.map(|report| {
51                 self.errors.add(&report.internal.borrow().1);
52                 report
53             })
54         })
55     }
56 }
57 
58 /// Determine if a module should be skipped. True if the module should be skipped, false otherwise.
should_skip_module<T: FormatHandler>( config: &Config, context: &FormatContext<'_, T>, input_is_stdin: bool, main_file: &FileName, path: &FileName, module: &Module<'_>, ) -> bool59 fn should_skip_module<T: FormatHandler>(
60     config: &Config,
61     context: &FormatContext<'_, T>,
62     input_is_stdin: bool,
63     main_file: &FileName,
64     path: &FileName,
65     module: &Module<'_>,
66 ) -> bool {
67     if contains_skip(module.attrs()) {
68         return true;
69     }
70 
71     if config.skip_children() && path != main_file {
72         return true;
73     }
74 
75     if !input_is_stdin && context.ignore_file(path) {
76         return true;
77     }
78 
79     // FIXME(calebcartwright) - we need to determine how we'll handle the
80     // `format_generated_files` option with stdin based input.
81     if !input_is_stdin && !config.format_generated_files() {
82         let source_file = context.parse_session.span_to_file_contents(module.span);
83         let src = source_file.src.as_ref().expect("SourceFile without src");
84 
85         if is_generated_file(src) {
86             return true;
87         }
88     }
89 
90     false
91 }
92 
echo_back_stdin(input: &str) -> Result<FormatReport, ErrorKind>93 fn echo_back_stdin(input: &str) -> Result<FormatReport, ErrorKind> {
94     if let Err(e) = io::stdout().write_all(input.as_bytes()) {
95         return Err(From::from(e));
96     }
97     Ok(FormatReport::new())
98 }
99 
100 // Format an entire crate (or subset of the module tree).
format_project<T: FormatHandler>( input: Input, config: &Config, handler: &mut T, is_macro_def: bool, ) -> Result<FormatReport, ErrorKind>101 fn format_project<T: FormatHandler>(
102     input: Input,
103     config: &Config,
104     handler: &mut T,
105     is_macro_def: bool,
106 ) -> Result<FormatReport, ErrorKind> {
107     let mut timer = Timer::start();
108 
109     let main_file = input.file_name();
110     let input_is_stdin = main_file == FileName::Stdin;
111 
112     let parse_session = ParseSess::new(config)?;
113     if config.skip_children() && parse_session.ignore_file(&main_file) {
114         return Ok(FormatReport::new());
115     }
116 
117     // Parse the crate.
118     let mut report = FormatReport::new();
119     let directory_ownership = input.to_directory_ownership();
120     let krate = match Parser::parse_crate(input, &parse_session) {
121         Ok(krate) => krate,
122         // Surface parse error via Session (errors are merged there from report)
123         Err(e) => {
124             let forbid_verbose = input_is_stdin || e != ParserError::ParsePanicError;
125             should_emit_verbose(forbid_verbose, config, || {
126                 eprintln!("The Rust parser panicked");
127             });
128             report.add_parsing_error();
129             return Ok(report);
130         }
131     };
132 
133     let mut context = FormatContext::new(&krate, report, parse_session, config, handler);
134     let files = modules::ModResolver::new(
135         &context.parse_session,
136         directory_ownership.unwrap_or(DirectoryOwnership::UnownedViaBlock),
137         !input_is_stdin && !config.skip_children(),
138     )
139     .visit_crate(&krate)?
140     .into_iter()
141     .filter(|(path, module)| {
142         input_is_stdin
143             || !should_skip_module(config, &context, input_is_stdin, &main_file, path, module)
144     })
145     .collect::<Vec<_>>();
146 
147     timer = timer.done_parsing();
148 
149     // Suppress error output if we have to do any further parsing.
150     context.parse_session.set_silent_emitter();
151 
152     for (path, module) in files {
153         if input_is_stdin && contains_skip(module.attrs()) {
154             return echo_back_stdin(
155                 context
156                     .parse_session
157                     .snippet_provider(module.span)
158                     .entire_snippet(),
159             );
160         }
161         should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
162         context.format_file(path, &module, is_macro_def)?;
163     }
164     timer = timer.done_formatting();
165 
166     should_emit_verbose(input_is_stdin, config, || {
167         println!(
168             "Spent {0:.3} secs in the parsing phase, and {1:.3} secs in the formatting phase",
169             timer.get_parse_time(),
170             timer.get_format_time(),
171         )
172     });
173 
174     Ok(context.report)
175 }
176 
177 // Used for formatting files.
178 struct FormatContext<'a, T: FormatHandler> {
179     krate: &'a ast::Crate,
180     report: FormatReport,
181     parse_session: ParseSess,
182     config: &'a Config,
183     handler: &'a mut T,
184 }
185 
186 impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
new( krate: &'a ast::Crate, report: FormatReport, parse_session: ParseSess, config: &'a Config, handler: &'a mut T, ) -> Self187     fn new(
188         krate: &'a ast::Crate,
189         report: FormatReport,
190         parse_session: ParseSess,
191         config: &'a Config,
192         handler: &'a mut T,
193     ) -> Self {
194         FormatContext {
195             krate,
196             report,
197             parse_session,
198             config,
199             handler,
200         }
201     }
202 
ignore_file(&self, path: &FileName) -> bool203     fn ignore_file(&self, path: &FileName) -> bool {
204         self.parse_session.ignore_file(path)
205     }
206 
207     // Formats a single file/module.
format_file( &mut self, path: FileName, module: &Module<'_>, is_macro_def: bool, ) -> Result<(), ErrorKind>208     fn format_file(
209         &mut self,
210         path: FileName,
211         module: &Module<'_>,
212         is_macro_def: bool,
213     ) -> Result<(), ErrorKind> {
214         let snippet_provider = self.parse_session.snippet_provider(module.span);
215         let mut visitor = FmtVisitor::from_parse_sess(
216             &self.parse_session,
217             self.config,
218             &snippet_provider,
219             self.report.clone(),
220         );
221         visitor.skip_context.update_with_attrs(&self.krate.attrs);
222         visitor.is_macro_def = is_macro_def;
223         visitor.last_pos = snippet_provider.start_pos();
224         visitor.skip_empty_lines(snippet_provider.end_pos());
225         visitor.format_separate_mod(module, snippet_provider.end_pos());
226 
227         debug_assert_eq!(
228             visitor.line_number,
229             count_newlines(&visitor.buffer),
230             "failed in format_file visitor.buffer:\n {:?}",
231             &visitor.buffer
232         );
233 
234         // For some reason, the source_map does not include terminating
235         // newlines so we must add one on for each file. This is sad.
236         source_file::append_newline(&mut visitor.buffer);
237 
238         format_lines(
239             &mut visitor.buffer,
240             &path,
241             &visitor.skipped_range.borrow(),
242             self.config,
243             &self.report,
244         );
245 
246         apply_newline_style(
247             self.config.newline_style(),
248             &mut visitor.buffer,
249             snippet_provider.entire_snippet(),
250         );
251 
252         if visitor.macro_rewrite_failure {
253             self.report.add_macro_format_failure();
254         }
255         self.report
256             .add_non_formatted_ranges(visitor.skipped_range.borrow().clone());
257 
258         self.handler.handle_formatted_file(
259             &self.parse_session,
260             path,
261             visitor.buffer.to_owned(),
262             &mut self.report,
263         )
264     }
265 }
266 
267 // Handle the results of formatting.
268 trait FormatHandler {
handle_formatted_file( &mut self, parse_session: &ParseSess, path: FileName, result: String, report: &mut FormatReport, ) -> Result<(), ErrorKind>269     fn handle_formatted_file(
270         &mut self,
271         parse_session: &ParseSess,
272         path: FileName,
273         result: String,
274         report: &mut FormatReport,
275     ) -> Result<(), ErrorKind>;
276 }
277 
278 impl<'b, T: Write + 'b> FormatHandler for Session<'b, T> {
279     // Called for each formatted file.
handle_formatted_file( &mut self, parse_session: &ParseSess, path: FileName, result: String, report: &mut FormatReport, ) -> Result<(), ErrorKind>280     fn handle_formatted_file(
281         &mut self,
282         parse_session: &ParseSess,
283         path: FileName,
284         result: String,
285         report: &mut FormatReport,
286     ) -> Result<(), ErrorKind> {
287         if let Some(ref mut out) = self.out {
288             match source_file::write_file(
289                 Some(parse_session),
290                 &path,
291                 &result,
292                 out,
293                 &mut *self.emitter,
294                 self.config.newline_style(),
295             ) {
296                 Ok(ref result) if result.has_diff => report.add_diff(),
297                 Err(e) => {
298                     // Create a new error with path_str to help users see which files failed
299                     let err_msg = format!("{}: {}", path, e);
300                     return Err(io::Error::new(e.kind(), err_msg).into());
301                 }
302                 _ => {}
303             }
304         }
305 
306         self.source_file.push((path, result));
307         Ok(())
308     }
309 }
310 
311 pub(crate) struct FormattingError {
312     pub(crate) line: usize,
313     pub(crate) kind: ErrorKind,
314     is_comment: bool,
315     is_string: bool,
316     pub(crate) line_buffer: String,
317 }
318 
319 impl FormattingError {
from_span( span: Span, parse_sess: &ParseSess, kind: ErrorKind, ) -> FormattingError320     pub(crate) fn from_span(
321         span: Span,
322         parse_sess: &ParseSess,
323         kind: ErrorKind,
324     ) -> FormattingError {
325         FormattingError {
326             line: parse_sess.line_of_byte_pos(span.lo()),
327             is_comment: kind.is_comment(),
328             kind,
329             is_string: false,
330             line_buffer: parse_sess.span_to_first_line_string(span),
331         }
332     }
333 
is_internal(&self) -> bool334     pub(crate) fn is_internal(&self) -> bool {
335         match self.kind {
336             ErrorKind::LineOverflow(..)
337             | ErrorKind::TrailingWhitespace
338             | ErrorKind::IoError(_)
339             | ErrorKind::ParseError
340             | ErrorKind::LostComment => true,
341             _ => false,
342         }
343     }
344 
msg_suffix(&self) -> &str345     pub(crate) fn msg_suffix(&self) -> &str {
346         if self.is_comment || self.is_string {
347             "set `error_on_unformatted = false` to suppress \
348              the warning against comments or string literals\n"
349         } else {
350             ""
351         }
352     }
353 
354     // (space, target)
format_len(&self) -> (usize, usize)355     pub(crate) fn format_len(&self) -> (usize, usize) {
356         match self.kind {
357             ErrorKind::LineOverflow(found, max) => (max, found - max),
358             ErrorKind::TrailingWhitespace
359             | ErrorKind::DeprecatedAttr
360             | ErrorKind::BadAttr
361             | ErrorKind::LostComment => {
362                 let trailing_ws_start = self
363                     .line_buffer
364                     .rfind(|c: char| !c.is_whitespace())
365                     .map(|pos| pos + 1)
366                     .unwrap_or(0);
367                 (
368                     trailing_ws_start,
369                     self.line_buffer.len() - trailing_ws_start,
370                 )
371             }
372             _ => unreachable!(),
373         }
374     }
375 }
376 
377 pub(crate) type FormatErrorMap = HashMap<FileName, Vec<FormattingError>>;
378 
379 #[derive(Default, Debug, PartialEq)]
380 pub(crate) struct ReportedErrors {
381     // Encountered e.g., an IO error.
382     pub(crate) has_operational_errors: bool,
383 
384     // Failed to reformat code because of parsing errors.
385     pub(crate) has_parsing_errors: bool,
386 
387     // Code is valid, but it is impossible to format it properly.
388     pub(crate) has_formatting_errors: bool,
389 
390     // Code contains macro call that was unable to format.
391     pub(crate) has_macro_format_failure: bool,
392 
393     // Failed an opt-in checking.
394     pub(crate) has_check_errors: bool,
395 
396     /// Formatted code differs from existing code (--check only).
397     pub(crate) has_diff: bool,
398 
399     /// Formatted code missed something, like lost comments or extra trailing space
400     pub(crate) has_unformatted_code_errors: bool,
401 }
402 
403 impl ReportedErrors {
404     /// Combine two summaries together.
add(&mut self, other: &ReportedErrors)405     pub(crate) fn add(&mut self, other: &ReportedErrors) {
406         self.has_operational_errors |= other.has_operational_errors;
407         self.has_parsing_errors |= other.has_parsing_errors;
408         self.has_formatting_errors |= other.has_formatting_errors;
409         self.has_macro_format_failure |= other.has_macro_format_failure;
410         self.has_check_errors |= other.has_check_errors;
411         self.has_diff |= other.has_diff;
412         self.has_unformatted_code_errors |= other.has_unformatted_code_errors;
413     }
414 }
415 
416 #[derive(Clone, Copy, Debug)]
417 enum Timer {
418     Disabled,
419     Initialized(Instant),
420     DoneParsing(Instant, Instant),
421     DoneFormatting(Instant, Instant, Instant),
422 }
423 
424 impl Timer {
start() -> Timer425     fn start() -> Timer {
426         if cfg!(target_arch = "wasm32") {
427             Timer::Disabled
428         } else {
429             Timer::Initialized(Instant::now())
430         }
431     }
done_parsing(self) -> Self432     fn done_parsing(self) -> Self {
433         match self {
434             Timer::Disabled => Timer::Disabled,
435             Timer::Initialized(init_time) => Timer::DoneParsing(init_time, Instant::now()),
436             _ => panic!("Timer can only transition to DoneParsing from Initialized state"),
437         }
438     }
439 
done_formatting(self) -> Self440     fn done_formatting(self) -> Self {
441         match self {
442             Timer::Disabled => Timer::Disabled,
443             Timer::DoneParsing(init_time, parse_time) => {
444                 Timer::DoneFormatting(init_time, parse_time, Instant::now())
445             }
446             _ => panic!("Timer can only transition to DoneFormatting from DoneParsing state"),
447         }
448     }
449 
450     /// Returns the time it took to parse the source files in seconds.
get_parse_time(&self) -> f32451     fn get_parse_time(&self) -> f32 {
452         match *self {
453             Timer::Disabled => panic!("this platform cannot time execution"),
454             Timer::DoneParsing(init, parse_time) | Timer::DoneFormatting(init, parse_time, _) => {
455                 // This should never underflow since `Instant::now()` guarantees monotonicity.
456                 Self::duration_to_f32(parse_time.duration_since(init))
457             }
458             Timer::Initialized(..) => unreachable!(),
459         }
460     }
461 
462     /// Returns the time it took to go from the parsed AST to the formatted output. Parsing time is
463     /// not included.
get_format_time(&self) -> f32464     fn get_format_time(&self) -> f32 {
465         match *self {
466             Timer::Disabled => panic!("this platform cannot time execution"),
467             Timer::DoneFormatting(_init, parse_time, format_time) => {
468                 Self::duration_to_f32(format_time.duration_since(parse_time))
469             }
470             Timer::DoneParsing(..) | Timer::Initialized(..) => unreachable!(),
471         }
472     }
473 
duration_to_f32(d: Duration) -> f32474     fn duration_to_f32(d: Duration) -> f32 {
475         d.as_secs() as f32 + d.subsec_nanos() as f32 / 1_000_000_000f32
476     }
477 }
478 
479 // Formatting done on a char by char or line by line basis.
480 // FIXME(#20): other stuff for parity with make tidy.
format_lines( text: &mut String, name: &FileName, skipped_range: &[(usize, usize)], config: &Config, report: &FormatReport, )481 fn format_lines(
482     text: &mut String,
483     name: &FileName,
484     skipped_range: &[(usize, usize)],
485     config: &Config,
486     report: &FormatReport,
487 ) {
488     let mut formatter = FormatLines::new(name, skipped_range, config);
489     formatter.iterate(text);
490 
491     if formatter.newline_count > 1 {
492         debug!("track truncate: {} {}", text.len(), formatter.newline_count);
493         let line = text.len() - formatter.newline_count + 1;
494         text.truncate(line);
495     }
496 
497     report.append(name.clone(), formatter.errors);
498 }
499 
500 struct FormatLines<'a> {
501     name: &'a FileName,
502     skipped_range: &'a [(usize, usize)],
503     last_was_space: bool,
504     line_len: usize,
505     cur_line: usize,
506     newline_count: usize,
507     errors: Vec<FormattingError>,
508     line_buffer: String,
509     current_line_contains_string_literal: bool,
510     format_line: bool,
511     config: &'a Config,
512 }
513 
514 impl<'a> FormatLines<'a> {
new( name: &'a FileName, skipped_range: &'a [(usize, usize)], config: &'a Config, ) -> FormatLines<'a>515     fn new(
516         name: &'a FileName,
517         skipped_range: &'a [(usize, usize)],
518         config: &'a Config,
519     ) -> FormatLines<'a> {
520         FormatLines {
521             name,
522             skipped_range,
523             last_was_space: false,
524             line_len: 0,
525             cur_line: 1,
526             newline_count: 0,
527             errors: vec![],
528             line_buffer: String::with_capacity(config.max_width() * 2),
529             current_line_contains_string_literal: false,
530             format_line: config.file_lines().contains_line(name, 1),
531             config,
532         }
533     }
534 
535     // Iterate over the chars in the file map.
iterate(&mut self, text: &mut String)536     fn iterate(&mut self, text: &mut String) {
537         for (kind, c) in CharClasses::new(text.chars()) {
538             if c == '\r' {
539                 continue;
540             }
541 
542             if c == '\n' {
543                 self.new_line(kind);
544             } else {
545                 self.char(c, kind);
546             }
547         }
548     }
549 
new_line(&mut self, kind: FullCodeCharKind)550     fn new_line(&mut self, kind: FullCodeCharKind) {
551         if self.format_line {
552             // Check for (and record) trailing whitespace.
553             if self.last_was_space {
554                 if self.should_report_error(kind, &ErrorKind::TrailingWhitespace)
555                     && !self.is_skipped_line()
556                 {
557                     self.push_err(
558                         ErrorKind::TrailingWhitespace,
559                         kind.is_comment(),
560                         kind.is_string(),
561                     );
562                 }
563                 self.line_len -= 1;
564             }
565 
566             // Check for any line width errors we couldn't correct.
567             let error_kind = ErrorKind::LineOverflow(self.line_len, self.config.max_width());
568             if self.line_len > self.config.max_width()
569                 && !self.is_skipped_line()
570                 && self.should_report_error(kind, &error_kind)
571             {
572                 let is_string = self.current_line_contains_string_literal;
573                 self.push_err(error_kind, kind.is_comment(), is_string);
574             }
575         }
576 
577         self.line_len = 0;
578         self.cur_line += 1;
579         self.format_line = self
580             .config
581             .file_lines()
582             .contains_line(self.name, self.cur_line);
583         self.newline_count += 1;
584         self.last_was_space = false;
585         self.line_buffer.clear();
586         self.current_line_contains_string_literal = false;
587     }
588 
char(&mut self, c: char, kind: FullCodeCharKind)589     fn char(&mut self, c: char, kind: FullCodeCharKind) {
590         self.newline_count = 0;
591         self.line_len += if c == '\t' {
592             self.config.tab_spaces()
593         } else {
594             1
595         };
596         self.last_was_space = c.is_whitespace();
597         self.line_buffer.push(c);
598         if kind.is_string() {
599             self.current_line_contains_string_literal = true;
600         }
601     }
602 
push_err(&mut self, kind: ErrorKind, is_comment: bool, is_string: bool)603     fn push_err(&mut self, kind: ErrorKind, is_comment: bool, is_string: bool) {
604         self.errors.push(FormattingError {
605             line: self.cur_line,
606             kind,
607             is_comment,
608             is_string,
609             line_buffer: self.line_buffer.clone(),
610         });
611     }
612 
should_report_error(&self, char_kind: FullCodeCharKind, error_kind: &ErrorKind) -> bool613     fn should_report_error(&self, char_kind: FullCodeCharKind, error_kind: &ErrorKind) -> bool {
614         let allow_error_report = if char_kind.is_comment()
615             || self.current_line_contains_string_literal
616             || error_kind.is_comment()
617         {
618             self.config.error_on_unformatted()
619         } else {
620             true
621         };
622 
623         match error_kind {
624             ErrorKind::LineOverflow(..) => {
625                 self.config.error_on_line_overflow() && allow_error_report
626             }
627             ErrorKind::TrailingWhitespace | ErrorKind::LostComment => allow_error_report,
628             _ => true,
629         }
630     }
631 
632     /// Returns `true` if the line with the given line number was skipped by `#[rustfmt::skip]`.
is_skipped_line(&self) -> bool633     fn is_skipped_line(&self) -> bool {
634         self.skipped_range
635             .iter()
636             .any(|&(lo, hi)| lo <= self.cur_line && self.cur_line <= hi)
637     }
638 }
639 
should_emit_verbose<F>(forbid_verbose_output: bool, config: &Config, f: F) where F: Fn(),640 fn should_emit_verbose<F>(forbid_verbose_output: bool, config: &Config, f: F)
641 where
642     F: Fn(),
643 {
644     if config.verbose() == Verbosity::Verbose && !forbid_verbose_output {
645         f();
646     }
647 }
648