2013-09-12 20:10:51 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-08-15 15:28:54 -05:00
|
|
|
use rustc;
|
|
|
|
use rustc::{driver, middle};
|
2013-10-12 16:40:41 -05:00
|
|
|
use rustc::middle::privacy;
|
2013-08-15 15:28:54 -05:00
|
|
|
|
|
|
|
use syntax::ast;
|
2013-08-30 12:55:24 -05:00
|
|
|
use syntax::diagnostic;
|
|
|
|
use syntax::parse;
|
|
|
|
use syntax;
|
2013-08-15 15:28:54 -05:00
|
|
|
|
|
|
|
use std::os;
|
|
|
|
use std::local_data;
|
2013-11-11 22:17:47 -06:00
|
|
|
use std::hashmap::{HashSet};
|
2013-08-15 15:28:54 -05:00
|
|
|
|
|
|
|
use visit_ast::RustdocVisitor;
|
|
|
|
use clean;
|
|
|
|
use clean::Clean;
|
|
|
|
|
|
|
|
pub struct DocContext {
|
2013-09-27 21:46:09 -05:00
|
|
|
crate: ast::Crate,
|
2013-12-22 13:23:04 -06:00
|
|
|
tycx: Option<middle::ty::ctxt>,
|
2013-08-15 15:28:54 -05:00
|
|
|
sess: driver::session::Session
|
|
|
|
}
|
|
|
|
|
2013-10-12 16:40:41 -05:00
|
|
|
pub struct CrateAnalysis {
|
|
|
|
exported_items: privacy::ExportedItems,
|
|
|
|
}
|
|
|
|
|
2013-08-15 15:28:54 -05:00
|
|
|
/// Parses, resolves, and typechecks the given crate
|
2013-10-12 16:40:41 -05:00
|
|
|
fn get_ast_and_resolve(cpath: &Path,
|
2013-11-24 22:31:21 -06:00
|
|
|
libs: HashSet<Path>, cfgs: ~[~str]) -> (DocContext, CrateAnalysis) {
|
2013-08-15 15:28:54 -05:00
|
|
|
use syntax::codemap::dummy_spanned;
|
2013-10-12 16:40:41 -05:00
|
|
|
use rustc::driver::driver::{file_input, build_configuration,
|
|
|
|
phase_1_parse_input,
|
|
|
|
phase_2_configure_and_expand,
|
|
|
|
phase_3_run_analysis_passes};
|
2013-08-15 15:28:54 -05:00
|
|
|
|
|
|
|
let parsesess = parse::new_parse_sess(None);
|
|
|
|
let input = file_input(cpath.clone());
|
|
|
|
|
|
|
|
let sessopts = @driver::session::options {
|
|
|
|
binary: @"rustdoc",
|
2013-09-26 19:21:59 -05:00
|
|
|
maybe_sysroot: Some(@os::self_exe_path().unwrap().dir_path()),
|
2013-08-15 15:28:54 -05:00
|
|
|
addl_lib_search_paths: @mut libs,
|
2013-12-02 23:26:40 -06:00
|
|
|
outputs: ~[driver::session::OutputDylib],
|
2013-08-15 15:28:54 -05:00
|
|
|
.. (*rustc::driver::session::basic_options()).clone()
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let diagnostic_handler = syntax::diagnostic::mk_handler(None);
|
|
|
|
let span_diagnostic_handler =
|
|
|
|
syntax::diagnostic::mk_span_handler(diagnostic_handler, parsesess.cm);
|
|
|
|
|
2013-08-30 12:55:24 -05:00
|
|
|
let sess = driver::driver::build_session_(sessopts,
|
|
|
|
parsesess.cm,
|
|
|
|
@diagnostic::DefaultEmitter as
|
|
|
|
@diagnostic::Emitter,
|
|
|
|
span_diagnostic_handler);
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2013-09-12 14:11:06 -05:00
|
|
|
let mut cfg = build_configuration(sess);
|
2013-11-24 22:31:21 -06:00
|
|
|
for cfg_ in cfgs.move_iter() {
|
|
|
|
cfg.push(@dummy_spanned(ast::MetaWord(cfg_.to_managed())));
|
|
|
|
}
|
2013-08-15 15:28:54 -05:00
|
|
|
|
|
|
|
let mut crate = phase_1_parse_input(sess, cfg.clone(), &input);
|
|
|
|
crate = phase_2_configure_and_expand(sess, cfg, crate);
|
2013-10-12 16:40:41 -05:00
|
|
|
let driver::driver::CrateAnalysis {
|
2013-11-28 14:22:53 -06:00
|
|
|
exported_items, ty_cx, ..
|
2013-10-12 16:40:41 -05:00
|
|
|
} = phase_3_run_analysis_passes(sess, &crate);
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("crate: {:?}", crate);
|
2013-12-22 13:23:04 -06:00
|
|
|
return (DocContext { crate: crate, tycx: Some(ty_cx), sess: sess },
|
2013-11-11 22:17:47 -06:00
|
|
|
CrateAnalysis { exported_items: exported_items });
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
2013-11-24 22:31:21 -06:00
|
|
|
pub fn run_core (libs: HashSet<Path>, cfgs: ~[~str], path: &Path) -> (clean::Crate, CrateAnalysis) {
|
|
|
|
let (ctxt, analysis) = get_ast_and_resolve(path, libs, cfgs);
|
2013-10-12 16:40:41 -05:00
|
|
|
let ctxt = @ctxt;
|
2013-08-15 15:28:54 -05:00
|
|
|
local_data::set(super::ctxtkey, ctxt);
|
|
|
|
|
|
|
|
let v = @mut RustdocVisitor::new();
|
2013-09-27 21:46:09 -05:00
|
|
|
v.visit(&ctxt.crate);
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2013-10-12 16:40:41 -05:00
|
|
|
(v.clean(), analysis)
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|