Skip to content

Commit 1792d9e

Browse files
committed
Use string stored in codemap for pretty-printing comments and literals
Closes #1665
1 parent ec4d05d commit 1792d9e

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

src/comp/driver/driver.rs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ enum pp_mode { ppm_normal, ppm_expanded, ppm_typed, ppm_identified, }
2222

2323
fn default_configuration(sess: session, argv0: str, input: str) ->
2424
ast::crate_cfg {
25-
let libc =
26-
alt sess.targ_cfg.os {
27-
session::os_win32 { "msvcrt.dll" }
28-
session::os_macos { "libc.dylib" }
29-
session::os_linux { "libc.so.6" }
30-
session::os_freebsd { "libc.so.7" }
31-
_ { "libc.so" }
32-
};
25+
let libc = alt sess.targ_cfg.os {
26+
session::os_win32 { "msvcrt.dll" }
27+
session::os_macos { "libc.dylib" }
28+
session::os_linux { "libc.so.6" }
29+
session::os_freebsd { "libc.so.7" }
30+
_ { "libc.so" }
31+
};
3332

3433
let mk = attr::mk_name_value_item_str;
3534

@@ -77,26 +76,13 @@ fn parse_cfgspecs(cfgspecs: [str]) -> ast::crate_cfg {
7776
fn input_is_stdin(filename: str) -> bool { filename == "-" }
7877

7978
fn parse_input(sess: session, cfg: ast::crate_cfg, input: str)
80-
-> {crate: @ast::crate, src: @str} {
81-
let src = get_input_str(sess, input);
82-
let crate = if !input_is_stdin(input) {
79+
-> @ast::crate {
80+
if !input_is_stdin(input) {
8381
parser::parse_crate_from_file(input, cfg, sess.parse_sess)
8482
} else {
83+
let src = @str::unsafe_from_bytes(io::stdin().read_whole_stream());
8584
parser::parse_crate_from_source_str(input, src, cfg, sess.parse_sess)
86-
};
87-
{crate: crate, src: src}
88-
}
89-
90-
fn get_input_str(sess: session, infile: str) -> @str {
91-
let stream = if !input_is_stdin(infile) {
92-
alt io::file_reader(infile) {
93-
result::ok(reader) { reader }
94-
result::err(e) {
95-
sess.fatal(e)
96-
}
97-
}
98-
} else { io::stdin() };
99-
@str::unsafe_from_bytes(stream.read_whole_stream())
85+
}
10086
}
10187

10288
fn time<T>(do_it: bool, what: str, thunk: fn@() -> T) -> T {
@@ -141,11 +127,11 @@ enum compile_upto {
141127
fn compile_upto(sess: session, cfg: ast::crate_cfg,
142128
input: str, upto: compile_upto,
143129
outputs: option::t<output_filenames>)
144-
-> {crate: @ast::crate, tcx: option::t<ty::ctxt>, src: @str} {
130+
-> {crate: @ast::crate, tcx: option::t<ty::ctxt>} {
145131
let time_passes = sess.opts.time_passes;
146-
let {crate, src} =
147-
time(time_passes, "parsing", bind parse_input(sess, cfg, input));
148-
if upto == cu_parse { ret {crate: crate, tcx: none, src: src}; }
132+
let crate = time(time_passes, "parsing",
133+
bind parse_input(sess, cfg, input));
134+
if upto == cu_parse { ret {crate: crate, tcx: none}; }
149135

150136
sess.building_library = session::building_library(
151137
sess.opts.crate_type, crate, sess.opts.test);
@@ -160,7 +146,7 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
160146
time(time_passes, "expansion",
161147
bind syntax::ext::expand::expand_crate(sess, crate));
162148

163-
if upto == cu_expand { ret {crate: crate, tcx: none, src: src}; }
149+
if upto == cu_expand { ret {crate: crate, tcx: none}; }
164150
if sess.opts.libcore {
165151
crate = inject_libcore_reference(sess, crate);
166152
}
@@ -183,7 +169,7 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
183169
time(time_passes, "typechecking",
184170
bind typeck::check_crate(ty_cx, impl_map, crate));
185171

186-
if upto == cu_typeck { ret {crate: crate, tcx: some(ty_cx), src: src}; }
172+
if upto == cu_typeck { ret {crate: crate, tcx: some(ty_cx)}; }
187173

188174
time(time_passes, "block-use checking",
189175
bind middle::block_use::check_crate(ty_cx, crate));
@@ -206,7 +192,7 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
206192

207193
lint::check_crate(ty_cx, crate, sess.opts.lint_opts, time_passes);
208194

209-
if upto == cu_no_trans { ret {crate: crate, tcx: some(ty_cx), src: src}; }
195+
if upto == cu_no_trans { ret {crate: crate, tcx: some(ty_cx)}; }
210196
let outputs = option::get(outputs);
211197

212198
let (llmod, link_meta) =
@@ -222,12 +208,12 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
222208
sess.opts.output_type != link::output_type_exe ||
223209
sess.opts.static && sess.building_library;
224210

225-
if stop_after_codegen { ret {crate: crate, tcx: some(ty_cx), src: src}; }
211+
if stop_after_codegen { ret {crate: crate, tcx: some(ty_cx)}; }
226212

227213
time(time_passes, "Linking",
228214
bind link::link_binary(sess, outputs.obj_filename,
229215
outputs.out_filename, link_meta));
230-
ret {crate: crate, tcx: some(ty_cx), src: src};
216+
ret {crate: crate, tcx: some(ty_cx)};
231217
}
232218

233219
fn compile_input(sess: session, cfg: ast::crate_cfg, input: str,
@@ -286,7 +272,7 @@ fn pretty_print_input(sess: session, cfg: ast::crate_cfg, input: str,
286272
ppm_typed { cu_typeck }
287273
_ { cu_parse }
288274
};
289-
let {crate, tcx, src} = compile_upto(sess, cfg, input, upto, none);
275+
let {crate, tcx} = compile_upto(sess, cfg, input, upto, none);
290276

291277
let ann: pprust::pp_ann = pprust::no_ann();
292278
alt ppm {
@@ -299,6 +285,7 @@ fn pretty_print_input(sess: session, cfg: ast::crate_cfg, input: str,
299285
}
300286
ppm_expanded | ppm_normal {}
301287
}
288+
let src = codemap::get_filemap(sess.codemap, input).src;
302289
pprust::print_crate(sess.codemap, sess.span_diagnostic, crate, input,
303290
io::string_reader(*src), io::stdout(), ann);
304291
}

src/comp/syntax/codemap.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ fn get_filemap(cm: codemap, filename: str) -> filemap {
131131
// (or expected function, found _|_)
132132
fail; // ("asking for " + filename + " which we don't know about");
133133
}
134+
134135
//
135136
// Local Variables:
136137
// mode: rust

0 commit comments

Comments
 (0)