Skip to content

Commit fa6c18e

Browse files
committed
rustc: Refactor driver to better understand string sources
1 parent 50a3dd4 commit fa6c18e

File tree

3 files changed

+79
-32
lines changed

3 files changed

+79
-32
lines changed

src/rustc/driver/driver.rs

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,20 @@ import back::{x86, x86_64};
1818
enum pp_mode {ppm_normal, ppm_expanded, ppm_typed, ppm_identified,
1919
ppm_expanded_identified }
2020

21-
fn default_configuration(sess: session, argv0: str, input: str) ->
21+
#[doc = "
22+
The name used for source code that doesn't originate in a file
23+
(e.g. source from stdin or a string)
24+
"]
25+
fn anon_src() -> str { "<anon>" }
26+
27+
fn source_name(input: input) -> str {
28+
alt input {
29+
file_input(ifile) { ifile }
30+
str_input(_) { anon_src() }
31+
}
32+
}
33+
34+
fn default_configuration(sess: session, argv0: str, input: input) ->
2235
ast::crate_cfg {
2336
let libc = alt sess.targ_cfg.os {
2437
session::os_win32 { "msvcrt.dll" }
@@ -42,10 +55,10 @@ fn default_configuration(sess: session, argv0: str, input: str) ->
4255
mk("target_libc", libc),
4356
// Build bindings.
4457
mk("build_compiler", argv0),
45-
mk("build_input", input)];
58+
mk("build_input", source_name(input))];
4659
}
4760

48-
fn build_configuration(sess: session, argv0: str, input: str) ->
61+
fn build_configuration(sess: session, argv0: str, input: input) ->
4962
ast::crate_cfg {
5063
// Combine the configuration requested by the session (command line) with
5164
// some default and generated configuration items
@@ -71,15 +84,24 @@ fn parse_cfgspecs(cfgspecs: [str]) -> ast::crate_cfg {
7184
ret words;
7285
}
7386

74-
fn input_is_stdin(filename: str) -> bool { filename == "-" }
87+
enum input {
88+
#[doc = "Load source from file"]
89+
file_input(str),
90+
#[doc = "The string is the source"]
91+
str_input(str)
92+
}
7593

76-
fn parse_input(sess: session, cfg: ast::crate_cfg, input: str)
94+
fn parse_input(sess: session, cfg: ast::crate_cfg, input: input)
7795
-> @ast::crate {
78-
if !input_is_stdin(input) {
79-
parse::parse_crate_from_file(input, cfg, sess.parse_sess)
80-
} else {
81-
let src = @str::from_bytes(io::stdin().read_whole_stream());
82-
parse::parse_crate_from_source_str(input, src, cfg, sess.parse_sess)
96+
alt input {
97+
file_input(file) {
98+
parse::parse_crate_from_file(file, cfg, sess.parse_sess)
99+
}
100+
str_input(src) {
101+
// FIXME: Don't really want to box the source string
102+
parse::parse_crate_from_source_str(
103+
anon_src(), @src, cfg, sess.parse_sess)
104+
}
83105
}
84106
}
85107

@@ -102,7 +124,7 @@ enum compile_upto {
102124
}
103125

104126
fn compile_upto(sess: session, cfg: ast::crate_cfg,
105-
input: str, upto: compile_upto,
127+
input: input, upto: compile_upto,
106128
outputs: option<output_filenames>)
107129
-> {crate: @ast::crate, tcx: option<ty::ctxt>} {
108130
let time_passes = sess.opts.time_passes;
@@ -208,7 +230,7 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
208230
ret {crate: crate, tcx: some(ty_cx)};
209231
}
210232

211-
fn compile_input(sess: session, cfg: ast::crate_cfg, input: str,
233+
fn compile_input(sess: session, cfg: ast::crate_cfg, input: input,
212234
outdir: option<str>, output: option<str>) {
213235

214236
let upto = if sess.opts.parse_only { cu_parse }
@@ -218,7 +240,7 @@ fn compile_input(sess: session, cfg: ast::crate_cfg, input: str,
218240
compile_upto(sess, cfg, input, upto, some(outputs));
219241
}
220242

221-
fn pretty_print_input(sess: session, cfg: ast::crate_cfg, input: str,
243+
fn pretty_print_input(sess: session, cfg: ast::crate_cfg, input: input,
222244
ppm: pp_mode) {
223245
fn ann_paren_for_expr(node: pprust::ann_node) {
224246
alt node { pprust::node_expr(s, expr) { pprust::popen(s); } _ { } }
@@ -277,9 +299,10 @@ fn pretty_print_input(sess: session, cfg: ast::crate_cfg, input: str,
277299
}
278300
ppm_expanded | ppm_normal {}
279301
}
280-
let src = codemap::get_filemap(sess.codemap, input).src;
302+
let src = codemap::get_filemap(sess.codemap, source_name(input)).src;
281303
io::with_str_reader(*src) { |rdr|
282-
pprust::print_crate(sess.codemap, sess.span_diagnostic, crate, input,
304+
pprust::print_crate(sess.codemap, sess.span_diagnostic, crate,
305+
source_name(input),
283306
rdr, io::stdout(), ann);
284307
}
285308
}
@@ -549,7 +572,7 @@ fn opts() -> [getopts::opt] {
549572

550573
type output_filenames = @{out_filename: str, obj_filename:str};
551574

552-
fn build_output_filenames(ifile: str,
575+
fn build_output_filenames(input: input,
553576
odir: option<str>,
554577
ofile: option<str>,
555578
sess: session)
@@ -582,19 +605,25 @@ fn build_output_filenames(ifile: str,
582605
let dirname = alt odir {
583606
some(d) { d }
584607
none {
585-
if input_is_stdin(ifile) {
608+
alt input {
609+
str_input(_) {
586610
os::getcwd()
587-
} else {
611+
}
612+
file_input(ifile) {
588613
path::dirname(ifile)
614+
}
589615
}
590616
}
591617
};
592618

593-
let base_filename = if !input_is_stdin(ifile) {
619+
let base_filename = alt input {
620+
file_input(ifile) {
594621
let (path, _) = path::splitext(ifile);
595622
path::basename(path)
596-
} else {
623+
}
624+
str_input(_) {
597625
"rust_out"
626+
}
598627
};
599628
let base_path = path::connect(dirname, base_filename);
600629

@@ -659,7 +688,7 @@ mod test {
659688
};
660689
let sessopts = build_session_options(match, diagnostic::emit);
661690
let sess = build_session(sessopts, diagnostic::emit);
662-
let cfg = build_configuration(sess, "whatever", "whatever");
691+
let cfg = build_configuration(sess, "whatever", str_input(""));
663692
assert (attr::contains_name(cfg, "test"));
664693
}
665694

@@ -675,7 +704,7 @@ mod test {
675704
};
676705
let sessopts = build_session_options(match, diagnostic::emit);
677706
let sess = build_session(sessopts, diagnostic::emit);
678-
let cfg = build_configuration(sess, "whatever", "whatever");
707+
let cfg = build_configuration(sess, "whatever", str_input(""));
679708
let test_items = attr::find_meta_items_by_name(cfg, "test");
680709
assert (vec::len(test_items) == 1u);
681710
}

src/rustc/driver/rustc.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import rustc::driver::driver::*;
1515
import rustc::syntax::codemap;
1616
import rustc::driver::diagnostic;
1717
import rustc::middle::lint;
18+
import io::reader_util;
1819

1920
fn version(argv0: str) {
2021
let mut vers = "unknown version";
@@ -138,32 +139,47 @@ fn run_compiler(args: [str], demitter: diagnostic::emitter) {
138139
version(binary);
139140
ret;
140141
}
141-
let ifile = alt vec::len(match.free) {
142+
let input = alt vec::len(match.free) {
142143
0u { early_error(demitter, "no input filename given") }
143-
1u { match.free[0] }
144+
1u {
145+
let ifile = match.free[0];
146+
if ifile == "-" {
147+
let src = str::from_bytes(io::stdin().read_whole_stream());
148+
str_input(src)
149+
} else {
150+
file_input(ifile)
151+
}
152+
}
144153
_ { early_error(demitter, "multiple input filenames provided") }
145154
};
146155

147156
let sopts = build_session_options(match, demitter);
148157
let sess = build_session(sopts, demitter);
149158
let odir = getopts::opt_maybe_str(match, "out-dir");
150159
let ofile = getopts::opt_maybe_str(match, "o");
151-
let cfg = build_configuration(sess, binary, ifile);
160+
let cfg = build_configuration(sess, binary, input);
152161
let pretty =
153162
option::map(getopts::opt_default(match, "pretty",
154163
"normal"),
155164
bind parse_pretty(sess, _));
156165
alt pretty {
157-
some::<pp_mode>(ppm) { pretty_print_input(sess, cfg, ifile, ppm); ret; }
166+
some::<pp_mode>(ppm) { pretty_print_input(sess, cfg, input, ppm); ret; }
158167
none::<pp_mode> {/* continue */ }
159168
}
160169
let ls = opt_present(match, "ls");
161170
if ls {
162-
list_metadata(sess, ifile, io::stdout());
171+
alt input {
172+
file_input(ifile) {
173+
list_metadata(sess, ifile, io::stdout());
174+
}
175+
str_input(_) {
176+
early_error(demitter, "can not list metadata for stdin");
177+
}
178+
}
163179
ret;
164180
}
165181

166-
compile_input(sess, cfg, ifile, odir, ofile);
182+
compile_input(sess, cfg, input, odir, ofile);
167183
}
168184

169185
/*

src/rustdoc/parse.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[doc = "AST-parsing helpers"];
22

33
import rustc::driver::driver;
4+
import driver::{file_input, str_input};
45
import rustc::driver::session;
56
import rustc::driver::diagnostic;
67
import rustc::syntax::ast;
@@ -33,14 +34,15 @@ fn from_str(source: str) -> @ast::crate {
3334
}
3435

3536
fn from_file_sess(sess: session::session, file: str) -> @ast::crate {
36-
parse::parse_crate_from_file(file, cfg(sess), sess.parse_sess)
37+
parse::parse_crate_from_file(
38+
file, cfg(sess, file_input(file)), sess.parse_sess)
3739
}
3840

3941
fn from_str_sess(sess: session::session, source: str) -> @ast::crate {
4042
parse::parse_crate_from_source_str(
41-
"-", @source, cfg(sess), sess.parse_sess)
43+
"-", @source, cfg(sess, str_input(source)), sess.parse_sess)
4244
}
4345

44-
fn cfg(sess: session::session) -> ast::crate_cfg {
45-
driver::default_configuration(sess, "rustdoc", "<anon>")
46+
fn cfg(sess: session::session, input: driver::input) -> ast::crate_cfg {
47+
driver::default_configuration(sess, "rustdoc", input)
4648
}

0 commit comments

Comments
 (0)