(index<- ) ./librustdoc/core.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Fri May 9 13:02:28 2014
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 use rustc::middle::lint;
16
17 use syntax::ast;
18 use syntax::parse::token;
19 use syntax;
20
21 use std::cell::RefCell;
22 use std::os;
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 pub src: Path,
38 }
39
40 impl DocContext {
41 pub fn sess<'a>(&'a self) -> &'a driver::session::Session {
42 match self.maybe_typed {
43 Typed(ref tcx) => &tcx.sess,
44 NotTyped(ref sess) => sess
45 }
46 }
47 }
48
49 pub struct CrateAnalysis {
50 pub exported_items: privacy::ExportedItems,
51 pub public_items: privacy::PublicItems,
52 }
53
54 /// Parses, resolves, and typechecks the given crate
55 fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<~str>)
56 -> (DocContext, CrateAnalysis) {
57 use syntax::codemap::dummy_spanned;
58 use rustc::driver::driver::{FileInput, build_configuration,
59 phase_1_parse_input,
60 phase_2_configure_and_expand,
61 phase_3_run_analysis_passes};
62
63 let input = FileInput(cpath.clone());
64
65 let sessopts = driver::session::Options {
66 maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
67 addl_lib_search_paths: RefCell::new(libs),
68 crate_types: vec!(driver::session::CrateTypeDylib),
69 lint_opts: vec!((lint::Warnings, lint::allow)),
70 ..rustc::driver::session::basic_options().clone()
71 };
72
73
74 let codemap = syntax::codemap::CodeMap::new();
75 let diagnostic_handler = syntax::diagnostic::default_handler();
76 let span_diagnostic_handler =
77 syntax::diagnostic::mk_span_handler(diagnostic_handler, codemap);
78
79 let sess = driver::driver::build_session_(sessopts,
80 Some(cpath.clone()),
81 span_diagnostic_handler);
82
83 let mut cfg = build_configuration(&sess);
84 for cfg_ in cfgs.move_iter() {
85 let cfg_ = token::intern_and_get_ident(cfg_);
86 cfg.push(@dummy_spanned(ast::MetaWord(cfg_)));
87 }
88
89 let krate = phase_1_parse_input(&sess, cfg, &input);
90 let (krate, ast_map) = phase_2_configure_and_expand(&sess, &mut Loader::new(&sess),
91 krate, &from_str("rustdoc").unwrap());
92 let driver::driver::CrateAnalysis {
93 exported_items, public_items, ty_cx, ..
94 } = phase_3_run_analysis_passes(sess, &krate, ast_map);
95
96 debug!("crate: {:?}", krate);
97 (DocContext {
98 krate: krate,
99 maybe_typed: Typed(ty_cx),
100 src: cpath.clone(),
101 }, CrateAnalysis {
102 exported_items: exported_items,
103 public_items: public_items,
104 })
105 }
106
107 pub fn run_core(libs: HashSet<Path>, cfgs: Vec<~str>, path: &Path)
108 -> (clean::Crate, CrateAnalysis) {
109 let (ctxt, analysis) = get_ast_and_resolve(path, libs, cfgs);
110 let ctxt = @ctxt;
111 super::ctxtkey.replace(Some(ctxt));
112
113 let krate = {
114 let mut v = RustdocVisitor::new(ctxt, Some(&analysis));
115 v.visit(&ctxt.krate);
116 v.clean()
117 };
118
119 (krate, analysis)
120 }
librustdoc/core.rs:33:1-33:1 -struct- definition:
pub struct DocContext {
pub krate: ast::Crate,
pub maybe_typed: MaybeTyped,
references:- 796: debug!("crate: {:?}", krate);
97: (DocContext {
98: krate: krate,
librustdoc/test.rs:
72: let ctx = @core::DocContext {
73: krate: krate,
librustdoc/core.rs:
55: fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<~str>)
56: -> (DocContext, CrateAnalysis) {
57: use syntax::codemap::dummy_spanned;
librustdoc/visit_ast.rs:
31: impl<'a> RustdocVisitor<'a> {
32: pub fn new<'b>(cx: &'b core::DocContext,
33: analysis: Option<&'b core::CrateAnalysis>) -> RustdocVisitor<'b> {
librustdoc/lib.rs:
82: local_data_key!(pub ctxtkey: @core::DocContext)
83: local_data_key!(pub analysiskey: core::CrateAnalysis)
librustdoc/visit_ast.rs:
26: pub attrs: Vec<ast::Attribute>,
27: pub cx: &'a core::DocContext,
28: pub analysis: Option<&'a core::CrateAnalysis>,
librustdoc/core.rs:48:1-48:1 -struct- definition:
pub struct CrateAnalysis {
pub exported_items: privacy::ExportedItems,
pub public_items: privacy::PublicItems,
references:- 655: fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<~str>)
56: -> (DocContext, CrateAnalysis) {
57: use syntax::codemap::dummy_spanned;
--
107: pub fn run_core(libs: HashSet<Path>, cfgs: Vec<~str>, path: &Path)
108: -> (clean::Crate, CrateAnalysis) {
109: let (ctxt, analysis) = get_ast_and_resolve(path, libs, cfgs);
librustdoc/visit_ast.rs:
27: pub cx: &'a core::DocContext,
28: pub analysis: Option<&'a core::CrateAnalysis>,
29: }
--
32: pub fn new<'b>(cx: &'b core::DocContext,
33: analysis: Option<&'b core::CrateAnalysis>) -> RustdocVisitor<'b> {
34: RustdocVisitor {
librustdoc/lib.rs:
82: local_data_key!(pub ctxtkey: @core::DocContext)
83: local_data_key!(pub analysiskey: core::CrateAnalysis)
librustdoc/core.rs:
100: src: cpath.clone(),
101: }, CrateAnalysis {
102: exported_items: exported_items,