Skip to content

Commit a082f77

Browse files
committed
macros: Add env! macro
Signed-off-by: Ondřej Machota <[email protected]> macros: Add env! macro and fix format Signed-off-by: Ondřej Machota <[email protected]>
1 parent e9ab95c commit a082f77

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

gcc/rust/expand/rust-macro-builtins.cc

+67
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,71 @@ MacroBuiltin::concat (Location invoc_locus, AST::MacroInvocData &invoc)
306306
return AST::ASTFragment ({node});
307307
}
308308

309+
/* Expand builtin macro env!(), which inspects an environment variable at
310+
compile time. */
311+
312+
AST::ASTFragment
313+
MacroBuiltin::env (Location invoc_locus, AST::MacroInvocData &invoc)
314+
{
315+
auto invoc_token_tree = invoc.get_delim_tok_tree ();
316+
MacroInvocLexer lex (invoc_token_tree.to_token_stream ());
317+
Parser<MacroInvocLexer> parser (std::move (lex));
318+
319+
auto last_token_id = macro_end_token (invoc_token_tree, parser);
320+
321+
if (parser.peek_current_token ()->get_id () != STRING_LITERAL)
322+
{
323+
if (parser.peek_current_token ()->get_id () == last_token_id)
324+
rust_error_at (invoc_locus, "env! takes 1 or 2 arguments");
325+
else
326+
rust_error_at (parser.peek_current_token ()->get_locus (),
327+
"argument must be a string literal");
328+
return AST::ASTFragment::create_error ();
329+
}
330+
331+
auto lit_expr = parser.parse_literal_expr ();
332+
333+
parser.maybe_skip_token (COMMA);
334+
335+
std::unique_ptr<AST::LiteralExpr> error_expr = nullptr;
336+
337+
if (parser.peek_current_token ()->get_id () != last_token_id)
338+
{
339+
if (parser.peek_current_token ()->get_id () != STRING_LITERAL)
340+
{
341+
rust_error_at (parser.peek_current_token ()->get_locus (),
342+
"argument must be a string literal");
343+
return AST::ASTFragment::create_error ();
344+
}
345+
346+
error_expr = parser.parse_literal_expr ();
347+
parser.maybe_skip_token (COMMA);
348+
}
349+
350+
if (parser.peek_current_token ()->get_id () != last_token_id)
351+
{
352+
rust_error_at (invoc_locus, "env! takes 1 or 2 arguments");
353+
return AST::ASTFragment::create_error ();
354+
}
355+
356+
parser.skip_token (last_token_id);
357+
358+
auto env_value = getenv (lit_expr->as_string ().c_str ());
359+
360+
if (env_value == nullptr)
361+
{
362+
if (error_expr == nullptr)
363+
{
364+
rust_error_at (invoc_locus, "environment variable '%s' not defined",
365+
lit_expr->as_string ().c_str ());
366+
}
367+
else
368+
rust_error_at (invoc_locus, "%s", error_expr->as_string ().c_str ());
369+
return AST::ASTFragment::create_error ();
370+
}
371+
372+
auto node = AST::SingleASTNode (make_string (invoc_locus, env_value));
373+
return AST::ASTFragment ({node});
374+
}
375+
309376
} // namespace Rust

gcc/rust/expand/rust-macro-builtins.h

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class MacroBuiltin
8989

9090
static AST::ASTFragment concat (Location invoc_locus,
9191
AST::MacroInvocData &invoc);
92+
93+
static AST::ASTFragment env (Location invoc_locus,
94+
AST::MacroInvocData &invoc);
9295
};
9396
} // namespace Rust
9497

gcc/rust/util/rust-hir-map.cc

+1
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
755755
{"include_str", MacroBuiltin::include_str},
756756
{"compile_error", MacroBuiltin::compile_error},
757757
{"concat", MacroBuiltin::concat},
758+
{"env", MacroBuiltin::env},
758759
};
759760

760761
auto builtin = builtin_macros.find (macro->get_rule_name ());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
macro_rules! env {
2+
() => {{}};
3+
}
4+
5+
fn main () {
6+
let message = "error message";
7+
env! (message); // { dg-error "argument must be a string literal" "" }
8+
env! (); // { dg-error "env! takes 1 or 2 arguments" "" }
9+
env! (,); // { dg-error "argument must be a string literal" "" }
10+
env! (1); // { dg-error "argument must be a string literal" "" }
11+
env! ("NOT_DEFINED"); // { dg-error "environment variable 'NOT_DEFINED' not defined" "" }
12+
env! ("NOT_DEFINED",); // { dg-error "environment variable 'NOT_DEFINED' not defined" "" }
13+
env! ("NOT_DEFINED", 1); // { dg-error "argument must be a string literal" "" }
14+
env! ("NOT_DEFINED", "two", "three"); // { dg-error "env! takes 1 or 2 arguments" "" }
15+
env! ("NOT_DEFINED", "expected error message"); // { dg-error "expected error message" "" }
16+
env! ("NOT_DEFINED", "expected error message",); // { dg-error "expected error message" "" }
17+
env! (1, "two"); // { dg-error "argument must be a string literal" "" }
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// { dg-output "VALUE\n" }
2+
3+
macro_rules! env {
4+
() => {{}};
5+
}
6+
7+
extern "C" {
8+
fn printf(fmt: *const i8, ...);
9+
}
10+
11+
fn print(s: &str) {
12+
printf("%s\n" as *const str as *const i8, s as *const str as *const i8);
13+
}
14+
15+
fn main() -> i32 {
16+
let val = env!("ENV_MACRO_TEST");
17+
18+
print(val);
19+
20+
0
21+
}

gcc/testsuite/rust/execute/torture/execute.exp

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dg-init
2626
set saved-dg-do-what-default ${dg-do-what-default}
2727

2828
set dg-do-what-default "run"
29+
setenv ENV_MACRO_TEST VALUE
2930
gcc-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.rs]] "" ""
3031
set dg-do-what-default ${saved-dg-do-what-default}
3132

0 commit comments

Comments
 (0)