rustc: Allow a custom diagnostic emitter when building the handler

This commit is contained in:
Brian Anderson 2012-01-13 19:00:09 -08:00
parent 7cbd90f501
commit e4849d5e5d
5 changed files with 36 additions and 17 deletions

View File

@ -104,7 +104,7 @@ fn load_pkg(filename: str) -> option::t<pkg> {
let sess = @{ let sess = @{
cm: cm, cm: cm,
mutable next_id: 0, mutable next_id: 0,
diagnostic: diagnostic::mk_codemap_handler(cm) diagnostic: diagnostic::mk_codemap_handler(cm, none)
}; };
let c = parser::parse_crate_from_crate_file(filename, [], sess); let c = parser::parse_crate_from_crate_file(filename, [], sess);

View File

@ -3,10 +3,14 @@
import syntax::codemap; import syntax::codemap;
import codemap::span; import codemap::span;
export emit_diagnostic; export emitter, emit_diagnostic;
export diagnostictype, fatal, error, warning, note; export diagnostictype, fatal, error, warning, note;
export handler, mk_codemap_handler; export handler, mk_codemap_handler;
type emitter = fn@(cmsp: option<(codemap::codemap, span)>,
msg: str, t: diagnostictype);
iface handler { iface handler {
fn span_fatal(sp: span, msg: str) -> !; fn span_fatal(sp: span, msg: str) -> !;
fn fatal(msg: str) -> !; fn fatal(msg: str) -> !;
@ -26,24 +30,25 @@
type codemap_t = @{ type codemap_t = @{
cm: codemap::codemap, cm: codemap::codemap,
mutable err_count: uint mutable err_count: uint,
emit: emitter
}; };
impl codemap_handler of handler for codemap_t { impl codemap_handler of handler for codemap_t {
fn span_fatal(sp: span, msg: str) -> ! { fn span_fatal(sp: span, msg: str) -> ! {
emit_diagnostic(some((self.cm, sp)), msg, fatal); self.emit(some((self.cm, sp)), msg, fatal);
fail; fail;
} }
fn fatal(msg: str) -> ! { fn fatal(msg: str) -> ! {
emit_diagnostic(none, msg, fatal); self.emit(none, msg, fatal);
fail; fail;
} }
fn span_err(sp: span, msg: str) { fn span_err(sp: span, msg: str) {
emit_diagnostic(some((self.cm, sp)), msg, error); self.emit(some((self.cm, sp)), msg, error);
self.err_count += 1u; self.err_count += 1u;
} }
fn err(msg: str) { fn err(msg: str) {
emit_diagnostic(none, msg, error); self.emit(none, msg, error);
self.err_count += 1u; self.err_count += 1u;
} }
fn has_errors() -> bool { self.err_count > 0u } fn has_errors() -> bool { self.err_count > 0u }
@ -53,16 +58,16 @@ fn abort_if_errors() {
} }
} }
fn span_warn(sp: span, msg: str) { fn span_warn(sp: span, msg: str) {
emit_diagnostic(some((self.cm, sp)), msg, warning); self.emit(some((self.cm, sp)), msg, warning);
} }
fn warn(msg: str) { fn warn(msg: str) {
emit_diagnostic(none, msg, warning); self.emit(none, msg, warning);
} }
fn span_note(sp: span, msg: str) { fn span_note(sp: span, msg: str) {
emit_diagnostic(some((self.cm, sp)), msg, note); self.emit(some((self.cm, sp)), msg, note);
} }
fn note(msg: str) { fn note(msg: str) {
emit_diagnostic(none, msg, note); self.emit(none, msg, note);
} }
fn span_bug(sp: span, msg: str) -> ! { fn span_bug(sp: span, msg: str) -> ! {
self.span_fatal(sp, #fmt["internal compiler error %s", msg]); self.span_fatal(sp, #fmt["internal compiler error %s", msg]);
@ -76,10 +81,24 @@ fn span_unimpl(sp: span, msg: str) -> ! {
fn unimpl(msg: str) -> ! { self.bug("unimplemented " + msg); } fn unimpl(msg: str) -> ! { self.bug("unimplemented " + msg); }
} }
fn mk_codemap_handler(cm: codemap::codemap) -> handler { fn mk_codemap_handler(cm: codemap::codemap,
emitter: option<emitter>) -> handler {
let emit = alt emitter {
some(e) { e }
none. {
let f = fn@(cmsp: option<(codemap::codemap, span)>,
msg: str, t: diagnostictype) {
emit_diagnostic(cmsp, msg, t);
};
f
}
};
@{ @{
cm: cm, cm: cm,
mutable err_count: 0u, mutable err_count: 0u,
emit: emit
} as handler } as handler
} }

View File

@ -452,7 +452,7 @@ fn build_session(sopts: @session::options, input: str) -> session::session {
sopts.target_triple, sopts.target_triple,
sopts.addl_lib_search_paths); sopts.addl_lib_search_paths);
let codemap = codemap::new_codemap(); let codemap = codemap::new_codemap();
let diagnostic_handler = diagnostic::mk_codemap_handler(codemap); let diagnostic_handler = diagnostic::mk_codemap_handler(codemap, none);
@{targ_cfg: target_cfg, @{targ_cfg: target_cfg,
opts: sopts, opts: sopts,
cstore: cstore, cstore: cstore,

View File

@ -262,7 +262,7 @@ fn check_variants_T<T: copy>(
let str3 = let str3 =
as_str(bind pprust::print_crate( as_str(bind pprust::print_crate(
codemap, codemap,
diagnostic::mk_codemap_handler(codemap), diagnostic::mk_codemap_handler(codemap, none),
crate2, crate2,
filename, filename,
io::string_reader(""), _, io::string_reader(""), _,
@ -419,7 +419,7 @@ fn parse_and_print(code: str) -> str {
let sess = @{ let sess = @{
cm: cm, cm: cm,
mutable next_id: 0, mutable next_id: 0,
diagnostic: diagnostic::mk_codemap_handler(cm) diagnostic: diagnostic::mk_codemap_handler(cm, none)
}; };
write_file(filename, code); write_file(filename, code);
let crate = parser::parse_crate_from_source_str( let crate = parser::parse_crate_from_source_str(
@ -566,7 +566,7 @@ fn check_variants(files: [str], cx: context) {
let sess = @{ let sess = @{
cm: cm, cm: cm,
mutable next_id: 0, mutable next_id: 0,
diagnostic: diagnostic::mk_codemap_handler(cm) diagnostic: diagnostic::mk_codemap_handler(cm, none)
}; };
let crate = let crate =
parser::parse_crate_from_source_str( parser::parse_crate_from_source_str(

View File

@ -194,7 +194,7 @@ fn main(argv: [str]) {
let sess = @{ let sess = @{
cm: cm, cm: cm,
mutable next_id: 0, mutable next_id: 0,
diagnostic: diagnostic::mk_codemap_handler(cm) diagnostic: diagnostic::mk_codemap_handler(cm, none)
}; };
let rd = { ps: pprust::rust_printer(w), w: w }; let rd = { ps: pprust::rust_printer(w), w: w };
doc_header(rd, argv[1]); doc_header(rd, argv[1]);