Skip to content

Commit 3ecd681

Browse files
Add simple static checker using clang-query
1 parent c6b6b8f commit 3ecd681

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ libtool
3333
*~
3434
*.log
3535
*.trs
36+
compile_commands.commands.json
37+
compile_commands.json
3638
src/libsecp256k1-config.h
3739
src/libsecp256k1-config.h.in
3840
src/ecmult_static_context.h

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ addons:
1212
- libgmp-dev
1313
- valgrind
1414
- libtool-bin
15+
- bear
1516
compiler:
1617
- clang
1718
- gcc
@@ -47,6 +48,7 @@ matrix:
4748
- libgmp-dev:i386
4849
- valgrind
4950
- libtool-bin
51+
- bear
5052
- libc6-dbg:i386
5153
- compiler: clang
5254
env: HOST=i686-linux-gnu
@@ -57,6 +59,7 @@ matrix:
5759
- gcc-multilib
5860
- valgrind
5961
- libtool-bin
62+
- bear
6063
- libc6-dbg:i386
6164
- compiler: gcc
6265
env: HOST=i686-linux-gnu
@@ -104,5 +107,6 @@ after_script:
104107
- cat ./exhaustive_tests.log
105108
- cat ./valgrind_ctime_test.log
106109
- cat ./bench.log
110+
- cat ./compile_commands.json
107111
- $CC --version
108112
- valgrind --version

clang-query.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
set -u
4+
5+
matcher=$(cat <<'EOF'
6+
set print-matcher true
7+
8+
# expressions of any floating point type (unless in a system header)
9+
match expr(allOf(unless(isExpansionInSystemHeader()), hasType(realFloatingPointType())))
10+
11+
# calls to memcmp (secp256k1_memcmp_var should be used instead)
12+
match callExpr(callee(functionDecl(hasName("memcmp"))))
13+
14+
quit
15+
EOF
16+
)
17+
18+
output=$(echo "$matcher" | clang-query src/*.c)
19+
status=$?
20+
if [ $status -ne 0 ]
21+
then
22+
exit $status
23+
fi
24+
echo "$output"
25+
# For some reason, clang-query returns a zero status even if clang failed to process the file.
26+
# This is not a big issue. If clang errors, we'll notice that when trying to compile with clang anyway.
27+
# We still try to catch this case by grepping also for "error:".
28+
echo "$output" | grep -qE "^Match #|error:"
29+
if [ $? -eq 0 ]
30+
then
31+
exit 1
32+
fi
33+
34+
echo
35+
exit 0

contrib/travis.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ fi
2323

2424
if [ -n "$BUILD" ]
2525
then
26-
make -j2 "$BUILD"
26+
if [ "$TRAVIS_COMPILER" = "clang" ]
27+
then
28+
# Use bear to generate compile_commands.json
29+
# This needs to be the first make command because otherwise make does not invoke the compiler because the files are up to date.
30+
# We need to update this to "bear -- make" when we move to bear 3.0.
31+
bear make -j2
32+
fi
33+
make -j2 "$BUILD"
2734
fi
2835
if [ "$RUN_VALGRIND" = "yes" ]
2936
then
@@ -66,3 +73,9 @@ if [ "$CTIMETEST" = "yes" ]
6673
then
6774
./libtool --mode=execute valgrind --error-exitcode=42 ./valgrind_ctime_test > valgrind_ctime_test.log 2>&1
6875
fi
76+
77+
# This would also run on gcc builds but there's no need to run it for both compilers.
78+
if [ "$TRAVIS_COMPILER" = "clang" ]
79+
then
80+
./clang-query.sh
81+
fi

0 commit comments

Comments
 (0)