1 use std::cell::RefCell;
5 use rustc_data_structures::sync::{Lrc, Send};
6 use rustc_errors::emitter::{Emitter, EmitterWriter};
7 use rustc_errors::{ColorConfig, Diagnostic, Handler, Level as DiagnosticLevel};
8 use rustc_session::parse::ParseSess as RawParseSess;
10 source_map::{FilePathMapping, SourceMap},
11 symbol, BytePos, Span,
14 use crate::config::file_lines::LineRange;
15 use crate::ignore_path::IgnorePathSet;
16 use crate::source_map::LineRangeUtils;
17 use crate::utils::starts_with_newline;
18 use crate::visitor::SnippetProvider;
19 use crate::{Config, ErrorKind, FileName};
21 /// ParseSess holds structs necessary for constructing a parser.
22 pub(crate) struct ParseSess {
23 parse_sess: RawParseSess,
24 ignore_path_set: Rc<IgnorePathSet>,
25 can_reset_errors: Rc<RefCell<bool>>,
28 /// Emitter which discards every error.
31 impl Emitter for SilentEmitter {
32 fn source_map(&self) -> Option<&Lrc<SourceMap>> {
35 fn emit_diagnostic(&mut self, _db: &Diagnostic) {}
38 fn silent_emitter() -> Box<dyn Emitter + Send> {
39 Box::new(SilentEmitter {})
42 /// Emit errors against every files expect ones specified in the `ignore_path_set`.
43 struct SilentOnIgnoredFilesEmitter {
44 ignore_path_set: Rc<IgnorePathSet>,
45 source_map: Rc<SourceMap>,
46 emitter: Box<dyn Emitter + Send>,
47 has_non_ignorable_parser_errors: bool,
48 can_reset: Rc<RefCell<bool>>,
51 impl SilentOnIgnoredFilesEmitter {
52 fn handle_non_ignoreable_error(&mut self, db: &Diagnostic) {
53 self.has_non_ignorable_parser_errors = true;
54 *self.can_reset.borrow_mut() = false;
55 self.emitter.emit_diagnostic(db);
59 impl Emitter for SilentOnIgnoredFilesEmitter {
60 fn source_map(&self) -> Option<&Lrc<SourceMap>> {
63 fn emit_diagnostic(&mut self, db: &Diagnostic) {
64 if db.level == DiagnosticLevel::Fatal {
65 return self.handle_non_ignoreable_error(db);
67 if let Some(primary_span) = &db.span.primary_span() {
68 let file_name = self.source_map.span_to_filename(*primary_span);
69 if let rustc_span::FileName::Real(rustc_span::RealFileName::Named(ref path)) = file_name
73 .is_match(&FileName::Real(path.to_path_buf()))
75 if !self.has_non_ignorable_parser_errors {
76 *self.can_reset.borrow_mut() = true;
82 self.handle_non_ignoreable_error(db);
87 source_map: Rc<SourceMap>,
88 ignore_path_set: Rc<IgnorePathSet>,
89 can_reset: Rc<RefCell<bool>>,
90 hide_parse_errors: bool,
92 let supports_color = term::stderr().map_or(false, |term| term.supports_color());
93 let color_cfg = if supports_color {
99 let emitter = if hide_parse_errors {
102 Box::new(EmitterWriter::stderr(
104 Some(source_map.clone()),
111 Handler::with_emitter(
114 Box::new(SilentOnIgnoredFilesEmitter {
115 has_non_ignorable_parser_errors: false,
125 pub(crate) fn new(config: &Config) -> Result<ParseSess, ErrorKind> {
126 let ignore_path_set = match IgnorePathSet::from_ignore_list(&config.ignore()) {
127 Ok(ignore_path_set) => Rc::new(ignore_path_set),
128 Err(e) => return Err(ErrorKind::InvalidGlobPattern(e)),
130 let source_map = Rc::new(SourceMap::new(FilePathMapping::empty()));
131 let can_reset_errors = Rc::new(RefCell::new(false));
133 let handler = default_handler(
134 Rc::clone(&source_map),
135 Rc::clone(&ignore_path_set),
136 Rc::clone(&can_reset_errors),
137 config.hide_parse_errors(),
139 let parse_sess = RawParseSess::with_span_handler(handler, source_map);
148 pub(crate) fn default_submod_path(
151 relative: Option<symbol::Ident>,
153 ) -> rustc_expand::module::ModulePath<'_> {
154 rustc_expand::module::default_submod_path(
157 rustc_span::DUMMY_SP,
163 pub(crate) fn is_file_parsed(&self, path: &Path) -> bool {
166 .get_source_file(&rustc_span::FileName::Real(
167 rustc_span::RealFileName::Named(path.to_path_buf()),
172 pub(crate) fn ignore_file(&self, path: &FileName) -> bool {
173 self.ignore_path_set.as_ref().is_match(&path)
176 pub(crate) fn set_silent_emitter(&mut self) {
177 self.parse_sess.span_diagnostic = Handler::with_emitter(true, None, silent_emitter());
180 pub(crate) fn span_to_filename(&self, span: Span) -> FileName {
181 self.parse_sess.source_map().span_to_filename(span).into()
184 pub(crate) fn span_to_first_line_string(&self, span: Span) -> String {
185 let file_lines = self.parse_sess.source_map().span_to_lines(span).ok();
190 .get_line(fl.lines[0].line_index)
191 .map_or_else(String::new, |s| s.to_string()),
192 None => String::new(),
196 pub(crate) fn line_of_byte_pos(&self, pos: BytePos) -> usize {
197 self.parse_sess.source_map().lookup_char_pos(pos).line
200 pub(crate) fn span_to_debug_info(&self, span: Span) -> String {
201 self.parse_sess.source_map().span_to_string(span)
204 pub(crate) fn inner(&self) -> &RawParseSess {
208 pub(crate) fn snippet_provider(&self, span: Span) -> SnippetProvider {
209 let source_file = self.parse_sess.source_map().lookup_char_pos(span.lo()).file;
210 SnippetProvider::new(
211 source_file.start_pos,
213 Rc::clone(source_file.src.as_ref().unwrap()),
217 pub(crate) fn get_original_snippet(&self, file_name: &FileName) -> Option<Rc<String>> {
220 .get_source_file(&file_name.into())
221 .and_then(|source_file| source_file.src.clone())
225 // Methods that should be restricted within the syntux module.
227 pub(super) fn emit_diagnostics(&self, diagnostics: Vec<Diagnostic>) {
228 for diagnostic in diagnostics {
229 self.parse_sess.span_diagnostic.emit_diagnostic(&diagnostic);
233 pub(crate) fn emit_or_cancel_diagnostic(&self, diagnostic: &mut Diagnostic) {
234 self.parse_sess.span_diagnostic.emit_diagnostic(diagnostic);
235 // The Handler will check whether the diagnostic should be emitted
236 // based on the user's rustfmt configuration and the originating file
237 // that caused the parser error. If the Handler determined it should skip
238 // emission then we need to ensure the diagnostic is cancelled.
239 if !diagnostic.cancelled() {
244 pub(super) fn can_reset_errors(&self) -> bool {
245 *self.can_reset_errors.borrow()
248 pub(super) fn has_errors(&self) -> bool {
249 self.parse_sess.span_diagnostic.has_errors()
252 pub(super) fn reset_errors(&self) {
253 self.parse_sess.span_diagnostic.reset_err_count();
257 impl LineRangeUtils for ParseSess {
258 fn lookup_line_range(&self, span: Span) -> LineRange {
262 .span_to_snippet(span)
263 .unwrap_or_default();
264 let lo = self.parse_sess.source_map().lookup_line(span.lo()).unwrap();
265 let hi = self.parse_sess.source_map().lookup_line(span.hi()).unwrap();
268 lo.sf.name, hi.sf.name,
269 "span crossed file boundary: lo: {:?}, hi: {:?}",
273 // in case the span starts with a newline, the line range is off by 1 without the
275 let offset = 1 + if starts_with_newline(&snippet) { 1 } else { 0 };
276 // Line numbers start at 1
279 lo: lo.line + offset,
280 hi: hi.line + offset,
291 use crate::config::IgnoreList;
292 use crate::is_nightly_channel;
293 use crate::utils::mk_sp;
294 use rustc_span::{FileName as SourceMapFileName, MultiSpan, RealFileName, DUMMY_SP};
295 use std::path::PathBuf;
298 num_emitted_errors: Rc<RefCell<u32>>,
301 impl Emitter for TestEmitter {
302 fn source_map(&self) -> Option<&Lrc<SourceMap>> {
305 fn emit_diagnostic(&mut self, _db: &Diagnostic) {
306 *self.num_emitted_errors.borrow_mut() += 1;
310 fn build_diagnostic(level: DiagnosticLevel, span: Option<MultiSpan>) -> Diagnostic {
317 span: span.unwrap_or_else(MultiSpan::new),
323 num_emitted_errors: Rc<RefCell<u32>>,
324 can_reset: Rc<RefCell<bool>>,
325 source_map: Option<Rc<SourceMap>>,
326 ignore_list: Option<IgnoreList>,
327 ) -> SilentOnIgnoredFilesEmitter {
328 let emitter_writer = TestEmitter { num_emitted_errors };
330 source_map.unwrap_or_else(|| Rc::new(SourceMap::new(FilePathMapping::empty())));
331 let ignore_path_set =
332 Rc::new(IgnorePathSet::from_ignore_list(&ignore_list.unwrap_or_default()).unwrap());
333 SilentOnIgnoredFilesEmitter {
334 has_non_ignorable_parser_errors: false,
336 emitter: Box::new(emitter_writer),
342 fn get_ignore_list(config: &str) -> IgnoreList {
343 Config::from_toml(config, Path::new("")).unwrap().ignore()
347 fn handles_fatal_parse_error_in_ignored_file() {
348 let num_emitted_errors = Rc::new(RefCell::new(0));
349 let can_reset_errors = Rc::new(RefCell::new(false));
350 let ignore_list = get_ignore_list(r#"ignore = ["foo.rs"]"#);
351 let source_map = Rc::new(SourceMap::new(FilePathMapping::empty()));
353 String::from(r#"extern "system" fn jni_symbol!( funcName ) ( ... ) -> {} "#);
354 source_map.new_source_file(
355 SourceMapFileName::Real(RealFileName::Named(PathBuf::from("foo.rs"))),
358 let mut emitter = build_emitter(
359 Rc::clone(&num_emitted_errors),
360 Rc::clone(&can_reset_errors),
361 Some(Rc::clone(&source_map)),
364 let span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1)));
365 let fatal_diagnostic = build_diagnostic(DiagnosticLevel::Fatal, Some(span));
366 emitter.emit_diagnostic(&fatal_diagnostic);
367 assert_eq!(*num_emitted_errors.borrow(), 1);
368 assert_eq!(*can_reset_errors.borrow(), false);
372 fn handles_recoverable_parse_error_in_ignored_file() {
373 if !is_nightly_channel!() {
376 let num_emitted_errors = Rc::new(RefCell::new(0));
377 let can_reset_errors = Rc::new(RefCell::new(false));
378 let ignore_list = get_ignore_list(r#"ignore = ["foo.rs"]"#);
379 let source_map = Rc::new(SourceMap::new(FilePathMapping::empty()));
380 let source = String::from(r#"pub fn bar() { 1x; }"#);
381 source_map.new_source_file(
382 SourceMapFileName::Real(RealFileName::Named(PathBuf::from("foo.rs"))),
385 let mut emitter = build_emitter(
386 Rc::clone(&num_emitted_errors),
387 Rc::clone(&can_reset_errors),
388 Some(Rc::clone(&source_map)),
391 let span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1)));
392 let non_fatal_diagnostic = build_diagnostic(DiagnosticLevel::Warning, Some(span));
393 emitter.emit_diagnostic(&non_fatal_diagnostic);
394 assert_eq!(*num_emitted_errors.borrow(), 0);
395 assert_eq!(*can_reset_errors.borrow(), true);
399 fn handles_recoverable_parse_error_in_non_ignored_file() {
400 if !is_nightly_channel!() {
403 let num_emitted_errors = Rc::new(RefCell::new(0));
404 let can_reset_errors = Rc::new(RefCell::new(false));
405 let source_map = Rc::new(SourceMap::new(FilePathMapping::empty()));
406 let source = String::from(r#"pub fn bar() { 1x; }"#);
407 source_map.new_source_file(
408 SourceMapFileName::Real(RealFileName::Named(PathBuf::from("foo.rs"))),
411 let mut emitter = build_emitter(
412 Rc::clone(&num_emitted_errors),
413 Rc::clone(&can_reset_errors),
414 Some(Rc::clone(&source_map)),
417 let span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1)));
418 let non_fatal_diagnostic = build_diagnostic(DiagnosticLevel::Warning, Some(span));
419 emitter.emit_diagnostic(&non_fatal_diagnostic);
420 assert_eq!(*num_emitted_errors.borrow(), 1);
421 assert_eq!(*can_reset_errors.borrow(), false);
425 fn handles_mix_of_recoverable_parse_error() {
426 if !is_nightly_channel!() {
429 let num_emitted_errors = Rc::new(RefCell::new(0));
430 let can_reset_errors = Rc::new(RefCell::new(false));
431 let source_map = Rc::new(SourceMap::new(FilePathMapping::empty()));
432 let ignore_list = get_ignore_list(r#"ignore = ["foo.rs"]"#);
433 let bar_source = String::from(r#"pub fn bar() { 1x; }"#);
434 let foo_source = String::from(r#"pub fn foo() { 1x; }"#);
436 String::from(r#"extern "system" fn jni_symbol!( funcName ) ( ... ) -> {} "#);
437 source_map.new_source_file(
438 SourceMapFileName::Real(RealFileName::Named(PathBuf::from("bar.rs"))),
441 source_map.new_source_file(
442 SourceMapFileName::Real(RealFileName::Named(PathBuf::from("foo.rs"))),
445 source_map.new_source_file(
446 SourceMapFileName::Real(RealFileName::Named(PathBuf::from("fatal.rs"))),
449 let mut emitter = build_emitter(
450 Rc::clone(&num_emitted_errors),
451 Rc::clone(&can_reset_errors),
452 Some(Rc::clone(&source_map)),
455 let bar_span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1)));
456 let foo_span = MultiSpan::from_span(mk_sp(BytePos(21), BytePos(22)));
457 let bar_diagnostic = build_diagnostic(DiagnosticLevel::Warning, Some(bar_span));
458 let foo_diagnostic = build_diagnostic(DiagnosticLevel::Warning, Some(foo_span));
459 let fatal_diagnostic = build_diagnostic(DiagnosticLevel::Fatal, None);
460 emitter.emit_diagnostic(&bar_diagnostic);
461 emitter.emit_diagnostic(&foo_diagnostic);
462 emitter.emit_diagnostic(&fatal_diagnostic);
463 assert_eq!(*num_emitted_errors.borrow(), 2);
464 assert_eq!(*can_reset_errors.borrow(), false);