Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement emit-ast option ( see issue #10485 ) #10711

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
@@ -27,12 +27,19 @@ use util::ppaux;

use std::hashmap::{HashMap,HashSet};
use std::io;
use std::io::Decorator;
use std::io::fs;
use std::io::fs::File;
use std::io::mem::MemReader;
use std::io::mem::MemWriter;
use std::os;
use std::str;
use std::vec;
use extra::getopts::groups::{optopt, optmulti, optflag, optflagopt};
use extra::getopts;
use extra::json;
use extra::serialize::Encodable;
use extra::treemap;
use syntax::ast;
use syntax::abi;
use syntax::attr;
@@ -567,6 +574,34 @@ pub fn pretty_print_input(sess: Session,
is_expanded);
}

pub fn emit_ast(sess: Session,
cfg: ast::CrateConfig,
input: &input,
ofile: &Option<Path>) {
let expanded_crate = {
let crate = phase_1_parse_input(sess, cfg.clone(), input);
phase_2_configure_and_expand(sess, cfg, crate)
};

let crate_json_str = {
let w = @mut MemWriter::new();
expanded_crate.encode(&mut json::Encoder(w as @mut io::Writer));
str::from_utf8(*w.inner_ref())
};
let crate_json = match json::from_str(crate_json_str) {
Ok(j) => j,
Err(_) => fail!("Rust generated JSON is invalid??")
};

let mut json = ~treemap::TreeMap::new();
json.insert(~"crate", crate_json);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should include the compiler version, it can be retrieved by option_env!("CFG_VERSION") which gives Option<&'static str> that is Some when the CFG_VERSION env var exists, and None if it doesn't.

I actually think adding a static VERSION: Option<&'static str> = option_env!("CFG_VERSION"); to the librustc/lib.rs file, and using that in the 2 places that CFG_VERSION is currently used and here would be best (those two places are version in the lib.rs file, and compile_unit_metadata in librustc/middle/trans/debuginfo.rs; the latter would change to ::VERSION.unwrap("unknown")).

Something like

json.insert(~"version", json::String(::VERSION.unwrap("unknown").to_owned()))

FWIW, the ::... syntax means "from the crate root".

(Probably worth keeping that as a seperate commit for now... but I'm not particularly sure or fussed.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed generated string intermediate value as you suggested.

(btw, do I need to provide a patch in only on commit?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple commits are fine (although you may be asked to squash commits that are not particularly distinct).

let writer = match *ofile {
None => @mut io::stdout() as @mut Writer,
Some(ref out_file) => @mut File::create(out_file) as @mut Writer
};
json::Object(json).to_writer(writer);
}

pub fn get_os(triple: &str) -> Option<abi::Os> {
for &(name, os) in os_names.iter() {
if triple.contains(name) { return Some(os) }
@@ -910,6 +945,7 @@ pub fn optgroups() -> ~[getopts::groups::OptGroup] {
typed (crates expanded, with type annotations),
or identified (fully parenthesized,
AST nodes and blocks with IDs)", "TYPE"),
optflag("", "emit-ast", "Dump syntax in JSON after phase 2"),
optflag("S", "", "Compile only; do not assemble or link"),
optflag("", "save-temps",
"Write intermediate files (.bc, .opt.bc, .o)
6 changes: 5 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ extern mod syntax;
use driver::driver::{host_triple, optgroups, early_error};
use driver::driver::{str_input, file_input, build_session_options};
use driver::driver::{build_session, build_configuration, parse_pretty};
use driver::driver::{PpMode, pretty_print_input, list_metadata};
use driver::driver::{PpMode, pretty_print_input, emit_ast, list_metadata};
use driver::driver::{compile_input};
use driver::session;
use middle::lint;
@@ -269,6 +269,10 @@ pub fn run_compiler(args: &[~str], demitter: @diagnostic::Emitter) {
}
None::<PpMode> => {/* continue */ }
}
if matches.opt_present("emit-ast") {
emit_ast(sess, cfg, &input, &ofile);
return;
}
let ls = matches.opt_present("ls");
if ls {
match input {