aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcc/rust/ast/rust-ast-full-test.cc30
-rw-r--r--gcc/rust/ast/rust-item.h9
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc11
3 files changed, 31 insertions, 19 deletions
diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc
index 888a834d..89b24c1 100644
--- a/gcc/rust/ast/rust-ast-full-test.cc
+++ b/gcc/rust/ast/rust-ast-full-test.cc
@@ -4027,14 +4027,15 @@ filename_from_path_attribute (std::vector<Attribute> &outer_attrs)
return path_attr.get_attr_input ().as_string ();
}
-std::string
+void
Module::get_filename ()
{
rust_assert (kind == Module::ModuleKind::UNLOADED);
+ rust_assert (module_file.empty ());
auto path_string = filename_from_path_attribute (get_outer_attrs ());
if (!path_string.empty ())
- return path_string;
+ return;
// This corresponds to the path of the file 'including' the module. So the
// file that contains the 'mod <file>;' directive
@@ -4080,38 +4081,41 @@ Module::get_filename ()
rust_error_at (locus, "no candidate found for module %s",
module_name.c_str ());
- return file_mod_found ? expected_file_path
- : current_directory_name + expected_dir_path;
+ module_file = file_mod_found ? expected_file_path
+ : current_directory_name + expected_dir_path;
}
void
Module::load_items ()
{
- std::string mod_file = get_filename ();
+ get_filename ();
// We will already have errored out appropriately in the get_filename ()
// method
- if (mod_file.empty ())
+ if (module_file.empty ())
return;
- RAIIFile file_wrap (mod_file.c_str ());
+ RAIIFile file_wrap (module_file.c_str ());
Linemap *linemap = Session::get_instance ().linemap;
if (file_wrap.get_raw () == nullptr)
- rust_fatal_error (Location (), "cannot open module file %s: %m",
- mod_file.c_str ());
+ {
+ rust_error_at (Location (), "cannot open module file %s: %m",
+ module_file.c_str ());
+ return;
+ }
- rust_debug ("Attempting to parse file %s", mod_file.c_str ());
+ rust_debug ("Attempting to parse file %s", module_file.c_str ());
- Lexer lex (mod_file.c_str (), std::move (file_wrap), linemap);
+ Lexer lex (module_file.c_str (), std::move (file_wrap), linemap);
Parser<Lexer> parser (std::move (lex));
- auto items = parser.parse_items ();
+ auto parsed_items = parser.parse_items ();
for (const auto &error : parser.get_errors ())
error.emit_error ();
- items = std::move(items);
+ items = std::move (parsed_items);
kind = ModuleKind::LOADED;
}
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 112ca28..4886992 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -982,6 +982,10 @@ private:
// bool has_items;
std::vector<std::unique_ptr<Item>> items;
+ // Filename the module refers to. Empty string on LOADED modules or if an
+ // error occured when dealing with UNLOADED modules
+ std::string module_file;
+
void clone_items (const std::vector<std::unique_ptr<Item>> &other_items)
{
items.reserve (other_items.size ());
@@ -1051,8 +1055,9 @@ public:
return *this;
}
- // Search for the filename associated with an external module
- std::string get_filename ();
+ // Search for the filename associated with an external module, storing it in
+ // module_file
+ void get_filename ();
// Load the items contained in an external module
void load_items ();
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index 8134f7e..e531b21 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -1896,11 +1896,14 @@ public:
return;
}
}
- else
+
+ // Parse the module's items if they haven't been expanded and the file
+ // should be parsed (i.e isn't hidden behind an untrue or impossible cfg
+ // directive)
+ if (!module.is_marked_for_strip ()
+ && module.get_kind () == AST::Module::ModuleKind::UNLOADED)
{
- std::string mod_file = module.get_filename ();
- if (!mod_file.empty ())
- rust_debug ("Module filename found: %s", mod_file.c_str ());
+ module.load_items ();
}
// strip items if required