Skip to content

Commit 321be1b

Browse files
bors[bot]liushuyu
andauthored
Merge #1096
1096: macros: add include! macro r=CohenArthur a=liushuyu - add `include!` macro Co-authored-by: liushuyu <[email protected]>
2 parents 27ad381 + f876cba commit 321be1b

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,60 @@ 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 filename
429+
= source_relative_path (lit_expr->as_string (), invoc_locus);
430+
auto target_filename
431+
= Rust::Session::get_instance ().include_extra_file (std::move (filename));
432+
433+
RAIIFile target_file (target_filename);
434+
Linemap *linemap = Session::get_instance ().linemap;
435+
436+
if (target_file.get_raw () == nullptr)
437+
{
438+
rust_error_at (lit_expr->get_locus (),
439+
"cannot open included file %qs: %m", target_filename);
440+
return AST::ASTFragment::create_error ();
441+
}
442+
443+
rust_debug ("Attempting to parse included file %s", target_filename);
444+
445+
Lexer lex (target_filename, std::move (target_file), linemap);
446+
Parser<Lexer> parser (std::move (lex));
447+
448+
auto parsed_items = parser.parse_items ();
449+
bool has_error = !parser.get_errors ().empty ();
450+
451+
for (const auto &error : parser.get_errors ())
452+
error.emit_error ();
453+
454+
if (has_error)
455+
{
456+
// inform the user that the errors above are from a included file
457+
rust_inform (invoc_locus, "included from here");
458+
return AST::ASTFragment::create_error ();
459+
}
460+
461+
std::vector<AST::SingleASTNode> nodes{};
462+
for (auto &item : parsed_items)
463+
{
464+
AST::SingleASTNode node (std::move (item));
465+
nodes.push_back (node);
466+
}
467+
468+
return AST::ASTFragment (nodes);
469+
}
470+
415471
} // 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/rust-session-manager.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ struct Session
235235
* every file so eh. */
236236
std::string injected_crate_name;
237237

238+
/* extra files get included during late stages of compilation (e.g. macro
239+
* expansion) */
240+
std::vector<std::string> extra_files;
241+
238242
// backend wrapper to GCC GENERIC
239243
Backend *backend;
240244

@@ -267,6 +271,15 @@ struct Session
267271
void parse_files (int num_files, const char **files);
268272
void init_options ();
269273

274+
/* This function saves the filename data into the session manager using the
275+
* `move` semantics, and returns a C-style string referencing the input
276+
* std::string */
277+
inline const char *include_extra_file (std::string filename)
278+
{
279+
extra_files.push_back (std::move (filename));
280+
return extra_files.back ().c_str ();
281+
}
282+
270283
private:
271284
Session () = default;
272285
void parse_file (const char *filename);

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)