rustfmt_nightly/
formatting.rs

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