Skip to content

Commit

Permalink
RFC69: C++ code formatting - config, scripts and workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and rouault committed Dec 17, 2022
1 parent 26ec6f2 commit 5d80bc1
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
7 changes: 5 additions & 2 deletions scripts/clang-format → .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
SortIncludes: false
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Never
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
Expand All @@ -63,5 +64,7 @@ SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
...

---
Language: Json
BasedOnStyle: llvm
30 changes: 30 additions & 0 deletions .github/workflows/clang-format-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

name: clang-format Check

on: [push, pull_request]

jobs:

formatting-check:

name: Formatting Check
runs-on: ubuntu-latest

steps:
- name: Checkout GDAL
uses: actions/checkout@v3
with:
fetch-depth: 100

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.7

- name: Install Run clang-format style check for C/C++ programs.
run: pip install clang-format==15.0.4

- name: Check new code with clang-format
run: ./scripts/clang-format.sh


22 changes: 22 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,25 @@ repos:
examples/|
autotest/ogr/data/
)
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: 'v15.0.4'
hooks:
- id: clang-format
exclude: >
(?x)^(
swig/|
third_party/|
autotest/ogr/data/|
alg/internal_libqhull/|
frmts/gtiff/libtiff/|
frmts/gtiff/libgeotiff/|
frmts/jpeg/libjpeg/|
frmts/jpeg/libjpeg12/|
frmts/png/libpng/|
frmts/gif/giflib/|
frmts/zlib/|
frmts/pcidsk/sdk|
frmts/grib/degrib/degrib|
frmts/grib/degrib/g2clib|
ogr/ogrsf_frmts/geojson/libjson/
)
65 changes: 65 additions & 0 deletions scripts/clang-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/sh
set -eu

if ! (clang-format --version >/dev/null); then
echo "clang-format not available. Install it with 'pre-commit'"
exit 1
fi

# GNU prefix command for mac os support (gsed)
GP=
# shellcheck disable=SC2039
case "${OSTYPE:-}" in
darwin*)
GP=g
;;
esac

# determine changed files
FILES=$(git diff --diff-filter=AM --name-only HEAD~1| tr '\n' ' ' | sort -u)

if [ -z "$FILES" ]; then
echo "nothing was modified"
exit 0
fi

STYLEDIFF=/tmp/clang-format.diff
true > $STYLEDIFF

for f in $FILES; do
if ! [ -f "$f" ]; then
echo "$f was removed." >>/tmp/ctest-important.log
continue
fi

# echo "Checking $f"
case "$f" in
*.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H)
;;

*)
continue
;;
esac

m="$f.prepare"
cp "$f" "$m"
clang-format -i "$f"
if diff -u "$m" "$f" >>$STYLEDIFF; then
rm "$m"
else
echo "File $f is not styled properly."
fi
done

if [ -s "$STYLEDIFF" ]; then
echo
echo "Required code formatting updates:"
cat "$STYLEDIFF"

cat <<EOF
Tips to prevent and resolve:
* Run pre-commit to install clang-format for C++ code style
EOF
exit 1
fi

0 comments on commit 5d80bc1

Please sign in to comment.