Skip to content

Commit e4849d5

Browse files
committed
rustc: Allow a custom diagnostic emitter when building the handler
1 parent 7cbd90f commit e4849d5

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

src/cargo/cargo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn load_pkg(filename: str) -> option::t<pkg> {
104104
let sess = @{
105105
cm: cm,
106106
mutable next_id: 0,
107-
diagnostic: diagnostic::mk_codemap_handler(cm)
107+
diagnostic: diagnostic::mk_codemap_handler(cm, none)
108108
};
109109
let c = parser::parse_crate_from_crate_file(filename, [], sess);
110110

src/comp/driver/diagnostic.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import io::writer_util;
33
import syntax::codemap;
44
import codemap::span;
55

6-
export emit_diagnostic;
6+
export emitter, emit_diagnostic;
77
export diagnostictype, fatal, error, warning, note;
88
export handler, mk_codemap_handler;
99

10+
type emitter = fn@(cmsp: option<(codemap::codemap, span)>,
11+
msg: str, t: diagnostictype);
12+
13+
1014
iface handler {
1115
fn span_fatal(sp: span, msg: str) -> !;
1216
fn fatal(msg: str) -> !;
@@ -26,24 +30,25 @@ iface handler {
2630

2731
type codemap_t = @{
2832
cm: codemap::codemap,
29-
mutable err_count: uint
33+
mutable err_count: uint,
34+
emit: emitter
3035
};
3136

3237
impl codemap_handler of handler for codemap_t {
3338
fn span_fatal(sp: span, msg: str) -> ! {
34-
emit_diagnostic(some((self.cm, sp)), msg, fatal);
39+
self.emit(some((self.cm, sp)), msg, fatal);
3540
fail;
3641
}
3742
fn fatal(msg: str) -> ! {
38-
emit_diagnostic(none, msg, fatal);
43+
self.emit(none, msg, fatal);
3944
fail;
4045
}
4146
fn span_err(sp: span, msg: str) {
42-
emit_diagnostic(some((self.cm, sp)), msg, error);
47+
self.emit(some((self.cm, sp)), msg, error);
4348
self.err_count += 1u;
4449
}
4550
fn err(msg: str) {
46-
emit_diagnostic(none, msg, error);
51+
self.emit(none, msg, error);
4752
self.err_count += 1u;
4853
}
4954
fn has_errors() -> bool { self.err_count > 0u }
@@ -53,16 +58,16 @@ impl codemap_handler of handler for codemap_t {
5358
}
5459
}
5560
fn span_warn(sp: span, msg: str) {
56-
emit_diagnostic(some((self.cm, sp)), msg, warning);
61+
self.emit(some((self.cm, sp)), msg, warning);
5762
}
5863
fn warn(msg: str) {
59-
emit_diagnostic(none, msg, warning);
64+
self.emit(none, msg, warning);
6065
}
6166
fn span_note(sp: span, msg: str) {
62-
emit_diagnostic(some((self.cm, sp)), msg, note);
67+
self.emit(some((self.cm, sp)), msg, note);
6368
}
6469
fn note(msg: str) {
65-
emit_diagnostic(none, msg, note);
70+
self.emit(none, msg, note);
6671
}
6772
fn span_bug(sp: span, msg: str) -> ! {
6873
self.span_fatal(sp, #fmt["internal compiler error %s", msg]);
@@ -76,10 +81,24 @@ impl codemap_handler of handler for codemap_t {
7681
fn unimpl(msg: str) -> ! { self.bug("unimplemented " + msg); }
7782
}
7883

79-
fn mk_codemap_handler(cm: codemap::codemap) -> handler {
84+
fn mk_codemap_handler(cm: codemap::codemap,
85+
emitter: option<emitter>) -> handler {
86+
87+
let emit = alt emitter {
88+
some(e) { e }
89+
none. {
90+
let f = fn@(cmsp: option<(codemap::codemap, span)>,
91+
msg: str, t: diagnostictype) {
92+
emit_diagnostic(cmsp, msg, t);
93+
};
94+
f
95+
}
96+
};
97+
8098
@{
8199
cm: cm,
82100
mutable err_count: 0u,
101+
emit: emit
83102
} as handler
84103
}
85104

src/comp/driver/driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ fn build_session(sopts: @session::options, input: str) -> session::session {
452452
sopts.target_triple,
453453
sopts.addl_lib_search_paths);
454454
let codemap = codemap::new_codemap();
455-
let diagnostic_handler = diagnostic::mk_codemap_handler(codemap);
455+
let diagnostic_handler = diagnostic::mk_codemap_handler(codemap, none);
456456
@{targ_cfg: target_cfg,
457457
opts: sopts,
458458
cstore: cstore,

src/fuzzer/fuzzer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ fn check_variants_T<T: copy>(
262262
let str3 =
263263
as_str(bind pprust::print_crate(
264264
codemap,
265-
diagnostic::mk_codemap_handler(codemap),
265+
diagnostic::mk_codemap_handler(codemap, none),
266266
crate2,
267267
filename,
268268
io::string_reader(""), _,
@@ -419,7 +419,7 @@ fn parse_and_print(code: str) -> str {
419419
let sess = @{
420420
cm: cm,
421421
mutable next_id: 0,
422-
diagnostic: diagnostic::mk_codemap_handler(cm)
422+
diagnostic: diagnostic::mk_codemap_handler(cm, none)
423423
};
424424
write_file(filename, code);
425425
let crate = parser::parse_crate_from_source_str(
@@ -566,7 +566,7 @@ fn check_variants(files: [str], cx: context) {
566566
let sess = @{
567567
cm: cm,
568568
mutable next_id: 0,
569-
diagnostic: diagnostic::mk_codemap_handler(cm)
569+
diagnostic: diagnostic::mk_codemap_handler(cm, none)
570570
};
571571
let crate =
572572
parser::parse_crate_from_source_str(

src/rustdoc/rustdoc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn main(argv: [str]) {
194194
let sess = @{
195195
cm: cm,
196196
mutable next_id: 0,
197-
diagnostic: diagnostic::mk_codemap_handler(cm)
197+
diagnostic: diagnostic::mk_codemap_handler(cm, none)
198198
};
199199
let rd = { ps: pprust::rust_printer(w), w: w };
200200
doc_header(rd, argv[1]);

0 commit comments

Comments
 (0)