Skip to content

Commit 1e356f9

Browse files
authored
Merge pull request #4107 from tgross35/format-macro-bodies
ci: use some tricks to format macro bodies
2 parents f92da8e + 6269954 commit 1e356f9

File tree

110 files changed

+3054
-2808
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+3054
-2808
lines changed

.git-blame-ignore-revs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Format macro bodies
2+
a0c7f8017b964a2de8bc3aabebdabd4a8f2c0905

ci/style.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ enum State {
9797

9898
fn check_style(file: &str, path: &Path, err: &mut Errors) {
9999
let mut state = State::Start;
100-
let mut s_macros = 0;
100+
101+
// FIXME: see below
102+
// let mut s_macros = 0;
103+
101104
let mut f_macros = 0;
102105
let mut in_impl = false;
103106

@@ -140,7 +143,8 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) {
140143
} else if line.starts_with("type ") && !in_impl {
141144
State::Typedefs
142145
} else if line.starts_with("s! {") {
143-
s_macros += 1;
146+
// FIXME: see below
147+
// s_macros += 1;
144148
State::Structs
145149
} else if line.starts_with("s_no_extra_traits! {") {
146150
// multiple macros of this type are allowed
@@ -175,10 +179,13 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) {
175179
f_macros += 1;
176180
err.error(path, i, "multiple f! macros in one module");
177181
}
178-
if s_macros == 2 {
179-
s_macros += 1;
180-
err.error(path, i, "multiple s! macros in one module");
181-
}
182+
183+
// FIXME(#4109): multiple should be allowed if at least one is `cfg(not) within `cfg_if`.
184+
// For now just disable this and check by hand.
185+
// if s_macros == 2 {
186+
// s_macros += 1;
187+
// err.error(path, i, "multiple s! macros in one module");
188+
// }
182189

183190
state = line_state;
184191
}

ci/style.sh

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,64 @@
22

33
set -eux
44

5+
if [ -n "${CI:-}" ]; then
6+
rustup toolchain install nightly -c rustfmt --allow-downgrade
7+
rustup override set nightly
8+
9+
check="--check"
10+
fi
11+
512
rustc ci/style.rs && ./style src
613

7-
rustup toolchain install nightly -c rustfmt --allow-downgrade
8-
rustup override set nightly
914
command -v rustfmt
1015
rustfmt -V
11-
cargo fmt --all -- --check
16+
17+
# Save a list of all source files
18+
tmpfile="file-list~" # trailing tilde for gitignore
19+
find src -name '*.rs' > "$tmpfile"
20+
21+
# Before formatting, replace all macro identifiers with a function signature.
22+
# This allows `rustfmt` to format it.
23+
while IFS= read -r file; do
24+
if [ "$file" = "src/macros.rs" ]; then
25+
# Too much special syntax in `macros.rs` that we don't want to format
26+
continue
27+
fi
28+
29+
# Turn all braced macro `foo! { /* ... */ }` invocations into
30+
# `fn foo_fmt_tmp() { /* ... */ }`.
31+
perl -pi -e 's/(?!macro_rules)\b(\w+)!\s*\{/fn $1_fmt_tmp() {/g' "$file"
32+
33+
# Replace `if #[cfg(...)]` within `cfg_if` with `if cfg_tmp!([...])` which
34+
# `rustfmt` will format. We put brackets within the parens so it is easy to
35+
# match (trying to match parentheses would catch the first closing `)` which
36+
# wouldn't be correct for something like `all(any(...), ...)`).
37+
perl -pi -0777 -e 's/if #\[cfg\((.*?)\)\]/if cfg_tmp!([$1])/gms' "$file"
38+
39+
# We have some instances of `{const}` that make macros happy but aren't
40+
# valid syntax. Replace this with just the keyword, plus an indicator
41+
# comment on the preceding line (which is where rustfmt puts it. Also
42+
# rust-lang/rustfmt#5464).
43+
perl -pi -e 's/^(\s*)(.*)\{const\}/$1\/\* FMT-CONST \*\/\n$1$2const/g' "$file"
44+
45+
# Format the file. We need to invoke `rustfmt` directly since `cargo fmt`
46+
# can't figure out the module tree with the hacks in place.
47+
failed=false
48+
rustfmt --config-path rustfmt.toml "$file" ${check:+"$check"} || failed=true
49+
50+
# Restore all changes to the files.
51+
perl -pi -e 's/fn (\w+)_fmt_tmp\(\)/$1!/g' "$file"
52+
perl -pi -0777 -e 's/cfg_tmp!\(\[(.*?)\]\)/#[cfg($1)]/gms' "$file"
53+
perl -pi -0777 -e 's/\/\* FMT-CONST \*\/(?:\n\s*)?(.*?)const/$1\{const\}/gms' "$file"
54+
55+
# Defer emitting the failure until after the files get reset
56+
if [ "$failed" != "false" ]; then
57+
echo "Formatting failed"
58+
exit 1
59+
fi
60+
done < "$tmpfile"
61+
62+
rm "$tmpfile"
1263

1364
if shellcheck --version ; then
1465
find . -name '*.sh' -print0 | xargs -0 shellcheck

rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
error_on_line_overflow = true
2+
edition = "2021"

src/fixed_width_ints.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ pub type uint32_t = u32;
2020
pub type uint64_t = u64;
2121

2222
cfg_if! {
23-
if #[cfg(all(target_arch = "aarch64", not(any(target_os = "windows", target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "watchos"))))] {
23+
if #[cfg(all(
24+
target_arch = "aarch64",
25+
not(any(
26+
target_os = "windows",
27+
target_os = "macos",
28+
target_os = "ios",
29+
target_os = "tvos",
30+
target_os = "watchos"
31+
))
32+
))] {
2433
// This introduces partial support for FFI with __int128 and
2534
// equivalent types on platforms where Rust's definition is validated
2635
// to match the standard C ABI of that platform.
@@ -93,7 +102,15 @@ cfg_if! {
93102

94103
// static_assert_eq!(core::mem::size_of::<__uint128_t>(), _SIZE_128);
95104
// static_assert_eq!(core::mem::align_of::<__uint128_t>(), _ALIGN_128);
96-
} else if #[cfg(all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "watchos")))] {
105+
} else if #[cfg(all(
106+
target_arch = "aarch64",
107+
any(
108+
target_os = "macos",
109+
target_os = "ios",
110+
target_os = "tvos",
111+
target_os = "watchos"
112+
)
113+
))] {
97114
/// C `__int128_t`
98115
pub type __int128_t = i128;
99116
/// C `__uint128_t`

0 commit comments

Comments
 (0)