11use std:: collections:: BTreeMap ;
22use std:: ffi:: OsStr ;
33use std:: fmt;
4+ use std:: io;
5+ use std:: io:: Read ;
6+ use std:: path:: Path ;
47use std:: path:: PathBuf ;
58use std:: str:: FromStr ;
69
@@ -9,14 +12,14 @@ use rustc_session::config::{
912 self , parse_crate_types_from_list, parse_externs, parse_target_triple, CrateType ,
1013} ;
1114use rustc_session:: config:: { get_cmd_lint_options, nightly_options} ;
12- use rustc_session:: config:: {
13- CodegenOptions , ErrorOutputType , Externs , JsonUnusedExterns , UnstableOptions ,
14- } ;
15+ use rustc_session:: config:: { CodegenOptions , ErrorOutputType , Externs , Input } ;
16+ use rustc_session:: config:: { JsonUnusedExterns , UnstableOptions } ;
1517use rustc_session:: getopts;
1618use rustc_session:: lint:: Level ;
1719use rustc_session:: search_paths:: SearchPath ;
1820use rustc_session:: EarlyDiagCtxt ;
1921use rustc_span:: edition:: Edition ;
22+ use rustc_span:: FileName ;
2023use rustc_target:: spec:: TargetTriple ;
2124
2225use crate :: core:: new_dcx;
@@ -60,7 +63,7 @@ impl TryFrom<&str> for OutputFormat {
6063pub ( crate ) struct Options {
6164 // Basic options / Options passed directly to rustc
6265 /// The crate root or Markdown file to load.
63- pub ( crate ) input : PathBuf ,
66+ pub ( crate ) input : Input ,
6467 /// The name of the crate being documented.
6568 pub ( crate ) crate_name : Option < String > ,
6669 /// Whether or not this is a bin crate
@@ -179,7 +182,7 @@ impl fmt::Debug for Options {
179182 }
180183
181184 f. debug_struct ( "Options" )
182- . field ( "input" , & self . input )
185+ . field ( "input" , & self . input . source_name ( ) )
183186 . field ( "crate_name" , & self . crate_name )
184187 . field ( "bin_crate" , & self . bin_crate )
185188 . field ( "proc_macro_crate" , & self . proc_macro_crate )
@@ -320,6 +323,23 @@ impl RenderOptions {
320323 }
321324}
322325
326+ /// Create the input (string or file path)
327+ ///
328+ /// Warning: Return an unrecoverable error in case of error!
329+ fn make_input ( early_dcx : & EarlyDiagCtxt , input : & str ) -> Input {
330+ if input == "-" {
331+ let mut src = String :: new ( ) ;
332+ if io:: stdin ( ) . read_to_string ( & mut src) . is_err ( ) {
333+ // Immediately stop compilation if there was an issue reading
334+ // the input (for example if the input stream is not UTF-8).
335+ early_dcx. early_fatal ( "couldn't read from stdin, as it did not contain valid UTF-8" ) ;
336+ }
337+ Input :: Str { name : FileName :: anon_source_code ( & src) , input : src }
338+ } else {
339+ Input :: File ( PathBuf :: from ( input) )
340+ }
341+ }
342+
323343impl Options {
324344 /// Parses the given command-line for options. If an error message or other early-return has
325345 /// been printed, returns `Err` with the exit code.
@@ -447,15 +467,16 @@ impl Options {
447467
448468 let ( lint_opts, describe_lints, lint_cap) = get_cmd_lint_options ( early_dcx, matches) ;
449469
450- let input = PathBuf :: from ( if describe_lints {
470+ let input = if describe_lints {
451471 "" // dummy, this won't be used
452- } else if matches. free . is_empty ( ) {
453- dcx. fatal ( "missing file operand" ) ;
454- } else if matches. free . len ( ) > 1 {
455- dcx. fatal ( "too many file operands" ) ;
456472 } else {
457- & matches. free [ 0 ]
458- } ) ;
473+ match matches. free . as_slice ( ) {
474+ [ ] => dcx. fatal ( "missing file operand" ) ,
475+ [ input] => input,
476+ _ => dcx. fatal ( "too many file operands" ) ,
477+ }
478+ } ;
479+ let input = make_input ( early_dcx, & input) ;
459480
460481 let externs = parse_externs ( early_dcx, matches, & unstable_opts) ;
461482 let extern_html_root_urls = match parse_extern_html_roots ( matches) {
@@ -792,8 +813,10 @@ impl Options {
792813 }
793814
794815 /// Returns `true` if the file given as `self.input` is a Markdown file.
795- pub ( crate ) fn markdown_input ( & self ) -> bool {
796- self . input . extension ( ) . is_some_and ( |e| e == "md" || e == "markdown" )
816+ pub ( crate ) fn markdown_input ( & self ) -> Option < & Path > {
817+ self . input
818+ . opt_path ( )
819+ . filter ( |p| matches ! ( p. extension( ) , Some ( e) if e == "md" || e == "markdown" ) )
797820 }
798821}
799822
0 commit comments