public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] macros: save included filename into session manager
@ 2022-06-08 12:32 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:32 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:f876cba554fcc2d4c71f7ccec55555894a005ab2

commit f876cba554fcc2d4c71f7ccec55555894a005ab2
Author: liushuyu <liushuyu011@gmail.com>
Date:   Mon Apr 18 03:36:02 2022 -0600

    macros: save included filename into session manager

Diff:
---
 gcc/rust/expand/rust-macro-builtins.cc | 14 ++++++++------
 gcc/rust/rust-session-manager.h        | 13 +++++++++++++
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc
index e4beb953b5f..fb302416fff 100644
--- a/gcc/rust/expand/rust-macro-builtins.cc
+++ b/gcc/rust/expand/rust-macro-builtins.cc
@@ -425,22 +425,24 @@ MacroBuiltin::include (Location invoc_locus, AST::MacroInvocData &invoc)
   if (lit_expr == nullptr)
     return AST::ASTFragment::create_error ();
 
-  std::string target_filename
+  std::string filename
     = source_relative_path (lit_expr->as_string (), invoc_locus);
+  auto target_filename
+    = Rust::Session::get_instance ().include_extra_file (std::move (filename));
 
-  RAIIFile target_file (target_filename.c_str ());
+  RAIIFile target_file (target_filename);
   Linemap *linemap = Session::get_instance ().linemap;
 
   if (target_file.get_raw () == nullptr)
     {
-      rust_error_at (lit_expr->get_locus (), "cannot open included file %s: %m",
-		     target_filename.c_str ());
+      rust_error_at (lit_expr->get_locus (),
+		     "cannot open included file %qs: %m", target_filename);
       return AST::ASTFragment::create_error ();
     }
 
-  rust_debug ("Attempting to parse included file %s", target_filename.c_str ());
+  rust_debug ("Attempting to parse included file %s", target_filename);
 
-  Lexer lex (target_filename.c_str (), std::move (target_file), linemap);
+  Lexer lex (target_filename, std::move (target_file), linemap);
   Parser<Lexer> parser (std::move (lex));
 
   auto parsed_items = parser.parse_items ();
diff --git a/gcc/rust/rust-session-manager.h b/gcc/rust/rust-session-manager.h
index 6fa83d97ca6..0c15711ab0e 100644
--- a/gcc/rust/rust-session-manager.h
+++ b/gcc/rust/rust-session-manager.h
@@ -235,6 +235,10 @@ struct Session
    * every file so eh. */
   std::string injected_crate_name;
 
+  /* extra files get included during late stages of compilation (e.g. macro
+   * expansion) */
+  std::vector<std::string> extra_files;
+
   // backend wrapper to GCC GENERIC
   Backend *backend;
 
@@ -267,6 +271,15 @@ public:
   void parse_files (int num_files, const char **files);
   void init_options ();
 
+  /* This function saves the filename data into the session manager using the
+   * `move` semantics, and returns a C-style string referencing the input
+   * std::string */
+  inline const char *include_extra_file (std::string filename)
+  {
+    extra_files.push_back (std::move (filename));
+    return extra_files.back ().c_str ();
+  }
+
 private:
   Session () = default;
   void parse_file (const char *filename);


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:32 [gcc/devel/rust/master] macros: save included filename into session manager Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).