about summary refs log tree commit diff
path: root/src/libsyntax_pos
diff options
context:
space:
mode:
authorEduard-Mihai Burtescu <edy.burt@gmail.com>2019-10-30 18:54:40 +0200
committerEduard-Mihai Burtescu <edy.burt@gmail.com>2019-10-30 18:54:40 +0200
commitc17f89d6ede71cb0f8f0286307b1b83a642aff91 (patch)
tree66666d0fd2591bf4b71db78b53356f14f3ac13ec /src/libsyntax_pos
parent0b7e28a1610924a27471ffdb59a2885709b3b415 (diff)
downloadrust-c17f89d6ede71cb0f8f0286307b1b83a642aff91.tar.gz
rust-c17f89d6ede71cb0f8f0286307b1b83a642aff91.zip
caller_location: point to macro invocation sites, like file!/line!.
Diffstat (limited to 'src/libsyntax_pos')
-rw-r--r--src/libsyntax_pos/hygiene.rs19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/libsyntax_pos/hygiene.rs b/src/libsyntax_pos/hygiene.rs
index e28d9326757..2a48f8e44aa 100644
--- a/src/libsyntax_pos/hygiene.rs
+++ b/src/libsyntax_pos/hygiene.rs
@@ -28,7 +28,7 @@
 use crate::GLOBALS;
 use crate::{Span, DUMMY_SP};
 use crate::edition::Edition;
-use crate::symbol::{kw, Symbol};
+use crate::symbol::{kw, sym, Symbol};
 
 use rustc_serialize::{Encodable, Decodable, Encoder, Decoder};
 use rustc_data_structures::fx::FxHashMap;
@@ -119,6 +119,23 @@ impl ExpnId {
     pub fn outer_expn_is_descendant_of(self, ctxt: SyntaxContext) -> bool {
         HygieneData::with(|data| data.is_descendant_of(self, data.outer_expn(ctxt)))
     }
+
+    /// Returns span for the macro which originally caused this expansion to happen.
+    ///
+    /// Stops backtracing at include! boundary.
+    pub fn expansion_cause(mut self) -> Option<Span> {
+        let mut last_macro = None;
+        loop {
+            let expn_data = self.expn_data();
+            // Stop going up the backtrace once include! is encountered
+            if expn_data.is_root() || expn_data.kind.descr() == sym::include {
+                break;
+            }
+            self = expn_data.call_site.ctxt().outer_expn();
+            last_macro = Some(expn_data.call_site);
+        }
+        last_macro
+    }
 }
 
 #[derive(Debug)]