forked from NatLabRockies/EnergyPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
259 lines (238 loc) · 11.5 KB
/
test_code_integrity.yml
File metadata and controls
259 lines (238 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
name: Code Integrity
on:
push:
branches: [ develop ]
pull_request:
branches: [ develop ]
workflow_dispatch:
jobs:
code_integrity_checks:
name: Static Code Analysis
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 2
- name: Pip install dependencies
if: always()
run: |
pip install pre-commit tabulate
- name: Run pre-commit
id: pre_commit
if: always()
run: |
# Skip clang-format since we run it separately. The only reason is that I'm not yet willing to remove the step summary table
export SKIP='clang-format'
# check_for_enum_scope_usage.py is slow so it's added as a "manual" hook stage, so it doesn't run on each pre-commit
# Here we pass --hook-stage manual so it's run also
if [ "${GITHUB_EVENT_NAME}" = "pull_request" ]; then
pre-commit run --verbose --hook-stage manual --from-ref HEAD^ --to-ref HEAD
else
pre-commit run --verbose --hook-stage manual --all-files
fi
# This can take up to 2min30 (between 14 and 30s usually)
- name: Install clang-format-19
shell: bash
run: |
# 14s to install from apt, getting 19.1.1
# Getting it from LLVM takes 1min13s
# sudo apt-get update && apt-get install -y wget gnupg lsb-release software-properties-common
# wget -qO - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/llvm-archive-keyring.gpg
# echo "deb [signed-by=/usr/share/keyrings/llvm-archive-keyring.gpg] http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-19 main" | sudo tee /etc/apt/sources.list.d/llvm.list
sudo apt-get update -qq
sudo apt-get install -y -q clang-format-19
clang-format-19 --version
- name: Run clang-format against C++ files touched by the PR
if: ${{ github.event_name == 'pull_request' }}
shell: bash
run: |
clang-format-19 --version
begin_group() { echo -e "::group::\033[93m$1\033[0m"; }
begin_group "Find changed files"
echo "GITHUB_REF=$GITHUB_REF GITHUB_BASE_REF=$GITHUB_BASE_REF GITHUB_HEAD_REF=$GITHUB_HEAD_REF"
# too slow on E+: git fetch --all --quiet
# first find if any files changed
# HEAD^ works in PRs because actions/checkout checks out a merge commit by default in PR contexts
# and I specified a fetch-depth of 2
readarray -t changed_files < <(git diff --name-only --diff-filter=AM HEAD^ HEAD src/ tst/ | /bin/grep -E '\.(cpp|cc|c|hpp|hh|h)$')
file_count=${#changed_files[@]}
if [ $file_count -eq 0 ]; then
echo "No files of type (cpp, c, hpp, h) changed. Skipping clang-formatting"
exit 0
else
begin_group "Found $file_count C/C++ changed files"
for file in "${changed_files[@]}"; do
echo "$file"
done
echo "::endgroup::"
fi
echo "::endgroup::"
begin_group "Run clang-format for changes files"
# Using \0 as a terminator in case we'd ever have files with spaces
git diff -z --diff-filter=AM --name-only HEAD^ HEAD src/ tst/ \
| /bin/grep -z -E '\.(cpp|cc|c|hpp|hh|h)$' \
| xargs -0 -P "$(nproc)" -n 1 clang-format-19 -style=file -i -fallback-style=none --verbose
# clang-format will auto correct files so prepare the diff and use this as artifact
git diff > clang_format.patch
echo "::endgroup::"
# Delete if nothing otherwise exit 1 to indicate a failed job
if [ ! -s clang_format.patch ]; then
rm clang_format.patch
exit 0
else
incorrect_count=$(git diff --name-only | wc -l)
incorrect_percent=$(awk "BEGIN { printf \"%.2f\", ($incorrect_count/$file_count)*100 }")
begin_group "clang-format auto corrected $incorrect_count files:"
git diff --name-only
echo "::endgroup::"
echo "::error title=Clang Format Check Failed::Formatting issues detected in $incorrect_count files"
echo -e "\nPlease correct these files by running clang-format-19 locally, or download the artifact "
echo 'and run `patch -p1 < /path/to/clang_format.patch`'
{
echo "## Clang Format Check Summary"
echo ""
echo "| Item | Value |"
echo "|---------------------------------------|-----------|"
echo "| Number of Files Analyzed | $file_count |"
echo "| Number of Files Incorrectly Formatted | $incorrect_count |"
echo "| % Files Incorrectly Formatted | ${incorrect_percent}% |"
} >> "$GITHUB_STEP_SUMMARY"
exit 1
fi
- name: Run clang-format for entire codebase
id: clang_format_on_push
if: ${{ github.event_name == 'push' }}
shell: bash
run: |
clang-format-19 --version
begin_group() { echo -e "::group::\033[93m$1\033[0m"; }
begin_group "Run clang-format"
find src tst \( -name "*.hpp" -o -name "*.h" -o -name "*.hh" -o -name "*.cc" -o -name "*.cpp" -o -name "*.c" \) \
-print0 | xargs -0 -P "$(nproc)" -n 1 clang-format-19 -style=file -i -fallback-style=none --verbose
# clang-format will auto correct files so prepare the diff and use this as artifact
git diff > clang_format.patch
echo "::endgroup::"
# Delete if nothing otherwise exit 1 to indicate a failed job
if [ ! -s clang_format.patch ]; then
rm clang_format.patch
exit 0
else
file_count=$(find src tst \( -name "*.hpp" -o -name "*.h" -o -name "*.hh" -o -name "*.cc" -o -name "*.cpp" -o -name "*.c" \) | wc -l)
incorrect_count=$(git diff --name-only | wc -l)
incorrect_percent=$(awk "BEGIN { printf \"%.2f\", ($incorrect_count/$file_count)*100 }")
begin_group "clang-format auto corrected $incorrect_count files:"
git diff --name-only
echo "::endgroup::"
echo "::error title=Clang Format Check Failed::Formatting issues detected in $incorrect_count files"
{
echo "## Clang Format Check Summary"
echo ""
echo "| Item | Value |"
echo "|---------------------------------------|-----------|"
echo "| Number of Files Analyzed | $file_count |"
echo "| Number of Files Incorrectly Formatted | $incorrect_count |"
echo "| % Files Incorrectly Formatted | ${incorrect_percent}% |"
} >> "$GITHUB_STEP_SUMMARY"
exit 1
fi
- name: Upload clang-format patch as artifact
if: ${{ failure() }}
uses: actions/upload-artifact@v6
with:
name: EnergyPlus-${{ github.sha }}-clang_format.patch
path: clang_format.patch
- name: Commit Auto-corrections
shell: bash
if: ${{ always() && github.event_name == 'push' }}
run: |
git add -u
if [[ $(git diff --cached --exit-code) ]]; then
echo "Commiting Lint Autocorrects"
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git config --global user.name 'github-actions[bot]'
msg=""
# Construct something like "clang format and pre-commit", "clang-format" or "pre-commit"
if [[ "${{ steps.clang_format_on_push.outcome }}" == "failure" ]]; then
msg="clang-format"
fi
if [[ "${{ steps.pre_commit.outcome }}" == "failure" ]]; then
if [[ -n "$msg" ]]; then
msg="$msg and pre-commit"
else
msg="pre-commit"
fi
fi
git commit -m "[chore] Commit $msg autocorrects"
echo '' >> .git-blame-ignore-revs
git log -n1 --pretty='format:# %C(auto)%s [%an, %as]%n%H%n' >> .git-blame-ignore-revs
git add .git-blame-ignore-revs
git commit -m "Add $msg autocorrects to git-blame-ignore-revs"
git push
else
echo "No Autocorrect needed"
fi
- name: Install cppcheck
if: always()
run: sudo apt-get install cppcheck
- name: Run CppCheck
id: cpp_check_run
if: always()
# TODO: Evaluate the long list of flags here
run: >
cppcheck
-D__cppcheck__ -UEP_Count_Calls -DEP_NO_OPENGL -UGROUND_PLOT -DLINK_WITH_PYTHON -DMSVC_DEBUG -DSKYLINE_MATRIX_REMOVE_ZERO_COLUMNS -U_OPENMP -Ugeneratetestdata
-DEP_cache_GlycolSpecificHeat -DEP_cache_PsyTsatFnPb -UEP_nocache_Psychrometrics -UEP_psych_errors -UEP_psych_stats
--force
--std=c++17
--inline-suppr
--suppress=missingInclude
--suppress=missingIncludeSystem
--suppress=unusedFunction:src/EnergyPlus/api/autosizing.cc
--suppress=unusedFunction:src/EnergyPlus/api/datatransfer.cc
--suppress=unusedFunction:src/EnergyPlus/api/func.cc
--suppress=unusedFunction:src/EnergyPlus/api/runtime.cc
--suppress=unusedFunction:src/EnergyPlus/api/state.cc
--suppress=unusedFunction:src/EnergyPlus/Psychrometrics.cc
--enable=all
-j $(nproc)
--template='[{file}:{line}]:({severity}),[{id}],{message}'
--suppress="uninitvar:*" \
--suppress="arrayIndexThenCheck:*" \
--suppress="checkLevelNormal:*" \
--suppress="constParameterPointer:*" \
--suppress="constParameterReference:*" \
--suppress="constVariable:*" \
--suppress="constVariablePointer:*" \
--suppress="constVariableReference:*" \
--suppress="cstyleCast:*" \
--suppress="duplicateCondition:*" \
--suppress="duplicateExpression:*" \
--suppress="knownConditionTrueFalse:*" \
--suppress="multiCondition:*" \
--suppress="noExplicitConstructor:*" \
--suppress="passedByValueCallback:*" \
--suppress="redundantAssignment:*" \
--suppress="redundantCondition:*" \
--suppress="redundantInitialization:*" \
--suppress="sameIteratorExpression:*" \
--suppress="shadowFunction:*" \
--suppress="shadowVariable:*" \
--suppress="unassignedVariable:*" \
--suppress="unmatchedSuppression:*" \
--suppress="unpreciseMathCall:*" \
--suppress="unreadVariable:*" \
--suppress="useInitializationList:*" \
--suppress="useStlAlgorithm:*" \
--suppress="uselessAssignmentArg:*" \
--suppress="uselessCallsSubstr:*" \
./src
3>&1 1>&2 2>&3 | tee cppcheck.txt
- name: Parse and colorize cppcheck
if: always() && steps.cpp_check_run.outcome == 'success'
run: python ./scripts/dev/colorize_cppcheck_results.py
- name: Upload cppcheck results as artifact
if: always()
uses: actions/upload-artifact@v6
with:
name: EnergyPlus-${{ github.sha }}-cppcheck_results.txt
path: cppcheck.txt