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 ;
6
+ use std:: path:: Path ;
4
7
use std:: path:: PathBuf ;
5
8
use std:: str:: FromStr ;
6
9
@@ -9,14 +12,14 @@ use rustc_session::config::{
9
12
self , parse_crate_types_from_list, parse_externs, parse_target_triple, CrateType ,
10
13
} ;
11
14
use 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 } ;
15
17
use rustc_session:: getopts;
16
18
use rustc_session:: lint:: Level ;
17
19
use rustc_session:: search_paths:: SearchPath ;
18
20
use rustc_session:: EarlyDiagCtxt ;
19
21
use rustc_span:: edition:: Edition ;
22
+ use rustc_span:: FileName ;
20
23
use rustc_target:: spec:: TargetTriple ;
21
24
22
25
use crate :: core:: new_dcx;
@@ -60,7 +63,7 @@ impl TryFrom<&str> for OutputFormat {
60
63
pub ( crate ) struct Options {
61
64
// Basic options / Options passed directly to rustc
62
65
/// The crate root or Markdown file to load.
63
- pub ( crate ) input : PathBuf ,
66
+ pub ( crate ) input : Input ,
64
67
/// The name of the crate being documented.
65
68
pub ( crate ) crate_name : Option < String > ,
66
69
/// Whether or not this is a bin crate
@@ -179,7 +182,7 @@ impl fmt::Debug for Options {
179
182
}
180
183
181
184
f. debug_struct ( "Options" )
182
- . field ( "input" , & self . input )
185
+ . field ( "input" , & self . input . source_name ( ) )
183
186
. field ( "crate_name" , & self . crate_name )
184
187
. field ( "bin_crate" , & self . bin_crate )
185
188
. field ( "proc_macro_crate" , & self . proc_macro_crate )
@@ -320,6 +323,23 @@ impl RenderOptions {
320
323
}
321
324
}
322
325
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
+
323
343
impl Options {
324
344
/// Parses the given command-line for options. If an error message or other early-return has
325
345
/// been printed, returns `Err` with the exit code.
@@ -447,15 +467,16 @@ impl Options {
447
467
448
468
let ( lint_opts, describe_lints, lint_cap) = get_cmd_lint_options ( early_dcx, matches) ;
449
469
450
- let input = PathBuf :: from ( if describe_lints {
470
+ let input = if describe_lints {
451
471
"" // 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" ) ;
456
472
} 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) ;
459
480
460
481
let externs = parse_externs ( early_dcx, matches, & unstable_opts) ;
461
482
let extern_html_root_urls = match parse_extern_html_roots ( matches) {
@@ -792,8 +813,10 @@ impl Options {
792
813
}
793
814
794
815
/// 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" ) )
797
820
}
798
821
}
799
822
0 commit comments