-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CI lint for invalid whitespace in files. Fix existing issues. (#681)
``` # Errors on whitespace issues in '*.{h,cpp,td,txt,mlir,yml}' files $ lint_whitespace_checks [flags] -f Auto-fix whitespace issues ``` Perform the following whitespace checks: - No trailing whitespace (clang-format covers this for `h/cpp`, but not `md/yml/td/txt/mlir`) - Files must end in EOL (many editors enforce this, but not clang-format/tidy) Can auto-fix issues using the `-f` flag. Example of failure message when I added whitespace and removed EOL from end of file: https://github.com/GleasonK/stablehlo/actions/runs/3605629099/jobs/6076206738
- Loading branch information
Showing
9 changed files
with
137 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Copyright 2022 The StableHLO Authors. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
print_usage() { | ||
echo "Usage: $0 [-f]" | ||
echo " -f Auto-fix whitespace issues." | ||
} | ||
|
||
FORMAT_MODE='validate' | ||
while getopts 'f' flag; do | ||
case "${flag}" in | ||
f) FORMAT_MODE="fix" ;; | ||
*) print_usage | ||
exit 1 ;; | ||
esac | ||
done | ||
shift $(( OPTIND - 1 )) | ||
|
||
if [[ $# -ne 0 ]] ; then | ||
print_usage | ||
exit 1 | ||
fi | ||
|
||
get_source_files() { | ||
find . -iname '*.h' -o -iname '*.cpp' -o -iname '*.td' -o -iname '*.md' -o -iname '*.txt' -o -iname '*.mlir' -o -iname '*.yml' | ||
} | ||
|
||
files_without_eof_newline() { | ||
get_source_files | xargs -L1 bash -c 'test "$(tail -c 1 "$0")" && echo "$0"' | ||
} | ||
|
||
files_with_trailing_whitespace() { | ||
get_source_files | xargs grep -lP '[ \t]+$' | ||
} | ||
|
||
fix_files_without_eof_newline() { | ||
echo $1 | xargs --no-run-if-empty sed -i -e '$a\' | ||
} | ||
|
||
fix_files_with_trailing_whitespace() { | ||
echo $1 | xargs --no-run-if-empty sed -i 's/[ \t]*$//' | ||
} | ||
|
||
EOF_NL=$(files_without_eof_newline) | ||
TRAIL_WS=$(files_with_trailing_whitespace) | ||
|
||
if [[ $FORMAT_MODE == 'fix' ]]; then | ||
echo "Fixing EOF newlines..." | ||
fix_files_without_eof_newline "$EOF_NL" | ||
echo "Fixing trailing whitespaces..." | ||
fix_files_with_trailing_whitespace "$TRAIL_WS" | ||
else | ||
if [ ! -z "$EOF_NL$TRAIL_WS" ]; then | ||
echo "Missing newline at EOF:" | ||
echo $EOF_NL | ||
echo "Has trailing whitespace:" | ||
echo $TRAIL_WS | ||
echo | ||
echo "Auto-fix using:" | ||
echo " $ lint_whitespace_checks.sh -f" | ||
exit 1 | ||
else | ||
echo "No whitespace issues found." | ||
fi | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.