Skip to content

Commit f01b85b

Browse files
committed
Auto merge of #31382 - DanielJCampbell:SaveSpans, r=nrc
r? @nrc
2 parents e64ca8c + b361b7f commit f01b85b

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

src/librustc_trans/save/dump_csv.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -847,13 +847,17 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
847847
if !self.mac_defs.contains(&data.callee_span)
848848
&& !data.imported {
849849
self.mac_defs.insert(data.callee_span);
850-
self.fmt.macro_str(data.callee_span, data.callee_span,
851-
data.name.clone(), qualname.clone());
850+
if let Some(sub_span) = self.span.span_for_macro_def_name(data.callee_span) {
851+
self.fmt.macro_str(data.callee_span, sub_span,
852+
data.name.clone(), qualname.clone());
853+
}
852854
}
853855
if !self.mac_uses.contains(&data.span) {
854-
self.mac_uses.insert(data.span);
855-
self.fmt.macro_use_str(data.span, data.span, data.name,
856-
qualname, data.scope);
856+
self.mac_uses.insert(data.span);
857+
if let Some(sub_span) = self.span.span_for_macro_use_name(data.span) {
858+
self.fmt.macro_use_str(data.span, sub_span, data.name,
859+
qualname, data.scope);
860+
}
857861
}
858862
}
859863
}

src/librustc_trans/save/span_utils.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ impl<'a> SpanUtils<'a> {
378378
}
379379
}
380380

381-
// Given a macro_rules definition span, return the span of the macro's name.
382-
pub fn span_for_macro_name(&self, span: Span) -> Option<Span> {
381+
// Return the name for a macro definition (identifier after first `!`)
382+
pub fn span_for_macro_def_name(&self, span: Span) -> Option<Span> {
383383
let mut toks = self.retokenise_span(span);
384384
loop {
385385
let ts = toks.real_token();
@@ -397,6 +397,26 @@ impl<'a> SpanUtils<'a> {
397397
}
398398
}
399399

400+
// Return the name for a macro use (identifier before first `!`).
401+
pub fn span_for_macro_use_name(&self, span:Span) -> Option<Span> {
402+
let mut toks = self.retokenise_span(span);
403+
let mut prev = toks.real_token();
404+
loop {
405+
if prev.tok == token::Eof {
406+
return None;
407+
}
408+
let ts = toks.real_token();
409+
if ts.tok == token::Not {
410+
if prev.tok.is_ident() {
411+
return self.make_sub_span(span, Some(prev.sp));
412+
} else {
413+
return None;
414+
}
415+
}
416+
prev = ts;
417+
}
418+
}
419+
400420
/// Return true if the span is generated code, and
401421
/// it is not a subspan of the root callsite.
402422
///

src/libsyntax/codemap.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -1052,9 +1052,18 @@ impl CodeMap {
10521052
/// the macro callsite that expanded to it.
10531053
pub fn source_callsite(&self, sp: Span) -> Span {
10541054
let mut span = sp;
1055+
// Special case - if a macro is parsed as an argument to another macro, the source
1056+
// callsite is the first callsite, which is also source-equivalent to the span.
1057+
let mut first = true;
10551058
while span.expn_id != NO_EXPANSION && span.expn_id != COMMAND_LINE_EXPN {
10561059
if let Some(callsite) = self.with_expn_info(span.expn_id,
10571060
|ei| ei.map(|ei| ei.call_site.clone())) {
1061+
if first && span.source_equal(&callsite) {
1062+
if self.lookup_char_pos(span.lo).file.is_real_file() {
1063+
return Span { expn_id: NO_EXPANSION, .. span };
1064+
}
1065+
}
1066+
first = false;
10581067
span = callsite;
10591068
}
10601069
else {
@@ -1071,10 +1080,20 @@ impl CodeMap {
10711080
/// corresponding to the source callsite.
10721081
pub fn source_callee(&self, sp: Span) -> Option<NameAndSpan> {
10731082
let mut span = sp;
1083+
// Special case - if a macro is parsed as an argument to another macro, the source
1084+
// callsite is source-equivalent to the span, and the source callee is the first callee.
1085+
let mut first = true;
10741086
while let Some(callsite) = self.with_expn_info(span.expn_id,
10751087
|ei| ei.map(|ei| ei.call_site.clone())) {
1088+
if first && span.source_equal(&callsite) {
1089+
if self.lookup_char_pos(span.lo).file.is_real_file() {
1090+
return self.with_expn_info(span.expn_id,
1091+
|ei| ei.map(|ei| ei.callee.clone()));
1092+
}
1093+
}
1094+
first = false;
10761095
if let Some(_) = self.with_expn_info(callsite.expn_id,
1077-
|ei| ei.map(|ei| ei.call_site.clone())) {
1096+
|ei| ei.map(|ei| ei.call_site.clone())) {
10781097
span = callsite;
10791098
}
10801099
else {

0 commit comments

Comments
 (0)