2 use rustc_driver::{self, driver, target_features, abort_on_err};
3 use rustc::session::{self, config};
4 use rustc::hir::def_id::{DefId, DefIndex, DefIndexAddressSpace, CrateNum, LOCAL_CRATE};
5 use rustc::hir::def::Def;
6 use rustc::hir::{self, HirVec};
7 use rustc::middle::cstore::CrateStore;
8 use rustc::middle::privacy::AccessLevels;
9 use rustc::ty::{self, TyCtxt, AllArenas};
10 use rustc::hir::map as hir_map;
11 use rustc::lint::{self, LintPass};
12 use rustc::session::config::ErrorOutputType;
13 use rustc::util::nodemap::{FxHashMap, FxHashSet};
14 use rustc_resolve as resolve;
15 use rustc_metadata::creader::CrateLoader;
16 use rustc_metadata::cstore::CStore;
17 use rustc_target::spec::TargetTriple;
19 use syntax::ast::{self, Ident, NodeId};
20 use syntax::source_map;
21 use syntax::feature_gate::UnstableFeatures;
22 use syntax::json::JsonEmitter;
24 use syntax::symbol::keywords;
25 use syntax_pos::DUMMY_SP;
26 use errors::{self, FatalError};
27 use errors::emitter::{Emitter, EmitterWriter};
28 use parking_lot::ReentrantMutex;
30 use std::cell::RefCell;
32 use rustc_data_structures::sync::{self, Lrc};
36 use visit_ast::RustdocVisitor;
37 use config::{Options as RustdocOptions, RenderOptions};
39 use clean::{get_path_for_type, Clean, MAX_DEF_ID, AttributesExt};
40 use html::render::RenderInfo;
43 pub use rustc::session::config::{Input, Options, CodegenOptions};
44 pub use rustc::session::search_paths::SearchPath;
46 pub type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;
48 pub struct DocContext<'a, 'tcx: 'a, 'rcx: 'a> {
49 pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
50 pub resolver: &'a RefCell<resolve::Resolver<'rcx>>,
51 /// The stack of module NodeIds up till this point
52 pub crate_name: Option<String>,
53 pub cstore: Rc<CStore>,
54 // Note that external items for which `doc(hidden)` applies to are shown as
55 // non-reachable while local items aren't. This is because we're reusing
56 // the access levels from crateanalysis.
57 /// Later on moved into `html::render::CACHE_KEY`
58 pub renderinfo: RefCell<RenderInfo>,
59 /// Later on moved through `clean::Crate` into `html::render::CACHE_KEY`
60 pub external_traits: Arc<ReentrantMutex<RefCell<FxHashMap<DefId, clean::Trait>>>>,
61 /// Used while populating `external_traits` to ensure we don't process the same trait twice at
63 pub active_extern_traits: RefCell<Vec<DefId>>,
64 // The current set of type and lifetime substitutions,
65 // for expanding type aliases at the HIR level:
67 /// Table type parameter definition -> substituted type
68 pub ty_substs: RefCell<FxHashMap<Def, clean::Type>>,
69 /// Table node id of lifetime parameter definition -> substituted lifetime
70 pub lt_substs: RefCell<FxHashMap<DefId, clean::Lifetime>>,
71 /// Table DefId of `impl Trait` in argument position -> bounds
72 pub impl_trait_bounds: RefCell<FxHashMap<DefId, Vec<clean::GenericBound>>>,
73 pub send_trait: Option<DefId>,
74 pub fake_def_ids: RefCell<FxHashMap<CrateNum, DefId>>,
75 pub all_fake_def_ids: RefCell<FxHashSet<DefId>>,
76 /// Maps (type_id, trait_id) -> auto trait impl
77 pub generated_synthetics: RefCell<FxHashSet<(DefId, DefId)>>,
78 pub all_traits: Vec<DefId>,
81 impl<'a, 'tcx, 'rcx> DocContext<'a, 'tcx, 'rcx> {
82 pub fn sess(&self) -> &session::Session {
86 /// Call the closure with the given parameters set as
87 /// the substitutions for a type alias' RHS.
88 pub fn enter_alias<F, R>(&self,
89 ty_substs: FxHashMap<Def, clean::Type>,
90 lt_substs: FxHashMap<DefId, clean::Lifetime>,
92 where F: FnOnce() -> R {
93 let (old_tys, old_lts) =
94 (mem::replace(&mut *self.ty_substs.borrow_mut(), ty_substs),
95 mem::replace(&mut *self.lt_substs.borrow_mut(), lt_substs));
97 *self.ty_substs.borrow_mut() = old_tys;
98 *self.lt_substs.borrow_mut() = old_lts;
102 // This is an ugly hack, but it's the simplest way to handle synthetic impls without greatly
103 // refactoring either librustdoc or librustc. In particular, allowing new DefIds to be
104 // registered after the AST is constructed would require storing the defid mapping in a
105 // RefCell, decreasing the performance for normal compilation for very little gain.
107 // Instead, we construct 'fake' def ids, which start immediately after the last DefId in
108 // DefIndexAddressSpace::Low. In the Debug impl for clean::Item, we explicitly check for fake
109 // def ids, as we'll end up with a panic if we use the DefId Debug impl for fake DefIds
110 pub fn next_def_id(&self, crate_num: CrateNum) -> DefId {
112 let next_id = if crate_num == LOCAL_CRATE {
117 .next_id(DefIndexAddressSpace::Low)
120 .def_path_table(crate_num)
121 .next_id(DefIndexAddressSpace::Low)
130 let mut fake_ids = self.fake_def_ids.borrow_mut();
132 let def_id = fake_ids.entry(crate_num).or_insert(start_def_id).clone();
137 index: DefIndex::from_array_index(
138 def_id.index.as_array_index() + 1,
139 def_id.index.address_space(),
144 MAX_DEF_ID.with(|m| {
146 .entry(def_id.krate.clone())
147 .or_insert(start_def_id);
150 self.all_fake_def_ids.borrow_mut().insert(def_id);
155 /// Like the function of the same name on the HIR map, but skips calling it on fake DefIds.
156 /// (This avoids a slice-index-out-of-bounds panic.)
157 pub fn as_local_node_id(&self, def_id: DefId) -> Option<NodeId> {
158 if self.all_fake_def_ids.borrow().contains(&def_id) {
161 self.tcx.hir().as_local_node_id(def_id)
165 pub fn get_real_ty<F>(&self,
168 real_name: &Option<Ident>,
169 generics: &ty::Generics,
171 where F: Fn(DefId) -> Def {
172 let path = get_path_for_type(self.tcx, def_id, def_ctor);
173 let mut segments = path.segments.into_vec();
174 let last = segments.pop().expect("segments were empty");
176 segments.push(hir::PathSegment::new(
177 real_name.unwrap_or(last.ident),
180 self.generics_to_path_params(generics.clone()),
184 let new_path = hir::Path {
187 segments: HirVec::from_vec(segments),
191 id: ast::DUMMY_NODE_ID,
192 node: hir::TyKind::Path(hir::QPath::Resolved(None, P(new_path))),
194 hir_id: hir::DUMMY_HIR_ID,
198 pub fn generics_to_path_params(&self, generics: ty::Generics) -> hir::GenericArgs {
199 let mut args = vec![];
201 for param in generics.params.iter() {
203 ty::GenericParamDefKind::Lifetime => {
204 let name = if param.name == "" {
205 hir::ParamName::Plain(keywords::StaticLifetime.ident())
207 hir::ParamName::Plain(ast::Ident::from_interned_str(param.name))
210 args.push(hir::GenericArg::Lifetime(hir::Lifetime {
211 id: ast::DUMMY_NODE_ID,
213 name: hir::LifetimeName::Param(name),
216 ty::GenericParamDefKind::Type {..} => {
217 args.push(hir::GenericArg::Type(self.ty_param_to_ty(param.clone())));
223 args: HirVec::from_vec(args),
224 bindings: HirVec::new(),
225 parenthesized: false,
229 pub fn ty_param_to_ty(&self, param: ty::GenericParamDef) -> hir::Ty {
230 debug!("ty_param_to_ty({:?}) {:?}", param, param.def_id);
232 id: ast::DUMMY_NODE_ID,
233 node: hir::TyKind::Path(hir::QPath::Resolved(
237 def: Def::TyParam(param.def_id),
238 segments: HirVec::from_vec(vec![
239 hir::PathSegment::from_ident(Ident::from_interned_str(param.name))
244 hir_id: hir::DUMMY_HIR_ID,
249 pub trait DocAccessLevels {
250 fn is_doc_reachable(&self, did: DefId) -> bool;
253 impl DocAccessLevels for AccessLevels<DefId> {
254 fn is_doc_reachable(&self, did: DefId) -> bool {
259 /// Creates a new diagnostic `Handler` that can be used to emit warnings and errors.
261 /// If the given `error_format` is `ErrorOutputType::Json` and no `SourceMap` is given, a new one
262 /// will be created for the handler.
263 pub fn new_handler(error_format: ErrorOutputType,
264 source_map: Option<Lrc<source_map::SourceMap>>,
265 treat_err_as_bug: bool,
267 ) -> errors::Handler {
268 // rustdoc doesn't override (or allow to override) anything from this that is relevant here, so
269 // stick to the defaults
270 let sessopts = Options::default();
271 let emitter: Box<dyn Emitter + sync::Send> = match error_format {
272 ErrorOutputType::HumanReadable(color_config) => Box::new(
273 EmitterWriter::stderr(
275 source_map.map(|cm| cm as _),
277 sessopts.debugging_opts.teach,
278 ).ui_testing(ui_testing)
280 ErrorOutputType::Json(pretty) => {
281 let source_map = source_map.unwrap_or_else(
282 || Lrc::new(source_map::SourceMap::new(sessopts.file_path_mapping())));
288 ).ui_testing(ui_testing)
291 ErrorOutputType::Short(color_config) => Box::new(
292 EmitterWriter::stderr(
294 source_map.map(|cm| cm as _),
300 errors::Handler::with_emitter_and_flags(
302 errors::HandlerFlags {
303 can_emit_warnings: true,
305 report_delayed_bugs: false,
306 external_macro_backtrace: false,
312 pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOptions, Vec<String>) {
313 // Parse, resolve, and typecheck the given crate.
337 let cpath = Some(input.clone());
338 let input = Input::File(input);
340 let intra_link_resolution_failure_name = lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE.name;
341 let warnings_lint_name = lint::builtin::WARNINGS.name;
342 let missing_docs = rustc_lint::builtin::MISSING_DOCS.name;
343 let missing_doc_example = rustc_lint::builtin::MISSING_DOC_CODE_EXAMPLES.name;
344 let private_doc_tests = rustc_lint::builtin::PRIVATE_DOC_TESTS.name;
346 // In addition to those specific lints, we also need to whitelist those given through
347 // command line, otherwise they'll get ignored and we don't want that.
348 let mut whitelisted_lints = vec![warnings_lint_name.to_owned(),
349 intra_link_resolution_failure_name.to_owned(),
350 missing_docs.to_owned(),
351 missing_doc_example.to_owned(),
352 private_doc_tests.to_owned()];
354 whitelisted_lints.extend(lint_opts.iter().map(|(lint, _)| lint).cloned());
356 let lints = lint::builtin::HardwiredLints.get_lints()
358 .chain(rustc_lint::SoftLints.get_lints().into_iter())
360 if lint.name == warnings_lint_name ||
361 lint.name == intra_link_resolution_failure_name {
364 Some((lint.name_lower(), lint::Allow))
367 .chain(lint_opts.into_iter())
368 .collect::<Vec<_>>();
370 let host_triple = TargetTriple::from_triple(config::host_triple());
371 // plays with error output here!
372 let sessopts = config::Options {
375 crate_types: vec![config::CrateType::Rlib],
376 lint_opts: if !display_warnings {
381 lint_cap: Some(lint_cap.unwrap_or_else(|| lint::Forbid)),
384 target_triple: target.unwrap_or(host_triple),
385 // Ensure that rustdoc works even if rustc is feature-staged
386 unstable_features: UnstableFeatures::Allow,
387 actually_rustdoc: true,
388 debugging_opts: debugging_options.clone(),
394 driver::spawn_thread_pool(sessopts, move |sessopts| {
395 let source_map = Lrc::new(source_map::SourceMap::new(sessopts.file_path_mapping()));
396 let diagnostic_handler = new_handler(error_format,
397 Some(source_map.clone()),
398 debugging_options.treat_err_as_bug,
399 debugging_options.ui_testing);
401 let mut sess = session::build_session_(
402 sessopts, cpath, diagnostic_handler, source_map,
405 lint::builtin::HardwiredLints.get_lints()
407 .chain(rustc_lint::SoftLints.get_lints().into_iter())
409 // We don't want to whitelist *all* lints so let's
410 // ignore those ones.
411 if whitelisted_lints.iter().any(|l| &lint.name == l) {
418 sess.driver_lint_caps.insert(lint::LintId::of(l),
422 let codegen_backend = rustc_driver::get_codegen_backend(&sess);
423 let cstore = Rc::new(CStore::new(codegen_backend.metadata_loader()));
424 rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
426 let mut cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs));
427 target_features::add_configuration(&mut cfg, &sess, &*codegen_backend);
428 sess.parse_sess.config = cfg;
430 let control = &driver::CompileController::basic();
432 let krate = match driver::phase_1_parse_input(control, &sess, &input) {
440 let name = match crate_name {
441 Some(ref crate_name) => crate_name.clone(),
442 None => ::rustc_codegen_utils::link::find_crate_name(Some(&sess), &krate.attrs, &input),
445 let mut crate_loader = CrateLoader::new(&sess, &cstore, &name);
447 let resolver_arenas = resolve::Resolver::arenas();
448 let result = driver::phase_2_configure_and_expand_inner(&sess,
454 resolve::MakeGlobMap::No,
458 let driver::InnerExpansionResult {
462 } = abort_on_err(result, &sess);
464 // We need to hold on to the complete resolver, so we clone everything
465 // for the analysis passes to use. Suboptimal, but necessary in the
466 // current architecture.
467 let defs = resolver.definitions.clone();
468 let resolutions = ty::Resolutions {
469 freevars: resolver.freevars.clone(),
470 export_map: resolver.export_map.clone(),
471 trait_map: resolver.trait_map.clone(),
472 maybe_unused_trait_imports: resolver.maybe_unused_trait_imports.clone(),
473 maybe_unused_extern_crates: resolver.maybe_unused_extern_crates.clone(),
474 extern_prelude: resolver.extern_prelude.iter().map(|(ident, entry)| {
475 (ident.name, entry.introduced_by_item)
478 let analysis = ty::CrateAnalysis {
479 glob_map: if resolver.make_glob_map { Some(resolver.glob_map.clone()) } else { None },
482 let mut arenas = AllArenas::new();
483 let hir_map = hir_map::map_crate(&sess, &*cstore, &mut hir_forest, &defs);
484 let output_filenames = driver::build_output_filenames(&input,
490 let resolver = RefCell::new(resolver);
491 abort_on_err(driver::phase_3_run_analysis_passes(&*codegen_backend,
501 |tcx, _, _, result| {
503 sess.fatal("Compilation failed, aborting rustdoc");
506 let access_levels = tcx.privacy_access_levels(LOCAL_CRATE);
508 // Convert from a NodeId set to a DefId set since we don't always have easy access
509 // to the map from defid -> nodeid
510 let access_levels = AccessLevels {
511 map: access_levels.map.iter()
512 .map(|(&k, &v)| (tcx.hir().local_def_id(k), v))
516 let send_trait = if crate_name == Some("core".to_string()) {
517 clean::path_to_def_local(&tcx, &["marker", "Send"])
519 clean::path_to_def(&tcx, &["core", "marker", "Send"])
522 let mut renderinfo = RenderInfo::default();
523 renderinfo.access_levels = access_levels;
525 let ctxt = DocContext {
529 cstore: cstore.clone(),
530 external_traits: Default::default(),
531 active_extern_traits: Default::default(),
532 renderinfo: RefCell::new(renderinfo),
533 ty_substs: Default::default(),
534 lt_substs: Default::default(),
535 impl_trait_bounds: Default::default(),
536 send_trait: send_trait,
537 fake_def_ids: Default::default(),
538 all_fake_def_ids: Default::default(),
539 generated_synthetics: Default::default(),
540 all_traits: tcx.all_traits(LOCAL_CRATE).to_vec(),
542 debug!("crate: {:?}", tcx.hir().krate());
545 let mut v = RustdocVisitor::new(&ctxt);
546 v.visit(tcx.hir().krate());
550 fn report_deprecated_attr(name: &str, diag: &errors::Handler) {
551 let mut msg = diag.struct_warn(&format!("the `#![doc({})]` attribute is \
552 considered deprecated", name));
553 msg.warn("please see https://github.com/rust-lang/rust/issues/44136");
555 if name == "no_default_passes" {
556 msg.help("you may want to use `#![doc(document_private_items)]`");
562 // Process all of the crate attributes, extracting plugin metadata along
563 // with the passes which we are supposed to run.
564 for attr in krate.module.as_ref().unwrap().attrs.lists("doc") {
565 let diag = ctxt.sess().diagnostic();
567 let name = attr.name().map(|s| s.as_str());
568 let name = name.as_ref().map(|s| &s[..]);
570 if name == Some("no_default_passes") {
571 report_deprecated_attr("no_default_passes", diag);
572 if default_passes == passes::DefaultPassOption::Default {
573 default_passes = passes::DefaultPassOption::None;
576 } else if let Some(value) = attr.value_str() {
577 let sink = match name {
579 report_deprecated_attr("passes = \"...\"", diag);
583 report_deprecated_attr("plugins = \"...\"", diag);
584 eprintln!("WARNING: #![doc(plugins = \"...\")] no longer functions; \
585 see CVE-2018-1000622");
590 for p in value.as_str().split_whitespace() {
591 sink.push(p.to_string());
595 if attr.is_word() && name == Some("document_private_items") {
596 if default_passes == passes::DefaultPassOption::Default {
597 default_passes = passes::DefaultPassOption::Private;
602 let mut passes: Vec<String> =
603 passes::defaults(default_passes).iter().map(|p| p.to_string()).collect();
604 passes.extend(manual_passes);
606 for pass in &passes {
607 // the "unknown pass" error will be reported when late passes are run
608 if let Some(pass) = passes::find_pass(pass).and_then(|p| p.early_fn()) {
609 krate = pass(krate, &ctxt);
613 ctxt.sess().abort_if_errors();
615 (krate, ctxt.renderinfo.into_inner(), render_options, passes)