diff options
| author | Jonas Schievink <jonasschievink@gmail.com> | 2019-06-22 21:51:51 +0200 |
|---|---|---|
| committer | Jonas Schievink <jonasschievink@gmail.com> | 2019-07-23 17:17:31 +0200 |
| commit | edb21873cced7a179571aa3a9c25eb1cfc05c2db (patch) | |
| tree | f40ace2b908649c585a7bfc5740a63048c58be93 /src/libsyntax/ext/base.rs | |
| parent | 8ccf52c1c6cffde2905d7ad0ebfa36a9e8a0c319 (diff) | |
| download | rust-edb21873cced7a179571aa3a9c25eb1cfc05c2db.tar.gz rust-edb21873cced7a179571aa3a9c25eb1cfc05c2db.zip | |
Make path::resolve a method on ExtCtxt
Diffstat (limited to 'src/libsyntax/ext/base.rs')
| -rw-r--r-- | src/libsyntax/ext/base.rs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 926c9e88efe..11b7a984aaa 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -1,6 +1,6 @@ use crate::ast::{self, Attribute, Name, PatKind}; use crate::attr::{HasAttrs, Stability, Deprecation}; -use crate::source_map::{SourceMap, Spanned, respan}; +use crate::source_map::{SourceMap, Spanned, FileName, respan}; use crate::edition::Edition; use crate::ext::expand::{self, AstFragment, Invocation}; use crate::ext::hygiene::{ExpnId, SyntaxContext, Transparency}; @@ -889,6 +889,31 @@ impl<'a> ExtCtxt<'a> { pub fn check_unused_macros(&self) { self.resolver.check_unused_macros(); } + + /// Resolve a path mentioned inside Rust code. + /// + /// This unifies the logic used for resolving `include_X!`, and `#[doc(include)]` file paths. + /// + /// Returns an absolute path to the file that `path` refers to. + pub fn resolve_path(&self, path: impl Into<PathBuf>, span: Span) -> PathBuf { + let path = path.into(); + + // Relative paths are resolved relative to the file in which they are found + // after macro expansion (that is, they are unhygienic). + if !path.is_absolute() { + let callsite = span.source_callsite(); + let mut result = match self.source_map().span_to_unmapped_path(callsite) { + FileName::Real(path) => path, + FileName::DocTest(path, _) => path, + other => panic!("cannot resolve relative path in non-file source `{}`", other), + }; + result.pop(); + result.push(path); + result + } else { + path + } + } } /// Extracts a string literal from the macro expanded version of `expr`, |
