@@ -54,7 +54,28 @@ const char *kHIRDumpFile = "gccrs.hir.dump";
54
54
const char *kHIRTypeResolutionDumpFile = " gccrs.type-resolution.dump" ;
55
55
const char *kTargetOptionsDumpFile = " gccrs.target-options.dump" ;
56
56
57
- const std::string kDefaultCrateName = " example" ;
57
+ const std::string kDefaultCrateName = " rust_out" ;
58
+
59
+ static std::string
60
+ infer_crate_name (const std::string filename)
61
+ {
62
+ if (filename == " -" )
63
+ return kDefaultCrateName ;
64
+
65
+ std::string crate = std::string (filename);
66
+ size_t path_sep = crate.find_last_of (file_separator);
67
+
68
+ // find the base filename
69
+ if (path_sep != std::string::npos)
70
+ crate.erase (0 , path_sep + 1 );
71
+
72
+ // find the file stem name (remove file extension)
73
+ size_t ext_position = crate.find_last_of (' .' );
74
+ if (ext_position != std::string::npos)
75
+ crate.erase (ext_position);
76
+
77
+ return crate;
78
+ }
58
79
59
80
// Implicitly enable a target_feature (and recursively enable dependencies).
60
81
void
@@ -311,10 +332,6 @@ Session::init ()
311
332
312
333
// setup backend to GCC GIMPLE
313
334
backend = rust_get_backend ();
314
-
315
- // set the default crate name if crate name was unset
316
- if (options.crate_name .empty ())
317
- options.set_crate_name (kDefaultCrateName );
318
335
}
319
336
320
337
/* Initialise default options. Actually called before handle_option, unlike init
@@ -479,6 +496,25 @@ Session::enable_dump (std::string arg)
479
496
void
480
497
Session::parse_files (int num_files, const char **files)
481
498
{
499
+ rust_assert (num_files > 0 );
500
+
501
+ if (options.crate_name .empty ())
502
+ {
503
+ /* HACK: We use the first file to infer the crate name, which might be
504
+ * incorrect: since rustc only allows one file to be supplied in the
505
+ * command-line */
506
+ auto crate_name = infer_crate_name (files[0 ]);
507
+ rust_debug_loc (Location (), " inferred crate name: %s" ,
508
+ crate_name.c_str ());
509
+ if (!options.set_crate_name (crate_name))
510
+ {
511
+ rust_inform (Location (),
512
+ " crate name inferred from the input file %<%s%>" ,
513
+ files[0 ]);
514
+ return ;
515
+ }
516
+ }
517
+
482
518
auto mappings = Analysis::Mappings::get ();
483
519
CrateNum crate_num = mappings->setup_crate_mappings (options.crate_name );
484
520
mappings->set_current_crate (crate_num);
0 commit comments