aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCohenArthur <arthur.cohen@epita.fr>2021-08-06 23:49:47 +0200
committerCohenArthur <arthur.cohen@epita.fr>2021-08-12 16:55:14 +0200
commitb4993a629348279023b2c0159169ed62f10ac453 (patch)
treea8be78ea928d605ce6ea8f29a896fcbb48f42af4
parent53756e1a01c7ac93e8467f577f50c491ca4b2525 (diff)
downloadgcc-b4993a629348279023b2c0159169ed62f10ac453.zip
gcc-b4993a629348279023b2c0159169ed62f10ac453.tar.gz
gcc-b4993a629348279023b2c0159169ed62f10ac453.tar.bz2
module: Add basic filename discovery
-rw-r--r--gcc/rust/ast/rust-ast-full-test.cc70
-rw-r--r--gcc/rust/ast/rust-item.h3
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc4
-rw-r--r--gcc/rust/parse/rust-parse-impl.h1
-rw-r--r--gcc/rust/rust-system.h8
5 files changed, 86 insertions, 0 deletions
diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc
index dfd9cdb..6033ce9 100644
--- a/gcc/rust/ast/rust-ast-full-test.cc
+++ b/gcc/rust/ast/rust-ast-full-test.cc
@@ -17,6 +17,9 @@ You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
+// FIXME: This does not work on Windows
+#include <unistd.h>
+
#include "rust-ast-full.h"
#include "rust-diagnostics.h"
#include "rust-ast-visitor.h"
@@ -4050,6 +4053,73 @@ Module::add_crate_name (std::vector<std::string> &names) const
item->add_crate_name (names);
}
+static bool
+file_exists (const std::string path)
+{
+ // Simply check if the file exists
+ // FIXME: This does not work on Windows
+ return access (path.c_str (), F_OK) != -1;
+}
+
+// FIXME: This function should also check if the module has a `path` outer
+// attribute and fetch the path from here in that case, i.e:
+// ```
+// #[path="<dir>/<subdir>/<file>.rs"]
+// mod <mod_name>;
+// ```
+std::string
+Module::get_filename ()
+{
+ rust_assert (kind == Module::ModuleKind::UNLOADED);
+
+ // This corresponds to the path of the file 'including' the module. So the
+ // file that contains the 'mod <file>;' directive
+ std::string including_fname (outer_filename);
+
+ std::string expected_file_path = module_name + ".rs";
+ std::string expected_dir_path = "mod.rs";
+
+ auto dir_slash_pos = including_fname.rfind (file_separator);
+ std::string current_directory_name;
+
+ // If we haven't found a file_separator, then we have to look for files in the
+ // current directory ('.')
+ if (dir_slash_pos == std::string::npos)
+ current_directory_name = std::string (".") + file_separator;
+ else
+ current_directory_name
+ = including_fname.substr (0, dir_slash_pos) + file_separator;
+
+ // FIXME: We also have to search for
+ // <directory>/<including_fname>/<module_name>.rs In rustc, this is done via
+ // the concept of `DirOwnernship`, which is based on whether or not the
+ // current file is titled `mod.rs`.
+
+ // First, we search for <directory>/<module_name>.rs
+ bool file_mod_found
+ = file_exists (current_directory_name + expected_file_path);
+
+ // Then, search for <directory>/<module_name>/mod.rs
+ current_directory_name += module_name + file_separator;
+ bool dir_mod_found = file_exists (current_directory_name + expected_dir_path);
+
+ bool multiple_candidates_found = file_mod_found && dir_mod_found;
+ bool no_candidates_found = !file_mod_found && !dir_mod_found;
+
+ if (multiple_candidates_found)
+ rust_error_at (locus,
+ "two candidates found for module %s: %s.rs and %s%smod.rs",
+ module_name.c_str (), module_name.c_str (),
+ module_name.c_str (), file_separator);
+
+ if (no_candidates_found)
+ 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;
+}
+
void
Attribute::parse_attr_to_meta_item ()
{
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 9e57f5a..dac76f5 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -1053,6 +1053,9 @@ public:
return *this;
}
+ // Search for the filename associated with an external module
+ std::string get_filename ();
+
void accept_vis (ASTVisitor &vis) override;
/* Override that runs the function recursively on all items contained within
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index 0a25471..4998dc8 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -2036,6 +2036,10 @@ public:
return;
}
}
+ else
+ {
+ std::string mod_file = module.get_filename ();
+ }
// strip items if required
expand_pointer_allow_strip (module.get_items ());
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 0e50168..731e0b3 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -2101,6 +2101,7 @@ Parser<ManagedTokenSource>::parse_module (AST::Visibility vis,
case SEMICOLON:
lexer.skip_token ();
+ // Construct an external module
return std::unique_ptr<AST::Module> (
new AST::Module (std::move (name), std::move (vis),
std::move (outer_attrs), locus,
diff --git a/gcc/rust/rust-system.h b/gcc/rust/rust-system.h
index d62e676..40bca63 100644
--- a/gcc/rust/rust-system.h
+++ b/gcc/rust/rust-system.h
@@ -59,6 +59,14 @@
#include "diagnostic-core.h" /* For error_at and friends. */
#include "intl.h" /* For _(). */
+// File separator to use based on whether or not the OS we're working with is
+// DOS-based
+#if defined(HAVE_DOS_BASED_FILE_SYSTEM)
+constexpr static const char *file_separator = "\\";
+#else
+constexpr static const char *file_separator = "/";
+#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
+
// When using gcc, rust_assert is just gcc_assert.
#define rust_assert(EXPR) gcc_assert (EXPR)