From d3ff16d4962f0cfa5bee197789cc7b57bc98ddb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 13 Apr 2019 20:55:38 +0900 Subject: [PATCH] Emit location while serializing --- common/src/pos.rs | 2 +- common/src/syntax_pos/mod.rs | 13 +++++++++- common/src/syntax_pos/span_encoding.rs | 35 ++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/common/src/pos.rs b/common/src/pos.rs index 64b21d85061..29ba036205e 100644 --- a/common/src/pos.rs +++ b/common/src/pos.rs @@ -3,7 +3,7 @@ use fold::{FoldWith, VisitWith}; pub use syntax_pos::{ hygiene, BytePos, CharPos, ExpnInfo, FileName, Globals, Loc, LocWithOpt, Mark, MultiSpan, SourceFile, SourceFileAndBytePos, SourceFileAndLine, Span, SpanData, SpanLinesError, - SyntaxContext, DUMMY_SP, GLOBALS, NO_EXPANSION, + SyntaxContext, CM, DUMMY_SP, GLOBALS, NO_EXPANSION, }; /// diff --git a/common/src/syntax_pos/mod.rs b/common/src/syntax_pos/mod.rs index d9ac1af2d65..b92624849a8 100644 --- a/common/src/syntax_pos/mod.rs +++ b/common/src/syntax_pos/mod.rs @@ -2,7 +2,7 @@ pub use self::{ hygiene::{ExpnInfo, Mark, SyntaxContext}, span_encoding::{Span, DUMMY_SP}, }; -use crate::sync::Lock; +use crate::{sync::Lock, SourceMap}; use rustc_data_structures::stable_hasher::StableHasher; use serde::{Deserialize, Serialize}; use std::{ @@ -45,6 +45,17 @@ pub static GLOBALS: ::scoped_tls::ScopedKey = ::scoped_tls::ScopedKey { _marker: ::std::marker::PhantomData, }; +/// SourceMap used while serializing Span. +pub static CM: ::scoped_tls::ScopedKey = ::scoped_tls::ScopedKey { + inner: { + thread_local!(static FOO: ::std::cell::Cell = { + ::std::cell::Cell::new(0) + }); + &FOO + }, + _marker: ::std::marker::PhantomData, +}; + /// Differentiates between real files and common virtual files. #[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)] pub enum FileName { diff --git a/common/src/syntax_pos/span_encoding.rs b/common/src/syntax_pos/span_encoding.rs index 64a4199394c..7974b209a01 100644 --- a/common/src/syntax_pos/span_encoding.rs +++ b/common/src/syntax_pos/span_encoding.rs @@ -14,6 +14,7 @@ // The encoding format for inline spans were obtained by optimizing over crates // in rustc/libstd. See https://internals.rust-lang.org/t/rfc-compiler-refactoring-spans/1357/28 use super::hygiene::SyntaxContext; +use crate::syntax_pos::CM; use fxhash::FxHashMap; use serde::{ de::Deserializer, @@ -34,6 +35,29 @@ use GLOBALS; #[repr(packed)] pub struct Span(u32); +#[derive(Serialize)] +struct Loc { + pub start: LineCol, + pub end: LineCol, +} + +#[derive(Serialize)] +struct LineCol { + pub line: usize, + pub column: usize, +} + +macro_rules! ser { + ($cm:expr, $bp:expr) => {{ + let loc = $cm.lookup_char_pos($bp); + + LineCol { + line: loc.line, + column: loc.col_display, + } + }}; +} + impl Serialize for Span { fn serialize(&self, serializer: S) -> Result where @@ -44,6 +68,17 @@ impl Serialize for Span { s.serialize_field("start", &data.lo.0)?; s.serialize_field("end", &data.hi.0)?; s.serialize_field("ctxt", &data.ctxt)?; + + CM.with(|cm| { + s.serialize_field( + "loc", + &Loc { + start: ser!(cm, data.lo), + end: ser!(cm, data.hi), + }, + ) + })?; + s.end() } }