1
Fork 0

buffer lexer errors in rustdoc syntax checking

This commit is contained in:
Andy Russell 2019-12-12 16:28:26 -05:00
parent 760ce94c69
commit 1ad928ee52
No known key found for this signature in database
GPG key ID: BE2221033EDBC374
2 changed files with 48 additions and 112 deletions

View file

@ -1,6 +1,7 @@
use errors::Applicability;
use errors::{emitter::Emitter, Applicability, Diagnostic, Handler};
use rustc_data_structures::sync::{Lock, Lrc};
use rustc_parse::lexer::StringReader as Lexer;
use rustc_span::source_map::FilePathMapping;
use rustc_span::source_map::{FilePathMapping, SourceMap};
use rustc_span::{FileName, InnerSpan};
use syntax::sess::ParseSess;
use syntax::token;
@ -27,7 +28,13 @@ struct SyntaxChecker<'a, 'tcx> {
impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) {
let sess = ParseSess::new(FilePathMapping::empty());
let buffered_messages = Lrc::new(Lock::new(vec![]));
let emitter = BufferEmitter { messages: Lrc::clone(&buffered_messages) };
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let handler = Handler::with_emitter(false, None, Box::new(emitter));
let sess = ParseSess::with_span_handler(handler, cm);
let source_file = sess.source_map().new_source_file(
FileName::Custom(String::from("doctest")),
dox[code_block.code].to_owned(),
@ -93,6 +100,11 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
diag
};
// FIXME(#67563): Provide more context for these errors by displaying the spans inline.
for message in buffered_messages.borrow().iter() {
diag.note(&message);
}
diag.emit();
}
}
@ -110,6 +122,20 @@ impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
}
}
struct BufferEmitter {
messages: Lrc<Lock<Vec<String>>>,
}
impl Emitter for BufferEmitter {
fn emit_diagnostic(&mut self, diag: &Diagnostic) {
self.messages.borrow_mut().push(format!("error from rustc: {}", diag.message[0].0));
}
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
None
}
}
enum CodeBlockInvalid {
SyntaxError,
Empty,