mirror of
https://github.com/swc-project/swc.git
synced 2024-12-28 08:04:43 +03:00
Emit location while serializing
This commit is contained in:
parent
fbbafc5712
commit
d3ff16d496
@ -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,
|
||||
};
|
||||
|
||||
///
|
||||
|
@ -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<Globals> = ::scoped_tls::ScopedKey {
|
||||
_marker: ::std::marker::PhantomData,
|
||||
};
|
||||
|
||||
/// SourceMap used while serializing Span.
|
||||
pub static CM: ::scoped_tls::ScopedKey<SourceMap> = ::scoped_tls::ScopedKey {
|
||||
inner: {
|
||||
thread_local!(static FOO: ::std::cell::Cell<usize> = {
|
||||
::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 {
|
||||
|
@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user