]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/core.rs
db6e01838048f9d5edf1f2801633bde781dd69d7
[rust.git] / src / librustdoc / core.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use rustc_lint;
12 use rustc_driver::{self, driver, target_features, abort_on_err};
13 use rustc::session::{self, config};
14 use rustc::hir::def_id::{DefId, DefIndex, DefIndexAddressSpace, CrateNum, LOCAL_CRATE};
15 use rustc::hir::def::Def;
16 use rustc::hir::{self, HirVec};
17 use rustc::middle::cstore::CrateStore;
18 use rustc::middle::privacy::AccessLevels;
19 use rustc::ty::{self, TyCtxt, AllArenas};
20 use rustc::hir::map as hir_map;
21 use rustc::lint::{self, LintPass};
22 use rustc::session::config::ErrorOutputType;
23 use rustc::util::nodemap::{FxHashMap, FxHashSet};
24 use rustc_resolve as resolve;
25 use rustc_metadata::creader::CrateLoader;
26 use rustc_metadata::cstore::CStore;
27 use rustc_target::spec::TargetTriple;
28
29 use syntax::ast::{self, Ident};
30 use syntax::source_map;
31 use syntax::edition::Edition;
32 use syntax::feature_gate::UnstableFeatures;
33 use syntax::json::JsonEmitter;
34 use syntax::ptr::P;
35 use syntax::symbol::keywords;
36 use syntax_pos::DUMMY_SP;
37 use errors;
38 use errors::emitter::{Emitter, EmitterWriter};
39 use parking_lot::ReentrantMutex;
40
41 use std::cell::RefCell;
42 use std::mem;
43 use rustc_data_structures::sync::{self, Lrc};
44 use std::rc::Rc;
45 use std::sync::Arc;
46 use std::path::PathBuf;
47
48 use visit_ast::RustdocVisitor;
49 use clean;
50 use clean::{get_path_for_type, Clean, MAX_DEF_ID, AttributesExt};
51 use html::render::RenderInfo;
52 use passes;
53
54 pub use rustc::session::config::{Input, Options, CodegenOptions};
55 pub use rustc::session::search_paths::SearchPaths;
56
57 pub type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;
58
59 pub struct DocContext<'a, 'tcx: 'a, 'rcx: 'a, 'cstore: 'rcx> {
60     pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
61     pub resolver: &'a RefCell<resolve::Resolver<'rcx, 'cstore>>,
62     /// The stack of module NodeIds up till this point
63     pub crate_name: Option<String>,
64     pub cstore: Rc<CStore>,
65     // Note that external items for which `doc(hidden)` applies to are shown as
66     // non-reachable while local items aren't. This is because we're reusing
67     // the access levels from crateanalysis.
68     /// Later on moved into `html::render::CACHE_KEY`
69     pub renderinfo: RefCell<RenderInfo>,
70     /// Later on moved through `clean::Crate` into `html::render::CACHE_KEY`
71     pub external_traits: Arc<ReentrantMutex<RefCell<FxHashMap<DefId, clean::Trait>>>>,
72     /// Used while populating `external_traits` to ensure we don't process the same trait twice at
73     /// the same time.
74     pub active_extern_traits: RefCell<Vec<DefId>>,
75     // The current set of type and lifetime substitutions,
76     // for expanding type aliases at the HIR level:
77
78     /// Table type parameter definition -> substituted type
79     pub ty_substs: RefCell<FxHashMap<Def, clean::Type>>,
80     /// Table node id of lifetime parameter definition -> substituted lifetime
81     pub lt_substs: RefCell<FxHashMap<DefId, clean::Lifetime>>,
82     /// Table DefId of `impl Trait` in argument position -> bounds
83     pub impl_trait_bounds: RefCell<FxHashMap<DefId, Vec<clean::GenericBound>>>,
84     pub send_trait: Option<DefId>,
85     pub fake_def_ids: RefCell<FxHashMap<CrateNum, DefId>>,
86     pub all_fake_def_ids: RefCell<FxHashSet<DefId>>,
87     /// Maps (type_id, trait_id) -> auto trait impl
88     pub generated_synthetics: RefCell<FxHashSet<(DefId, DefId)>>,
89     pub all_traits: Vec<DefId>,
90 }
91
92 impl<'a, 'tcx, 'rcx, 'cstore> DocContext<'a, 'tcx, 'rcx, 'cstore> {
93     pub fn sess(&self) -> &session::Session {
94         &self.tcx.sess
95     }
96
97     /// Call the closure with the given parameters set as
98     /// the substitutions for a type alias' RHS.
99     pub fn enter_alias<F, R>(&self,
100                              ty_substs: FxHashMap<Def, clean::Type>,
101                              lt_substs: FxHashMap<DefId, clean::Lifetime>,
102                              f: F) -> R
103     where F: FnOnce() -> R {
104         let (old_tys, old_lts) =
105             (mem::replace(&mut *self.ty_substs.borrow_mut(), ty_substs),
106              mem::replace(&mut *self.lt_substs.borrow_mut(), lt_substs));
107         let r = f();
108         *self.ty_substs.borrow_mut() = old_tys;
109         *self.lt_substs.borrow_mut() = old_lts;
110         r
111     }
112
113     // This is an ugly hack, but it's the simplest way to handle synthetic impls without greatly
114     // refactoring either librustdoc or librustc. In particular, allowing new DefIds to be
115     // registered after the AST is constructed would require storing the defid mapping in a
116     // RefCell, decreasing the performance for normal compilation for very little gain.
117     //
118     // Instead, we construct 'fake' def ids, which start immediately after the last DefId in
119     // DefIndexAddressSpace::Low. In the Debug impl for clean::Item, we explicitly check for fake
120     // def ids, as we'll end up with a panic if we use the DefId Debug impl for fake DefIds
121     pub fn next_def_id(&self, crate_num: CrateNum) -> DefId {
122         let start_def_id = {
123             let next_id = if crate_num == LOCAL_CRATE {
124                 self.tcx
125                     .hir
126                     .definitions()
127                     .def_path_table()
128                     .next_id(DefIndexAddressSpace::Low)
129             } else {
130                 self.cstore
131                     .def_path_table(crate_num)
132                     .next_id(DefIndexAddressSpace::Low)
133             };
134
135             DefId {
136                 krate: crate_num,
137                 index: next_id,
138             }
139         };
140
141         let mut fake_ids = self.fake_def_ids.borrow_mut();
142
143         let def_id = fake_ids.entry(crate_num).or_insert(start_def_id).clone();
144         fake_ids.insert(
145             crate_num,
146             DefId {
147                 krate: crate_num,
148                 index: DefIndex::from_array_index(
149                     def_id.index.as_array_index() + 1,
150                     def_id.index.address_space(),
151                 ),
152             },
153         );
154
155         MAX_DEF_ID.with(|m| {
156             m.borrow_mut()
157                 .entry(def_id.krate.clone())
158                 .or_insert(start_def_id);
159         });
160
161         self.all_fake_def_ids.borrow_mut().insert(def_id);
162
163         def_id.clone()
164     }
165
166     pub fn get_real_ty<F>(&self,
167                           def_id: DefId,
168                           def_ctor: &F,
169                           real_name: &Option<Ident>,
170                           generics: &ty::Generics,
171     ) -> hir::Ty
172     where F: Fn(DefId) -> Def {
173         let path = get_path_for_type(self.tcx, def_id, def_ctor);
174         let mut segments = path.segments.into_vec();
175         let last = segments.pop().expect("segments were empty");
176
177         segments.push(hir::PathSegment::new(
178             real_name.unwrap_or(last.ident),
179             self.generics_to_path_params(generics.clone()),
180             false,
181         ));
182
183         let new_path = hir::Path {
184             span: path.span,
185             def: path.def,
186             segments: HirVec::from_vec(segments),
187         };
188
189         hir::Ty {
190             id: ast::DUMMY_NODE_ID,
191             node: hir::TyKind::Path(hir::QPath::Resolved(None, P(new_path))),
192             span: DUMMY_SP,
193             hir_id: hir::DUMMY_HIR_ID,
194         }
195     }
196
197     pub fn generics_to_path_params(&self, generics: ty::Generics) -> hir::GenericArgs {
198         let mut args = vec![];
199
200         for param in generics.params.iter() {
201             match param.kind {
202                 ty::GenericParamDefKind::Lifetime => {
203                     let name = if param.name == "" {
204                         hir::ParamName::Plain(keywords::StaticLifetime.ident())
205                     } else {
206                         hir::ParamName::Plain(ast::Ident::from_interned_str(param.name))
207                     };
208
209                     args.push(hir::GenericArg::Lifetime(hir::Lifetime {
210                         id: ast::DUMMY_NODE_ID,
211                         span: DUMMY_SP,
212                         name: hir::LifetimeName::Param(name),
213                     }));
214                 }
215                 ty::GenericParamDefKind::Type {..} => {
216                     args.push(hir::GenericArg::Type(self.ty_param_to_ty(param.clone())));
217                 }
218             }
219         }
220
221         hir::GenericArgs {
222             args: HirVec::from_vec(args),
223             bindings: HirVec::new(),
224             parenthesized: false,
225         }
226     }
227
228     pub fn ty_param_to_ty(&self, param: ty::GenericParamDef) -> hir::Ty {
229         debug!("ty_param_to_ty({:?}) {:?}", param, param.def_id);
230         hir::Ty {
231             id: ast::DUMMY_NODE_ID,
232             node: hir::TyKind::Path(hir::QPath::Resolved(
233                 None,
234                 P(hir::Path {
235                     span: DUMMY_SP,
236                     def: Def::TyParam(param.def_id),
237                     segments: HirVec::from_vec(vec![
238                         hir::PathSegment::from_ident(Ident::from_interned_str(param.name))
239                     ]),
240                 }),
241             )),
242             span: DUMMY_SP,
243             hir_id: hir::DUMMY_HIR_ID,
244         }
245     }
246 }
247
248 pub trait DocAccessLevels {
249     fn is_doc_reachable(&self, did: DefId) -> bool;
250 }
251
252 impl DocAccessLevels for AccessLevels<DefId> {
253     fn is_doc_reachable(&self, did: DefId) -> bool {
254         self.is_public(did)
255     }
256 }
257
258 /// Creates a new diagnostic `Handler` that can be used to emit warnings and errors.
259 ///
260 /// If the given `error_format` is `ErrorOutputType::Json` and no `SourceMap` is given, a new one
261 /// will be created for the handler.
262 pub fn new_handler(error_format: ErrorOutputType,
263                    source_map: Option<Lrc<source_map::SourceMap>>,
264                    treat_err_as_bug: bool,
265 ) -> errors::Handler {
266     // rustdoc doesn't override (or allow to override) anything from this that is relevant here, so
267     // stick to the defaults
268     let sessopts = Options::default();
269     let emitter: Box<dyn Emitter + sync::Send> = match error_format {
270         ErrorOutputType::HumanReadable(color_config) => Box::new(
271             EmitterWriter::stderr(
272                 color_config,
273                 source_map.map(|cm| cm as _),
274                 false,
275                 sessopts.debugging_opts.teach,
276             ).ui_testing(sessopts.debugging_opts.ui_testing)
277         ),
278         ErrorOutputType::Json(pretty) => {
279             let source_map = source_map.unwrap_or_else(
280                 || Lrc::new(source_map::SourceMap::new(sessopts.file_path_mapping())));
281             Box::new(
282                 JsonEmitter::stderr(
283                     None,
284                     source_map,
285                     pretty,
286                 ).ui_testing(sessopts.debugging_opts.ui_testing)
287             )
288         },
289         ErrorOutputType::Short(color_config) => Box::new(
290             EmitterWriter::stderr(
291                 color_config,
292                 source_map.map(|cm| cm as _),
293                 true,
294                 false)
295         ),
296     };
297
298     errors::Handler::with_emitter_and_flags(
299         emitter,
300         errors::HandlerFlags {
301             can_emit_warnings: true,
302             treat_err_as_bug,
303             report_delayed_bugs: false,
304             external_macro_backtrace: false,
305             ..Default::default()
306         },
307     )
308 }
309
310 pub fn run_core(search_paths: SearchPaths,
311                 cfgs: Vec<String>,
312                 externs: config::Externs,
313                 input: Input,
314                 triple: Option<TargetTriple>,
315                 maybe_sysroot: Option<PathBuf>,
316                 allow_warnings: bool,
317                 crate_name: Option<String>,
318                 force_unstable_if_unmarked: bool,
319                 edition: Edition,
320                 cg: CodegenOptions,
321                 error_format: ErrorOutputType,
322                 cmd_lints: Vec<(String, lint::Level)>,
323                 lint_cap: Option<lint::Level>,
324                 describe_lints: bool,
325                 mut manual_passes: Vec<String>,
326                 mut default_passes: passes::DefaultPassOption,
327                 treat_err_as_bug: bool,
328 ) -> (clean::Crate, RenderInfo, Vec<String>) {
329     // Parse, resolve, and typecheck the given crate.
330
331     let cpath = match input {
332         Input::File(ref p) => Some(p.clone()),
333         _ => None
334     };
335
336     let intra_link_resolution_failure_name = lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE.name;
337     let warnings_lint_name = lint::builtin::WARNINGS.name;
338     let missing_docs = rustc_lint::builtin::MISSING_DOCS.name;
339
340     // In addition to those specific lints, we also need to whitelist those given through
341     // command line, otherwise they'll get ignored and we don't want that.
342     let mut whitelisted_lints = vec![warnings_lint_name.to_owned(),
343                                      intra_link_resolution_failure_name.to_owned(),
344                                      missing_docs.to_owned()];
345
346     whitelisted_lints.extend(cmd_lints.iter().map(|(lint, _)| lint).cloned());
347
348     let lints = lint::builtin::HardwiredLints.get_lints()
349                     .into_iter()
350                     .chain(rustc_lint::SoftLints.get_lints().into_iter())
351                     .filter_map(|lint| {
352                         if lint.name == warnings_lint_name ||
353                            lint.name == intra_link_resolution_failure_name {
354                             None
355                         } else {
356                             Some((lint.name_lower(), lint::Allow))
357                         }
358                     })
359                     .chain(cmd_lints.into_iter())
360                     .collect::<Vec<_>>();
361
362     let host_triple = TargetTriple::from_triple(config::host_triple());
363     // plays with error output here!
364     let sessopts = config::Options {
365         maybe_sysroot,
366         search_paths,
367         crate_types: vec![config::CrateType::Rlib],
368         lint_opts: if !allow_warnings {
369             lints
370         } else {
371             vec![]
372         },
373         lint_cap: Some(lint_cap.unwrap_or_else(|| lint::Forbid)),
374         cg,
375         externs,
376         target_triple: triple.unwrap_or(host_triple),
377         // Ensure that rustdoc works even if rustc is feature-staged
378         unstable_features: UnstableFeatures::Allow,
379         actually_rustdoc: true,
380         debugging_opts: config::DebuggingOptions {
381             force_unstable_if_unmarked,
382             ..config::basic_debugging_options()
383         },
384         error_format,
385         edition,
386         describe_lints,
387         ..Options::default()
388     };
389     driver::spawn_thread_pool(sessopts, move |sessopts| {
390         let source_map = Lrc::new(source_map::SourceMap::new(sessopts.file_path_mapping()));
391         let diagnostic_handler = new_handler(error_format,
392                                              Some(source_map.clone()),
393                                              treat_err_as_bug);
394
395         let mut sess = session::build_session_(
396             sessopts, cpath, diagnostic_handler, source_map,
397         );
398
399         lint::builtin::HardwiredLints.get_lints()
400                                      .into_iter()
401                                      .chain(rustc_lint::SoftLints.get_lints().into_iter())
402                                      .filter_map(|lint| {
403                                          // We don't want to whitelist *all* lints so let's
404                                          // ignore those ones.
405                                          if whitelisted_lints.iter().any(|l| &lint.name == l) {
406                                              None
407                                          } else {
408                                              Some(lint)
409                                          }
410                                      })
411                                      .for_each(|l| {
412                                          sess.driver_lint_caps.insert(lint::LintId::of(l),
413                                                                       lint::Allow);
414                                      });
415
416         let codegen_backend = rustc_driver::get_codegen_backend(&sess);
417         let cstore = Rc::new(CStore::new(codegen_backend.metadata_loader()));
418         rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
419
420         let mut cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs));
421         target_features::add_configuration(&mut cfg, &sess, &*codegen_backend);
422         sess.parse_sess.config = cfg;
423
424         let control = &driver::CompileController::basic();
425
426         let krate = panictry!(driver::phase_1_parse_input(control, &sess, &input));
427
428         let name = match crate_name {
429             Some(ref crate_name) => crate_name.clone(),
430             None => ::rustc_codegen_utils::link::find_crate_name(Some(&sess), &krate.attrs, &input),
431         };
432
433         let mut crate_loader = CrateLoader::new(&sess, &cstore, &name);
434
435         let resolver_arenas = resolve::Resolver::arenas();
436         let result = driver::phase_2_configure_and_expand_inner(&sess,
437                                                         &cstore,
438                                                         krate,
439                                                         None,
440                                                         &name,
441                                                         None,
442                                                         resolve::MakeGlobMap::No,
443                                                         &resolver_arenas,
444                                                         &mut crate_loader,
445                                                         |_| Ok(()));
446         let driver::InnerExpansionResult {
447             mut hir_forest,
448             mut resolver,
449             ..
450         } = abort_on_err(result, &sess);
451
452         resolver.ignore_extern_prelude_feature = true;
453
454         // We need to hold on to the complete resolver, so we clone everything
455         // for the analysis passes to use. Suboptimal, but necessary in the
456         // current architecture.
457         let defs = resolver.definitions.clone();
458         let resolutions = ty::Resolutions {
459             freevars: resolver.freevars.clone(),
460             export_map: resolver.export_map.clone(),
461             trait_map: resolver.trait_map.clone(),
462             maybe_unused_trait_imports: resolver.maybe_unused_trait_imports.clone(),
463             maybe_unused_extern_crates: resolver.maybe_unused_extern_crates.clone(),
464         };
465         let analysis = ty::CrateAnalysis {
466             access_levels: Lrc::new(AccessLevels::default()),
467             name: name.to_string(),
468             glob_map: if resolver.make_glob_map { Some(resolver.glob_map.clone()) } else { None },
469         };
470
471         let arenas = AllArenas::new();
472         let hir_map = hir_map::map_crate(&sess, &*cstore, &mut hir_forest, &defs);
473         let output_filenames = driver::build_output_filenames(&input,
474                                                             &None,
475                                                             &None,
476                                                             &[],
477                                                             &sess);
478
479         let resolver = RefCell::new(resolver);
480         abort_on_err(driver::phase_3_run_analysis_passes(&*codegen_backend,
481                                                         control,
482                                                         &sess,
483                                                         &*cstore,
484                                                         hir_map,
485                                                         analysis,
486                                                         resolutions,
487                                                         &arenas,
488                                                         &name,
489                                                         &output_filenames,
490                                                         |tcx, analysis, _, result| {
491             if result.is_err() {
492                 sess.fatal("Compilation failed, aborting rustdoc");
493             }
494
495             let ty::CrateAnalysis { access_levels, .. } = analysis;
496
497             // Convert from a NodeId set to a DefId set since we don't always have easy access
498             // to the map from defid -> nodeid
499             let access_levels = AccessLevels {
500                 map: access_levels.map.iter()
501                                     .map(|(&k, &v)| (tcx.hir.local_def_id(k), v))
502                                     .collect()
503             };
504
505             let send_trait = if crate_name == Some("core".to_string()) {
506                 clean::path_to_def_local(&tcx, &["marker", "Send"])
507             } else {
508                 clean::path_to_def(&tcx, &["core", "marker", "Send"])
509             };
510
511             let mut renderinfo = RenderInfo::default();
512             renderinfo.access_levels = access_levels;
513
514             let ctxt = DocContext {
515                 tcx,
516                 resolver: &resolver,
517                 crate_name,
518                 cstore: cstore.clone(),
519                 external_traits: Default::default(),
520                 active_extern_traits: Default::default(),
521                 renderinfo: RefCell::new(renderinfo),
522                 ty_substs: Default::default(),
523                 lt_substs: Default::default(),
524                 impl_trait_bounds: Default::default(),
525                 send_trait: send_trait,
526                 fake_def_ids: RefCell::new(FxHashMap()),
527                 all_fake_def_ids: RefCell::new(FxHashSet()),
528                 generated_synthetics: RefCell::new(FxHashSet()),
529                 all_traits: tcx.all_traits(LOCAL_CRATE).to_vec(),
530             };
531             debug!("crate: {:?}", tcx.hir.krate());
532
533             let mut krate = {
534                 let mut v = RustdocVisitor::new(&ctxt);
535                 v.visit(tcx.hir.krate());
536                 v.clean(&ctxt)
537             };
538
539             fn report_deprecated_attr(name: &str, diag: &errors::Handler) {
540                 let mut msg = diag.struct_warn(&format!("the `#![doc({})]` attribute is \
541                                                          considered deprecated", name));
542                 msg.warn("please see https://github.com/rust-lang/rust/issues/44136");
543
544                 if name == "no_default_passes" {
545                     msg.help("you may want to use `#![doc(document_private_items)]`");
546                 }
547
548                 msg.emit();
549             }
550
551             // Process all of the crate attributes, extracting plugin metadata along
552             // with the passes which we are supposed to run.
553             for attr in krate.module.as_ref().unwrap().attrs.lists("doc") {
554                 let diag = ctxt.sess().diagnostic();
555
556                 let name = attr.name().map(|s| s.as_str());
557                 let name = name.as_ref().map(|s| &s[..]);
558                 if attr.is_word() {
559                     if name == Some("no_default_passes") {
560                         report_deprecated_attr("no_default_passes", diag);
561                         if default_passes == passes::DefaultPassOption::Default {
562                             default_passes = passes::DefaultPassOption::None;
563                         }
564                     }
565                 } else if let Some(value) = attr.value_str() {
566                     let sink = match name {
567                         Some("passes") => {
568                             report_deprecated_attr("passes = \"...\"", diag);
569                             &mut manual_passes
570                         },
571                         Some("plugins") => {
572                             report_deprecated_attr("plugins = \"...\"", diag);
573                             eprintln!("WARNING: #![doc(plugins = \"...\")] no longer functions; \
574                                       see CVE-2018-1000622");
575                             continue
576                         },
577                         _ => continue,
578                     };
579                     for p in value.as_str().split_whitespace() {
580                         sink.push(p.to_string());
581                     }
582                 }
583
584                 if attr.is_word() && name == Some("document_private_items") {
585                     if default_passes == passes::DefaultPassOption::Default {
586                         default_passes = passes::DefaultPassOption::Private;
587                     }
588                 }
589             }
590
591             let mut passes: Vec<String> =
592                 passes::defaults(default_passes).iter().map(|p| p.to_string()).collect();
593             passes.extend(manual_passes);
594
595             for pass in &passes {
596                 // the "unknown pass" error will be reported when late passes are run
597                 if let Some(pass) = passes::find_pass(pass).and_then(|p| p.early_fn()) {
598                     krate = pass(krate, &ctxt);
599                 }
600             }
601
602             ctxt.sess().abort_if_errors();
603
604             (krate, ctxt.renderinfo.into_inner(), passes)
605         }), &sess)
606     })
607 }