Skip to content

Commit e753fa4

Browse files
committed
macros: add include! macro
1 parent 27ad381 commit e753fa4

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,58 @@ MacroBuiltin::cfg (Location invoc_locus, AST::MacroInvocData &invoc)
412412
return AST::ASTFragment ({literal_exp});
413413
}
414414

415+
/* Expand builtin macro include!(), which includes a source file at the current
416+
scope compile time. */
417+
418+
AST::ASTFragment
419+
MacroBuiltin::include (Location invoc_locus, AST::MacroInvocData &invoc)
420+
{
421+
/* Get target filename from the macro invocation, which is treated as a path
422+
relative to the include!-ing file (currently being compiled). */
423+
auto lit_expr
424+
= parse_single_string_literal (invoc.get_delim_tok_tree (), invoc_locus);
425+
if (lit_expr == nullptr)
426+
return AST::ASTFragment::create_error ();
427+
428+
std::string target_filename
429+
= source_relative_path (lit_expr->as_string (), invoc_locus);
430+
431+
RAIIFile target_file (target_filename.c_str ());
432+
Linemap *linemap = Session::get_instance ().linemap;
433+
434+
if (target_file.get_raw () == nullptr)
435+
{
436+
rust_error_at (lit_expr->get_locus (), "cannot open included file %s: %m",
437+
target_filename.c_str ());
438+
return AST::ASTFragment::create_error ();
439+
}
440+
441+
rust_debug ("Attempting to parse included file %s", target_filename.c_str ());
442+
443+
Lexer lex (target_filename.c_str (), std::move (target_file), linemap);
444+
Parser<Lexer> parser (std::move (lex));
445+
446+
auto parsed_items = parser.parse_items ();
447+
bool has_error = !parser.get_errors ().empty ();
448+
449+
for (const auto &error : parser.get_errors ())
450+
error.emit_error ();
451+
452+
if (has_error)
453+
{
454+
// inform the user that the errors above are from a included file
455+
rust_inform (invoc_locus, "included from here");
456+
return AST::ASTFragment::create_error ();
457+
}
458+
459+
std::vector<AST::SingleASTNode> nodes{};
460+
for (auto &item : parsed_items)
461+
{
462+
AST::SingleASTNode node (std::move (item));
463+
nodes.push_back (node);
464+
}
465+
466+
return AST::ASTFragment (nodes);
467+
}
468+
415469
} // namespace Rust

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ class MacroBuiltin
9595

9696
static AST::ASTFragment cfg (Location invoc_locus,
9797
AST::MacroInvocData &invoc);
98+
99+
static AST::ASTFragment include (Location invoc_locus,
100+
AST::MacroInvocData &invoc);
98101
};
99102
} // namespace Rust
100103

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
757757
{"concat", MacroBuiltin::concat},
758758
{"env", MacroBuiltin::env},
759759
{"cfg", MacroBuiltin::cfg},
760+
{"include", MacroBuiltin::include},
760761
};
761762

762763
auto builtin = builtin_macros.find (macro->get_rule_name ());

0 commit comments

Comments
 (0)