Skip to content

Commit 1388a44

Browse files
committed
Add clang-format check for PR diff to CI
1 parent af036cf commit 1388a44

File tree

7 files changed

+1069
-8
lines changed

7 files changed

+1069
-8
lines changed

.github/workflows/continuous_integration.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -924,11 +924,4 @@ jobs:
924924
- uses: actions/checkout@v4
925925

926926
- name: Check for tabs
927-
# Ensure that there are no tabs in source code.
928-
# GREP returns 0 (true) if there are any matches, and
929-
# we don't want any matches. If there are matches,
930-
# print a helpful message, and make the test fail by using "false".
931-
# The GREP command here checks for any tab characters in the the files
932-
# that match the specified pattern. GREP does not pick up explicit tabs
933-
# (e.g., literally a \t in a source file).
934-
run: if grep --line-num --recursive --exclude-dir="*dependencies*" --exclude-dir="*snopt*" --include={CMakeLists.txt,*.cpp,*.c,*.h} -P "\t" . ; then echo "Tabs found in the lines shown above. See CONTRIBUTING.md about tabs."; false; fi
927+
run: scripts/tools/check-tabs.sh
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: static-analysis
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
format:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
with:
12+
fetch-depth: 0 # Fetch full history for branches
13+
14+
- name: Fetch the main branch
15+
run: git fetch origin main:refs/remotes/origin/main
16+
17+
- name: Check code formatting with `clang-format` against main
18+
run: scripts/tools/check-format.sh --branch remotes/origin/main

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ install
2020
# OpenSim
2121
err.log
2222
out.log
23+
logs/*.log
24+
logs/*.dot
25+
logs/*.svg
2326

2427
# Mac
2528
.DS_Store

logs/.gitkeep

Whitespace-only changes.

scripts/tools/check-format.sh

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/bin/bash
2+
# Check the changed code against the clang-format code style for the repository.
3+
# Uses a copy of https://github.com/llvm/llvm-project/blob/main/clang/tools/clang-format/git-clang-format
4+
# located in the scripts directory to check the formatting of the diff against main.
5+
# The script will exit with a 1 if a diff is detected and a 0 if it isn't.
6+
# If a diff is detected, you should run the script to fix the issues locally and commit again.
7+
8+
# Disable exit on error so we can handle return codes manually
9+
set +e
10+
11+
# === Argument Parsing ===
12+
13+
FIX=0
14+
BASE_REF="main"
15+
OPEN_SIM_BASE="."
16+
TOOLS_DIR="./scripts/tools"
17+
LOG_DIR="./logs"
18+
POSITIONAL=()
19+
20+
# Parse optional --fix flag
21+
while [[ $# -gt 0 ]]; do
22+
case "$1" in
23+
-f|--fix)
24+
FIX=1
25+
shift
26+
;;
27+
-b|--branch)
28+
BASE_REF="$2"
29+
shift 2
30+
;;
31+
-h|--help)
32+
echo "Usage: $0 [OPTIONS] [<opensim-base-dir>] [<tools-dir>]
33+
34+
Options:
35+
-f, --fix Automatically apply formatting fixes if issues are found.
36+
-b, --branch <name> Set the git base branch to diff against. Default: main
37+
-h, --help Show this help message.
38+
39+
Positional Arguments:
40+
<opensim-base-dir> Path to the OpenSim base directory. Default: current directory
41+
<tools-dir> Path to the tools directory containing git-clang-format.py. Default: ./scripts/tools
42+
43+
Examples:
44+
$0
45+
$0 -b develop
46+
$0 --fix $OPEN_SIM_BASE $TOOLS_DIR
47+
"
48+
exit 0
49+
;;
50+
-*)
51+
echo "Unknown option: $1"
52+
exit 1
53+
;;
54+
*)
55+
POSITIONAL+=("$1")
56+
shift
57+
;;
58+
esac
59+
done
60+
61+
# Restore positional arguments
62+
set -- "${POSITIONAL[@]}"
63+
64+
# Assign positional arguments with defaults
65+
if [ ${#POSITIONAL[@]} -gt 0 ]; then
66+
OPEN_SIM_BASE="${POSITIONAL[0]}"
67+
fi
68+
69+
if [ ${#POSITIONAL[@]} -gt 1 ]; then
70+
TOOLS_DIR="${POSITIONAL[1]}"
71+
fi
72+
73+
OPEN_SIM_BASE=$(realpath "$OPEN_SIM_BASE")
74+
TOOLS_DIR=$(realpath "$TOOLS_DIR")
75+
76+
# Independent log directory (relative to current working directory)
77+
LOG_FILE="$LOG_DIR/clang-format.log"
78+
GIT_CLANG_FORMAT="$TOOLS_DIR/git-clang-format.py"
79+
80+
# Ensure git-clang-format.py exists
81+
if [ ! -f "$GIT_CLANG_FORMAT" ]; then
82+
echo "Error: git-clang-format.py not found at $GIT_CLANG_FORMAT"
83+
exit 1
84+
fi
85+
86+
# Create log directory if it doesn't exist
87+
mkdir -p "$LOG_DIR"
88+
89+
# Clear or create the log file
90+
: > "$LOG_FILE"
91+
92+
# Save current directory
93+
ORIG_DIR=$(pwd)
94+
95+
# Change to OpenSim base directory
96+
cd "$OPEN_SIM_BASE" || {
97+
echo "Error: Failed to cd into $OPEN_SIM_BASE"
98+
exit 1
99+
}
100+
101+
# Run git-clang-format with logging (from OpenSim base dir)
102+
"$GIT_CLANG_FORMAT" "$BASE_REF" --diff | tee -a "$ORIG_DIR/$LOG_FILE"
103+
rc1=${PIPESTATUS[0]}
104+
105+
"$GIT_CLANG_FORMAT" "$BASE_REF" --diffstat | tee -a "$ORIG_DIR/$LOG_FILE"
106+
rc2=${PIPESTATUS[0]}
107+
108+
# Combine exit codes
109+
FORMAT_ERROR=$(( rc1 || rc2 ))
110+
111+
# If --fix is specified and formatting issues exist, apply the fix
112+
if [ "$FORMAT_ERROR" -ne 0 ] && [ "$FIX" -eq 1 ]; then
113+
echo | tee -a "$ORIG_DIR/$LOG_FILE"
114+
echo "Applying automatic formatting fixes..." | tee -a "$ORIG_DIR/$LOG_FILE"
115+
"$GIT_CLANG_FORMAT" "$BASE_REF" | tee -a "$ORIG_DIR/$LOG_FILE"
116+
echo "Fixes applied. Please review and re-commit your changes." | tee -a "$ORIG_DIR/$LOG_FILE"
117+
fi
118+
119+
120+
# Return to original directory
121+
cd "$ORIG_DIR"
122+
123+
# Final message
124+
if [ "$FORMAT_ERROR" -ne 0 ]; then
125+
if [ "$FIX" -eq 1 ]; then
126+
echo "Fix mode enabled. Formatting was corrected." | tee -a "$LOG_FILE"
127+
FORMAT_ERROR=0
128+
else
129+
echo | tee -a "$LOG_FILE"
130+
echo "Formatting issues found! Run the following command to fix them:" | tee -a "$LOG_FILE"
131+
echo | tee -a "$LOG_FILE"
132+
echo " $0 --fix --branch \"$BASE_REF\" \"$OPEN_SIM_BASE\" \"$TOOLS_DIR\"" | tee -a "$LOG_FILE"
133+
echo | tee -a "$LOG_FILE"
134+
fi
135+
else
136+
echo "No formatting issues found." | tee -a "$LOG_FILE"
137+
fi
138+
139+
exit $FORMAT_ERROR

scripts/tools/check-tabs.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
# Ensure that there are no tabs in source code.
3+
# GREP returns 0 (true) if there are any matches, and
4+
# we don't want any matches. If there are matches,
5+
# print a helpful message, and make the test fail by using "false".
6+
# The GREP command here checks for any tab characters in the the files
7+
# that match the specified pattern. GREP does not pick up explicit tabs
8+
# (e.g., literally a \t in a source file).
9+
10+
11+
OPEN_SIM_BASE="."
12+
13+
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
14+
echo "Usage: $0 [<opensim-base-dir>]
15+
16+
Checks source files for tab characters.
17+
18+
Arguments:
19+
<opensim-base-dir> Optional. Path to the OpenSim base directory. Default: current directory
20+
21+
Examples:
22+
$0
23+
$0 ./src/OpenSim
24+
"
25+
exit 0
26+
fi
27+
# Use the first argument if provided
28+
if [ $# -ge 1 ]; then
29+
OPEN_SIM_BASE="$1"
30+
fi
31+
32+
OPEN_SIM_BASE=$(realpath "$OPEN_SIM_BASE") || {
33+
echo "Error: '$1' is not a valid directory."
34+
exit 1
35+
}
36+
37+
if [ ! -d "$OPEN_SIM_BASE" ]; then
38+
echo "Directory $OPEN_SIM_BASE does not exist."
39+
exit 1
40+
fi
41+
42+
# Search for tab characters in source files
43+
if grep --line-number --recursive \
44+
--exclude-dir="*dependencies*" \
45+
--exclude-dir="*snopt*" \
46+
--include={CMakeLists.txt,*.cpp,*.c,*.h} \
47+
-P "\t" "$OPEN_SIM_BASE"; then
48+
echo "Tabs found in the lines shown above. See CONTRIBUTING.md about tabs."
49+
exit 1
50+
fi

0 commit comments

Comments
 (0)