Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@andelf
Created May 13, 2014 16:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andelf/a90f5f89a90120876005 to your computer and use it in GitHub Desktop.
Save andelf/a90f5f89a90120876005 to your computer and use it in GitHub Desktop.
// copied from rustdoc
/// Parses, resolves, and typechecks the given crate
fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<~str>) -> (ast::Crate, CrateAnalysis) {
use syntax::codemap::dummy_spanned;
use rustc::driver::driver::{FileInput, build_configuration,
phase_1_parse_input,
phase_2_configure_and_expand,
phase_3_run_analysis_passes};
let input = FileInput(cpath.clone());
let sessopts = driver::session::Options {
// rust sys rootos::self_exe_path().unwrap().dir_path()),
maybe_sysroot: Some(Path::new("/Users/wangshuyu/opt/rust/")),
addl_lib_search_paths: RefCell::new(libs),
crate_types: vec!(driver::session::CrateTypeDylib),
lint_opts: vec!((lint::Warnings, lint::allow)),
..rustc::driver::session::basic_options().clone()
};
let sess = driver::driver::build_session(sessopts, Some(cpath.clone()));
let mut cfg = build_configuration(&sess);
for cfg_ in cfgs.move_iter() {
let cfg_ = token::intern_and_get_ident(cfg_);
cfg.push(@dummy_spanned(ast::MetaWord(cfg_)));
}
// input => AST
let krate = phase_1_parse_input(&sess, cfg, &input);
let (krate, ast_map) = phase_2_configure_and_expand(&sess, &mut Loader::new(&sess),
krate, &from_str("rustdoc").unwrap());
// pub public_items: PublicItems,
let anlys : CrateAnalysis = phase_3_run_analysis_passes(sess, &krate, ast_map);
(krate, anlys)
}
pub fn run_core(libs: HashSet<Path>, cfgs: Vec<~str>, path: &Path) -> (ast::Crate, CrateAnalysis) {
let (krate, analysis) = get_ast_and_resolve(path, libs, cfgs);
(krate, analysis)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment