Skip to content

Commit 16c1ed5

Browse files
committed
---
yaml --- r: 3910 b: refs/heads/master c: 8d2706c h: refs/heads/master v: v3
1 parent adc786a commit 16c1ed5

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 7376e751aeed046c29ff4b892b359002de145d2d
2+
refs/heads/master: 8d2706cca8c26d5e74b4c0111f89d49d61029875

trunk/src/comp/driver/rustc.rs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,19 @@ fn default_configuration(session::session sess, str argv0, str input) ->
6363
fn build_configuration(session::session sess, str argv0,
6464
str input) -> ast::crate_cfg {
6565
// Combine the configuration requested by the session (command line) with
66-
// some default configuration items
67-
ret sess.get_opts().cfg + default_configuration(sess, argv0, input);
66+
// some default and generated configuration items
67+
auto default_cfg = default_configuration(sess, argv0, input);
68+
auto user_cfg = sess.get_opts().cfg;
69+
auto gen_cfg = {
70+
// If the user wants a test runner, then add the test cfg
71+
if (sess.get_opts().test
72+
&& !attr::contains_name(user_cfg, "test")) {
73+
~[attr::mk_word_item("test")]
74+
} else {
75+
~[]
76+
}
77+
};
78+
ret user_cfg + gen_cfg + default_cfg;
6879
}
6980

7081
// Convert strings provided as --cfg [cfgspec] into a crate_cfg
@@ -374,9 +385,8 @@ fn parse_pretty(session::session sess, &str name) -> pp_mode {
374385
"`identified`");
375386
}
376387

377-
fn main(vec[str] args) {
378-
auto opts =
379-
[optflag("h"), optflag("help"), optflag("v"), optflag("version"),
388+
fn opts() -> vec[getopts::opt] {
389+
ret [optflag("h"), optflag("help"), optflag("v"), optflag("version"),
380390
optflag("glue"), optflag("emit-llvm"), optflagopt("pretty"),
381391
optflag("ls"), optflag("parse-only"), optflag("O"),
382392
optopt("OptLevel"), optmulti("L"), optflag("S"),
@@ -385,10 +395,13 @@ fn main(vec[str] args) {
385395
optflag("time-llvm-passes"), optflag("no-typestate"),
386396
optflag("noverify"), optmulti("cfg"), optflag("test"),
387397
optflag("lib"), optflag("static")];
398+
}
399+
400+
fn main(vec[str] args) {
388401
auto binary = vec::shift[str](args);
389402
auto binary_dir = fs::dirname(binary);
390403
auto match =
391-
alt (getopts::getopts(args, opts)) {
404+
alt (getopts::getopts(args, opts())) {
392405
case (getopts::success(?m)) { m }
393406
case (getopts::failure(?f)) {
394407
log_err #fmt("error: %s", getopts::fail_str(f));
@@ -572,6 +585,40 @@ fn main(vec[str] args) {
572585
run::run_program("rm", [saved_out_filename + ".o"]);
573586
}
574587
}
588+
589+
#[cfg(test)]
590+
mod test {
591+
592+
import std::ivec;
593+
594+
// When the user supplies --test we should implicitly supply --cfg test
595+
#[test]
596+
fn test_switch_implies_cfg_test() {
597+
auto match = alt (getopts::getopts(["--test"], opts())) {
598+
getopts::success(?m) { m }
599+
};
600+
auto sessopts = build_session_options("whatever", match, "whatever");
601+
auto sess = build_session(sessopts);
602+
auto cfg = build_configuration(sess, "whatever", "whatever");
603+
assert attr::contains_name(cfg, "test");
604+
}
605+
606+
// When the user supplies --test and --cfg test, don't implicitly add
607+
// another --cfg test
608+
#[test]
609+
fn test_switch_implies_cfg_test_unless_cfg_test() {
610+
auto match = alt (getopts::getopts(["--test",
611+
"--cfg=test"], opts())) {
612+
getopts::success(?m) { m }
613+
};
614+
auto sessopts = build_session_options("whatever", match, "whatever");
615+
auto sess = build_session(sessopts);
616+
auto cfg = build_configuration(sess, "whatever", "whatever");
617+
auto test_items = attr::find_meta_items_by_name(cfg, "test");
618+
assert ivec::len(test_items) == 1u;
619+
}
620+
}
621+
575622
// Local Variables:
576623
// mode: rust
577624
// fill-column: 78;

trunk/src/comp/front/attr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export find_linkage_metas;
1414
export find_attrs_by_name;
1515
export find_meta_items_by_name;
1616
export contains;
17+
export contains_name;
1718
export sort_meta_items;
1819
export remove_meta_items_by_name;
1920
export require_unique_names;
@@ -145,6 +146,11 @@ fn contains(&(@ast::meta_item)[] haystack, @ast::meta_item needle) -> bool {
145146
ret false;
146147
}
147148

149+
fn contains_name(&(@ast::meta_item)[] metas, ast::ident name) -> bool {
150+
auto matches = find_meta_items_by_name(metas, name);
151+
ret ivec::len(matches) > 0u;
152+
}
153+
148154
// FIXME: This needs to sort by meta_item variant in addition to the item name
149155
fn sort_meta_items(&(@ast::meta_item)[] items) -> (@ast::meta_item)[] {
150156
fn lteq(&@ast::meta_item ma, &@ast::meta_item mb) -> bool {

0 commit comments

Comments
 (0)