Skip to content

Commit e19309c

Browse files
committed
Merge pull request #212 from alexcrichton/merge
Merge a few PRs together
2 parents 88396ac + dd3b423 commit e19309c

File tree

28 files changed

+828
-570
lines changed

28 files changed

+828
-570
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ script:
1616
else
1717
cargo build;
1818
cargo build --no-default-features;
19+
rustc ci/style.rs && ./style src;
1920
fi
2021
os:
2122
- linux

ci/style.rs

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
//! Simple script to verify the coding style of this library
2+
//!
3+
//! ## How to run
4+
//!
5+
//! The first argument to this script is the directory to run on, so running
6+
//! this script should be as simple as:
7+
//!
8+
//! ```notrust
9+
//! rustc ci/style.rs
10+
//! ./style src
11+
//! ```
12+
//!
13+
//! ## Guidelines
14+
//!
15+
//! The current style is:
16+
//!
17+
//! * No trailing whitespace
18+
//! * No tabs
19+
//! * 80-character lines
20+
//! * `extern` instead of `extern "C"`
21+
//! * Specific module layout:
22+
//! 1. use directives
23+
//! 2. typedefs
24+
//! 3. structs
25+
//! 4. constants
26+
//! 5. f! { ... } functions
27+
//! 6. extern functions
28+
//! 7. modules + pub use
29+
//!
30+
//! Things not verified:
31+
//!
32+
//! * alignment
33+
//! * 4-space tabs
34+
//! * leading colons on paths
35+
36+
use std::env;
37+
use std::fs;
38+
use std::io::prelude::*;
39+
use std::path::Path;
40+
41+
macro_rules! t {
42+
($e:expr) => (match $e {
43+
Ok(e) => e,
44+
Err(e) => panic!("{} failed with {}", stringify!($e), e),
45+
})
46+
}
47+
48+
fn main() {
49+
let arg = env::args().skip(1).next().unwrap_or(".".to_string());
50+
51+
let mut errors = Errors { errs: false };
52+
walk(Path::new(&arg), &mut errors);
53+
54+
if errors.errs {
55+
panic!("found some lint errors");
56+
} else {
57+
println!("good style!");
58+
}
59+
}
60+
61+
fn walk(path: &Path, err: &mut Errors) {
62+
for entry in t!(path.read_dir()).map(|e| t!(e)) {
63+
let path = entry.path();
64+
if t!(entry.file_type()).is_dir() {
65+
walk(&path, err);
66+
continue
67+
}
68+
69+
let name = entry.file_name().into_string().unwrap();
70+
match &name[..] {
71+
n if !n.ends_with(".rs") => continue,
72+
73+
"dox.rs" |
74+
"lib.rs" |
75+
"macros.rs" => continue,
76+
77+
_ => {}
78+
}
79+
80+
let mut contents = String::new();
81+
t!(t!(fs::File::open(&path)).read_to_string(&mut contents));
82+
83+
check_style(&contents, &path, err);
84+
}
85+
}
86+
87+
struct Errors {
88+
errs: bool,
89+
}
90+
91+
#[derive(Clone, Copy, PartialEq)]
92+
enum State {
93+
Start,
94+
Imports,
95+
Typedefs,
96+
Structs,
97+
Constants,
98+
FunctionDefinitions,
99+
Functions,
100+
Modules,
101+
}
102+
103+
fn check_style(file: &str, path: &Path, err: &mut Errors) {
104+
let mut state = State::Start;
105+
let mut s_macros = 0;
106+
let mut f_macros = 0;
107+
let mut prev_blank = false;
108+
109+
for (i, line) in file.lines().enumerate() {
110+
if line == "" {
111+
if prev_blank {
112+
err.error(path, i, "double blank line");
113+
}
114+
prev_blank = true;
115+
} else {
116+
prev_blank = false;
117+
}
118+
if line != line.trim_right() {
119+
err.error(path, i, "trailing whitespace");
120+
}
121+
if line.contains("\t") {
122+
err.error(path, i, "tab character");
123+
}
124+
if line.len() > 80 {
125+
err.error(path, i, "line longer than 80 chars");
126+
}
127+
if line.contains("extern \"C\"") {
128+
err.error(path, i, "use `extern` instead of `extern \"C\"");
129+
}
130+
if line.contains("#[cfg(") && !line.contains(" if ") {
131+
if state != State::Structs {
132+
err.error(path, i, "use cfg_if! and submodules \
133+
instead of #[cfg]");
134+
}
135+
}
136+
137+
let line = line.trim_left();
138+
let is_pub = line.starts_with("pub ");
139+
let line = if is_pub {&line[4..]} else {line};
140+
141+
let line_state = if line.starts_with("use ") {
142+
if is_pub {
143+
State::Modules
144+
} else {
145+
State::Imports
146+
}
147+
} else if line.starts_with("const ") {
148+
State::Constants
149+
} else if line.starts_with("type ") {
150+
State::Typedefs
151+
} else if line.starts_with("s! {") {
152+
s_macros += 1;
153+
State::Structs
154+
} else if line.starts_with("f! {") {
155+
f_macros += 1;
156+
State::FunctionDefinitions
157+
} else if line.starts_with("extern ") {
158+
State::Functions
159+
} else if line.starts_with("mod ") {
160+
State::Modules
161+
} else {
162+
continue
163+
};
164+
165+
if state as usize > line_state as usize {
166+
err.error(path, i, &format!("{} found after {} when \
167+
it belongs before",
168+
line_state.desc(), state.desc()));
169+
}
170+
171+
if f_macros == 2 {
172+
f_macros += 1;
173+
err.error(path, i, "multiple f! macros in one module");
174+
}
175+
if s_macros == 2 {
176+
s_macros += 1;
177+
err.error(path, i, "multiple s! macros in one module");
178+
}
179+
180+
state = line_state;
181+
}
182+
}
183+
184+
impl State {
185+
fn desc(&self) -> &str {
186+
match *self {
187+
State::Start => "start",
188+
State::Imports => "import",
189+
State::Typedefs => "typedef",
190+
State::Structs => "struct",
191+
State::Constants => "constant",
192+
State::FunctionDefinitions => "function definition",
193+
State::Functions => "extern function",
194+
State::Modules => "module",
195+
}
196+
}
197+
}
198+
199+
impl Errors {
200+
fn error(&mut self, path: &Path, line: usize, msg: &str) {
201+
self.errs = true;
202+
println!("{}:{} - {}", path.display(), line + 1, msg);
203+
}
204+
}

src/unix/bsd/apple/b32.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
pub type c_long = i32;
44
pub type c_ulong = u32;
55

6-
pub const __PTHREAD_MUTEX_SIZE__: usize = 40;
7-
pub const __PTHREAD_COND_SIZE__: usize = 24;
8-
pub const __PTHREAD_RWLOCK_SIZE__: usize = 124;
9-
106
s! {
117
pub struct pthread_attr_t {
128
__sig: c_long,
139
__opaque: [::c_char; 36]
1410
}
1511
}
12+
13+
pub const __PTHREAD_MUTEX_SIZE__: usize = 40;
14+
pub const __PTHREAD_COND_SIZE__: usize = 24;
15+
pub const __PTHREAD_RWLOCK_SIZE__: usize = 124;

src/unix/bsd/apple/b64.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
pub type c_long = i64;
44
pub type c_ulong = u64;
55

6-
pub const __PTHREAD_MUTEX_SIZE__: usize = 56;
7-
pub const __PTHREAD_COND_SIZE__: usize = 40;
8-
pub const __PTHREAD_RWLOCK_SIZE__: usize = 192;
9-
106
s! {
117
pub struct pthread_attr_t {
128
__sig: c_long,
139
__opaque: [::c_char; 56]
1410
}
1511
}
12+
13+
pub const __PTHREAD_MUTEX_SIZE__: usize = 56;
14+
pub const __PTHREAD_COND_SIZE__: usize = 40;
15+
pub const __PTHREAD_RWLOCK_SIZE__: usize = 192;

src/unix/bsd/freebsdlike/dragonfly/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ pub type c_ulong = u64;
88
pub type time_t = i64;
99
pub type suseconds_t = i64;
1010

11+
pub type uuid_t = ::uuid;
12+
13+
pub type fsblkcnt_t = u64;
14+
pub type fsfilcnt_t = u64;
15+
1116
s! {
1217
pub struct dirent {
1318
pub d_fileno: ::ino_t,
@@ -75,11 +80,6 @@ s! {
7580
}
7681
}
7782

78-
pub type uuid_t = ::uuid;
79-
80-
pub type fsblkcnt_t = u64;
81-
pub type fsfilcnt_t = u64;
82-
8383
pub const RAND_MAX: ::c_int = 0x7fff_ffff;
8484
pub const PTHREAD_STACK_MIN: ::size_t = 1024;
8585
pub const KERN_PROC_PATHNAME: ::c_int = 9;

src/unix/bsd/freebsdlike/freebsd/mod.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ pub type ino_t = u32;
44
pub type nlink_t = u16;
55
pub type blksize_t = u32;
66

7+
pub type fsblkcnt_t = ::uint64_t;
8+
pub type fsfilcnt_t = ::uint64_t;
9+
710
s! {
811
pub struct dirent {
912
pub d_fileno: u32,
@@ -28,9 +31,6 @@ s! {
2831
}
2932
}
3033

31-
pub type fsblkcnt_t = ::uint64_t;
32-
pub type fsfilcnt_t = ::uint64_t;
33-
3434
pub const RAND_MAX: ::c_int = 0x7fff_fffd;
3535
pub const PTHREAD_STACK_MIN: ::size_t = 2048;
3636
pub const KERN_PROC_PATHNAME: ::c_int = 12;
@@ -57,6 +57,9 @@ pub const POSIX_FADV_WILLNEED: ::c_int = 3;
5757
pub const POSIX_FADV_DONTNEED: ::c_int = 4;
5858
pub const POSIX_FADV_NOREUSE: ::c_int = 5;
5959

60+
pub const MADV_PROTECT: ::c_int = 10;
61+
pub const RUSAGE_THREAD: ::c_int = 1;
62+
6063
extern {
6164
pub fn __error() -> *mut ::c_int;
6265

@@ -70,7 +73,9 @@ extern {
7073
pub fn posix_fadvise(fd: ::c_int, offset: ::off_t, len: ::off_t,
7174
advise: ::c_int) -> ::c_int;
7275
pub fn mkostemp(template: *mut ::c_char, flags: ::c_int) -> ::c_int;
73-
pub fn mkostemps(template: *mut ::c_char, suffixlen: ::c_int, flags: ::c_int) -> ::c_int;
76+
pub fn mkostemps(template: *mut ::c_char,
77+
suffixlen: ::c_int,
78+
flags: ::c_int) -> ::c_int;
7479
}
7580

7681
cfg_if! {

src/unix/bsd/freebsdlike/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,6 @@ pub const RLIM_INFINITY: rlim_t = 0x7fff_ffff_ffff_ffff;
385385

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

391389
pub const MADV_NORMAL: ::c_int = 0;
392390
pub const MADV_RANDOM: ::c_int = 1;
@@ -398,8 +396,6 @@ pub const MADV_NOSYNC: ::c_int = 6;
398396
pub const MADV_AUTOSYNC: ::c_int = 7;
399397
pub const MADV_NOCORE: ::c_int = 8;
400398
pub const MADV_CORE: ::c_int = 9;
401-
#[cfg(not(target_os = "dragonfly"))]
402-
pub const MADV_PROTECT: ::c_int = 10;
403399

404400
pub const MINCORE_INCORE: ::c_int = 0x1;
405401
pub const MINCORE_REFERENCED: ::c_int = 0x2;
@@ -578,9 +574,13 @@ extern {
578574
newlen: ::size_t)
579575
-> ::c_int;
580576
pub fn pthread_set_name_np(tid: ::pthread_t, name: *const ::c_char);
581-
pub fn sched_setscheduler(pid: ::pid_t, policy: ::c_int, param: *const sched_param) -> ::c_int;
577+
pub fn sched_setscheduler(pid: ::pid_t,
578+
policy: ::c_int,
579+
param: *const sched_param) -> ::c_int;
582580
pub fn sched_getscheduler(pid: ::pid_t) -> ::c_int;
583-
pub fn memrchr(cx: *const ::c_void, c: ::c_int, n: ::size_t) -> *mut ::c_void;
581+
pub fn memrchr(cx: *const ::c_void,
582+
c: ::c_int,
583+
n: ::size_t) -> *mut ::c_void;
584584
pub fn sendfile(fd: ::c_int,
585585
s: ::c_int,
586586
offset: ::off_t,

src/unix/bsd/openbsdlike/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,15 @@ extern {
384384
pub fn __errno() -> *mut ::c_int;
385385
pub fn shm_open(name: *const ::c_char, oflag: ::c_int, mode: ::mode_t)
386386
-> ::c_int;
387-
pub fn memrchr(cx: *const ::c_void, c: ::c_int, n: ::size_t) -> *mut ::c_void;
387+
pub fn memrchr(cx: *const ::c_void,
388+
c: ::c_int,
389+
n: ::size_t) -> *mut ::c_void;
388390
pub fn mkostemp(template: *mut ::c_char, flags: ::c_int) -> ::c_int;
389-
pub fn mkostemps(template: *mut ::c_char, suffixlen: ::c_int, flags: ::c_int) -> ::c_int;
391+
pub fn mkostemps(template: *mut ::c_char,
392+
suffixlen: ::c_int,
393+
flags: ::c_int) -> ::c_int;
390394
pub fn futimens(fd: ::c_int, times: *const ::timespec) -> ::c_int;
395+
pub fn fdatasync(fd: ::c_int) -> ::c_int;
391396
}
392397

393398
cfg_if! {

0 commit comments

Comments
 (0)