Browse Source

add option to enable/disable -Wextra on gcc/clang

wintest
gnzlbg 7 years ago
parent
commit
bda154096e
  1. 50
      src/lib.rs
  2. 26
      tests/test.rs

50
src/lib.rs

@ -117,6 +117,7 @@ pub struct Build {
static_flag: Option<bool>,
warnings_into_errors: bool,
warnings: bool,
extra_warnings: bool,
}
/// Represents the types of errors that may occur while using cc-rs.
@ -218,13 +219,18 @@ impl ToolFamily {
}
/// What the flags to enable all warnings
fn warnings_flags(&self) -> &'static [&'static str] {
static MSVC_FLAGS: &'static [&'static str] = &["/W4"];
static GNU_CLANG_FLAGS: &'static [&'static str] = &["-Wall", "-Wextra"];
fn warnings_flags(&self) -> &'static str {
match *self {
ToolFamily::Msvc => "/W4",
ToolFamily::Gnu | ToolFamily::Clang => "-Wall",
}
}
/// What the flags to enable extra warnings
fn extra_warnings_flags(&self) -> &'static str {
match *self {
ToolFamily::Msvc => &MSVC_FLAGS,
ToolFamily::Gnu | ToolFamily::Clang => &GNU_CLANG_FLAGS,
ToolFamily::Msvc => "",
ToolFamily::Gnu | ToolFamily::Clang => "-Wextra",
}
}
@ -304,6 +310,7 @@ impl Build {
pic: None,
static_crt: None,
warnings: true,
extra_warnings: true,
warnings_into_errors: false,
}
}
@ -573,6 +580,29 @@ impl Build {
/// ```
pub fn warnings(&mut self, warnings: bool) -> &mut Build {
self.warnings = warnings;
self.extra_warnings = warnings;
self
}
/// Set extra warnings flags.
///
/// Adds some flags:
/// - nothing for MSVC.
/// - "-Wextra" for GNU and Clang.
///
/// Enabled by default.
///
/// # Example
///
/// ```no_run
/// // Disables -Wextra, -Wall remains enabled:
/// cc::Build::new()
/// .file("src/foo.c")
/// .extra_warnings(false)
/// .compile("libfoo.a");
/// ```
pub fn extra_warnings(&mut self, warnings: bool) -> &mut Build {
self.extra_warnings = warnings;
self
}
@ -1233,9 +1263,13 @@ impl Build {
}
if self.warnings {
for flag in cmd.family.warnings_flags().iter() {
cmd.push_cc_arg(flag.into());
}
let wflags = cmd.family.warnings_flags().into();
cmd.push_cc_arg(wflags);
}
if self.extra_warnings {
let wflags = cmd.family.extra_warnings_flags().into();
cmd.push_cc_arg(wflags);
}
for flag in self.flags.iter() {

26
tests/test.rs

@ -71,6 +71,32 @@ fn gnu_warnings() {
test.cmd(0).must_have("-Wall").must_have("-Wextra");
}
#[test]
fn gnu_extra_warnings0() {
let test = Test::gnu();
test.gcc()
.warnings(true)
.extra_warnings(false)
.flag("-Wno-missing-field-initializers")
.file("foo.c")
.compile("foo");
test.cmd(0).must_have("-Wall").must_not_have("-Wextra");
}
#[test]
fn gnu_extra_warnings1() {
let test = Test::gnu();
test.gcc()
.warnings(false)
.extra_warnings(true)
.flag("-Wno-missing-field-initializers")
.file("foo.c")
.compile("foo");
test.cmd(0).must_not_have("-Wall").must_have("-Wextra");
}
#[test]
fn gnu_warnings_overridable() {
let test = Test::gnu();

Loading…
Cancel
Save