Skip to content

Commit d43f60e

Browse files
committed
rustc: Fold --pretty and --typed-pretty into a single option with an optional argument
1 parent 8c3809a commit d43f60e

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

src/comp/driver/rustc.rs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ import std::getopts;
2929
import std::getopts::optopt;
3030
import std::getopts::optmulti;
3131
import std::getopts::optflag;
32+
import std::getopts::optflagopt;
3233
import std::getopts::opt_present;
3334

3435
import back::link::output_type;
3536

37+
tag pp_mode {
38+
ppm_normal;
39+
ppm_typed;
40+
}
41+
3642
fn default_environment(session::session sess,
3743
str argv0,
3844
str input) -> eval::env {
@@ -112,19 +118,20 @@ fn compile_input(session::session sess,
112118
}
113119

114120
fn pretty_print_input(session::session sess, eval::env env, str input,
115-
bool typed) {
121+
pp_mode ppm) {
116122
auto def = tup(ast::local_crate, 0);
117123
auto p = front::parser::new_parser(sess, env, def, input, 0u, 0u);
118124
auto crate = front::parser::parse_crate_from_source_file(p);
119125

120126
auto mode;
121-
if (typed) {
122-
auto def_map = resolve::resolve_crate(sess, crate);
123-
auto ty_cx = ty::mk_ctxt(sess, def_map);
124-
typeck::check_crate(ty_cx, crate);
125-
mode = pprust::mo_typed(ty_cx);
126-
} else {
127-
mode = pprust::mo_untyped;
127+
alt (ppm) {
128+
case (ppm_typed) {
129+
auto def_map = resolve::resolve_crate(sess, crate);
130+
auto ty_cx = ty::mk_ctxt(sess, def_map);
131+
typeck::check_crate(ty_cx, crate);
132+
mode = pprust::mo_typed(ty_cx);
133+
}
134+
case (ppm_normal) { mode = pprust::mo_untyped; }
128135
}
129136

130137
pprust::print_file(sess, crate.node.module, input, std::io::stdout(),
@@ -150,8 +157,7 @@ options:
150157
-o <filename> write output to <filename>
151158
--glue generate glue.bc file
152159
--shared compile a shared-library crate
153-
--pretty pretty-print the input instead of compiling
154-
--typed-pretty pretty-print the input with types instead of compiling
160+
--pretty [type] pretty-print the input instead of compiling
155161
--ls list the symbols defined by a crate file
156162
-L <path> add a directory to the library search path
157163
--noverify suppress LLVM verification step (slight speedup)
@@ -304,12 +310,20 @@ fn build_session(@session::options sopts) -> session::session {
304310
ret sess;
305311
}
306312

313+
fn parse_pretty(session::session sess, &str name) -> pp_mode {
314+
if (str::eq(name, "normal")) { ret ppm_normal; }
315+
else if (str::eq(name, "typed")) { ret ppm_typed; }
316+
else {
317+
sess.err("argument to `pretty` must be either `normal` or `typed`");
318+
}
319+
}
320+
307321
fn main(vec[str] args) {
308322

309323
auto opts = [optflag("h"), optflag("help"),
310324
optflag("v"), optflag("version"),
311325
optflag("glue"), optflag("emit-llvm"),
312-
optflag("pretty"), optflag("typed-pretty"),
326+
optflagopt("pretty"),
313327
optflag("ls"), optflag("parse-only"),
314328
optflag("O"), optopt("OptLevel"),
315329
optflag("shared"), optmulti("L"),
@@ -365,13 +379,19 @@ fn main(vec[str] args) {
365379
auto ifile = match.free.(0);
366380
let str saved_out_filename = "";
367381
auto env = default_environment(sess, binary, ifile);
368-
auto pretty = opt_present(match, "pretty");
369-
auto typed_pretty = opt_present(match, "typed-pretty");
382+
auto pretty = option::map[str,pp_mode](bind parse_pretty(sess, _),
383+
getopts::opt_default(match, "pretty", "normal"));
370384
auto ls = opt_present(match, "ls");
371-
if (pretty || typed_pretty) {
372-
pretty_print_input(sess, env, ifile, typed_pretty);
373-
ret;
374-
} else if (ls) {
385+
386+
alt (pretty) {
387+
case (some[pp_mode](?ppm)) {
388+
pretty_print_input(sess, env, ifile, ppm);
389+
ret;
390+
}
391+
case (none[pp_mode]) { /* continue */ }
392+
}
393+
394+
if (ls) {
375395
front::creader::list_file_metadata(ifile, std::io::stdout());
376396
ret;
377397
} else {
@@ -401,6 +421,8 @@ fn main(vec[str] args) {
401421

402422
// If the user wants an exe generated we need to invoke
403423
// gcc to link the object file with some libs
424+
//
425+
// TODO: Factor this out of main.
404426
if (sopts.output_type == link::output_type_exe) {
405427

406428
//FIXME: Should we make the 'stage3's variable here?

0 commit comments

Comments
 (0)