11use std:: collections:: BTreeMap ;
22use std:: ffi:: OsStr ;
33use std:: fmt;
4+ use std:: io;
5+ use std:: io:: Read ;
46use std:: path:: PathBuf ;
57use std:: str:: FromStr ;
68
@@ -9,14 +11,14 @@ use rustc_session::config::{
911 self , parse_crate_types_from_list, parse_externs, parse_target_triple, CrateType ,
1012} ;
1113use 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 } ;
1516use rustc_session:: getopts;
1617use rustc_session:: lint:: Level ;
1718use rustc_session:: search_paths:: SearchPath ;
1819use rustc_session:: EarlyDiagCtxt ;
1920use rustc_span:: edition:: Edition ;
21+ use rustc_span:: FileName ;
2022use rustc_target:: spec:: TargetTriple ;
2123
2224use crate :: core:: new_dcx;
@@ -60,7 +62,7 @@ impl TryFrom<&str> for OutputFormat {
6062pub ( crate ) struct Options {
6163 // Basic options / Options passed directly to rustc
6264 /// The crate root or Markdown file to load.
63- pub ( crate ) input : PathBuf ,
65+ pub ( crate ) input : Input ,
6466 /// The name of the crate being documented.
6567 pub ( crate ) crate_name : Option < String > ,
6668 /// Whether or not this is a bin crate
@@ -179,7 +181,7 @@ impl fmt::Debug for Options {
179181 }
180182
181183 f. debug_struct ( "Options" )
182- . field ( "input" , & self . input )
184+ . field ( "input" , & self . input . source_name ( ) )
183185 . field ( "crate_name" , & self . crate_name )
184186 . field ( "bin_crate" , & self . bin_crate )
185187 . field ( "proc_macro_crate" , & self . proc_macro_crate )
@@ -322,6 +324,23 @@ impl RenderOptions {
322324 }
323325}
324326
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+
325344impl Options {
326345 /// Parses the given command-line for options. If an error message or other early-return has
327346 /// been printed, returns `Err` with the exit code.
@@ -450,15 +469,16 @@ impl Options {
450469
451470 let ( lint_opts, describe_lints, lint_cap) = get_cmd_lint_options ( early_dcx, matches) ;
452471
453- let input = PathBuf :: from ( if describe_lints {
472+ let input = if describe_lints {
454473 "" // 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" ) ;
459474 } 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) ;
462482
463483 let externs = parse_externs ( early_dcx, matches, & unstable_opts) ;
464484 let extern_html_root_urls = match parse_extern_html_roots ( matches) {
@@ -797,7 +817,11 @@ impl Options {
797817
798818 /// Returns `true` if the file given as `self.input` is a Markdown file.
799819 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" )
801825 }
802826}
803827
0 commit comments