forked from rust-lang/rustfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatting.rs
535 lines (475 loc) · 16 KB
/
formatting.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// High level formatting functions.
use std::time::{Duration, Instant};
use rustc_ast::{ast, attr::HasAttrs};
use rustc_span::symbol;
pub(crate) use syntux::session::ParseSess;
use crate::config::{Config, FileName};
use crate::formatting::{
comment::{CharClasses, FullCodeCharKind},
generated::is_generated_file,
modules::{FileModMap, Module},
newline_style::apply_newline_style,
report::NonFormattedRange,
syntux::parser::{DirectoryOwnership, Parser, ParserError},
utils::{contains_skip, count_newlines},
visitor::FmtVisitor,
};
use crate::{
result::{ErrorKind, FormatError, OperationError},
FormatReport, FormatResult, Input, OperationSetting, Verbosity,
};
#[macro_use]
mod utils;
mod attr;
mod chains;
mod closures;
mod comment;
mod expr;
mod generated;
mod imports;
mod items;
mod lists;
mod macros;
mod matches;
mod missed_spans;
pub(crate) mod modules;
mod newline_style;
mod overflow;
mod pairs;
mod patterns;
mod reorder;
mod rewrite;
mod shape;
mod skip;
pub(crate) mod source_map;
mod spanned;
mod stmt;
mod string;
mod syntux;
mod types;
mod vertical;
pub(crate) mod visitor;
pub(crate) mod report;
pub(crate) fn format_input_inner(
input: Input,
config: &Config,
operation_setting: OperationSetting,
is_macro_def: bool,
) -> Result<FormatReport, OperationError> {
if !config.version_meets_requirement() {
return Err(OperationError::VersionMismatch);
}
rustc_span::with_session_globals(config.edition().into(), || {
format_project(input, config, operation_setting, is_macro_def)
})
}
fn format_project(
input: Input,
config: &Config,
operation_setting: OperationSetting,
is_macro_def: bool,
) -> Result<FormatReport, OperationError> {
let mut timer = Timer::start();
let format_report = FormatReport::new();
let main_file = input.file_name();
let input_is_stdin = main_file == FileName::Stdin;
let mut parse_session = ParseSess::new(config)?;
if !operation_setting.recursive && parse_session.ignore_file(&main_file) {
format_report.add_ignored_file(main_file);
return Ok(format_report);
}
// Parse the crate.
let directory_ownership = input.to_directory_ownership(operation_setting.recursive);
let original_snippet = if let Input::Text(ref str) = input {
Some(str.to_owned())
} else {
None
};
let krate = match Parser::parse_crate(config, input, directory_ownership, &parse_session) {
Ok(krate) => krate,
Err(e) => {
return Err(OperationError::ParseError {
input: main_file,
is_panic: e == ParserError::ParsePanicError,
});
}
};
if !operation_setting.recursive {
// Suppress error output for sub-modules if we are not in recurisve mode.
parse_session.set_silent_emitter();
}
let files = modules::ModResolver::new(
&parse_session,
directory_ownership.unwrap_or(DirectoryOwnership::UnownedViaMod),
!input_is_stdin && operation_setting.recursive,
)
.visit_crate(&krate)?;
timer = timer.done_parsing();
// Suppress error output if we have to do any further parsing.
parse_session.set_silent_emitter();
for (path, module) in &files {
let should_ignore = (!input_is_stdin && parse_session.ignore_file(&path))
|| (!config.format_generated_files()
&& is_generated_file(&path, original_snippet.as_ref()));
if (!operation_setting.recursive && path != &main_file) || should_ignore {
continue;
}
if contains_skip(module.attrs()) {
continue;
}
should_emit_verbose(input_is_stdin, operation_setting.verbosity, || {
println!("Formatting {}", path)
});
format_file(
&parse_session,
config,
&krate,
path,
&module,
&format_report,
&files,
original_snippet.clone(),
is_macro_def,
)?;
}
timer = timer.done_formatting();
should_emit_verbose(input_is_stdin, operation_setting.verbosity, || {
println!(
"Spent {0:.3} secs in the parsing phase, and {1:.3} secs in the formatting phase",
timer.get_parse_time(),
timer.get_format_time(),
)
});
Ok(format_report)
}
fn format_file(
parse_session: &ParseSess,
config: &Config,
krate: &ast::Crate,
path: &FileName,
module: &Module<'_>,
report: &FormatReport,
file_mod_map: &FileModMap<'_>,
original_snippet: Option<String>,
is_macro_def: bool,
) -> Result<(), OperationError> {
let snippet_provider = parse_session.snippet_provider(module.as_ref().inner);
let mut visitor = FmtVisitor::from_parse_sess(
&parse_session,
config,
&snippet_provider,
file_mod_map,
report.clone(),
);
visitor.is_macro_def = is_macro_def;
visitor.skip_context.update_with_attrs(&krate.attrs);
visitor.last_pos = snippet_provider.start_pos();
visitor.skip_empty_lines(snippet_provider.end_pos());
visitor.format_separate_mod(module, snippet_provider.end_pos());
debug_assert_eq!(
visitor.line_number,
count_newlines(&visitor.buffer),
"failed in format_file visitor.buffer:\n {:?}",
&visitor.buffer
);
// For some reason, the source_map does not include terminating
// newlines so we must add one on for each file. This is sad.
visitor.buffer.push('\n');
format_lines(
&mut visitor.buffer,
&path,
&visitor.skipped_range.borrow(),
config,
report.clone(),
);
// SourceFile's in the SourceMap will always have Unix-style line endings
// See: https://github.com/rust-lang/rustfmt/issues/3850
// So we must check the file system to get the original file value in order
// to detect newline_style conflicts.
// Otherwise, parse session is around (cfg(not(test))) and newline_style has been
// left as the default value, then try getting source from the parse session
// source map instead of hitting the file system.
let original_text = match original_snippet {
Some(snippet) => snippet,
None => std::fs::read_to_string(path.as_path().ok_or_else(|| {
OperationError::IoError(std::io::Error::from(std::io::ErrorKind::InvalidInput))
})?)?,
};
apply_newline_style(config.newline_style(), &mut visitor.buffer, &original_text);
if visitor.macro_rewrite_failure {
report.add_macro_format_failure(path.clone());
}
let format_result = FormatResult::success(
visitor.buffer.to_owned(),
visitor.skipped_range.borrow().clone(),
original_text,
config.newline_style(),
);
report.add_format_result(path.clone(), format_result);
Ok(())
}
#[derive(Clone, Copy, Debug)]
enum Timer {
Disabled,
Initialized(Instant),
DoneParsing(Instant, Instant),
DoneFormatting(Instant, Instant, Instant),
}
impl Timer {
fn start() -> Timer {
if cfg!(all(target_arch = "wasm32", not(target_os = "wasi"))) {
Timer::Disabled
} else {
Timer::Initialized(Instant::now())
}
}
fn done_parsing(self) -> Self {
match self {
Timer::Disabled => Timer::Disabled,
Timer::Initialized(init_time) => Timer::DoneParsing(init_time, Instant::now()),
_ => panic!("Timer can only transition to DoneParsing from Initialized state"),
}
}
fn done_formatting(self) -> Self {
match self {
Timer::Disabled => Timer::Disabled,
Timer::DoneParsing(init_time, parse_time) => {
Timer::DoneFormatting(init_time, parse_time, Instant::now())
}
_ => panic!("Timer can only transition to DoneFormatting from DoneParsing state"),
}
}
/// Returns the time it took to parse the source files in seconds.
fn get_parse_time(&self) -> f32 {
match *self {
Timer::Disabled => panic!("this platform cannot time execution"),
Timer::DoneParsing(init, parse_time) | Timer::DoneFormatting(init, parse_time, _) => {
// This should never underflow since `Instant::now()` guarantees monotonicity.
Self::duration_to_f32(parse_time.duration_since(init))
}
Timer::Initialized(..) => unreachable!(),
}
}
/// Returns the time it took to go from the parsed AST to the formatted output. Parsing time is
/// not included.
fn get_format_time(&self) -> f32 {
match *self {
Timer::Disabled => panic!("this platform cannot time execution"),
Timer::DoneFormatting(_init, parse_time, format_time) => {
Self::duration_to_f32(format_time.duration_since(parse_time))
}
Timer::DoneParsing(..) | Timer::Initialized(..) => unreachable!(),
}
}
fn duration_to_f32(d: Duration) -> f32 {
d.as_secs() as f32 + d.subsec_nanos() as f32 / 1_000_000_000f32
}
}
// Formatting done on a char by char or line by line basis.
// FIXME(#20): other stuff for parity with make tidy.
fn format_lines(
text: &mut String,
name: &FileName,
skipped_range: &[NonFormattedRange],
config: &Config,
report: FormatReport,
) {
let mut formatter = FormatLines::new(name, skipped_range, config);
if let Some(false) = formatter.check_license(text) {
report.add_license_failure(name.clone());
}
formatter.iterate(text);
if formatter.newline_count > 1 {
debug!("track truncate: {} {}", text.len(), formatter.newline_count);
let line = text.len() - formatter.newline_count + 1;
text.truncate(line);
}
report.append_errors(name.clone(), formatter.errors.into_iter());
}
struct FormatLines<'a> {
name: &'a FileName,
skipped_range: &'a [NonFormattedRange],
last_was_space: bool,
line_len: usize,
cur_line: usize,
newline_count: usize,
errors: Vec<FormatError>,
line_buffer: String,
current_line_contains_string_literal: bool,
format_line: bool,
config: &'a Config,
}
impl<'a> FormatLines<'a> {
fn new(
name: &'a FileName,
skipped_range: &'a [NonFormattedRange],
config: &'a Config,
) -> FormatLines<'a> {
FormatLines {
name,
skipped_range,
last_was_space: false,
line_len: 0,
cur_line: 1,
newline_count: 0,
errors: vec![],
line_buffer: String::with_capacity(config.max_width() * 2),
current_line_contains_string_literal: false,
format_line: config.file_lines().contains_line(name, 1),
config,
}
}
fn check_license(&mut self, text: &str) -> Option<bool> {
self.config
.license_template
.as_ref()
.map(|license_template| license_template.is_match(text))
}
// Iterate over the chars in the file map.
fn iterate(&mut self, text: &str) {
for (kind, c) in CharClasses::new(text.chars()) {
if c == '\r' {
continue;
}
if c == '\n' {
self.new_line(kind);
} else {
self.char(c, kind);
}
}
}
fn new_line(&mut self, kind: FullCodeCharKind) {
if self.format_line {
// Check for (and record) trailing whitespace.
if self.last_was_space {
if self.should_report_error(kind, &ErrorKind::TrailingWhitespace)
&& !self.is_skipped_line()
{
self.push_err(ErrorKind::TrailingWhitespace);
}
self.line_len -= 1;
}
// Check for any line width errors we couldn't correct.
let error_kind = ErrorKind::LineOverflow(self.line_len, self.config.max_width());
if self.line_len > self.config.max_width()
&& !self.is_skipped_line()
&& self.should_report_error(kind, &error_kind)
{
self.push_err(error_kind);
}
}
self.line_len = 0;
self.cur_line += 1;
self.format_line = self
.config
.file_lines()
.contains_line(self.name, self.cur_line);
self.newline_count += 1;
self.last_was_space = false;
self.line_buffer.clear();
self.current_line_contains_string_literal = false;
}
fn char(&mut self, c: char, kind: FullCodeCharKind) {
self.newline_count = 0;
self.line_len += if c == '\t' {
self.config.tab_spaces()
} else {
1
};
self.last_was_space = c.is_whitespace();
self.line_buffer.push(c);
if kind.is_string() {
self.current_line_contains_string_literal = true;
}
}
fn push_err(&mut self, kind: ErrorKind) {
self.errors.push(FormatError::new(
kind,
self.cur_line,
self.line_buffer.clone(),
));
}
fn should_report_error(&self, char_kind: FullCodeCharKind, error_kind: &ErrorKind) -> bool {
let allow_error_report =
if char_kind.is_comment() || self.current_line_contains_string_literal {
self.config.error_on_unformatted()
} else {
true
};
match error_kind {
ErrorKind::LineOverflow(..) => {
self.config.error_on_line_overflow() && allow_error_report
}
ErrorKind::TrailingWhitespace => allow_error_report,
_ => true,
}
}
/// Returns `true` if the line with the given line number was skipped by `#[rustfmt::skip]`.
fn is_skipped_line(&self) -> bool {
self.skipped_range
.iter()
.any(|range| range.contains(self.cur_line))
}
}
fn should_emit_verbose<F>(forbid_verbose_output: bool, verbosity: Verbosity, f: F)
where
F: Fn(),
{
if verbosity == Verbosity::Verbose && !forbid_verbose_output {
f();
}
}
/// Result of formatting a snippet of code along with ranges of lines that didn't get formatted,
/// i.e., that got returned as they were originally.
#[derive(Debug, Clone, Default)]
pub(crate) struct FormattedSnippet {
snippet: String,
non_formatted_ranges: Vec<NonFormattedRange>,
}
impl FormattedSnippet {
/// In case the snippet needed to be wrapped in a function, this shifts down the ranges of
/// non-formatted code.
fn unwrap_code_block(&mut self) {
self.non_formatted_ranges.iter_mut().for_each(|range| {
*range = range.shift_up();
});
}
/// Returns `true` if the line n did not get formatted.
pub(crate) fn is_line_non_formatted(&self, n: usize) -> bool {
self.non_formatted_ranges
.iter()
.any(|range| range.contains(n))
}
}
impl AsRef<str> for FormattedSnippet {
fn as_ref(&self) -> &str {
self.snippet.as_str()
}
}
impl Input {
fn file_name(&self) -> FileName {
match *self {
Input::File(ref file) => FileName::Real(file.clone()),
Input::Text(..) => FileName::Stdin,
}
}
fn to_directory_ownership(&self, recursive: bool) -> Option<DirectoryOwnership> {
match self {
// On recursive mode, we assume that input is the root file.
Input::File(..) if recursive => None,
Input::File(ref file) => {
// If there exists a directory with the same name as an input,
// then the input should be parsed as a sub module.
let file_stem = file.file_stem()?;
if file.parent()?.to_path_buf().join(file_stem).is_dir() {
Some(DirectoryOwnership::Owned {
relative: file_stem.to_str().map(symbol::Ident::from_str),
})
} else {
None
}
}
Input::Text(..) => None,
}
}
}