Skip to content

Assert that code is properly rustfmted in CI #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,9 @@ cache:
before_install: . ./ci/before_install.sh

script:
- cargo test --features "$BINDGEN_FEATURES assert_no_dangling_items"
- cargo test --release --features "$BINDGEN_FEATURES assert_no_dangling_items"
- git add -A
- git diff @
- git diff-index --quiet HEAD
- cargo build --features "$BINDGEN_FEATURES docs_"
- cd tests/expectations
- cargo test
- cd ../../bindgen-integration
- cargo test --features "$BINDGEN_FEATURES"
- cargo test --release --features "$BINDGEN_FEATURES"
- ./ci/assert-rustfmt.sh
- BINDGEN_FEATURES="$BINDGEN_FEATURES" ./ci/assert-docs.sh
- BINDGEN_FEATURES="$BINDGEN_FEATURES" ./ci/test.sh

notifications:
webhooks: http://build.servo.org:54856/travis
20 changes: 11 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,26 @@ $ cargo test -p tests_expectations

## Automatic code formatting

There's a `rustfmt.toml` file in the repo. Ideally changes should be consistent
with the style, though that's not enforced right now.
We use [`rustfmt`](https://github.com/rust-lang-nursery/rustfmt) to enforce a
consistent code style across the whole `bindgen` code base. This is enforced in
CI, and your pull requests will get automatically rejected if you don't
re-format with the latest `rustfmt` before pushing.

[`rustfmt`](https://github.com/rust-lang-nursery/rustfmt) can catch and fix
automatically all the coding style issues it finds. In order to use it it
suffices to do:
You can install the latest version of `rustfmt` with this command:

```
$ cargo fmt
$ cargo install -f rustfmt
```

For it to work, you need to have `rustfmt` installed. To do so:
Ensure that `~/.cargo/bin` is on your path.

Once that is taken care of, you can (re)format all code by running this command:

```
$ cargo install rustfmt
$ cargo fmt
```

And ensure `~/.cargo/bin` is on your path.
The code style is described in the `rustfmt.toml` file in top level of the repo.

## Debug Logging

Expand Down
6 changes: 6 additions & 0 deletions ci/assert-docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

set -xeu
cd "$(dirname "$0")/.."

cargo build --features "$BINDGEN_FEATURES docs_"
8 changes: 8 additions & 0 deletions ci/assert-no-diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -xeu
cd "$(dirname "$0")/.."

git add -u
git diff @
git diff-index --quiet HEAD
16 changes: 16 additions & 0 deletions ci/assert-rustfmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -xeu
cd "$(dirname "$0")/.."

# Ensure we have the most up-to-date `rustfmt`.
cargo install -f rustfmt

# Run `rustfmt` on the crate! If `rustfmt` can't make a long line shorter, it
# prints an error and exits non-zero, so tell it to kindly shut its yapper and
# make sure it doesn't cause us to exit this whole script non-zero.
cargo fmt --quiet || true

# Exit non-zero if this resulted in any diffs.
./ci/assert-no-diff.sh

26 changes: 26 additions & 0 deletions ci/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

set -xeu
cd "$(dirname "$0")/.."

# Regenerate the test headers' bindings in debug and release modes, and assert
# that we always get the expected generated bindings.

cargo test --features "$BINDGEN_FEATURES assert_no_dangling_items"
./ci/assert-no-diff.sh

cargo test --release --features "$BINDGEN_FEATURES assert_no_dangling_items"
./ci/assert-no-diff.sh

# Now test the expectations' size and alignment tests.

pushd tests/expectations
cargo test
cargo test --release
popd

# And finally, test our example bindgen + build.rs integration template project.

cd bindgen-integration
cargo test --features "$BINDGEN_FEATURES"
cargo test --release --features "$BINDGEN_FEATURES"
2 changes: 1 addition & 1 deletion src/chooser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A public API for more fine-grained customization of bindgen behavior.

pub use ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue};
pub use ir::int::IntKind;
pub use ir::enum_ty::{EnumVariantValue, EnumVariantCustomBehavior};
use std::fmt;

/// A trait to allow configuring different kinds of types in different
Expand Down
110 changes: 70 additions & 40 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ impl Cursor {
/// Is the referent a fully specialized template specialization without any
/// remaining free template arguments?
pub fn is_fully_specialized_template(&self) -> bool {
self.is_template_specialization() && self.num_template_args().unwrap_or(0) > 0
self.is_template_specialization() &&
self.num_template_args().unwrap_or(0) > 0
}

/// Is the referent a template specialization that still has remaining free
Expand Down Expand Up @@ -349,13 +350,11 @@ impl Cursor {
pub fn contains_cursor(&self, kind: CXCursorKind) -> bool {
let mut found = false;

self.visit(|c| {
if c.kind() == kind {
found = true;
CXChildVisit_Break
} else {
CXChildVisit_Continue
}
self.visit(|c| if c.kind() == kind {
found = true;
CXChildVisit_Break
} else {
CXChildVisit_Continue
});

found
Expand Down Expand Up @@ -846,8 +845,8 @@ impl SourceLocation {
&mut col,
&mut off);
(File {
x: file,
},
x: file,
},
line as usize,
col as usize,
off as usize)
Expand Down Expand Up @@ -1282,17 +1281,32 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult {

fn print_cursor<S: AsRef<str>>(depth: isize, prefix: S, c: &Cursor) {
let prefix = prefix.as_ref();
print_indent(depth, format!(" {}kind = {}", prefix, kind_to_str(c.kind())));
print_indent(depth, format!(" {}spelling = \"{}\"", prefix, c.spelling()));
print_indent(depth,
format!(" {}kind = {}", prefix, kind_to_str(c.kind())));
print_indent(depth,
format!(" {}spelling = \"{}\"", prefix, c.spelling()));
print_indent(depth, format!(" {}location = {}", prefix, c.location()));
print_indent(depth, format!(" {}is-definition? {}", prefix, c.is_definition()));
print_indent(depth, format!(" {}is-declaration? {}", prefix, c.is_declaration()));
print_indent(depth, format!(" {}is-anonymous? {}", prefix, c.is_anonymous()));
print_indent(depth, format!(" {}is-inlined-function? {}", prefix, c.is_inlined_function()));
print_indent(depth,
format!(" {}is-definition? {}",
prefix,
c.is_definition()));
print_indent(depth,
format!(" {}is-declaration? {}",
prefix,
c.is_declaration()));
print_indent(depth,
format!(" {}is-anonymous? {}", prefix, c.is_anonymous()));
print_indent(depth,
format!(" {}is-inlined-function? {}",
prefix,
c.is_inlined_function()));

let templ_kind = c.template_kind();
if templ_kind != CXCursor_NoDeclFound {
print_indent(depth, format!(" {}template-kind = {}", prefix, kind_to_str(templ_kind)));
print_indent(depth,
format!(" {}template-kind = {}",
prefix,
kind_to_str(templ_kind)));
}
if let Some(usr) = c.usr() {
print_indent(depth, format!(" {}usr = \"{}\"", prefix, usr));
Expand All @@ -1301,38 +1315,53 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult {
print_indent(depth, format!(" {}number-of-args = {}", prefix, num));
}
if let Some(num) = c.num_template_args() {
print_indent(depth, format!(" {}number-of-template-args = {}", prefix, num));
print_indent(depth,
format!(" {}number-of-template-args = {}",
prefix,
num));
}
if let Some(width) = c.bit_width() {
print_indent(depth, format!(" {}bit-width = {}", prefix, width));
}
if let Some(ty) = c.enum_type() {
print_indent(depth, format!(" {}enum-type = {}", prefix, type_to_str(ty.kind())));
print_indent(depth,
format!(" {}enum-type = {}",
prefix,
type_to_str(ty.kind())));
}
if let Some(val) = c.enum_val_signed() {
print_indent(depth, format!(" {}enum-val = {}", prefix, val));
}
if let Some(ty) = c.typedef_type() {
print_indent(depth, format!(" {}typedef-type = {}", prefix, type_to_str(ty.kind())));
print_indent(depth,
format!(" {}typedef-type = {}",
prefix,
type_to_str(ty.kind())));
}

if let Some(refd) = c.referenced() {
if refd != *c {
println!();
print_cursor(depth, String::from(prefix) + "referenced.", &refd);
print_cursor(depth,
String::from(prefix) + "referenced.",
&refd);
}
}

let canonical = c.canonical();
if canonical != *c {
println!();
print_cursor(depth, String::from(prefix) + "canonical.", &canonical);
print_cursor(depth,
String::from(prefix) + "canonical.",
&canonical);
}

if let Some(specialized) = c.specialized() {
if specialized != *c {
println!();
print_cursor(depth, String::from(prefix) + "specialized.", &specialized);
print_cursor(depth,
String::from(prefix) + "specialized.",
&specialized);
}
}
}
Expand All @@ -1346,19 +1375,22 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult {
return;
}

print_indent(depth, format!(" {}spelling = \"{}\"", prefix, ty.spelling()));
let num_template_args = unsafe {
clang_Type_getNumTemplateArguments(ty.x)
};
print_indent(depth,
format!(" {}spelling = \"{}\"", prefix, ty.spelling()));
let num_template_args =
unsafe { clang_Type_getNumTemplateArguments(ty.x) };
if num_template_args >= 0 {
print_indent(depth, format!(" {}number-of-template-args = {}",
prefix,
num_template_args));
print_indent(depth,
format!(" {}number-of-template-args = {}",
prefix,
num_template_args));
}
if let Some(num) = ty.num_elements() {
print_indent(depth, format!(" {}number-of-elements = {}", prefix, num));
print_indent(depth,
format!(" {}number-of-elements = {}", prefix, num));
}
print_indent(depth, format!(" {}is-variadic? {}", prefix, ty.is_variadic()));
print_indent(depth,
format!(" {}is-variadic? {}", prefix, ty.is_variadic()));

let canonical = ty.canonical_type();
if canonical != *ty {
Expand Down Expand Up @@ -1449,14 +1481,12 @@ impl EvalResult {
// unexposed type. Our solution is to just flat out ban all
// `CXType_Unexposed` from evaluation.
let mut found_cant_eval = false;
cursor.visit(|c| {
if c.kind() == CXCursor_TypeRef &&
c.cur_type().kind() == CXType_Unexposed {
found_cant_eval = true;
CXChildVisit_Break
} else {
CXChildVisit_Recurse
}
cursor.visit(|c| if c.kind() == CXCursor_TypeRef &&
c.cur_type().kind() == CXType_Unexposed {
found_cant_eval = true;
CXChildVisit_Break
} else {
CXChildVisit_Recurse
});
if found_cant_eval {
return None;
Expand Down
Loading