Skip to content

Commit e711e2d

Browse files
committed
Add -Z unstable-options debugging flag, which can then be used to
extend the `rustc` command line interface with options that we do not want to commit to making part of the long-term public interface for `rustc`.
1 parent 34d6800 commit e711e2d

File tree

2 files changed

+158
-49
lines changed

2 files changed

+158
-49
lines changed

src/librustc/session/config.rs

+122-42
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use syntax::parse::token::InternedString;
3434

3535
use std::collections::HashMap;
3636
use std::collections::hash_map::Entry::{Occupied, Vacant};
37-
use getopts::{optopt, optmulti, optflag, optflagopt};
3837
use getopts;
3938
use std::cell::{RefCell};
4039
use std::fmt;
@@ -278,7 +277,8 @@ debugging_opts! {
278277
PRINT_REGION_GRAPH,
279278
PARSE_ONLY,
280279
NO_TRANS,
281-
NO_ANALYSIS
280+
NO_ANALYSIS,
281+
UNSTABLE_OPTIONS
282282
]
283283
0
284284
}
@@ -330,7 +330,8 @@ pub fn debugging_opts_map() -> Vec<(&'static str, &'static str, u64)> {
330330
("no-trans", "Run all passes except translation; no output", NO_TRANS),
331331
("no-analysis", "Parse and expand the source, but run no analysis and",
332332
NO_TRANS),
333-
]
333+
("unstable-options", "Adds unstable command line options to rustc interface",
334+
UNSTABLE_OPTIONS)]
334335
}
335336

336337
#[deriving(Clone)]
@@ -653,95 +654,174 @@ pub fn build_target_config(opts: &Options, sp: &SpanHandler) -> Config {
653654
}
654655
}
655656

657+
/// Returns the "short" subset of the stable rustc command line options.
656658
pub fn short_optgroups() -> Vec<getopts::OptGroup> {
659+
rustc_short_optgroups().into_iter()
660+
.filter(|g|g.is_stable())
661+
.map(|g|g.opt_group)
662+
.collect()
663+
}
664+
665+
/// Returns all of the stable rustc command line options.
666+
pub fn optgroups() -> Vec<getopts::OptGroup> {
667+
rustc_optgroups().into_iter()
668+
.filter(|g|g.is_stable())
669+
.map(|g|g.opt_group)
670+
.collect()
671+
}
672+
673+
#[deriving(Copy, Clone, PartialEq, Eq, Show)]
674+
pub enum OptionStability { Stable, Unstable }
675+
676+
#[deriving(Clone, PartialEq, Eq)]
677+
pub struct RustcOptGroup {
678+
pub opt_group: getopts::OptGroup,
679+
pub stability: OptionStability,
680+
}
681+
682+
impl RustcOptGroup {
683+
pub fn is_stable(&self) -> bool {
684+
self.stability == OptionStability::Stable
685+
}
686+
687+
fn stable(g: getopts::OptGroup) -> RustcOptGroup {
688+
RustcOptGroup { opt_group: g, stability: OptionStability::Stable }
689+
}
690+
691+
fn unstable(g: getopts::OptGroup) -> RustcOptGroup {
692+
RustcOptGroup { opt_group: g, stability: OptionStability::Unstable }
693+
}
694+
}
695+
696+
// The `opt` local module holds wrappers around the `getopts` API that
697+
// adds extra rustc-specific metadata to each option; such metadata
698+
// is exposed by . The public
699+
// functions below ending with `_u` are the functions that return
700+
// *unstable* options, i.e. options that are only enabled when the
701+
// user also passes the `-Z unstable-options` debugging flag.
702+
mod opt {
703+
// The `fn opt_u` etc below are written so that we can use them
704+
// in the future; do not warn about them not being used right now.
705+
#![allow(dead_code)]
706+
707+
use getopts;
708+
use super::RustcOptGroup;
709+
710+
type R = RustcOptGroup;
711+
type S<'a> = &'a str;
712+
713+
fn stable(g: getopts::OptGroup) -> R { RustcOptGroup::stable(g) }
714+
fn unstable(g: getopts::OptGroup) -> R { RustcOptGroup::unstable(g) }
715+
716+
// FIXME (pnkfelix): We default to stable since the current set of
717+
// options is defacto stable. However, it would be good to revise the
718+
// code so that a stable option is the thing that takes extra effort
719+
// to encode.
720+
721+
pub fn opt(a: S, b: S, c: S, d: S) -> R { stable(getopts::optopt(a, b, c, d)) }
722+
pub fn multi(a: S, b: S, c: S, d: S) -> R { stable(getopts::optmulti(a, b, c, d)) }
723+
pub fn flag(a: S, b: S, c: S) -> R { stable(getopts::optflag(a, b, c)) }
724+
pub fn flagopt(a: S, b: S, c: S, d: S) -> R { stable(getopts::optflagopt(a, b, c, d)) }
725+
726+
pub fn opt_u(a: S, b: S, c: S, d: S) -> R { unstable(getopts::optopt(a, b, c, d)) }
727+
pub fn multi_u(a: S, b: S, c: S, d: S) -> R { unstable(getopts::optmulti(a, b, c, d)) }
728+
pub fn flag_u(a: S, b: S, c: S) -> R { unstable(getopts::optflag(a, b, c)) }
729+
pub fn flagopt_u(a: S, b: S, c: S, d: S) -> R { unstable(getopts::optflagopt(a, b, c, d)) }
730+
}
731+
732+
/// Returns the "short" subset of the rustc command line options,
733+
/// including metadata for each option, such as whether the option is
734+
/// part of the stable long-term interface for rustc.
735+
pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
657736
vec![
658-
optflag("h", "help", "Display this message"),
659-
optmulti("", "cfg", "Configure the compilation environment", "SPEC"),
660-
optmulti("L", "", "Add a directory to the library search path", "PATH"),
661-
optmulti("l", "", "Link the generated crate(s) to the specified native
737+
opt::flag("h", "help", "Display this message"),
738+
opt::multi("", "cfg", "Configure the compilation environment", "SPEC"),
739+
opt::multi("L", "", "Add a directory to the library search path", "PATH"),
740+
opt::multi("l", "", "Link the generated crate(s) to the specified native
662741
library NAME. The optional KIND can be one of,
663742
static, dylib, or framework. If omitted, dylib is
664743
assumed.", "NAME[:KIND]"),
665-
optmulti("", "crate-type", "Comma separated list of types of crates
744+
opt::multi("", "crate-type", "Comma separated list of types of crates
666745
for the compiler to emit",
667746
"[bin|lib|rlib|dylib|staticlib|dep-info]"),
668-
optopt("", "crate-name", "Specify the name of the crate being built",
747+
opt::opt("", "crate-name", "Specify the name of the crate being built",
669748
"NAME"),
670-
optmulti("", "emit", "Comma separated list of types of output for \
749+
opt::multi("", "emit", "Comma separated list of types of output for \
671750
the compiler to emit",
672751
"[asm|llvm-bc|llvm-ir|obj|link]"),
673-
optmulti("", "print", "Comma separated list of compiler information to \
752+
opt::multi("", "print", "Comma separated list of compiler information to \
674753
print on stdout",
675754
"[crate-name|output-file-names|sysroot]"),
676-
optflag("g", "", "Equivalent to --debuginfo=2"),
677-
optflag("O", "", "Equivalent to --opt-level=2"),
678-
optopt("o", "", "Write output to <filename>", "FILENAME"),
679-
optopt("", "out-dir", "Write output to compiler-chosen filename \
755+
opt::flag("g", "", "Equivalent to --debuginfo=2"),
756+
opt::flag("O", "", "Equivalent to --opt-level=2"),
757+
opt::opt("o", "", "Write output to <filename>", "FILENAME"),
758+
opt::opt("", "out-dir", "Write output to compiler-chosen filename \
680759
in <dir>", "DIR"),
681-
optopt("", "explain", "Provide a detailed explanation of an error \
760+
opt::opt("", "explain", "Provide a detailed explanation of an error \
682761
message", "OPT"),
683-
optflag("", "test", "Build a test harness"),
684-
optopt("", "target", "Target triple cpu-manufacturer-kernel[-os] \
762+
opt::flag("", "test", "Build a test harness"),
763+
opt::opt("", "target", "Target triple cpu-manufacturer-kernel[-os] \
685764
to compile for (see chapter 3.4 of \
686765
http://www.sourceware.org/autobook/
687766
for details)",
688767
"TRIPLE"),
689-
optmulti("W", "warn", "Set lint warnings", "OPT"),
690-
optmulti("A", "allow", "Set lint allowed", "OPT"),
691-
optmulti("D", "deny", "Set lint denied", "OPT"),
692-
optmulti("F", "forbid", "Set lint forbidden", "OPT"),
693-
optmulti("C", "codegen", "Set a codegen option", "OPT[=VALUE]"),
694-
optflag("V", "version", "Print version info and exit"),
695-
optflag("v", "verbose", "Use verbose output"),
768+
opt::multi("W", "warn", "Set lint warnings", "OPT"),
769+
opt::multi("A", "allow", "Set lint allowed", "OPT"),
770+
opt::multi("D", "deny", "Set lint denied", "OPT"),
771+
opt::multi("F", "forbid", "Set lint forbidden", "OPT"),
772+
opt::multi("C", "codegen", "Set a codegen option", "OPT[=VALUE]"),
773+
opt::flag("V", "version", "Print version info and exit"),
774+
opt::flag("v", "verbose", "Use verbose output"),
696775
]
697776
}
698777

699-
// rustc command line options
700-
pub fn optgroups() -> Vec<getopts::OptGroup> {
701-
let mut opts = short_optgroups();
778+
/// Returns all rustc command line options, including metadata for
779+
/// each option, such as whether the option is part of the stable
780+
/// long-term interface for rustc.
781+
pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
782+
let mut opts = rustc_short_optgroups();
702783
opts.push_all(&[
703-
optmulti("", "extern", "Specify where an external rust library is \
784+
opt::multi("", "extern", "Specify where an external rust library is \
704785
located",
705786
"NAME=PATH"),
706-
optopt("", "opt-level", "Optimize with possible levels 0-3", "LEVEL"),
707-
optopt("", "sysroot", "Override the system root", "PATH"),
708-
optmulti("Z", "", "Set internal debugging options", "FLAG"),
709-
optopt("", "color", "Configure coloring of output:
787+
opt::opt("", "opt-level", "Optimize with possible levels 0-3", "LEVEL"),
788+
opt::opt("", "sysroot", "Override the system root", "PATH"),
789+
opt::multi("Z", "", "Set internal debugging options", "FLAG"),
790+
opt::opt("", "color", "Configure coloring of output:
710791
auto = colorize, if output goes to a tty (default);
711792
always = always colorize output;
712793
never = never colorize output", "auto|always|never"),
713794

714795
// DEPRECATED
715-
optflag("", "print-crate-name", "Output the crate name and exit"),
716-
optflag("", "print-file-name", "Output the file(s) that would be \
796+
opt::flag("", "print-crate-name", "Output the crate name and exit"),
797+
opt::flag("", "print-file-name", "Output the file(s) that would be \
717798
written if compilation \
718799
continued and exit"),
719-
optopt("", "debuginfo", "Emit DWARF debug info to the objects created:
800+
opt::opt("", "debuginfo", "Emit DWARF debug info to the objects created:
720801
0 = no debug info,
721802
1 = line-tables only (for stacktraces and breakpoints),
722803
2 = full debug info with variable and type information \
723804
(same as -g)", "LEVEL"),
724-
optflag("", "no-trans", "Run all passes except translation; no output"),
725-
optflag("", "no-analysis", "Parse and expand the source, but run no \
805+
opt::flag("", "no-trans", "Run all passes except translation; no output"),
806+
opt::flag("", "no-analysis", "Parse and expand the source, but run no \
726807
analysis and produce no output"),
727-
optflag("", "parse-only", "Parse only; do not compile, assemble, \
808+
opt::flag("", "parse-only", "Parse only; do not compile, assemble, \
728809
or link"),
729-
optflagopt("", "pretty",
810+
opt::flagopt("", "pretty",
730811
"Pretty-print the input instead of compiling;
731812
valid types are: `normal` (un-annotated source),
732813
`expanded` (crates expanded),
733814
`typed` (crates expanded, with type annotations),
734815
`expanded,identified` (fully parenthesized, AST nodes with IDs), or
735816
`flowgraph=<nodeid>` (graphviz formatted flowgraph for node)",
736817
"TYPE"),
737-
optflagopt("", "dep-info",
818+
opt::flagopt("", "dep-info",
738819
"Output dependency info to <filename> after compiling, \
739820
in a format suitable for use by Makefiles", "FILENAME"),
740821
]);
741822
opts
742823
}
743824

744-
745825
// Convert strings provided as --cfg [cfgspec] into a crate_cfg
746826
pub fn parse_cfgspecs(cfgspecs: Vec<String> ) -> ast::CrateConfig {
747827
cfgspecs.into_iter().map(|s| {

src/librustc_driver/lib.rs

+36-7
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,16 @@ pub fn version(binary: &str, matches: &getopts::Matches) {
196196
}
197197
}
198198

199-
fn usage(verbose: bool) {
199+
fn usage(verbose: bool, include_unstable_options: bool) {
200200
let groups = if verbose {
201-
config::optgroups()
201+
config::rustc_optgroups()
202202
} else {
203-
config::short_optgroups()
203+
config::rustc_short_optgroups()
204204
};
205+
let groups : Vec<_> = groups.into_iter()
206+
.filter(|x| include_unstable_options || x.is_stable())
207+
.map(|x|x.opt_group)
208+
.collect();
205209
let message = format!("Usage: rustc [OPTIONS] INPUT");
206210
let extra_help = if verbose {
207211
""
@@ -362,20 +366,45 @@ pub fn handle_options(mut args: Vec<String>) -> Option<getopts::Matches> {
362366
let _binary = args.remove(0).unwrap();
363367

364368
if args.is_empty() {
365-
usage(false);
369+
// user did not write `-v` nor `-Z unstable-options`, so do not
370+
// include that extra information.
371+
usage(false, false);
366372
return None;
367373
}
368374

369375
let matches =
370376
match getopts::getopts(args.as_slice(), config::optgroups().as_slice()) {
371377
Ok(m) => m,
372-
Err(f) => {
373-
early_error(f.to_string().as_slice());
378+
Err(f_stable_attempt) => {
379+
// redo option parsing, including unstable options this time,
380+
// in anticipation that the mishandled option was one of the
381+
// unstable ones.
382+
let all_groups : Vec<getopts::OptGroup>
383+
= config::rustc_optgroups().into_iter().map(|x|x.opt_group).collect();
384+
match getopts::getopts(args.as_slice(), all_groups.as_slice()) {
385+
Ok(m_unstable) => {
386+
let r = m_unstable.opt_strs("Z");
387+
let include_unstable_options = r.iter().any(|x| *x == "unstable-options");
388+
if include_unstable_options {
389+
m_unstable
390+
} else {
391+
early_error(f_stable_attempt.to_string().as_slice());
392+
}
393+
}
394+
Err(_) => {
395+
// ignore the error from the unstable attempt; just
396+
// pass the error we got from the first try.
397+
early_error(f_stable_attempt.to_string().as_slice());
398+
}
399+
}
374400
}
375401
};
376402

403+
let r = matches.opt_strs("Z");
404+
let include_unstable_options = r.iter().any(|x| *x == "unstable-options");
405+
377406
if matches.opt_present("h") || matches.opt_present("help") {
378-
usage(matches.opt_present("verbose"));
407+
usage(matches.opt_present("verbose"), include_unstable_options);
379408
return None;
380409
}
381410

0 commit comments

Comments
 (0)