summaryrefslogtreecommitdiffstats
path: root/src/wrapper.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/wrapper.rs')
-rw-r--r--src/wrapper.rs75
1 files changed, 69 insertions, 6 deletions
diff --git a/src/wrapper.rs b/src/wrapper.rs
index f5eb826..87e348d 100644
--- a/src/wrapper.rs
+++ b/src/wrapper.rs
@@ -3,8 +3,11 @@ use crate::detection::inside_proc_macro;
use crate::location::LineColumn;
use crate::{fallback, Delimiter, Punct, Spacing, TokenTree};
use core::fmt::{self, Debug, Display};
+#[cfg(span_locations)]
+use core::ops::Range;
use core::ops::RangeBounds;
use core::str::FromStr;
+use std::ffi::CStr;
use std::panic;
#[cfg(super_unstable)]
use std::path::PathBuf;
@@ -461,6 +464,17 @@ impl Span {
}
#[cfg(span_locations)]
+ pub fn byte_range(&self) -> Range<usize> {
+ match self {
+ #[cfg(proc_macro_span)]
+ Span::Compiler(s) => s.byte_range(),
+ #[cfg(not(proc_macro_span))]
+ Span::Compiler(_) => 0..0,
+ Span::Fallback(s) => s.byte_range(),
+ }
+ }
+
+ #[cfg(span_locations)]
pub fn start(&self) -> LineColumn {
match self {
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
@@ -833,19 +847,38 @@ impl Literal {
}
}
- pub fn string(t: &str) -> Literal {
+ pub fn string(string: &str) -> Literal {
if inside_proc_macro() {
- Literal::Compiler(proc_macro::Literal::string(t))
+ Literal::Compiler(proc_macro::Literal::string(string))
} else {
- Literal::Fallback(fallback::Literal::string(t))
+ Literal::Fallback(fallback::Literal::string(string))
}
}
- pub fn character(t: char) -> Literal {
+ pub fn character(ch: char) -> Literal {
if inside_proc_macro() {
- Literal::Compiler(proc_macro::Literal::character(t))
+ Literal::Compiler(proc_macro::Literal::character(ch))
+ } else {
+ Literal::Fallback(fallback::Literal::character(ch))
+ }
+ }
+
+ pub fn byte_character(byte: u8) -> Literal {
+ if inside_proc_macro() {
+ Literal::Compiler({
+ #[cfg(not(no_literal_byte_character))]
+ {
+ proc_macro::Literal::byte_character(byte)
+ }
+
+ #[cfg(no_literal_byte_character)]
+ {
+ let fallback = fallback::Literal::byte_character(byte);
+ fallback.repr.parse::<proc_macro::Literal>().unwrap()
+ }
+ })
} else {
- Literal::Fallback(fallback::Literal::character(t))
+ Literal::Fallback(fallback::Literal::byte_character(byte))
}
}
@@ -857,6 +890,25 @@ impl Literal {
}
}
+ pub fn c_string(string: &CStr) -> Literal {
+ if inside_proc_macro() {
+ Literal::Compiler({
+ #[cfg(not(no_literal_c_string))]
+ {
+ proc_macro::Literal::c_string(string)
+ }
+
+ #[cfg(no_literal_c_string)]
+ {
+ let fallback = fallback::Literal::c_string(string);
+ fallback.repr.parse::<proc_macro::Literal>().unwrap()
+ }
+ })
+ } else {
+ Literal::Fallback(fallback::Literal::c_string(string))
+ }
+ }
+
pub fn span(&self) -> Span {
match self {
Literal::Compiler(lit) => Span::Compiler(lit.span()),
@@ -928,3 +980,14 @@ impl Debug for Literal {
}
}
}
+
+#[cfg(span_locations)]
+pub(crate) fn invalidate_current_thread_spans() {
+ if inside_proc_macro() {
+ panic!(
+ "proc_macro2::extra::invalidate_current_thread_spans is not available in procedural macros"
+ );
+ } else {
+ crate::fallback::invalidate_current_thread_spans();
+ }
+}