]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/passes/check_code_block_syntax.rs
Introduce InnerSpan abstraction
[rust.git] / src / librustdoc / passes / check_code_block_syntax.rs
1 use errors::Applicability;
2 use syntax::parse::lexer::{StringReader as Lexer};
3 use syntax::parse::{ParseSess, token};
4 use syntax::source_map::FilePathMapping;
5 use syntax_pos::{InnerSpan, FileName};
6
7 use crate::clean;
8 use crate::core::DocContext;
9 use crate::fold::DocFolder;
10 use crate::html::markdown::{self, RustCodeBlock};
11 use crate::passes::Pass;
12
13 pub const CHECK_CODE_BLOCK_SYNTAX: Pass = Pass {
14     name: "check-code-block-syntax",
15     pass: check_code_block_syntax,
16     description: "validates syntax inside Rust code blocks",
17 };
18
19 pub fn check_code_block_syntax(krate: clean::Crate, cx: &DocContext<'_>) -> clean::Crate {
20     SyntaxChecker { cx }.fold_crate(krate)
21 }
22
23 struct SyntaxChecker<'a, 'tcx: 'a> {
24     cx: &'a DocContext<'tcx>,
25 }
26
27 impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
28     fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) {
29         let sess = ParseSess::new(FilePathMapping::empty());
30         let source_file = sess.source_map().new_source_file(
31             FileName::Custom(String::from("doctest")),
32             dox[code_block.code].to_owned(),
33         );
34
35         let errors = Lexer::new_or_buffered_errs(&sess, source_file, None).and_then(|mut lexer| {
36             while let Ok(token::Token { kind, .. }) = lexer.try_next_token() {
37                 if kind == token::Eof {
38                     break;
39                 }
40             }
41
42             let errors = lexer.buffer_fatal_errors();
43
44             if !errors.is_empty() {
45                 Err(errors)
46             } else {
47                 Ok(())
48             }
49         });
50
51         if let Err(errors) = errors {
52             let mut diag = if let Some(sp) =
53                 super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs)
54             {
55                 let mut diag = self
56                     .cx
57                     .sess()
58                     .struct_span_warn(sp, "could not parse code block as Rust code");
59
60                 for mut err in errors {
61                     diag.note(&format!("error from rustc: {}", err.message()));
62                     err.cancel();
63                 }
64
65                 if code_block.syntax.is_none() && code_block.is_fenced {
66                     let sp = sp.from_inner(InnerSpan::new(0, 3));
67                     diag.span_suggestion(
68                         sp,
69                         "mark blocks that do not contain Rust code as text",
70                         String::from("```text"),
71                         Applicability::MachineApplicable,
72                     );
73                 }
74
75                 diag
76             } else {
77                 // We couldn't calculate the span of the markdown block that had the error, so our
78                 // diagnostics are going to be a bit lacking.
79                 let mut diag = self.cx.sess().struct_span_warn(
80                     super::span_of_attrs(&item.attrs),
81                     "doc comment contains an invalid Rust code block",
82                 );
83
84                 for mut err in errors {
85                     // Don't bother reporting the error, because we can't show where it happened.
86                     err.cancel();
87                 }
88
89                 if code_block.syntax.is_none() && code_block.is_fenced {
90                     diag.help("mark blocks that do not contain Rust code as text: ```text");
91                 }
92
93                 diag
94             };
95
96             diag.emit();
97         }
98     }
99 }
100
101 impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
102     fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
103         if let Some(dox) = &item.attrs.collapsed_doc_value() {
104             for code_block in markdown::rust_code_blocks(&dox) {
105                 self.check_rust_syntax(&item, &dox, code_block);
106             }
107         }
108
109         self.fold_item_recur(item)
110     }
111 }