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::metadata::creader::Loader;
14 use rustc::middle::privacy;
17 use syntax::parse::token;
20 use std::cell::RefCell;
23 use collections::HashSet;
25 use visit_ast::RustdocVisitor;
30 Typed(middle::ty::ctxt),
31 NotTyped(driver::session::Session)
34 pub struct DocContext {
35 pub krate: ast::Crate,
36 pub maybe_typed: MaybeTyped
40 pub fn sess<'a>(&'a self) -> &'a driver::session::Session {
41 match self.maybe_typed {
42 Typed(ref tcx) => &tcx.sess,
43 NotTyped(ref sess) => sess
48 pub struct CrateAnalysis {
49 pub exported_items: privacy::ExportedItems,
50 pub public_items: privacy::PublicItems,
53 /// Parses, resolves, and typechecks the given crate
54 fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<~str>)
55 -> (DocContext, CrateAnalysis) {
56 use syntax::codemap::dummy_spanned;
57 use rustc::driver::driver::{FileInput, build_configuration,
59 phase_2_configure_and_expand,
60 phase_3_run_analysis_passes};
62 let input = FileInput(cpath.clone());
64 let sessopts = driver::session::Options {
65 maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
66 addl_lib_search_paths: RefCell::new(libs),
67 crate_types: vec!(driver::session::CrateTypeDylib),
68 ..rustc::driver::session::basic_options().clone()
72 let codemap = syntax::codemap::CodeMap::new();
73 let diagnostic_handler = syntax::diagnostic::default_handler();
74 let span_diagnostic_handler =
75 syntax::diagnostic::mk_span_handler(diagnostic_handler, codemap);
77 let sess = driver::driver::build_session_(sessopts,
79 span_diagnostic_handler);
81 let mut cfg = build_configuration(&sess);
82 for cfg_ in cfgs.move_iter() {
83 let cfg_ = token::intern_and_get_ident(cfg_);
84 cfg.push(@dummy_spanned(ast::MetaWord(cfg_)));
87 let krate = phase_1_parse_input(&sess, cfg, &input);
88 let (krate, ast_map) = phase_2_configure_and_expand(&sess, &mut Loader::new(&sess),
89 krate, &from_str("rustdoc").unwrap());
90 let driver::driver::CrateAnalysis {
91 exported_items, public_items, ty_cx, ..
92 } = phase_3_run_analysis_passes(sess, &krate, ast_map);
94 debug!("crate: {:?}", krate);
97 maybe_typed: Typed(ty_cx)
99 exported_items: exported_items,
100 public_items: public_items,
104 pub fn run_core(libs: HashSet<Path>, cfgs: Vec<~str>, path: &Path)
105 -> (clean::Crate, CrateAnalysis) {
106 let (ctxt, analysis) = get_ast_and_resolve(path, libs, cfgs);
108 local_data::set(super::ctxtkey, ctxt);
111 let mut v = RustdocVisitor::new(ctxt, Some(&analysis));
112 v.visit(&ctxt.krate);