Skip to content

Commit

Permalink
libsyntax: add COMMAND_LINE_SP and use it for spans generated from th…
Browse files Browse the repository at this point in the history
…e command line
  • Loading branch information
Manishearth committed Jan 8, 2015
1 parent c41cafb commit 0bd022c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/librustc/metadata/creader.rs
Expand Up @@ -26,7 +26,7 @@ use syntax::ast;
use syntax::abi;
use syntax::attr;
use syntax::attr::AttrMetaMethods;
use syntax::codemap::{DUMMY_SP, Span, mk_sp};
use syntax::codemap::{COMMAND_LINE_SP, Span, mk_sp};
use syntax::parse;
use syntax::parse::token::InternedString;
use syntax::parse::token;
Expand Down Expand Up @@ -456,7 +456,7 @@ impl<'a> CrateReader<'a> {
ident: s.to_string(),
id: ast::DUMMY_NODE_ID,
should_link: true,
}, DUMMY_SP)
}, COMMAND_LINE_SP)
}
};
let target_triple = &self.sess.opts.target_triple[];
Expand Down
7 changes: 7 additions & 0 deletions src/libsyntax/codemap.rs
Expand Up @@ -105,6 +105,11 @@ pub struct Span {

pub const DUMMY_SP: Span = Span { lo: BytePos(0), hi: BytePos(0), expn_id: NO_EXPANSION };

// Generic span to be used for code originating from the command line
pub const COMMAND_LINE_SP: Span = Span { lo: BytePos(0),
hi: BytePos(0),
expn_id: COMMAND_LINE_EXPN };

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)]
pub struct Spanned<T> {
pub node: T,
Expand Down Expand Up @@ -235,6 +240,8 @@ pub struct ExpnInfo {
pub struct ExpnId(u32);

pub const NO_EXPANSION: ExpnId = ExpnId(-1);
// For code appearing from the command line
pub const COMMAND_LINE_EXPN: ExpnId = ExpnId(-2);

impl ExpnId {
pub fn from_llvm_cookie(cookie: c_uint) -> ExpnId {
Expand Down
20 changes: 14 additions & 6 deletions src/libsyntax/diagnostic.rs
Expand Up @@ -13,7 +13,7 @@ pub use self::RenderSpan::*;
pub use self::ColorConfig::*;
use self::Destination::*;

use codemap::{Pos, Span};
use codemap::{COMMAND_LINE_SP, Pos, Span};
use codemap;
use diagnostics;

Expand Down Expand Up @@ -368,6 +368,9 @@ impl Emitter for EmitterWriter {
cmsp: Option<(&codemap::CodeMap, Span)>,
msg: &str, code: Option<&str>, lvl: Level) {
let error = match cmsp {
Some((cm, COMMAND_LINE_SP)) => emit(self, cm,
FileLine(COMMAND_LINE_SP),
msg, code, lvl, false),
Some((cm, sp)) => emit(self, cm, FullSpan(sp), msg, code, lvl, false),
None => print_diagnostic(self, "", lvl, msg, code),
};
Expand All @@ -390,8 +393,11 @@ impl Emitter for EmitterWriter {
fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, rsp: RenderSpan,
msg: &str, code: Option<&str>, lvl: Level, custom: bool) -> io::IoResult<()> {
let sp = rsp.span();
let ss = cm.span_to_string(sp);
let lines = cm.span_to_lines(sp);
let ss = if sp == COMMAND_LINE_SP {
"<command line option>".to_string()
} else {
cm.span_to_string(sp)
};
if custom {
// we want to tell compiletest/runtest to look at the last line of the
// span (since `custom_highlight_lines` displays an arrow to the end of
Expand All @@ -400,15 +406,17 @@ fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, rsp: RenderSpan,
let ses = cm.span_to_string(span_end);
try!(print_diagnostic(dst, &ses[], lvl, msg, code));
if rsp.is_full_span() {
try!(custom_highlight_lines(dst, cm, sp, lvl, lines));
try!(custom_highlight_lines(dst, cm, sp, lvl, cm.span_to_lines(sp)));
}
} else {
try!(print_diagnostic(dst, &ss[], lvl, msg, code));
if rsp.is_full_span() {
try!(highlight_lines(dst, cm, sp, lvl, lines));
try!(highlight_lines(dst, cm, sp, lvl, cm.span_to_lines(sp)));
}
}
try!(print_macro_backtrace(dst, cm, sp));
if sp != COMMAND_LINE_SP {
try!(print_macro_backtrace(dst, cm, sp));
}
match code {
Some(code) =>
match dst.registry.as_ref().and_then(|registry| registry.find_description(code)) {
Expand Down

0 comments on commit 0bd022c

Please sign in to comment.