1 // Copyright 2012-2013 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.
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.
12 use rustc::{driver, middle};
13 use rustc::middle::privacy;
17 use syntax::parse::token;
20 use std::cell::RefCell;
23 use std::collections::{HashMap, HashSet};
25 use visit_ast::RustdocVisitor;
30 Typed(middle::ty::ctxt),
31 NotTyped(driver::session::Session)
34 pub type ExternalPaths = RefCell<Option<HashMap<ast::DefId,
35 (Vec<String>, clean::TypeKind)>>>;
37 pub struct DocContext {
38 pub krate: ast::Crate,
39 pub maybe_typed: MaybeTyped,
41 pub external_paths: ExternalPaths,
42 pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
43 pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
44 pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
45 pub populated_crate_impls: RefCell<HashSet<ast::CrateNum>>,
49 pub fn sess<'a>(&'a self) -> &'a driver::session::Session {
50 match self.maybe_typed {
51 Typed(ref tcx) => &tcx.sess,
52 NotTyped(ref sess) => sess
57 pub struct CrateAnalysis {
58 pub exported_items: privacy::ExportedItems,
59 pub public_items: privacy::PublicItems,
60 pub external_paths: ExternalPaths,
61 pub external_traits: RefCell<Option<HashMap<ast::DefId, clean::Trait>>>,
62 pub external_typarams: RefCell<Option<HashMap<ast::DefId, String>>>,
63 pub inlined: RefCell<Option<HashSet<ast::DefId>>>,
66 /// Parses, resolves, and typechecks the given crate
67 fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<String>)
68 -> (DocContext, CrateAnalysis) {
69 use syntax::codemap::dummy_spanned;
70 use rustc::driver::driver::{FileInput,
72 phase_2_configure_and_expand,
73 phase_3_run_analysis_passes};
74 use rustc::driver::config::build_configuration;
76 let input = FileInput(cpath.clone());
78 let warning_lint = lint::builtin::warnings.name.to_string();
80 let sessopts = driver::config::Options {
81 maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
82 addl_lib_search_paths: RefCell::new(libs),
83 crate_types: vec!(driver::config::CrateTypeRlib),
84 lint_opts: vec!((warning_lint, lint::Allow)),
85 ..rustc::driver::config::basic_options().clone()
89 let codemap = syntax::codemap::CodeMap::new();
90 let diagnostic_handler = syntax::diagnostic::default_handler(syntax::diagnostic::Auto);
91 let span_diagnostic_handler =
92 syntax::diagnostic::mk_span_handler(diagnostic_handler, codemap);
94 let sess = driver::session::build_session_(sessopts,
96 span_diagnostic_handler);
98 let mut cfg = build_configuration(&sess);
99 for cfg_ in cfgs.move_iter() {
100 let cfg_ = token::intern_and_get_ident(cfg_.as_slice());
101 cfg.push(box(GC) dummy_spanned(ast::MetaWord(cfg_)));
104 let krate = phase_1_parse_input(&sess, cfg, &input);
105 let (krate, ast_map) = phase_2_configure_and_expand(&sess, krate,
106 &from_str("rustdoc").unwrap());
107 let driver::driver::CrateAnalysis {
108 exported_items, public_items, ty_cx, ..
109 } = phase_3_run_analysis_passes(sess, &krate, ast_map);
111 debug!("crate: {:?}", krate);
114 maybe_typed: Typed(ty_cx),
116 external_traits: RefCell::new(Some(HashMap::new())),
117 external_typarams: RefCell::new(Some(HashMap::new())),
118 external_paths: RefCell::new(Some(HashMap::new())),
119 inlined: RefCell::new(Some(HashSet::new())),
120 populated_crate_impls: RefCell::new(HashSet::new()),
122 exported_items: exported_items,
123 public_items: public_items,
124 external_paths: RefCell::new(None),
125 external_traits: RefCell::new(None),
126 external_typarams: RefCell::new(None),
127 inlined: RefCell::new(None),
131 pub fn run_core(libs: HashSet<Path>, cfgs: Vec<String>, path: &Path)
132 -> (clean::Crate, CrateAnalysis) {
133 let (ctxt, analysis) = get_ast_and_resolve(path, libs, cfgs);
134 let ctxt = box(GC) ctxt;
135 super::ctxtkey.replace(Some(ctxt));
138 let mut v = RustdocVisitor::new(&*ctxt, Some(&analysis));
139 v.visit(&ctxt.krate);
143 let external_paths = ctxt.external_paths.borrow_mut().take();
144 *analysis.external_paths.borrow_mut() = external_paths;
145 let map = ctxt.external_traits.borrow_mut().take();
146 *analysis.external_traits.borrow_mut() = map;
147 let map = ctxt.external_typarams.borrow_mut().take();
148 *analysis.external_typarams.borrow_mut() = map;
149 let map = ctxt.inlined.borrow_mut().take();
150 *analysis.inlined.borrow_mut() = map;