Add the stringify macro expansion as well as some tests.
gcc/rust/ChangeLog:
* ast/rust-macro.cc (builtin_macro_from_string): Add identifier
identification.
* ast/rust-macro.h (enum class): Add Stringify builtin macro
type.
* expand/rust-macro-builtins.cc (make_macro_path_str): Add path
for builtin stringify macro.
(MacroBuiltin::stringify_handler): Add handler for builtin
stringify macro.
* expand/rust-macro-builtins.h: Add stringify handler's
prototype.
* util/rust-hir-map.cc (Mappings::insert_macro_def): Add
stringify handler to builtin hir map.
gcc/testsuite/ChangeLog:
* rust/compile/stringify.rs: Add a basic test with some text.
* rust/execute/torture/builtin_macro_stringify.rs: Verify the
text is left as is without any other macro expansion.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
if (identifier == "include_str")
return BuiltinMacro::IncludeStr;
+ if (identifier == "stringify")
+ return BuiltinMacro::Stringify;
+
if (identifier == "compile_error")
return BuiltinMacro::CompileError;
Column,
IncludeBytes,
IncludeStr,
+ Stringify,
CompileError,
Concat,
Env,
case AST::BuiltinMacro::IncludeStr:
path_str = "include_str";
break;
+ case AST::BuiltinMacro::Stringify:
+ path_str = "stringify";
+ break;
case AST::BuiltinMacro::CompileError:
path_str = "compile_error";
break;
return AST::Fragment ({line_no}, std::move (tok));
}
+AST::Fragment
+MacroBuiltin::stringify_handler (Location invoc_locus,
+ AST::MacroInvocData &invoc)
+{
+ std::string content;
+ auto invoc_token_tree = invoc.get_delim_tok_tree ();
+ auto tokens = invoc_token_tree.to_token_stream ();
+
+ // Tokens stream includes the first and last delimiter
+ // which we need to skip.
+ for (auto token = tokens.cbegin () + 1; token < tokens.cend () - 1; token++)
+ {
+ // Rust stringify format has no garantees but the reference compiler
+ // removes spaces before some tokens depending on the lexer's behavior,
+ // let's mimick some of those behaviors.
+ auto token_id = (*token)->get_id ();
+ if (token_id != RIGHT_PAREN && token_id != EXCLAM
+ && token != tokens.cbegin () + 1)
+ {
+ content.push_back (' ');
+ }
+ content += (*token)->as_string ();
+ }
+
+ auto node = AST::SingleASTNode (make_string (invoc_locus, content));
+ auto token
+ = make_token (Token::make_string (invoc_locus, std::move (content)));
+ return AST::Fragment ({node}, std::move (token));
+} // namespace Rust
+
} // namespace Rust
static AST::Fragment include_str_handler (Location invoc_locus,
AST::MacroInvocData &invoc);
+ static AST::Fragment stringify_handler (Location invoc_locus,
+ AST::MacroInvocData &invoc);
+
static AST::Fragment compile_error_handler (Location invoc_locus,
AST::MacroInvocData &invoc);
{"column", MacroBuiltin::column_handler},
{"include_bytes", MacroBuiltin::include_bytes_handler},
{"include_str", MacroBuiltin::include_str_handler},
+ {"stringify", MacroBuiltin::stringify_handler},
{"compile_error", MacroBuiltin::compile_error_handler},
{"concat", MacroBuiltin::concat_handler},
{"env", MacroBuiltin::env_handler},
--- /dev/null
+#![feature(rustc_attrs)]
+
+#[rustc_builtin_macro]
+macro_rules! stringify {
+ () => {};
+}
+
+fn main() {
+ let _a = stringify!(sample text with parenthesis () and things! This will become a "string".);
+}
--- /dev/null
+// { dg-output "a! ()" }
+#![feature(rustc_attrs)]
+
+#[rustc_builtin_macro]
+macro_rules! stringify {
+ () => {};
+}
+
+macro_rules! a {
+ () => {
+ " foo"
+ };
+}
+
+extern "C" {
+ fn printf(fmt: *const i8, ...);
+}
+
+fn print(s: &str) {
+ unsafe {
+ printf(
+ "%s" as *const str as *const i8,
+ s as *const str as *const i8,
+ );
+ }
+}
+
+fn main() -> i32 {
+ let a = stringify!(a!());
+
+ print(a);
+
+ 0
+}