1use std::fs;
4use std::path::PathBuf;
5
6use rustc_data_structures::fx::FxIndexMap;
7use rustc_errors::DiagCtxtHandle;
8use rustc_hir::intravisit::{self, Visitor};
9use rustc_hir::{self as hir};
10use rustc_macros::{Decodable, Encodable};
11use rustc_middle::hir::nested_filter;
12use rustc_middle::ty::{self, TyCtxt};
13use rustc_serialize::opaque::{FileEncoder, MemDecoder};
14use rustc_serialize::{Decodable, Encodable};
15use rustc_session::getopts;
16use rustc_span::def_id::{CrateNum, DefPathHash, LOCAL_CRATE};
17use rustc_span::edition::Edition;
18use rustc_span::{BytePos, FileName, SourceFile};
19use tracing::{debug, trace, warn};
20
21use crate::formats::renderer::FormatRenderer;
22use crate::html::render::Context;
23use crate::{clean, config, formats};
24
25#[derive(Debug, Clone)]
26pub(crate) struct ScrapeExamplesOptions {
27 output_path: PathBuf,
28 target_crates: Vec<String>,
29 pub(crate) scrape_tests: bool,
30}
31
32impl ScrapeExamplesOptions {
33 pub(crate) fn new(matches: &getopts::Matches, dcx: DiagCtxtHandle<'_>) -> Option<Self> {
34 let output_path = matches.opt_str("scrape-examples-output-path");
35 let target_crates = matches.opt_strs("scrape-examples-target-crate");
36 let scrape_tests = matches.opt_present("scrape-tests");
37 match (output_path, !target_crates.is_empty(), scrape_tests) {
38 (Some(output_path), true, _) => Some(ScrapeExamplesOptions {
39 output_path: PathBuf::from(output_path),
40 target_crates,
41 scrape_tests,
42 }),
43 (Some(_), false, _) | (None, true, _) => {
44 dcx.fatal(
45 "must use --scrape-examples-output-path and --scrape-examples-target-crate \
46 together",
47 );
48 }
49 (None, false, true) => {
50 dcx.fatal(
51 "must use --scrape-examples-output-path and \
52 --scrape-examples-target-crate with --scrape-tests",
53 );
54 }
55 (None, false, false) => None,
56 }
57 }
58}
59
60#[derive(Encodable, Decodable, Debug, Clone)]
61pub(crate) struct SyntaxRange {
62 pub(crate) byte_span: (u32, u32),
63 pub(crate) line_span: (usize, usize),
64}
65
66impl SyntaxRange {
67 fn new(span: rustc_span::Span, file: &SourceFile) -> Option<Self> {
68 let get_pos = |bytepos: BytePos| file.original_relative_byte_pos(bytepos).0;
69 let get_line = |bytepos: BytePos| file.lookup_line(file.relative_position(bytepos));
70
71 Some(SyntaxRange {
72 byte_span: (get_pos(span.lo()), get_pos(span.hi())),
73 line_span: (get_line(span.lo())?, get_line(span.hi())?),
74 })
75 }
76}
77
78#[derive(Encodable, Decodable, Debug, Clone)]
79pub(crate) struct CallLocation {
80 pub(crate) call_expr: SyntaxRange,
81 pub(crate) call_ident: SyntaxRange,
82 pub(crate) enclosing_item: SyntaxRange,
83}
84
85impl CallLocation {
86 fn new(
87 expr_span: rustc_span::Span,
88 ident_span: rustc_span::Span,
89 enclosing_item_span: rustc_span::Span,
90 source_file: &SourceFile,
91 ) -> Option<Self> {
92 Some(CallLocation {
93 call_expr: SyntaxRange::new(expr_span, source_file)?,
94 call_ident: SyntaxRange::new(ident_span, source_file)?,
95 enclosing_item: SyntaxRange::new(enclosing_item_span, source_file)?,
96 })
97 }
98}
99
100#[derive(Encodable, Decodable, Debug, Clone)]
101pub(crate) struct CallData {
102 pub(crate) locations: Vec<CallLocation>,
103 pub(crate) url: String,
104 pub(crate) display_name: String,
105 pub(crate) edition: Edition,
106 pub(crate) is_bin: bool,
107}
108
109pub(crate) type FnCallLocations = FxIndexMap<PathBuf, CallData>;
110pub(crate) type AllCallLocations = FxIndexMap<DefPathHash, FnCallLocations>;
111
112struct FindCalls<'a, 'tcx> {
114 cx: Context<'tcx>,
115 target_crates: Vec<CrateNum>,
116 calls: &'a mut AllCallLocations,
117 bin_crate: bool,
118}
119
120impl<'a, 'tcx> Visitor<'tcx> for FindCalls<'a, 'tcx>
121where
122 'tcx: 'a,
123{
124 type NestedFilter = nested_filter::OnlyBodies;
125
126 fn nested_visit_map(&mut self) -> Self::Map {
127 self.cx.tcx().hir()
128 }
129
130 fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
131 intravisit::walk_expr(self, ex);
132
133 let tcx = self.cx.tcx();
134
135 let hir = tcx.hir();
139 if hir.maybe_body_owned_by(ex.hir_id.owner.def_id).is_none() {
140 return;
141 }
142
143 let (ty, call_span, ident_span) = match ex.kind {
145 hir::ExprKind::Call(f, _) => {
146 let types = tcx.typeck(ex.hir_id.owner.def_id);
147
148 if let Some(ty) = types.node_type_opt(f.hir_id) {
149 (ty, ex.span, f.span)
150 } else {
151 trace!("node_type_opt({}) = None", f.hir_id);
152 return;
153 }
154 }
155 hir::ExprKind::MethodCall(path, _, _, call_span) => {
156 let types = tcx.typeck(ex.hir_id.owner.def_id);
157 let Some(def_id) = types.type_dependent_def_id(ex.hir_id) else {
158 trace!("type_dependent_def_id({}) = None", ex.hir_id);
159 return;
160 };
161
162 let ident_span = path.ident.span;
163 (tcx.type_of(def_id).instantiate_identity(), call_span, ident_span)
164 }
165 _ => {
166 return;
167 }
168 };
169
170 if call_span.from_expansion() {
174 trace!("Rejecting expr from macro: {call_span:?}");
175 return;
176 }
177
178 let enclosing_item_span =
181 tcx.hir().span_with_body(tcx.hir().get_parent_item(ex.hir_id).into());
182 if enclosing_item_span.from_expansion() {
183 trace!("Rejecting expr ({call_span:?}) from macro item: {enclosing_item_span:?}");
184 return;
185 }
186
187 if !enclosing_item_span.contains(call_span) {
190 warn!(
191 "Attempted to scrape call at [{call_span:?}] whose enclosing item \
192 [{enclosing_item_span:?}] doesn't contain the span of the call."
193 );
194 return;
195 }
196
197 if !call_span.contains(ident_span) {
199 warn!(
200 "Attempted to scrape call at [{call_span:?}] whose identifier [{ident_span:?}] was \
201 not contained in the span of the call."
202 );
203 return;
204 }
205
206 if let ty::FnDef(def_id, _) = ty.kind() {
208 if self.target_crates.iter().all(|krate| *krate != def_id.krate) {
209 trace!("Rejecting expr from crate not being documented: {call_span:?}");
210 return;
211 }
212
213 let source_map = tcx.sess.source_map();
214 let file = source_map.lookup_char_pos(call_span.lo()).file;
215 let file_path = match file.name.clone() {
216 FileName::Real(real_filename) => real_filename.into_local_path(),
217 _ => None,
218 };
219
220 if let Some(file_path) = file_path {
221 let abs_path = match fs::canonicalize(file_path.clone()) {
222 Ok(abs_path) => abs_path,
223 Err(_) => {
224 trace!("Could not canonicalize file path: {}", file_path.display());
225 return;
226 }
227 };
228
229 let cx = &self.cx;
230 let clean_span = crate::clean::types::Span::new(call_span);
231 let url = match cx.href_from_span(clean_span, false) {
232 Some(url) => url,
233 None => {
234 trace!(
235 "Rejecting expr ({call_span:?}) whose clean span ({clean_span:?}) \
236 cannot be turned into a link"
237 );
238 return;
239 }
240 };
241
242 let mk_call_data = || {
243 let display_name = file_path.display().to_string();
244 let edition = call_span.edition();
245 let is_bin = self.bin_crate;
246
247 CallData { locations: Vec::new(), url, display_name, edition, is_bin }
248 };
249
250 let fn_key = tcx.def_path_hash(*def_id);
251 let fn_entries = self.calls.entry(fn_key).or_default();
252
253 trace!("Including expr: {call_span:?}");
254 let enclosing_item_span =
255 source_map.span_extend_to_prev_char(enclosing_item_span, '\n', false);
256 let location =
257 match CallLocation::new(call_span, ident_span, enclosing_item_span, &file) {
258 Some(location) => location,
259 None => {
260 trace!("Could not get serializable call location for {call_span:?}");
261 return;
262 }
263 };
264 fn_entries.entry(abs_path).or_insert_with(mk_call_data).locations.push(location);
265 }
266 }
267 }
268}
269
270pub(crate) fn run(
271 krate: clean::Crate,
272 mut renderopts: config::RenderOptions,
273 cache: formats::cache::Cache,
274 tcx: TyCtxt<'_>,
275 options: ScrapeExamplesOptions,
276 bin_crate: bool,
277) {
278 let inner = move || -> Result<(), String> {
279 renderopts.no_emit_shared = true;
281 let (cx, _) = Context::init(krate, renderopts, cache, tcx).map_err(|e| e.to_string())?;
282
283 let all_crates = tcx
287 .crates(())
288 .iter()
289 .chain([&LOCAL_CRATE])
290 .map(|crate_num| (crate_num, tcx.crate_name(*crate_num)))
291 .collect::<Vec<_>>();
292 let target_crates = options
293 .target_crates
294 .into_iter()
295 .flat_map(|target| all_crates.iter().filter(move |(_, name)| name.as_str() == target))
296 .map(|(crate_num, _)| **crate_num)
297 .collect::<Vec<_>>();
298
299 debug!("All crates in TyCtxt: {all_crates:?}");
300 debug!("Scrape examples target_crates: {target_crates:?}");
301
302 let mut calls = FxIndexMap::default();
304 let mut finder = FindCalls { calls: &mut calls, cx, target_crates, bin_crate };
305 tcx.hir().visit_all_item_likes_in_crate(&mut finder);
306
307 if tcx.dcx().has_errors().is_some() {
310 return Err(String::from("Compilation failed, aborting rustdoc"));
311 }
312
313 for fn_calls in calls.values_mut() {
315 for file_calls in fn_calls.values_mut() {
316 file_calls.locations.sort_by_key(|loc| loc.call_expr.byte_span.0);
317 }
318 }
319
320 let mut encoder = FileEncoder::new(options.output_path).map_err(|e| e.to_string())?;
322 calls.encode(&mut encoder);
323 encoder.finish().map_err(|(_path, e)| e.to_string())?;
324
325 Ok(())
326 };
327
328 if let Err(e) = inner() {
329 tcx.dcx().fatal(e);
330 }
331}
332
333pub(crate) fn load_call_locations(
336 with_examples: Vec<String>,
337 dcx: DiagCtxtHandle<'_>,
338) -> AllCallLocations {
339 let mut all_calls: AllCallLocations = FxIndexMap::default();
340 for path in with_examples {
341 let bytes = match fs::read(&path) {
342 Ok(bytes) => bytes,
343 Err(e) => dcx.fatal(format!("failed to load examples: {e}")),
344 };
345 let Ok(mut decoder) = MemDecoder::new(&bytes, 0) else {
346 dcx.fatal(format!("Corrupt metadata encountered in {path}"))
347 };
348 let calls = AllCallLocations::decode(&mut decoder);
349
350 for (function, fn_calls) in calls.into_iter() {
351 all_calls.entry(function).or_default().extend(fn_calls.into_iter());
352 }
353 }
354
355 all_calls
356}