]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/core.rs
Ignore tests broken by failing on ICE
[rust.git] / src / librustdoc / core.rs
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.
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;
12 use rustc::{driver, middle};
13 use rustc::metadata::creader::Loader;
14 use rustc::middle::privacy;
15
16 use syntax::ast;
17 use syntax::parse::token;
18 use syntax;
19
20 use std::cell::RefCell;
21 use std::os;
22 use std::local_data;
23 use collections::HashSet;
24
25 use visit_ast::RustdocVisitor;
26 use clean;
27 use clean::Clean;
28
29 pub enum MaybeTyped {
30     Typed(middle::ty::ctxt),
31     NotTyped(driver::session::Session)
32 }
33
34 pub struct DocContext {
35     pub krate: ast::Crate,
36     pub maybe_typed: MaybeTyped
37 }
38
39 impl DocContext {
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
44         }
45     }
46 }
47
48 pub struct CrateAnalysis {
49     pub exported_items: privacy::ExportedItems,
50     pub public_items: privacy::PublicItems,
51 }
52
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,
58                                 phase_1_parse_input,
59                                 phase_2_configure_and_expand,
60                                 phase_3_run_analysis_passes};
61
62     let input = FileInput(cpath.clone());
63
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()
69     };
70
71
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);
76
77     let sess = driver::driver::build_session_(sessopts,
78                                               Some(cpath.clone()),
79                                               span_diagnostic_handler);
80
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_)));
85     }
86
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);
93
94     debug!("crate: {:?}", krate);
95     (DocContext {
96         krate: krate,
97         maybe_typed: Typed(ty_cx)
98     }, CrateAnalysis {
99         exported_items: exported_items,
100         public_items: public_items,
101     })
102 }
103
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);
107     let ctxt = @ctxt;
108     local_data::set(super::ctxtkey, ctxt);
109
110     let krate = {
111         let mut v = RustdocVisitor::new(ctxt, Some(&analysis));
112         v.visit(&ctxt.krate);
113         v.clean()
114     };
115
116     (krate, analysis)
117 }