Skip to content

Commit

Permalink
Add simple static checker using clang-query
Browse files Browse the repository at this point in the history
  • Loading branch information
real-or-random committed Oct 16, 2020
1 parent c6b6b8f commit 931cc9b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ libtool
*~
*.log
*.trs
compile_commands.commands.json
compile_commands.json
src/libsecp256k1-config.h
src/libsecp256k1-config.h.in
src/ecmult_static_context.h
Expand Down
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ addons:
- libgmp-dev
- valgrind
- libtool-bin
- bear
compiler:
- clang
- gcc
Expand Down Expand Up @@ -47,6 +48,7 @@ matrix:
- libgmp-dev:i386
- valgrind
- libtool-bin
- bear
- libc6-dbg:i386
- compiler: clang
env: HOST=i686-linux-gnu
Expand All @@ -57,6 +59,7 @@ matrix:
- gcc-multilib
- valgrind
- libtool-bin
- bear
- libc6-dbg:i386
- compiler: gcc
env: HOST=i686-linux-gnu
Expand Down Expand Up @@ -104,5 +107,6 @@ after_script:
- cat ./exhaustive_tests.log
- cat ./valgrind_ctime_test.log
- cat ./bench.log
- cat ./compile_commands.json
- $CC --version
- valgrind --version
35 changes: 35 additions & 0 deletions clang-query.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh

set -u

matcher=$(cat <<'EOF'
set print-matcher true
# expressions of any floating point type (unless in a system header)
match expr(allOf(unless(isExpansionInSystemHeader()), hasType(realFloatingPointType())))
# calls to memcmp (secp256k1_memcmp_var should be used instead)
match callExpr(callee(functionDecl(hasName("memcmp"))))
quit
EOF
)

output=$(echo "$matcher" | clang-query src/*.c)
status=$?
if [ $status -ne 0 ]
then
exit $status
fi
echo "$output"
# For some reason, clang-query returns a zero status even if clang failed to process the file.
# This is not a big issue. If clang errors, we'll notice that when trying to compile with clang anyway.
# We still try to catch this case by grepping also for "error:".
echo "$output" | grep -qE "^Match #|error:"
if [ $? -eq 0 ]
then
exit 1
fi

echo
exit 0
15 changes: 14 additions & 1 deletion contrib/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ fi

if [ -n "$BUILD" ]
then
make -j2 "$BUILD"
if [ "$TRAVIS_COMPILER" = "clang" ]
then
# Use bear to generate compile_commands.json
# This needs to be the first make command because otherwise make does not invoke the compiler because the files are up to date.
# This needs to be a separate make command because bear does not seem to like our "make check".
bear -- make -j2
fi
make -j2 "$BUILD"
fi
if [ "$RUN_VALGRIND" = "yes" ]
then
Expand Down Expand Up @@ -66,3 +73,9 @@ if [ "$CTIMETEST" = "yes" ]
then
./libtool --mode=execute valgrind --error-exitcode=42 ./valgrind_ctime_test > valgrind_ctime_test.log 2>&1
fi

# This would also run on gcc builds but there's no need to run it for both compilers.
if [ "$TRAVIS_COMPILER" = "clang" ]
then
./clang-query.sh
fi

0 comments on commit 931cc9b

Please sign in to comment.