From 2ba6e98f39501dd7fd5568bcd31c9b3dba3dd58d Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Thu, 5 Oct 2017 18:34:43 +0100 Subject: [PATCH] Add custom flags after -Wall Right now we add them before, and we add -Wall by default. This allows users to manually turn off specific warnings, without having to resort to a dance of: ``` .warnings(false) .flag("-Wall") .flag("-Wno-foo") ``` This strikes a good balance of keeping -Wall on by default, while minimising the surprising experience observed in https://github.com/alexcrichton/cc-rs/issues/235. --- src/lib.rs | 12 ++++++------ tests/support/mod.rs | 10 ++++++++++ tests/test.rs | 13 +++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 591678f..681d1db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1093,6 +1093,12 @@ impl Build { cmd.args.push(directory.into()); } + if self.warnings { + for flag in cmd.family.warnings_flags().iter() { + cmd.args.push(flag.into()); + } + } + for flag in self.flags.iter() { cmd.args.push(flag.into()); } @@ -1112,12 +1118,6 @@ impl Build { } } - if self.warnings { - for flag in cmd.family.warnings_flags().iter() { - cmd.args.push(flag.into()); - } - } - if self.warnings_into_errors { cmd.args.push(cmd.family.warnings_to_errors_flag().into()); } diff --git a/tests/support/mod.rs b/tests/support/mod.rs index 4288546..bb56bf3 100644 --- a/tests/support/mod.rs +++ b/tests/support/mod.rs @@ -109,4 +109,14 @@ impl Execution { pub fn has(&self, p: &OsStr) -> bool { self.args.iter().any(|arg| OsStr::new(arg) == p) } + + pub fn must_have_in_order(&self, before: &str, after: &str) -> &Execution { + let before_position = self.args.iter().rposition(|x| OsStr::new(x) == OsStr::new(before)); + let after_position = self.args.iter().rposition(|x| OsStr::new(x) == OsStr::new(after)); + match (before_position, after_position) { + (Some(b), Some(a)) if b < a => {}, + (b, a) => { panic!("{:?} (last position: {:?}) did not appear before {:?} (last position: {:?})", before, b, after, a) }, + }; + self + } } diff --git a/tests/test.rs b/tests/test.rs index b8f1abf..7e5a28d 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -77,6 +77,7 @@ fn gnu_warnings() { let test = Test::gnu(); test.gcc() .warnings(true) + .flag("-Wno-missing-field-initializers") .file("foo.c") .compile("foo"); @@ -84,6 +85,18 @@ fn gnu_warnings() { .must_have("-Wextra"); } +#[test] +fn gnu_warnings_overridable() { + let test = Test::gnu(); + test.gcc() + .warnings(true) + .flag("-Wno-missing-field-initializers") + .file("foo.c") + .compile("foo"); + + test.cmd(0).must_have_in_order("-Wall", "-Wno-missing-field-initializers"); +} + #[test] fn gnu_x86_64() { for vendor in &["unknown-linux-gnu", "apple-darwin"] {