Skip to content

Commit f876cba

Browse files
committed
macros: save included filename into session manager
1 parent e753fa4 commit f876cba

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,22 +425,24 @@ MacroBuiltin::include (Location invoc_locus, AST::MacroInvocData &invoc)
425425
if (lit_expr == nullptr)
426426
return AST::ASTFragment::create_error ();
427427

428-
std::string target_filename
428+
std::string filename
429429
= source_relative_path (lit_expr->as_string (), invoc_locus);
430+
auto target_filename
431+
= Rust::Session::get_instance ().include_extra_file (std::move (filename));
430432

431-
RAIIFile target_file (target_filename.c_str ());
433+
RAIIFile target_file (target_filename);
432434
Linemap *linemap = Session::get_instance ().linemap;
433435

434436
if (target_file.get_raw () == nullptr)
435437
{
436-
rust_error_at (lit_expr->get_locus (), "cannot open included file %s: %m",
437-
target_filename.c_str ());
438+
rust_error_at (lit_expr->get_locus (),
439+
"cannot open included file %qs: %m", target_filename);
438440
return AST::ASTFragment::create_error ();
439441
}
440442

441-
rust_debug ("Attempting to parse included file %s", target_filename.c_str ());
443+
rust_debug ("Attempting to parse included file %s", target_filename);
442444

443-
Lexer lex (target_filename.c_str (), std::move (target_file), linemap);
445+
Lexer lex (target_filename, std::move (target_file), linemap);
444446
Parser<Lexer> parser (std::move (lex));
445447

446448
auto parsed_items = parser.parse_items ();

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);

0 commit comments

Comments
 (0)