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