1
1
use std:: collections:: BTreeMap ;
2
2
use std:: ffi:: OsStr ;
3
3
use std:: fmt;
4
+ use std:: io;
5
+ use std:: io:: Read ;
4
6
use std:: path:: PathBuf ;
5
7
use std:: str:: FromStr ;
6
8
@@ -9,14 +11,14 @@ use rustc_session::config::{
9
11
self , parse_crate_types_from_list, parse_externs, parse_target_triple, CrateType ,
10
12
} ;
11
13
use rustc_session:: config:: { get_cmd_lint_options, nightly_options} ;
12
- use rustc_session:: config:: {
13
- CodegenOptions , ErrorOutputType , Externs , JsonUnusedExterns , UnstableOptions ,
14
- } ;
14
+ use rustc_session:: config:: { CodegenOptions , ErrorOutputType , Externs , Input } ;
15
+ use rustc_session:: config:: { JsonUnusedExterns , UnstableOptions } ;
15
16
use rustc_session:: getopts;
16
17
use rustc_session:: lint:: Level ;
17
18
use rustc_session:: search_paths:: SearchPath ;
18
19
use rustc_session:: EarlyDiagCtxt ;
19
20
use rustc_span:: edition:: Edition ;
21
+ use rustc_span:: FileName ;
20
22
use rustc_target:: spec:: TargetTriple ;
21
23
22
24
use crate :: core:: new_dcx;
@@ -60,7 +62,7 @@ impl TryFrom<&str> for OutputFormat {
60
62
pub ( crate ) struct Options {
61
63
// Basic options / Options passed directly to rustc
62
64
/// The crate root or Markdown file to load.
63
- pub ( crate ) input : PathBuf ,
65
+ pub ( crate ) input : Input ,
64
66
/// The name of the crate being documented.
65
67
pub ( crate ) crate_name : Option < String > ,
66
68
/// Whether or not this is a bin crate
@@ -179,7 +181,7 @@ impl fmt::Debug for Options {
179
181
}
180
182
181
183
f. debug_struct ( "Options" )
182
- . field ( "input" , & self . input )
184
+ . field ( "input" , & self . input . source_name ( ) )
183
185
. field ( "crate_name" , & self . crate_name )
184
186
. field ( "bin_crate" , & self . bin_crate )
185
187
. field ( "proc_macro_crate" , & self . proc_macro_crate )
@@ -322,6 +324,23 @@ impl RenderOptions {
322
324
}
323
325
}
324
326
327
+ /// Create the input (string or file path)
328
+ ///
329
+ /// Warning: Return an unrecoverable error in case of error!
330
+ fn make_input ( early_dcx : & EarlyDiagCtxt , input : & str ) -> Input {
331
+ if input == "-" {
332
+ let mut src = String :: new ( ) ;
333
+ if io:: stdin ( ) . read_to_string ( & mut src) . is_err ( ) {
334
+ // Immediately stop compilation if there was an issue reading
335
+ // the input (for example if the input stream is not UTF-8).
336
+ early_dcx. early_fatal ( "couldn't read from stdin, as it did not contain valid UTF-8" ) ;
337
+ }
338
+ Input :: Str { name : FileName :: anon_source_code ( & src) , input : src }
339
+ } else {
340
+ Input :: File ( PathBuf :: from ( input) )
341
+ }
342
+ }
343
+
325
344
impl Options {
326
345
/// Parses the given command-line for options. If an error message or other early-return has
327
346
/// been printed, returns `Err` with the exit code.
@@ -450,15 +469,16 @@ impl Options {
450
469
451
470
let ( lint_opts, describe_lints, lint_cap) = get_cmd_lint_options ( early_dcx, matches) ;
452
471
453
- let input = PathBuf :: from ( if describe_lints {
472
+ let input = if describe_lints {
454
473
"" // dummy, this won't be used
455
- } else if matches. free . is_empty ( ) {
456
- dcx. fatal ( "missing file operand" ) ;
457
- } else if matches. free . len ( ) > 1 {
458
- dcx. fatal ( "too many file operands" ) ;
459
474
} else {
460
- & matches. free [ 0 ]
461
- } ) ;
475
+ match matches. free . as_slice ( ) {
476
+ [ ] => dcx. fatal ( "missing file operand" ) ,
477
+ [ input] => input,
478
+ _ => dcx. fatal ( "too many file operands" ) ,
479
+ }
480
+ } ;
481
+ let input = make_input ( early_dcx, & input) ;
462
482
463
483
let externs = parse_externs ( early_dcx, matches, & unstable_opts) ;
464
484
let extern_html_root_urls = match parse_extern_html_roots ( matches) {
@@ -797,7 +817,11 @@ impl Options {
797
817
798
818
/// Returns `true` if the file given as `self.input` is a Markdown file.
799
819
pub ( crate ) fn markdown_input ( & self ) -> bool {
800
- self . input . extension ( ) . is_some_and ( |e| e == "md" || e == "markdown" )
820
+ self . input
821
+ . opt_path ( )
822
+ . map ( std:: path:: Path :: extension)
823
+ . flatten ( )
824
+ . is_some_and ( |e| e == "md" || e == "markdown" )
801
825
}
802
826
}
803
827
0 commit comments