-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlint.sh
More file actions
executable file
·303 lines (268 loc) · 7.91 KB
/
lint.sh
File metadata and controls
executable file
·303 lines (268 loc) · 7.91 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env bash
# Copyright 2026 Comcast Cable Communications Management, LLC
#
# 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.
#
# SPDX-License-Identifier: Apache-2.0
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUILD_DIR="build-dev"
NO_BUILD=false
CLEAN=false
RUN_CLANG_TIDY=true
RUN_CPPCHECK=true
RUN_CLANG_FORMAT=true
APPLY_FIXES=false
FORMAT_FIX=false
CLANG_TIDY_PATHS=(src include test/unit test/component)
CLANG_FORMAT_PATHS=(src include test)
usage() {
cat <<EOF
Usage: ./lint.sh [options]
Runs C/C++ lint checks for firebolt-cpp-client.
Default behavior: run clang-format check, then build compile_commands.json via
./build.sh +tests, then run clang-tidy and cppcheck.
Options:
--clean Remove build directory before building
--no-build Skip build step and use existing compile database
--build-dir <dir> Build directory containing compile_commands.json (default: build-dev)
--tidy-path <p> Add path for clang-tidy scan (repeatable)
--format-path <p> Add path for clang-format scan (repeatable)
--fix Apply clang-tidy fix-its (clang-tidy only)
--format-fix Apply clang-format fixes in-place
--format-only Run clang-format only
--no-format Skip clang-format checks
--tidy-only Run clang-tidy only
--cppcheck-only Run cppcheck only
--help Show this help
Examples:
./lint.sh
./lint.sh --tidy-only
./lint.sh --tidy-only --fix
./lint.sh --format-only
./lint.sh --format-fix
./lint.sh --tidy-path test/api_test_app
./lint.sh --format-path include/firebolt
./lint.sh --no-build --build-dir build-dev
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--clean)
CLEAN=true
;;
--no-build)
NO_BUILD=true
;;
--build-dir)
if [[ $# -lt 2 || -z "${2:-}" || "$2" == --* ]]; then
echo "Missing value for --build-dir" >&2
usage
exit 1
fi
BUILD_DIR="${2:-}"
shift
;;
--tidy-path)
if [[ $# -lt 2 || -z "${2:-}" || "$2" == --* ]]; then
echo "Missing value for --tidy-path" >&2
usage
exit 1
fi
CLANG_TIDY_PATHS+=("${2:-}")
shift
;;
--format-path)
if [[ $# -lt 2 || -z "${2:-}" || "$2" == --* ]]; then
echo "Missing value for --format-path" >&2
usage
exit 1
fi
CLANG_FORMAT_PATHS+=("${2:-}")
shift
;;
--fix)
APPLY_FIXES=true
;;
--format-fix)
FORMAT_FIX=true
RUN_CLANG_FORMAT=true
;;
--format-only)
RUN_CLANG_FORMAT=true
RUN_CLANG_TIDY=false
RUN_CPPCHECK=false
;;
--no-format)
RUN_CLANG_FORMAT=false
;;
--tidy-only)
RUN_CLANG_FORMAT=false
RUN_CLANG_TIDY=true
RUN_CPPCHECK=false
;;
--cppcheck-only)
RUN_CLANG_FORMAT=false
RUN_CLANG_TIDY=false
RUN_CPPCHECK=true
;;
--help|-h)
usage
exit 0
;;
*)
echo "Unknown option: $1" >&2
usage
exit 1
;;
esac
shift
done
cd "$ROOT_DIR"
if [[ "$RUN_CLANG_TIDY" == true && "$NO_BUILD" == false && "$BUILD_DIR" != "build-dev" ]]; then
echo "--build-dir is only supported with --no-build (build step always uses build-dev)." >&2
exit 1
fi
if [[ "$RUN_CLANG_FORMAT" == false && "$RUN_CLANG_TIDY" == false && "$RUN_CPPCHECK" == false ]]; then
echo "Nothing to run: clang-format, clang-tidy, and cppcheck are all disabled." >&2
exit 1
fi
if [[ "$APPLY_FIXES" == true && "$RUN_CLANG_TIDY" == false ]]; then
echo "--fix requires clang-tidy to be enabled (remove --cppcheck-only)." >&2
exit 1
fi
if [[ "$FORMAT_FIX" == true && "$RUN_CLANG_FORMAT" == false ]]; then
echo "--format-fix requires clang-format to be enabled (remove --no-format)." >&2
exit 1
fi
if [[ "$RUN_CLANG_TIDY" == true ]] && ! command -v clang-tidy >/dev/null 2>&1; then
echo "clang-tidy not found. Install it (e.g. apt install clang-tidy)." >&2
exit 1
fi
if [[ "$RUN_CPPCHECK" == true ]] && ! command -v cppcheck >/dev/null 2>&1; then
echo "cppcheck not found. Install it (e.g. apt install cppcheck)." >&2
exit 1
fi
if [[ "$RUN_CLANG_FORMAT" == true ]] && ! command -v clang-format >/dev/null 2>&1; then
echo "clang-format not found. Install it (e.g. apt install clang-format)." >&2
exit 1
fi
if [[ "$CLEAN" == true ]]; then
rm -rf "$BUILD_DIR"
fi
if [[ "$NO_BUILD" == false && "$RUN_CLANG_TIDY" == true ]]; then
./build.sh +tests
fi
if [[ "$RUN_CLANG_TIDY" == true && ! -f "$BUILD_DIR/compile_commands.json" ]]; then
echo "Missing $BUILD_DIR/compile_commands.json. Run ./build.sh +tests first." >&2
exit 1
fi
if [[ "$RUN_CLANG_FORMAT" == true ]]; then
if [[ "$FORMAT_FIX" == true ]]; then
echo "[lint] Running clang-format with fixes enabled"
else
echo "[lint] Running clang-format check"
fi
format_paths=()
for p in "${CLANG_FORMAT_PATHS[@]}"; do
if [[ -e "$p" ]]; then
format_paths+=("$p")
fi
done
if [[ ${#format_paths[@]} -eq 0 ]]; then
echo "No valid clang-format paths found." >&2
exit 1
fi
mapfile -t format_files < <(
find "${format_paths[@]}" -type f \( -name "*.h" -o -name "*.hh" -o -name "*.hpp" -o -name "*.hxx" -o -name "*.c" -o -name "*.cc" -o -name "*.cpp" -o -name "*.cxx" \) | sort
)
if [[ ${#format_files[@]} -eq 0 ]]; then
echo "No C/C++ files found for clang-format." >&2
exit 1
fi
clang_format_failed=0
total_format_files=${#format_files[@]}
format_index=0
for f in "${format_files[@]}"; do
format_index=$((format_index + 1))
echo "[lint][clang-format] ${format_index}/${total_format_files}: $f"
if [[ "$FORMAT_FIX" == true ]]; then
clang-format -i "$f"
else
if ! clang-format --dry-run --Werror "$f"; then
clang_format_failed=1
fi
fi
done
if [[ "$FORMAT_FIX" == false && $clang_format_failed -ne 0 ]]; then
echo "clang-format reported issues." >&2
echo "Run ./lint.sh --format-fix to apply formatting automatically." >&2
exit 1
fi
fi
if [[ "$RUN_CLANG_TIDY" == true ]]; then
if [[ "$APPLY_FIXES" == true ]]; then
echo "[lint] Running clang-tidy with fixes enabled"
else
echo "[lint] Running clang-tidy"
fi
existing_paths=()
for p in "${CLANG_TIDY_PATHS[@]}"; do
if [[ -e "$p" ]]; then
existing_paths+=("$p")
fi
done
if [[ ${#existing_paths[@]} -eq 0 ]]; then
echo "No valid clang-tidy paths found." >&2
exit 1
fi
mapfile -t source_files < <(
find "${existing_paths[@]}" -type f \( -name "*.c" -o -name "*.cc" -o -name "*.cpp" -o -name "*.cxx" \) | sort
)
if [[ ${#source_files[@]} -eq 0 ]]; then
echo "No C/C++ source files found for clang-tidy." >&2
exit 1
fi
clang_tidy_failed=0
total_files=${#source_files[@]}
index=0
for f in "${source_files[@]}"; do
index=$((index + 1))
echo "[lint][clang-tidy] ${index}/${total_files}: $f"
clang_tidy_cmd=(clang-tidy -p "$BUILD_DIR")
if [[ "$APPLY_FIXES" == true ]]; then
clang_tidy_cmd+=("-fix")
fi
clang_tidy_cmd+=("$f")
if ! "${clang_tidy_cmd[@]}"; then
clang_tidy_failed=1
fi
done
if [[ $clang_tidy_failed -ne 0 ]]; then
echo "clang-tidy reported issues." >&2
exit 1
fi
fi
if [[ "$RUN_CPPCHECK" == true ]]; then
echo "[lint] Running cppcheck"
cppcheck \
--enable=warning,style,performance,portability \
--std=c++17 \
--language=c++ \
--inline-suppr \
--error-exitcode=1 \
-I include \
-I src \
src include test
fi
echo "[lint] Completed successfully"