Skip to content

Commit

Permalink
Merge pull request rust-lang#212 from alexcrichton/merge
Browse files Browse the repository at this point in the history
Merge a few PRs together
  • Loading branch information
alexcrichton committed Mar 6, 2016
2 parents 88396ac + dd3b423 commit e19309c
Show file tree
Hide file tree
Showing 28 changed files with 828 additions and 570 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ script:
else
cargo build;
cargo build --no-default-features;
rustc ci/style.rs && ./style src;
fi
os:
- linux
Expand Down
204 changes: 204 additions & 0 deletions ci/style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
//! Simple script to verify the coding style of this library
//!
//! ## How to run
//!
//! The first argument to this script is the directory to run on, so running
//! this script should be as simple as:
//!
//! ```notrust
//! rustc ci/style.rs
//! ./style src
//! ```
//!
//! ## Guidelines
//!
//! The current style is:
//!
//! * No trailing whitespace
//! * No tabs
//! * 80-character lines
//! * `extern` instead of `extern "C"`
//! * Specific module layout:
//! 1. use directives
//! 2. typedefs
//! 3. structs
//! 4. constants
//! 5. f! { ... } functions
//! 6. extern functions
//! 7. modules + pub use
//!
//! Things not verified:
//!
//! * alignment
//! * 4-space tabs
//! * leading colons on paths

use std::env;
use std::fs;
use std::io::prelude::*;
use std::path::Path;

macro_rules! t {
($e:expr) => (match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {}", stringify!($e), e),
})
}

fn main() {
let arg = env::args().skip(1).next().unwrap_or(".".to_string());

let mut errors = Errors { errs: false };
walk(Path::new(&arg), &mut errors);

if errors.errs {
panic!("found some lint errors");
} else {
println!("good style!");
}
}

fn walk(path: &Path, err: &mut Errors) {
for entry in t!(path.read_dir()).map(|e| t!(e)) {
let path = entry.path();
if t!(entry.file_type()).is_dir() {
walk(&path, err);
continue
}

let name = entry.file_name().into_string().unwrap();
match &name[..] {
n if !n.ends_with(".rs") => continue,

"dox.rs" |
"lib.rs" |
"macros.rs" => continue,

_ => {}
}

let mut contents = String::new();
t!(t!(fs::File::open(&path)).read_to_string(&mut contents));

check_style(&contents, &path, err);
}
}

struct Errors {
errs: bool,
}

#[derive(Clone, Copy, PartialEq)]
enum State {
Start,
Imports,
Typedefs,
Structs,
Constants,
FunctionDefinitions,
Functions,
Modules,
}

fn check_style(file: &str, path: &Path, err: &mut Errors) {
let mut state = State::Start;
let mut s_macros = 0;
let mut f_macros = 0;
let mut prev_blank = false;

for (i, line) in file.lines().enumerate() {
if line == "" {
if prev_blank {
err.error(path, i, "double blank line");
}
prev_blank = true;
} else {
prev_blank = false;
}
if line != line.trim_right() {
err.error(path, i, "trailing whitespace");
}
if line.contains("\t") {
err.error(path, i, "tab character");
}
if line.len() > 80 {
err.error(path, i, "line longer than 80 chars");
}
if line.contains("extern \"C\"") {
err.error(path, i, "use `extern` instead of `extern \"C\"");
}
if line.contains("#[cfg(") && !line.contains(" if ") {
if state != State::Structs {
err.error(path, i, "use cfg_if! and submodules \
instead of #[cfg]");
}
}

let line = line.trim_left();
let is_pub = line.starts_with("pub ");
let line = if is_pub {&line[4..]} else {line};

let line_state = if line.starts_with("use ") {
if is_pub {
State::Modules
} else {
State::Imports
}
} else if line.starts_with("const ") {
State::Constants
} else if line.starts_with("type ") {
State::Typedefs
} else if line.starts_with("s! {") {
s_macros += 1;
State::Structs
} else if line.starts_with("f! {") {
f_macros += 1;
State::FunctionDefinitions
} else if line.starts_with("extern ") {
State::Functions
} else if line.starts_with("mod ") {
State::Modules
} else {
continue
};

if state as usize > line_state as usize {
err.error(path, i, &format!("{} found after {} when \
it belongs before",
line_state.desc(), state.desc()));
}

if f_macros == 2 {
f_macros += 1;
err.error(path, i, "multiple f! macros in one module");
}
if s_macros == 2 {
s_macros += 1;
err.error(path, i, "multiple s! macros in one module");
}

state = line_state;
}
}

impl State {
fn desc(&self) -> &str {
match *self {
State::Start => "start",
State::Imports => "import",
State::Typedefs => "typedef",
State::Structs => "struct",
State::Constants => "constant",
State::FunctionDefinitions => "function definition",
State::Functions => "extern function",
State::Modules => "module",
}
}
}

impl Errors {
fn error(&mut self, path: &Path, line: usize, msg: &str) {
self.errs = true;
println!("{}:{} - {}", path.display(), line + 1, msg);
}
}
8 changes: 4 additions & 4 deletions src/unix/bsd/apple/b32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
pub type c_long = i32;
pub type c_ulong = u32;

pub const __PTHREAD_MUTEX_SIZE__: usize = 40;
pub const __PTHREAD_COND_SIZE__: usize = 24;
pub const __PTHREAD_RWLOCK_SIZE__: usize = 124;

s! {
pub struct pthread_attr_t {
__sig: c_long,
__opaque: [::c_char; 36]
}
}

pub const __PTHREAD_MUTEX_SIZE__: usize = 40;
pub const __PTHREAD_COND_SIZE__: usize = 24;
pub const __PTHREAD_RWLOCK_SIZE__: usize = 124;
8 changes: 4 additions & 4 deletions src/unix/bsd/apple/b64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
pub type c_long = i64;
pub type c_ulong = u64;

pub const __PTHREAD_MUTEX_SIZE__: usize = 56;
pub const __PTHREAD_COND_SIZE__: usize = 40;
pub const __PTHREAD_RWLOCK_SIZE__: usize = 192;

s! {
pub struct pthread_attr_t {
__sig: c_long,
__opaque: [::c_char; 56]
}
}

pub const __PTHREAD_MUTEX_SIZE__: usize = 56;
pub const __PTHREAD_COND_SIZE__: usize = 40;
pub const __PTHREAD_RWLOCK_SIZE__: usize = 192;
10 changes: 5 additions & 5 deletions src/unix/bsd/freebsdlike/dragonfly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ pub type c_ulong = u64;
pub type time_t = i64;
pub type suseconds_t = i64;

pub type uuid_t = ::uuid;

pub type fsblkcnt_t = u64;
pub type fsfilcnt_t = u64;

s! {
pub struct dirent {
pub d_fileno: ::ino_t,
Expand Down Expand Up @@ -75,11 +80,6 @@ s! {
}
}

pub type uuid_t = ::uuid;

pub type fsblkcnt_t = u64;
pub type fsfilcnt_t = u64;

pub const RAND_MAX: ::c_int = 0x7fff_ffff;
pub const PTHREAD_STACK_MIN: ::size_t = 1024;
pub const KERN_PROC_PATHNAME: ::c_int = 9;
Expand Down
13 changes: 9 additions & 4 deletions src/unix/bsd/freebsdlike/freebsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pub type ino_t = u32;
pub type nlink_t = u16;
pub type blksize_t = u32;

pub type fsblkcnt_t = ::uint64_t;
pub type fsfilcnt_t = ::uint64_t;

s! {
pub struct dirent {
pub d_fileno: u32,
Expand All @@ -28,9 +31,6 @@ s! {
}
}

pub type fsblkcnt_t = ::uint64_t;
pub type fsfilcnt_t = ::uint64_t;

pub const RAND_MAX: ::c_int = 0x7fff_fffd;
pub const PTHREAD_STACK_MIN: ::size_t = 2048;
pub const KERN_PROC_PATHNAME: ::c_int = 12;
Expand All @@ -57,6 +57,9 @@ pub const POSIX_FADV_WILLNEED: ::c_int = 3;
pub const POSIX_FADV_DONTNEED: ::c_int = 4;
pub const POSIX_FADV_NOREUSE: ::c_int = 5;

pub const MADV_PROTECT: ::c_int = 10;
pub const RUSAGE_THREAD: ::c_int = 1;

extern {
pub fn __error() -> *mut ::c_int;

Expand All @@ -70,7 +73,9 @@ extern {
pub fn posix_fadvise(fd: ::c_int, offset: ::off_t, len: ::off_t,
advise: ::c_int) -> ::c_int;
pub fn mkostemp(template: *mut ::c_char, flags: ::c_int) -> ::c_int;
pub fn mkostemps(template: *mut ::c_char, suffixlen: ::c_int, flags: ::c_int) -> ::c_int;
pub fn mkostemps(template: *mut ::c_char,
suffixlen: ::c_int,
flags: ::c_int) -> ::c_int;
}

cfg_if! {
Expand Down
12 changes: 6 additions & 6 deletions src/unix/bsd/freebsdlike/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,6 @@ pub const RLIM_INFINITY: rlim_t = 0x7fff_ffff_ffff_ffff;

pub const RUSAGE_SELF: ::c_int = 0;
pub const RUSAGE_CHILDREN: ::c_int = -1;
#[cfg(not(target_os = "dragonfly"))]
pub const RUSAGE_THREAD: ::c_int = 1;

pub const MADV_NORMAL: ::c_int = 0;
pub const MADV_RANDOM: ::c_int = 1;
Expand All @@ -398,8 +396,6 @@ pub const MADV_NOSYNC: ::c_int = 6;
pub const MADV_AUTOSYNC: ::c_int = 7;
pub const MADV_NOCORE: ::c_int = 8;
pub const MADV_CORE: ::c_int = 9;
#[cfg(not(target_os = "dragonfly"))]
pub const MADV_PROTECT: ::c_int = 10;

pub const MINCORE_INCORE: ::c_int = 0x1;
pub const MINCORE_REFERENCED: ::c_int = 0x2;
Expand Down Expand Up @@ -578,9 +574,13 @@ extern {
newlen: ::size_t)
-> ::c_int;
pub fn pthread_set_name_np(tid: ::pthread_t, name: *const ::c_char);
pub fn sched_setscheduler(pid: ::pid_t, policy: ::c_int, param: *const sched_param) -> ::c_int;
pub fn sched_setscheduler(pid: ::pid_t,
policy: ::c_int,
param: *const sched_param) -> ::c_int;
pub fn sched_getscheduler(pid: ::pid_t) -> ::c_int;
pub fn memrchr(cx: *const ::c_void, c: ::c_int, n: ::size_t) -> *mut ::c_void;
pub fn memrchr(cx: *const ::c_void,
c: ::c_int,
n: ::size_t) -> *mut ::c_void;
pub fn sendfile(fd: ::c_int,
s: ::c_int,
offset: ::off_t,
Expand Down
9 changes: 7 additions & 2 deletions src/unix/bsd/openbsdlike/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,15 @@ extern {
pub fn __errno() -> *mut ::c_int;
pub fn shm_open(name: *const ::c_char, oflag: ::c_int, mode: ::mode_t)
-> ::c_int;
pub fn memrchr(cx: *const ::c_void, c: ::c_int, n: ::size_t) -> *mut ::c_void;
pub fn memrchr(cx: *const ::c_void,
c: ::c_int,
n: ::size_t) -> *mut ::c_void;
pub fn mkostemp(template: *mut ::c_char, flags: ::c_int) -> ::c_int;
pub fn mkostemps(template: *mut ::c_char, suffixlen: ::c_int, flags: ::c_int) -> ::c_int;
pub fn mkostemps(template: *mut ::c_char,
suffixlen: ::c_int,
flags: ::c_int) -> ::c_int;
pub fn futimens(fd: ::c_int, times: *const ::timespec) -> ::c_int;
pub fn fdatasync(fd: ::c_int) -> ::c_int;
}

cfg_if! {
Expand Down
Loading

0 comments on commit e19309c

Please sign in to comment.