]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/core.rs
Auto merge of #54265 - arielb1:civilize-proc-macros, r=alexcrichton
[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, NodeId};
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     /// Like the function of the same name on the HIR map, but skips calling it on fake DefIds.
167     /// (This avoids a slice-index-out-of-bounds panic.)
168     pub fn as_local_node_id(&self, def_id: DefId) -> Option<NodeId> {
169         if self.all_fake_def_ids.borrow().contains(&def_id) {
170             None
171         } else {
172             self.tcx.hir.as_local_node_id(def_id)
173         }
174     }
175
176     pub fn get_real_ty<F>(&self,
177                           def_id: DefId,
178                           def_ctor: &F,
179                           real_name: &Option<Ident>,
180                           generics: &ty::Generics,
181     ) -> hir::Ty
182     where F: Fn(DefId) -> Def {
183         let path = get_path_for_type(self.tcx, def_id, def_ctor);
184         let mut segments = path.segments.into_vec();
185         let last = segments.pop().expect("segments were empty");
186
187         segments.push(hir::PathSegment::new(
188             real_name.unwrap_or(last.ident),
189             self.generics_to_path_params(generics.clone()),
190             false,
191         ));
192
193         let new_path = hir::Path {
194             span: path.span,
195             def: path.def,
196             segments: HirVec::from_vec(segments),
197         };
198
199         hir::Ty {
200             id: ast::DUMMY_NODE_ID,
201             node: hir::TyKind::Path(hir::QPath::Resolved(None, P(new_path))),
202             span: DUMMY_SP,
203             hir_id: hir::DUMMY_HIR_ID,
204         }
205     }
206
207     pub fn generics_to_path_params(&self, generics: ty::Generics) -> hir::GenericArgs {
208         let mut args = vec![];
209
210         for param in generics.params.iter() {
211             match param.kind {
212                 ty::GenericParamDefKind::Lifetime => {
213                     let name = if param.name == "" {
214                         hir::ParamName::Plain(keywords::StaticLifetime.ident())
215                     } else {
216                         hir::ParamName::Plain(ast::Ident::from_interned_str(param.name))
217                     };
218
219                     args.push(hir::GenericArg::Lifetime(hir::Lifetime {
220                         id: ast::DUMMY_NODE_ID,
221                         span: DUMMY_SP,
222                         name: hir::LifetimeName::Param(name),
223                     }));
224                 }
225                 ty::GenericParamDefKind::Type {..} => {
226                     args.push(hir::GenericArg::Type(self.ty_param_to_ty(param.clone())));
227                 }
228             }
229         }
230
231         hir::GenericArgs {
232             args: HirVec::from_vec(args),
233             bindings: HirVec::new(),
234             parenthesized: false,
235         }
236     }
237
238     pub fn ty_param_to_ty(&self, param: ty::GenericParamDef) -> hir::Ty {
239         debug!("ty_param_to_ty({:?}) {:?}", param, param.def_id);
240         hir::Ty {
241             id: ast::DUMMY_NODE_ID,
242             node: hir::TyKind::Path(hir::QPath::Resolved(
243                 None,
244                 P(hir::Path {
245                     span: DUMMY_SP,
246                     def: Def::TyParam(param.def_id),
247                     segments: HirVec::from_vec(vec![
248                         hir::PathSegment::from_ident(Ident::from_interned_str(param.name))
249                     ]),
250                 }),
251             )),
252             span: DUMMY_SP,
253             hir_id: hir::DUMMY_HIR_ID,
254         }
255     }
256 }
257
258 pub trait DocAccessLevels {
259     fn is_doc_reachable(&self, did: DefId) -> bool;
260 }
261
262 impl DocAccessLevels for AccessLevels<DefId> {
263     fn is_doc_reachable(&self, did: DefId) -> bool {
264         self.is_public(did)
265     }
266 }
267
268 /// Creates a new diagnostic `Handler` that can be used to emit warnings and errors.
269 ///
270 /// If the given `error_format` is `ErrorOutputType::Json` and no `SourceMap` is given, a new one
271 /// will be created for the handler.
272 pub fn new_handler(error_format: ErrorOutputType,
273                    source_map: Option<Lrc<source_map::SourceMap>>,
274                    treat_err_as_bug: bool,
275 ) -> errors::Handler {
276     // rustdoc doesn't override (or allow to override) anything from this that is relevant here, so
277     // stick to the defaults
278     let sessopts = Options::default();
279     let emitter: Box<dyn Emitter + sync::Send> = match error_format {
280         ErrorOutputType::HumanReadable(color_config) => Box::new(
281             EmitterWriter::stderr(
282                 color_config,
283                 source_map.map(|cm| cm as _),
284                 false,
285                 sessopts.debugging_opts.teach,
286             ).ui_testing(sessopts.debugging_opts.ui_testing)
287         ),
288         ErrorOutputType::Json(pretty) => {
289             let source_map = source_map.unwrap_or_else(
290                 || Lrc::new(source_map::SourceMap::new(sessopts.file_path_mapping())));
291             Box::new(
292                 JsonEmitter::stderr(
293                     None,
294                     source_map,
295                     pretty,
296                 ).ui_testing(sessopts.debugging_opts.ui_testing)
297             )
298         },
299         ErrorOutputType::Short(color_config) => Box::new(
300             EmitterWriter::stderr(
301                 color_config,
302                 source_map.map(|cm| cm as _),
303                 true,
304                 false)
305         ),
306     };
307
308     errors::Handler::with_emitter_and_flags(
309         emitter,
310         errors::HandlerFlags {
311             can_emit_warnings: true,
312             treat_err_as_bug,
313             report_delayed_bugs: false,
314             external_macro_backtrace: false,
315             ..Default::default()
316         },
317     )
318 }
319
320 pub fn run_core(search_paths: SearchPaths,
321                 cfgs: Vec<String>,
322                 externs: config::Externs,
323                 input: Input,
324                 triple: Option<TargetTriple>,
325                 maybe_sysroot: Option<PathBuf>,
326                 allow_warnings: bool,
327                 crate_name: Option<String>,
328                 force_unstable_if_unmarked: bool,
329                 edition: Edition,
330                 cg: CodegenOptions,
331                 error_format: ErrorOutputType,
332                 cmd_lints: Vec<(String, lint::Level)>,
333                 lint_cap: Option<lint::Level>,
334                 describe_lints: bool,
335                 mut manual_passes: Vec<String>,
336                 mut default_passes: passes::DefaultPassOption,
337                 treat_err_as_bug: bool,
338 ) -> (clean::Crate, RenderInfo, Vec<String>) {
339     // Parse, resolve, and typecheck the given crate.
340
341     let cpath = match input {
342         Input::File(ref p) => Some(p.clone()),
343         _ => None
344     };
345
346     let intra_link_resolution_failure_name = lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE.name;
347     let warnings_lint_name = lint::builtin::WARNINGS.name;
348     let missing_docs = rustc_lint::builtin::MISSING_DOCS.name;
349
350     // In addition to those specific lints, we also need to whitelist those given through
351     // command line, otherwise they'll get ignored and we don't want that.
352     let mut whitelisted_lints = vec![warnings_lint_name.to_owned(),
353                                      intra_link_resolution_failure_name.to_owned(),
354                                      missing_docs.to_owned()];
355
356     whitelisted_lints.extend(cmd_lints.iter().map(|(lint, _)| lint).cloned());
357
358     let lints = lint::builtin::HardwiredLints.get_lints()
359                     .into_iter()
360                     .chain(rustc_lint::SoftLints.get_lints().into_iter())
361                     .filter_map(|lint| {
362                         if lint.name == warnings_lint_name ||
363                            lint.name == intra_link_resolution_failure_name {
364                             None
365                         } else {
366                             Some((lint.name_lower(), lint::Allow))
367                         }
368                     })
369                     .chain(cmd_lints.into_iter())
370                     .collect::<Vec<_>>();
371
372     let host_triple = TargetTriple::from_triple(config::host_triple());
373     // plays with error output here!
374     let sessopts = config::Options {
375         maybe_sysroot,
376         search_paths,
377         crate_types: vec![config::CrateType::Rlib],
378         lint_opts: if !allow_warnings {
379             lints
380         } else {
381             vec![]
382         },
383         lint_cap: Some(lint_cap.unwrap_or_else(|| lint::Forbid)),
384         cg,
385         externs,
386         target_triple: triple.unwrap_or(host_triple),
387         // Ensure that rustdoc works even if rustc is feature-staged
388         unstable_features: UnstableFeatures::Allow,
389         actually_rustdoc: true,
390         debugging_opts: config::DebuggingOptions {
391             force_unstable_if_unmarked,
392             ..config::basic_debugging_options()
393         },
394         error_format,
395         edition,
396         describe_lints,
397         ..Options::default()
398     };
399     driver::spawn_thread_pool(sessopts, move |sessopts| {
400         let source_map = Lrc::new(source_map::SourceMap::new(sessopts.file_path_mapping()));
401         let diagnostic_handler = new_handler(error_format,
402                                              Some(source_map.clone()),
403                                              treat_err_as_bug);
404
405         let mut sess = session::build_session_(
406             sessopts, cpath, diagnostic_handler, source_map,
407         );
408
409         lint::builtin::HardwiredLints.get_lints()
410                                      .into_iter()
411                                      .chain(rustc_lint::SoftLints.get_lints().into_iter())
412                                      .filter_map(|lint| {
413                                          // We don't want to whitelist *all* lints so let's
414                                          // ignore those ones.
415                                          if whitelisted_lints.iter().any(|l| &lint.name == l) {
416                                              None
417                                          } else {
418                                              Some(lint)
419                                          }
420                                      })
421                                      .for_each(|l| {
422                                          sess.driver_lint_caps.insert(lint::LintId::of(l),
423                                                                       lint::Allow);
424                                      });
425
426         let codegen_backend = rustc_driver::get_codegen_backend(&sess);
427         let cstore = Rc::new(CStore::new(codegen_backend.metadata_loader()));
428         rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
429
430         let mut cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs));
431         target_features::add_configuration(&mut cfg, &sess, &*codegen_backend);
432         sess.parse_sess.config = cfg;
433
434         let control = &driver::CompileController::basic();
435
436         let krate = panictry!(driver::phase_1_parse_input(control, &sess, &input));
437
438         let name = match crate_name {
439             Some(ref crate_name) => crate_name.clone(),
440             None => ::rustc_codegen_utils::link::find_crate_name(Some(&sess), &krate.attrs, &input),
441         };
442
443         let mut crate_loader = CrateLoader::new(&sess, &cstore, &name);
444
445         let resolver_arenas = resolve::Resolver::arenas();
446         let result = driver::phase_2_configure_and_expand_inner(&sess,
447                                                         &cstore,
448                                                         krate,
449                                                         None,
450                                                         &name,
451                                                         None,
452                                                         resolve::MakeGlobMap::No,
453                                                         &resolver_arenas,
454                                                         &mut crate_loader,
455                                                         |_| Ok(()));
456         let driver::InnerExpansionResult {
457             mut hir_forest,
458             resolver,
459             ..
460         } = abort_on_err(result, &sess);
461
462         // We need to hold on to the complete resolver, so we clone everything
463         // for the analysis passes to use. Suboptimal, but necessary in the
464         // current architecture.
465         let defs = resolver.definitions.clone();
466         let resolutions = ty::Resolutions {
467             freevars: resolver.freevars.clone(),
468             export_map: resolver.export_map.clone(),
469             trait_map: resolver.trait_map.clone(),
470             maybe_unused_trait_imports: resolver.maybe_unused_trait_imports.clone(),
471             maybe_unused_extern_crates: resolver.maybe_unused_extern_crates.clone(),
472         };
473         let analysis = ty::CrateAnalysis {
474             access_levels: Lrc::new(AccessLevels::default()),
475             name: name.to_string(),
476             glob_map: if resolver.make_glob_map { Some(resolver.glob_map.clone()) } else { None },
477         };
478
479         let arenas = AllArenas::new();
480         let hir_map = hir_map::map_crate(&sess, &*cstore, &mut hir_forest, &defs);
481         let output_filenames = driver::build_output_filenames(&input,
482                                                             &None,
483                                                             &None,
484                                                             &[],
485                                                             &sess);
486
487         let resolver = RefCell::new(resolver);
488         abort_on_err(driver::phase_3_run_analysis_passes(&*codegen_backend,
489                                                         control,
490                                                         &sess,
491                                                         &*cstore,
492                                                         hir_map,
493                                                         analysis,
494                                                         resolutions,
495                                                         &arenas,
496                                                         &name,
497                                                         &output_filenames,
498                                                         |tcx, analysis, _, result| {
499             if result.is_err() {
500                 sess.fatal("Compilation failed, aborting rustdoc");
501             }
502
503             let ty::CrateAnalysis { access_levels, .. } = analysis;
504
505             // Convert from a NodeId set to a DefId set since we don't always have easy access
506             // to the map from defid -> nodeid
507             let access_levels = AccessLevels {
508                 map: access_levels.map.iter()
509                                     .map(|(&k, &v)| (tcx.hir.local_def_id(k), v))
510                                     .collect()
511             };
512
513             let send_trait = if crate_name == Some("core".to_string()) {
514                 clean::path_to_def_local(&tcx, &["marker", "Send"])
515             } else {
516                 clean::path_to_def(&tcx, &["core", "marker", "Send"])
517             };
518
519             let mut renderinfo = RenderInfo::default();
520             renderinfo.access_levels = access_levels;
521
522             let ctxt = DocContext {
523                 tcx,
524                 resolver: &resolver,
525                 crate_name,
526                 cstore: cstore.clone(),
527                 external_traits: Default::default(),
528                 active_extern_traits: Default::default(),
529                 renderinfo: RefCell::new(renderinfo),
530                 ty_substs: Default::default(),
531                 lt_substs: Default::default(),
532                 impl_trait_bounds: Default::default(),
533                 send_trait: send_trait,
534                 fake_def_ids: RefCell::new(FxHashMap()),
535                 all_fake_def_ids: RefCell::new(FxHashSet()),
536                 generated_synthetics: RefCell::new(FxHashSet()),
537                 all_traits: tcx.all_traits(LOCAL_CRATE).to_vec(),
538             };
539             debug!("crate: {:?}", tcx.hir.krate());
540
541             let mut krate = {
542                 let mut v = RustdocVisitor::new(&ctxt);
543                 v.visit(tcx.hir.krate());
544                 v.clean(&ctxt)
545             };
546
547             fn report_deprecated_attr(name: &str, diag: &errors::Handler) {
548                 let mut msg = diag.struct_warn(&format!("the `#![doc({})]` attribute is \
549                                                          considered deprecated", name));
550                 msg.warn("please see https://github.com/rust-lang/rust/issues/44136");
551
552                 if name == "no_default_passes" {
553                     msg.help("you may want to use `#![doc(document_private_items)]`");
554                 }
555
556                 msg.emit();
557             }
558
559             // Process all of the crate attributes, extracting plugin metadata along
560             // with the passes which we are supposed to run.
561             for attr in krate.module.as_ref().unwrap().attrs.lists("doc") {
562                 let diag = ctxt.sess().diagnostic();
563
564                 let name = attr.name().map(|s| s.as_str());
565                 let name = name.as_ref().map(|s| &s[..]);
566                 if attr.is_word() {
567                     if name == Some("no_default_passes") {
568                         report_deprecated_attr("no_default_passes", diag);
569                         if default_passes == passes::DefaultPassOption::Default {
570                             default_passes = passes::DefaultPassOption::None;
571                         }
572                     }
573                 } else if let Some(value) = attr.value_str() {
574                     let sink = match name {
575                         Some("passes") => {
576                             report_deprecated_attr("passes = \"...\"", diag);
577                             &mut manual_passes
578                         },
579                         Some("plugins") => {
580                             report_deprecated_attr("plugins = \"...\"", diag);
581                             eprintln!("WARNING: #![doc(plugins = \"...\")] no longer functions; \
582                                       see CVE-2018-1000622");
583                             continue
584                         },
585                         _ => continue,
586                     };
587                     for p in value.as_str().split_whitespace() {
588                         sink.push(p.to_string());
589                     }
590                 }
591
592                 if attr.is_word() && name == Some("document_private_items") {
593                     if default_passes == passes::DefaultPassOption::Default {
594                         default_passes = passes::DefaultPassOption::Private;
595                     }
596                 }
597             }
598
599             let mut passes: Vec<String> =
600                 passes::defaults(default_passes).iter().map(|p| p.to_string()).collect();
601             passes.extend(manual_passes);
602
603             for pass in &passes {
604                 // the "unknown pass" error will be reported when late passes are run
605                 if let Some(pass) = passes::find_pass(pass).and_then(|p| p.early_fn()) {
606                     krate = pass(krate, &ctxt);
607                 }
608             }
609
610             ctxt.sess().abort_if_errors();
611
612             (krate, ctxt.renderinfo.into_inner(), passes)
613         }), &sess)
614     })
615 }