Skip to content

Commit 95d110c

Browse files
committed
proc_macro: Add Literal::c_string constructor
1 parent aa7e9f2 commit 95d110c

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

library/proc_macro/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ mod diagnostic;
4545
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
4646
pub use diagnostic::{Diagnostic, Level, MultiSpan};
4747

48+
use std::ffi::CStr;
4849
use std::ops::{Range, RangeBounds};
4950
use std::path::PathBuf;
5051
use std::str::FromStr;
@@ -1351,6 +1352,15 @@ impl Literal {
13511352
Literal::new(bridge::LitKind::ByteStr, &string, None)
13521353
}
13531354

1355+
/// C string literal.
1356+
#[stable(feature = "c_str_literals", since = "1.76.0")]
1357+
pub fn c_string(string: &CStr) -> Literal {
1358+
let quoted = format!("{:?}", string);
1359+
assert!(quoted.starts_with('"') && quoted.ends_with('"'));
1360+
let symbol = &quoted[1..quoted.len() - 1];
1361+
Literal::new(bridge::LitKind::CStr, symbol, None)
1362+
}
1363+
13541364
/// Returns the span encompassing this literal.
13551365
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
13561366
pub fn span(&self) -> Span {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// force-host
2+
#![crate_type = "proc-macro"]
3+
4+
extern crate proc_macro;
5+
6+
use proc_macro::Literal;
7+
8+
fn test() {
9+
Literal::c_string(c"a"); //~ ERROR use of unstable library feature 'c_str_literals'
10+
}

tests/ui/proc-macro/auxiliary/api/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![crate_name = "proc_macro_api_tests"]
66
#![feature(proc_macro_span)]
77
#![feature(proc_macro_byte_character)]
8+
#![feature(c_str_literals)]
89
#![deny(dead_code)] // catch if a test function is never called
910

1011
extern crate proc_macro;

tests/ui/proc-macro/auxiliary/api/parse.rs

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ fn test_display_literal() {
2323
Literal::string("a \t ❤ ' \" \u{1}").to_string(),
2424
"\"a \\t ❤ ' \\\" \\u{1}\"",
2525
);
26+
assert_eq!(
27+
Literal::c_string(c"a \t ❤ ' \" \u{1}").to_string(),
28+
"\"a \\t ❤ ' \\\" \\u{1}\"",
29+
);
2630
assert_eq!(Literal::character('a').to_string(), "'a'");
2731
assert_eq!(Literal::character('\t').to_string(), "'\\t'");
2832
assert_eq!(Literal::character('❤').to_string(), "'❤'");
@@ -41,6 +45,7 @@ fn test_parse_literal() {
4145
assert_eq!("b'a'".parse::<Literal>().unwrap().to_string(), "b'a'");
4246
assert_eq!("\"\n\"".parse::<Literal>().unwrap().to_string(), "\"\n\"");
4347
assert_eq!("b\"\"".parse::<Literal>().unwrap().to_string(), "b\"\"");
48+
assert_eq!("c\"\"".parse::<Literal>().unwrap().to_string(), "c\"\"");
4449
assert_eq!("r##\"\"##".parse::<Literal>().unwrap().to_string(), "r##\"\"##");
4550
assert_eq!("10ulong".parse::<Literal>().unwrap().to_string(), "10ulong");
4651
assert_eq!("-10ulong".parse::<Literal>().unwrap().to_string(), "-10ulong");

0 commit comments

Comments
 (0)