xref: /rust-libc-0.2.174/ci/style.sh (revision 3f6dd37e)
1#!/bin/sh
2
3set -eux
4
5if [ -n "${CI:-}" ]; then
6    rustup toolchain install nightly -c rustfmt --allow-downgrade
7    rustup override set nightly
8
9    check="--check"
10fi
11
12rustc ci/style.rs && ./style src
13
14command -v rustfmt
15rustfmt -V
16
17# Run once to cover everything that isn't in `src/`
18cargo fmt
19
20# Save a list of all source files
21tmpfile="file-list~" # trailing tilde for gitignore
22find src -name '*.rs' > "$tmpfile"
23
24# Before formatting, replace all macro identifiers with a function signature.
25# This allows `rustfmt` to format it.
26while IFS= read -r file; do
27    if [ "$file" = "src/macros.rs" ]; then
28        # Too much special syntax in `macros.rs` that we don't want to format
29        continue
30    fi
31
32    # Turn all braced macro `foo! { /* ... */ }` invocations into
33    # `fn foo_fmt_tmp() { /* ... */ }`.
34    perl -pi -e 's/(?!macro_rules)\b(\w+)!\s*\{/fn $1_fmt_tmp() {/g' "$file"
35
36    # Replace `if #[cfg(...)]` within `cfg_if` with `if cfg_tmp!([...])` which
37    # `rustfmt` will format. We put brackets within the parens so it is easy to
38    # match (trying to match parentheses would catch the first closing `)` which
39    # wouldn't be correct for something like `all(any(...), ...)`).
40    perl -pi -0777 -e 's/if #\[cfg\((.*?)\)\]/if cfg_tmp!([$1])/gms' "$file"
41
42    # We have some instances of `{const}` that make macros happy but aren't
43    # valid syntax. Replace this with just the keyword, plus an indicator
44    # comment on the preceding line (which is where rustfmt puts it. Also
45    # rust-lang/rustfmt#5464).
46    perl -pi -e 's/^(\s*)(.*)\{const\}/$1\/\* FMT-CONST \*\/\n$1$2const/g' "$file"
47
48    # Format the file. We need to invoke `rustfmt` directly since `cargo fmt`
49    # can't figure out the module tree with the hacks in place.
50    failed=false
51    rustfmt --config-path rustfmt.toml "$file" ${check:+"$check"} || failed=true
52
53    # Restore all changes to the files.
54    perl -pi -e 's/fn (\w+)_fmt_tmp\(\)/$1!/g' "$file"
55    perl -pi -0777 -e 's/cfg_tmp!\(\[(.*?)\]\)/#[cfg($1)]/gms' "$file"
56    perl -pi -0777 -e 's/\/\* FMT-CONST \*\/(?:\n\s*)?(.*?)const/$1\{const\}/gms' "$file"
57
58    # Defer emitting the failure until after the files get reset
59    if [ "$failed" != "false" ]; then
60        echo "Formatting failed"
61        exit 1
62    fi
63done < "$tmpfile"
64
65rm "$tmpfile"
66
67if shellcheck --version ; then
68    find . -name '*.sh' -print0 | xargs -0 shellcheck
69else
70    echo "shellcheck not found"
71    exit 1
72fi
73
74# Ensure that `sort` output is not locale-dependent
75export LC_ALL=C
76
77for file in libc-test/semver/*.txt; do
78    case "$file" in
79      *TODO*) continue ;;
80    esac
81
82    if ! sort -C "$file"; then
83        echo "Unsorted semver file $file"
84        exit 1
85    fi
86
87    duplicates=$(uniq -d "$file")
88    if [ -n "$duplicates" ]; then
89        echo "Semver file $file contains duplicates:"
90        echo "$duplicates"
91
92        exit 1
93    fi
94done
95