|
| 1 | +//! Doctest functionality used only for doctests in `.rs` source files. |
| 2 | +
|
| 3 | +use std::env; |
| 4 | + |
| 5 | +use rustc_data_structures::{fx::FxHashSet, sync::Lrc}; |
| 6 | +use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID}; |
| 7 | +use rustc_hir::{self as hir, intravisit, CRATE_HIR_ID}; |
| 8 | +use rustc_middle::hir::map::Map; |
| 9 | +use rustc_middle::hir::nested_filter; |
| 10 | +use rustc_middle::ty::TyCtxt; |
| 11 | +use rustc_resolve::rustdoc::span_of_fragments; |
| 12 | +use rustc_session::Session; |
| 13 | +use rustc_span::source_map::SourceMap; |
| 14 | +use rustc_span::{BytePos, FileName, Pos, Span, DUMMY_SP}; |
| 15 | + |
| 16 | +use super::{DoctestVisitor, ScrapedDoctest}; |
| 17 | +use crate::clean::{types::AttributesExt, Attributes}; |
| 18 | +use crate::html::markdown::{self, ErrorCodes, LangString, MdRelLine}; |
| 19 | + |
| 20 | +struct RustCollector { |
| 21 | + source_map: Lrc<SourceMap>, |
| 22 | + tests: Vec<ScrapedDoctest>, |
| 23 | + cur_path: Vec<String>, |
| 24 | + position: Span, |
| 25 | +} |
| 26 | + |
| 27 | +impl RustCollector { |
| 28 | + fn get_filename(&self) -> FileName { |
| 29 | + let filename = self.source_map.span_to_filename(self.position); |
| 30 | + if let FileName::Real(ref filename) = filename |
| 31 | + && let Ok(cur_dir) = env::current_dir() |
| 32 | + && let Some(local_path) = filename.local_path() |
| 33 | + && let Ok(path) = local_path.strip_prefix(&cur_dir) |
| 34 | + { |
| 35 | + return path.to_owned().into(); |
| 36 | + } |
| 37 | + filename |
| 38 | + } |
| 39 | + |
| 40 | + fn get_base_line(&self) -> usize { |
| 41 | + let sp_lo = self.position.lo().to_usize(); |
| 42 | + let loc = self.source_map.lookup_char_pos(BytePos(sp_lo as u32)); |
| 43 | + loc.line |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl DoctestVisitor for RustCollector { |
| 48 | + fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine) { |
| 49 | + let line = self.get_base_line() + rel_line.offset(); |
| 50 | + self.tests.push(ScrapedDoctest { |
| 51 | + filename: self.get_filename(), |
| 52 | + line, |
| 53 | + logical_path: self.cur_path.clone(), |
| 54 | + langstr: config, |
| 55 | + text: test, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + fn visit_header(&mut self, _name: &str, _level: u32) {} |
| 60 | +} |
| 61 | + |
| 62 | +pub(super) struct HirCollector<'a, 'tcx> { |
| 63 | + sess: &'a Session, |
| 64 | + map: Map<'tcx>, |
| 65 | + codes: ErrorCodes, |
| 66 | + tcx: TyCtxt<'tcx>, |
| 67 | + enable_per_target_ignores: bool, |
| 68 | + collector: RustCollector, |
| 69 | +} |
| 70 | + |
| 71 | +impl<'a, 'tcx> HirCollector<'a, 'tcx> { |
| 72 | + pub fn new( |
| 73 | + sess: &'a Session, |
| 74 | + map: Map<'tcx>, |
| 75 | + codes: ErrorCodes, |
| 76 | + enable_per_target_ignores: bool, |
| 77 | + tcx: TyCtxt<'tcx>, |
| 78 | + ) -> Self { |
| 79 | + let collector = RustCollector { |
| 80 | + source_map: sess.psess.clone_source_map(), |
| 81 | + cur_path: vec![], |
| 82 | + position: DUMMY_SP, |
| 83 | + tests: vec![], |
| 84 | + }; |
| 85 | + Self { sess, map, codes, enable_per_target_ignores, tcx, collector } |
| 86 | + } |
| 87 | + |
| 88 | + pub fn collect_crate(mut self) -> Vec<ScrapedDoctest> { |
| 89 | + let tcx = self.tcx; |
| 90 | + self.visit_testable("".to_string(), CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID), |this| { |
| 91 | + tcx.hir().walk_toplevel_module(this) |
| 92 | + }); |
| 93 | + self.collector.tests |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl<'a, 'tcx> HirCollector<'a, 'tcx> { |
| 98 | + fn visit_testable<F: FnOnce(&mut Self)>( |
| 99 | + &mut self, |
| 100 | + name: String, |
| 101 | + def_id: LocalDefId, |
| 102 | + sp: Span, |
| 103 | + nested: F, |
| 104 | + ) { |
| 105 | + let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id)); |
| 106 | + if let Some(ref cfg) = ast_attrs.cfg(self.tcx, &FxHashSet::default()) { |
| 107 | + if !cfg.matches(&self.sess.psess, Some(self.tcx.features())) { |
| 108 | + return; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + let has_name = !name.is_empty(); |
| 113 | + if has_name { |
| 114 | + self.collector.cur_path.push(name); |
| 115 | + } |
| 116 | + |
| 117 | + // The collapse-docs pass won't combine sugared/raw doc attributes, or included files with |
| 118 | + // anything else, this will combine them for us. |
| 119 | + let attrs = Attributes::from_ast(ast_attrs); |
| 120 | + if let Some(doc) = attrs.opt_doc_value() { |
| 121 | + // Use the outermost invocation, so that doctest names come from where the docs were written. |
| 122 | + let span = ast_attrs |
| 123 | + .iter() |
| 124 | + .find(|attr| attr.doc_str().is_some()) |
| 125 | + .map(|attr| attr.span.ctxt().outer_expn().expansion_cause().unwrap_or(attr.span)) |
| 126 | + .unwrap_or(DUMMY_SP); |
| 127 | + self.collector.position = span; |
| 128 | + markdown::find_testable_code( |
| 129 | + &doc, |
| 130 | + &mut self.collector, |
| 131 | + self.codes, |
| 132 | + self.enable_per_target_ignores, |
| 133 | + Some(&crate::html::markdown::ExtraInfo::new( |
| 134 | + self.tcx, |
| 135 | + def_id.to_def_id(), |
| 136 | + span_of_fragments(&attrs.doc_strings).unwrap_or(sp), |
| 137 | + )), |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + nested(self); |
| 142 | + |
| 143 | + if has_name { |
| 144 | + self.collector.cur_path.pop(); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +impl<'a, 'tcx> intravisit::Visitor<'tcx> for HirCollector<'a, 'tcx> { |
| 150 | + type NestedFilter = nested_filter::All; |
| 151 | + |
| 152 | + fn nested_visit_map(&mut self) -> Self::Map { |
| 153 | + self.map |
| 154 | + } |
| 155 | + |
| 156 | + fn visit_item(&mut self, item: &'tcx hir::Item<'_>) { |
| 157 | + let name = match &item.kind { |
| 158 | + hir::ItemKind::Impl(impl_) => { |
| 159 | + rustc_hir_pretty::id_to_string(&self.map, impl_.self_ty.hir_id) |
| 160 | + } |
| 161 | + _ => item.ident.to_string(), |
| 162 | + }; |
| 163 | + |
| 164 | + self.visit_testable(name, item.owner_id.def_id, item.span, |this| { |
| 165 | + intravisit::walk_item(this, item); |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + fn visit_trait_item(&mut self, item: &'tcx hir::TraitItem<'_>) { |
| 170 | + self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| { |
| 171 | + intravisit::walk_trait_item(this, item); |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + fn visit_impl_item(&mut self, item: &'tcx hir::ImplItem<'_>) { |
| 176 | + self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| { |
| 177 | + intravisit::walk_impl_item(this, item); |
| 178 | + }); |
| 179 | + } |
| 180 | + |
| 181 | + fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'_>) { |
| 182 | + self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| { |
| 183 | + intravisit::walk_foreign_item(this, item); |
| 184 | + }); |
| 185 | + } |
| 186 | + |
| 187 | + fn visit_variant(&mut self, v: &'tcx hir::Variant<'_>) { |
| 188 | + self.visit_testable(v.ident.to_string(), v.def_id, v.span, |this| { |
| 189 | + intravisit::walk_variant(this, v); |
| 190 | + }); |
| 191 | + } |
| 192 | + |
| 193 | + fn visit_field_def(&mut self, f: &'tcx hir::FieldDef<'_>) { |
| 194 | + self.visit_testable(f.ident.to_string(), f.def_id, f.span, |this| { |
| 195 | + intravisit::walk_field_def(this, f); |
| 196 | + }); |
| 197 | + } |
| 198 | +} |
0 commit comments