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