Skip to content

Commit cbb967f

Browse files
author
Jorge Aparicio
committed
add a panic-strategy field to the target specification
Now a target can define its panic strategy in its specification. If a user doesn't specify a panic strategy via the command line, i.e. '-C panic', then the compiler will use the panic strategy defined by the target specification. Custom targets can pick their panic strategy via the "panic-strategy" field of their target specification JSON file. If omitted in the specification, the strategy defaults to "unwind". closes #36647
1 parent 49dd95b commit cbb967f

File tree

12 files changed

+82
-36
lines changed

12 files changed

+82
-36
lines changed

src/librustc/middle/cstore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use ty::{self, Ty, TyCtxt};
3232
use mir::repr::Mir;
3333
use mir::mir_map::MirMap;
3434
use session::Session;
35-
use session::config::PanicStrategy;
3635
use session::search_paths::PathKind;
3736
use util::nodemap::{NodeSet, DefIdMap};
3837
use std::path::PathBuf;
@@ -45,6 +44,7 @@ use syntax_pos::Span;
4544
use rustc_back::target::Target;
4645
use hir;
4746
use hir::intravisit::Visitor;
47+
use rustc_back::PanicStrategy;
4848

4949
pub use self::NativeLibraryKind::{NativeStatic, NativeFramework, NativeUnknown};
5050

src/librustc/middle/dependency_format.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@
6464
use hir::def_id::CrateNum;
6565

6666
use session;
67-
use session::config::{self, PanicStrategy};
67+
use session::config;
6868
use middle::cstore::LinkagePreference::{self, RequireStatic, RequireDynamic};
6969
use util::nodemap::FnvHashMap;
70+
use rustc_back::PanicStrategy;
7071

7172
/// A list of dependencies for a certain crate type.
7273
///
@@ -357,7 +358,7 @@ fn verify_ok(sess: &session::Session, list: &[Linkage]) {
357358
// only one, but we perform validation here that all the panic strategy
358359
// compilation modes for the whole DAG are valid.
359360
if let Some((cnum, found_strategy)) = panic_runtime {
360-
let desired_strategy = sess.opts.cg.panic.clone();
361+
let desired_strategy = sess.panic_strategy();
361362

362363
// First up, validate that our selected panic runtime is indeed exactly
363364
// our same strategy.

src/librustc/middle/weak_lang_items.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
//! Validity checking for weak lang items
1212
13-
use session::config::{self, PanicStrategy};
13+
use session::config;
1414
use session::Session;
1515
use middle::lang_items;
1616

17+
use rustc_back::PanicStrategy;
1718
use syntax::ast;
1819
use syntax::parse::token::InternedString;
1920
use syntax_pos::Span;
@@ -92,7 +93,7 @@ fn verify(sess: &Session, items: &lang_items::LanguageItems) {
9293
// symbols. Other panic runtimes ensure that the relevant symbols are
9394
// available to link things together, but they're never exercised.
9495
let mut whitelisted = HashSet::new();
95-
if sess.opts.cg.panic != PanicStrategy::Unwind {
96+
if sess.panic_strategy() != PanicStrategy::Unwind {
9697
whitelisted.insert(lang_items::EhPersonalityLangItem);
9798
whitelisted.insert(lang_items::EhUnwindResumeLangItem);
9899
}

src/librustc/session/config.rs

+12-22
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub use self::DebugInfoLevel::*;
1919
use session::{early_error, early_warn, Session};
2020
use session::search_paths::SearchPaths;
2121

22+
use rustc_back::PanicStrategy;
2223
use rustc_back::target::Target;
2324
use lint;
2425
use middle::cstore;
@@ -493,21 +494,6 @@ impl Passes {
493494
}
494495
}
495496

496-
#[derive(Clone, PartialEq, Hash, RustcEncodable, RustcDecodable)]
497-
pub enum PanicStrategy {
498-
Unwind,
499-
Abort,
500-
}
501-
502-
impl PanicStrategy {
503-
pub fn desc(&self) -> &str {
504-
match *self {
505-
PanicStrategy::Unwind => "unwind",
506-
PanicStrategy::Abort => "abort",
507-
}
508-
}
509-
}
510-
511497
/// Declare a macro that will define all CodegenOptions/DebuggingOptions fields and parsers all
512498
/// at once. The goal of this macro is to define an interface that can be
513499
/// programmatically used by the option parser in order to initialize the struct
@@ -620,7 +606,8 @@ macro_rules! options {
620606

621607
#[allow(dead_code)]
622608
mod $mod_set {
623-
use super::{$struct_name, Passes, SomePasses, AllPasses, PanicStrategy};
609+
use super::{$struct_name, Passes, SomePasses, AllPasses};
610+
use rustc_back::PanicStrategy;
624611

625612
$(
626613
pub fn $opt(cg: &mut $struct_name, v: Option<&str>) -> bool {
@@ -725,10 +712,10 @@ macro_rules! options {
725712
}
726713
}
727714

728-
fn parse_panic_strategy(slot: &mut PanicStrategy, v: Option<&str>) -> bool {
715+
fn parse_panic_strategy(slot: &mut Option<PanicStrategy>, v: Option<&str>) -> bool {
729716
match v {
730-
Some("unwind") => *slot = PanicStrategy::Unwind,
731-
Some("abort") => *slot = PanicStrategy::Abort,
717+
Some("unwind") => *slot = Some(PanicStrategy::Unwind),
718+
Some("abort") => *slot = Some(PanicStrategy::Abort),
732719
_ => return false
733720
}
734721
true
@@ -800,7 +787,7 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
800787
"explicitly enable the cfg(debug_assertions) directive"),
801788
inline_threshold: Option<usize> = (None, parse_opt_uint, [TRACKED],
802789
"set the inlining threshold for"),
803-
panic: PanicStrategy = (PanicStrategy::Unwind, parse_panic_strategy,
790+
panic: Option<PanicStrategy> = (None, parse_panic_strategy,
804791
[TRACKED], "panic strategy to compile crate with"),
805792
}
806793

@@ -1676,9 +1663,10 @@ mod dep_tracking {
16761663
use std::collections::BTreeMap;
16771664
use std::hash::{Hash, SipHasher};
16781665
use std::path::PathBuf;
1679-
use super::{Passes, PanicStrategy, CrateType, OptLevel, DebugInfoLevel,
1666+
use super::{Passes, CrateType, OptLevel, DebugInfoLevel,
16801667
OutputTypes, Externs, ErrorOutputType};
16811668
use syntax::feature_gate::UnstableFeatures;
1669+
use rustc_back::PanicStrategy;
16821670

16831671
pub trait DepTrackingHash {
16841672
fn hash(&self, &mut SipHasher, ErrorOutputType);
@@ -1717,6 +1705,7 @@ mod dep_tracking {
17171705
impl_dep_tracking_hash_via_hash!(Option<bool>);
17181706
impl_dep_tracking_hash_via_hash!(Option<usize>);
17191707
impl_dep_tracking_hash_via_hash!(Option<String>);
1708+
impl_dep_tracking_hash_via_hash!(Option<PanicStrategy>);
17201709
impl_dep_tracking_hash_via_hash!(Option<lint::Level>);
17211710
impl_dep_tracking_hash_via_hash!(Option<PathBuf>);
17221711
impl_dep_tracking_hash_via_hash!(CrateType);
@@ -1783,7 +1772,8 @@ mod tests {
17831772
use std::iter::FromIterator;
17841773
use std::path::PathBuf;
17851774
use std::rc::Rc;
1786-
use super::{OutputType, OutputTypes, Externs, PanicStrategy};
1775+
use super::{OutputType, OutputTypes, Externs};
1776+
use rustc_back::PanicStrategy;
17871777
use syntax::{ast, attr};
17881778
use syntax::parse::token::InternedString;
17891779
use syntax::codemap::dummy_spanned;

src/librustc/session/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use lint;
1515
use middle::cstore::CrateStore;
1616
use middle::dependency_format;
1717
use session::search_paths::PathKind;
18-
use session::config::{DebugInfoLevel, PanicStrategy};
18+
use session::config::DebugInfoLevel;
1919
use ty::tls;
2020
use util::nodemap::{NodeMap, FnvHashMap};
2121
use util::common::duration_to_secs_str;
@@ -33,6 +33,7 @@ use syntax::{ast, codemap};
3333
use syntax::feature_gate::AttributeType;
3434
use syntax_pos::{Span, MultiSpan};
3535

36+
use rustc_back::PanicStrategy;
3637
use rustc_back::target::Target;
3738
use rustc_data_structures::flock;
3839
use llvm;
@@ -306,9 +307,13 @@ impl Session {
306307
pub fn lto(&self) -> bool {
307308
self.opts.cg.lto
308309
}
310+
/// Returns the panic strategy for this compile session. If the user explicitly selected one
311+
/// using '-C panic', use that, otherwise use the panic strategy defined by the target.
312+
pub fn panic_strategy(&self) -> PanicStrategy {
313+
self.opts.cg.panic.unwrap_or(self.target.target.options.panic_strategy)
314+
}
309315
pub fn no_landing_pads(&self) -> bool {
310-
self.opts.debugging_opts.no_landing_pads ||
311-
self.opts.cg.panic == PanicStrategy::Abort
316+
self.opts.debugging_opts.no_landing_pads || self.panic_strategy() == PanicStrategy::Abort
312317
}
313318
pub fn unstable_options(&self) -> bool {
314319
self.opts.debugging_opts.unstable_options

src/librustc_back/lib.rs

+28
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,36 @@ extern crate libc;
4545
extern crate serialize;
4646
#[macro_use] extern crate log;
4747

48+
extern crate serialize as rustc_serialize; // used by deriving
49+
4850
pub mod tempdir;
4951
pub mod sha2;
5052
pub mod target;
5153
pub mod slice;
5254
pub mod dynamic_lib;
55+
56+
use serialize::json::{Json, ToJson};
57+
58+
#[derive(Clone, Copy, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
59+
pub enum PanicStrategy {
60+
Unwind,
61+
Abort,
62+
}
63+
64+
impl PanicStrategy {
65+
pub fn desc(&self) -> &str {
66+
match *self {
67+
PanicStrategy::Unwind => "unwind",
68+
PanicStrategy::Abort => "abort",
69+
}
70+
}
71+
}
72+
73+
impl ToJson for PanicStrategy {
74+
fn to_json(&self) -> Json {
75+
match *self {
76+
PanicStrategy::Abort => "abort".to_json(),
77+
PanicStrategy::Unwind => "unwind".to_json(),
78+
}
79+
}
80+
}

src/librustc_back/target/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ use std::default::Default;
5050
use std::io::prelude::*;
5151
use syntax::abi::Abi;
5252

53+
use PanicStrategy;
54+
5355
mod android_base;
5456
mod apple_base;
5557
mod apple_ios_base;
@@ -343,6 +345,9 @@ pub struct TargetOptions {
343345
/// Maximum integer size in bits that this target can perform atomic
344346
/// operations on.
345347
pub max_atomic_width: u64,
348+
349+
/// Panic strategy: "unwind" or "abort"
350+
pub panic_strategy: PanicStrategy,
346351
}
347352

348353
impl Default for TargetOptions {
@@ -392,6 +397,7 @@ impl Default for TargetOptions {
392397
has_elf_tls: false,
393398
obj_is_bitcode: false,
394399
max_atomic_width: 0,
400+
panic_strategy: PanicStrategy::Unwind,
395401
}
396402
}
397403
}
@@ -470,6 +476,19 @@ impl Target {
470476
.map(|o| o.as_u64()
471477
.map(|s| base.options.$key_name = s));
472478
} );
479+
($key_name:ident, PanicStrategy) => ( {
480+
let name = (stringify!($key_name)).replace("_", "-");
481+
obj.find(&name[..]).and_then(|o| o.as_string().and_then(|s| {
482+
match s {
483+
"unwind" => base.options.$key_name = PanicStrategy::Unwind,
484+
"abort" => base.options.$key_name = PanicStrategy::Abort,
485+
_ => return Some(Err(format!("'{}' is not a valid value for \
486+
panic-strategy. Use 'unwind' or 'abort'.",
487+
s))),
488+
}
489+
Some(Ok(()))
490+
})).unwrap_or(Ok(()))
491+
} );
473492
($key_name:ident, list) => ( {
474493
let name = (stringify!($key_name)).replace("_", "-");
475494
obj.find(&name[..]).map(|o| o.as_array()
@@ -530,6 +549,7 @@ impl Target {
530549
key!(has_elf_tls, bool);
531550
key!(obj_is_bitcode, bool);
532551
key!(max_atomic_width, u64);
552+
try!(key!(panic_strategy, PanicStrategy));
533553

534554
Ok(base)
535555
}
@@ -672,6 +692,7 @@ impl ToJson for Target {
672692
target_option_val!(has_elf_tls);
673693
target_option_val!(obj_is_bitcode);
674694
target_option_val!(max_atomic_width);
695+
target_option_val!(panic_strategy);
675696

676697
Json::Object(d)
677698
}

src/librustc_metadata/creader.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use schema::CrateRoot;
1818
use rustc::hir::def_id::{CrateNum, DefIndex};
1919
use rustc::hir::svh::Svh;
2020
use rustc::session::{config, Session};
21-
use rustc::session::config::PanicStrategy;
21+
use rustc_back::PanicStrategy;
2222
use rustc::session::search_paths::PathKind;
2323
use rustc::middle;
2424
use rustc::middle::cstore::{CrateStore, validate_crate_name, ExternCrate};
@@ -710,7 +710,7 @@ impl<'a> CrateReader<'a> {
710710
// The logic for finding the panic runtime here is pretty much the same
711711
// as the allocator case with the only addition that the panic strategy
712712
// compilation mode also comes into play.
713-
let desired_strategy = self.sess.opts.cg.panic.clone();
713+
let desired_strategy = self.sess.panic_strategy();
714714
let mut runtime_found = false;
715715
let mut needs_panic_runtime = attr::contains_name(&krate.attrs,
716716
"needs_panic_runtime");

src/librustc_metadata/csearch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc::hir::map::DefKey;
2626
use rustc::mir::repr::Mir;
2727
use rustc::mir::mir_map::MirMap;
2828
use rustc::util::nodemap::{NodeSet, DefIdMap};
29-
use rustc::session::config::PanicStrategy;
29+
use rustc_back::PanicStrategy;
3030

3131
use std::path::PathBuf;
3232
use syntax::ast;

src/librustc_metadata/cstore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc::hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefIndex, DefId};
1919
use rustc::hir::map::DefKey;
2020
use rustc::hir::svh::Svh;
2121
use rustc::middle::cstore::ExternCrate;
22-
use rustc::session::config::PanicStrategy;
22+
use rustc_back::PanicStrategy;
2323
use rustc_data_structures::indexed_vec::IndexVec;
2424
use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet, DefIdMap, FnvHashSet};
2525

src/librustc_metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12931293
triple: tcx.sess.opts.target_triple.clone(),
12941294
hash: link_meta.crate_hash,
12951295
disambiguator: tcx.sess.local_crate_disambiguator().to_string(),
1296-
panic_strategy: tcx.sess.opts.cg.panic.clone(),
1296+
panic_strategy: tcx.sess.panic_strategy(),
12971297
plugin_registrar_fn: tcx.sess.plugin_registrar_fn.get().map(|id| {
12981298
tcx.map.local_def_id(id).index
12991299
}),

src/librustc_metadata/schema.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc::middle::cstore::{LinkagePreference, NativeLibraryKind};
1818
use rustc::middle::lang_items;
1919
use rustc::mir;
2020
use rustc::ty::{self, Ty};
21-
use rustc::session::config::PanicStrategy;
21+
use rustc_back::PanicStrategy;
2222

2323
use rustc_serialize as serialize;
2424
use syntax::{ast, attr};

0 commit comments

Comments
 (0)