Skip to content

Commit 5817aea

Browse files
authored
Cleanup and refinement of our "clang-tidy" pipeline. (AcademySoftwareFoundation#2093)
Clang-tidy is part of clang tooling that's a static analyzer of everything from "unsafe" code that violates security guidelines, to stylistic fixes. There are many hundreds of tests. Initially we are just enabling some basic security and correctness tests. Will add more later. All you need to do to run it is make CLANG_TIDY=1 Or if running cmake directly, use -DCLANG_TIDY=1 Warning: it is a long compile. Set up a Travis test to run clang-tidy for static analysis. But after much trial and error, I just couldn't get it to run in the 50 minutes that Travis allows. So for now it's disabled. But if we ever have a CI solution that executes faster or has longer time limits, I would love to do a full static analysis on every PR. For now, I will have to be content running it by hand on my own machine from time to time (and for every tagged release).
1 parent 1dfb59d commit 5817aea

File tree

5 files changed

+69
-12
lines changed

5 files changed

+69
-12
lines changed

.clang-tidy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Checks: '-*,clang-analyzer-security*'
2+
WarningsAsErrors: ''
3+
HeaderFilterRegex: '(OpenImageIO/[a-zA-Z0-9_]+\.h)|(imageio)|(oiio)|(iv/)|(_pvt\.h)'
4+
AnalyzeTemporaryDtors: false
5+
User: lg
6+
CheckOptions:
7+
- key: modernize-use-nullptr.NullMacros
8+
value: 'NULL'
9+
- key: modernize-use-emplace.SmartPointers
10+
value: 'OIIO::intrusive_ptr'
11+
...

.travis.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,27 @@ install:
7171
export PATH=/usr/local/opt/qt5/bin:$PATH ;
7272
export PATH=/usr/local/opt/python/libexec/bin:$PATH ;
7373
export PYTHONPATH=/usr/local/lib/python${PYTHON_VERSION}/site-packages:$PYTHONPATH ;
74+
if [ "${CLANG_TIDY}" != "" ] ; then
75+
export PATH=/usr/local/Cellar/llvm/7.0.0_1/bin:$PATH ;
76+
echo $PATH ;
77+
fi ;
7478
elif [ $TRAVIS_OS_NAME == linux ] ; then
7579
CXX="ccache $CXX" CCACHE_CPP2=1 src/build-scripts/build_openexr.bash ;
7680
export ILMBASE_ROOT_DIR=$PWD/ext/openexr-install ;
7781
export OPENEXR_ROOT_DIR=$PWD/ext/openexr-install ;
7882
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OPENEXR_ROOT_DIR/lib ;
7983
CXX="ccache $CXX" CCACHE_CPP2=1 src/build-scripts/build_ocio.bash ;
8084
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD/ext/OpenColorIO/dist/lib ;
85+
if [ "${CLANG_TIDY}" != "" ] ; then
86+
export LLVM_VERSION=7.0.0 ;
87+
export LLVMTAR=clang+llvm-${LLVM_VERSION}-x86_64-linux-gnu-ubuntu-14.04.tar.xz ;
88+
time wget http://releases.llvm.org/${LLVM_VERSION}/${LLVMTAR} ;
89+
tar xf $LLVMTAR ;
90+
rm -f $LLVMTAR ;
91+
mv clang+llvm* llvm-install ;
92+
export LLVM_DIRECTORY=$PWD/llvm-install ;
93+
export PATH=$LLVM_DIRECTORY/bin:$PATH ;
94+
fi
8195
fi
8296
- src/build-scripts/install_test_images.bash
8397

@@ -99,7 +113,7 @@ script:
99113
- git diff --color ;
100114
THEDIFF=`git diff` ;
101115
if [ "$THEDIFF" != "" ] ; then
102-
echo "git diff was not empty. Failing clang-format check." ;
116+
echo "git diff was not empty. Failing clang-format or clang-tidy check." ;
103117
exit 1 ;
104118
fi
105119

@@ -172,7 +186,7 @@ matrix:
172186
## osx_image: xcode8.3
173187
## compiler: clang
174188
## env: LINKSTATIC=1 USE_FFMPEG=0 USE_OCIO=0 USE_OPENCV=0
175-
# Build with sanitizers
189+
# Dynamic analysis: build with sanitizers and run most tests.
176190
# Because this test takes so long to run, only build for PRs, direct
177191
# pushes to master or RB branches, or if the branch name includes
178192
# "sanitize". Other ordinary work branch pushes don't need to run this
@@ -239,6 +253,14 @@ matrix:
239253
compiler: clang
240254
env: OIIOTARGET=clang-format SKIP_TESTS=1 BUILD_MISSING_DEPS=0
241255
#if: branch =~ /(master|RB|travis|simd)/ OR type = pull_request
256+
# Static analysis: build with clang-tidy.
257+
# Disabled -- this takes so long to run, it can't complete in Travis's
258+
# 50 minute time limit. Left here just to show how it should work.
259+
# - name: "Static analysis / clang-tidy"
260+
# os: linux
261+
# compiler: gcc
262+
# env: CLANG_TIDY=1 CLANG_TIDY_FIX=1 SKIP_TESTS=1
263+
# if: branch =~ /(master|RB|travis|tidy)/ OR type = pull_request
242264
# Build with EMBEDPLUGINS=0, both platforms.
243265
# FIXME: doesn't work yet on Travis
244266
# - os: linux

site/spi/Makefile-bits-arnold

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,10 @@ else ifeq (${platform}, macosx)
199199
MY_CMAKE_FLAGS += \
200200
-DCMAKE_C_COMPILER=gcc-8 -DCMAKE_CXX_COMPILER=g++-8
201201
USE_LIBCPLUSPLUS := 0
202+
else ifeq (${COMPILER},clang6)
203+
MY_CMAKE_FLAGS += -DCMAKE_C_COMPILER=/usr/local/Cellar/llvm@6/6.0.1_1/bin/clang -DCMAKE_CXX_COMPILER=/usr/local/Cellar/llvm@6/6.0.1_1/bin/clang++
202204
else ifeq (${COMPILER},clang7)
203-
MY_CMAKE_FLAGS += -DCMAKE_C_COMPILER=/usr/local/Cellar/llvm/7.0.0/bin/clang -DCMAKE_CXX_COMPILER=/usr/local/Cellar/llvm/7.0.0/bin/clang++
205+
MY_CMAKE_FLAGS += -DCMAKE_C_COMPILER=/usr/local/Cellar/llvm/7.0.0_1/bin/clang -DCMAKE_CXX_COMPILER=/usr/local/Cellar/llvm/7.0.0_1/bin/clang++
204206
else ifeq (${COMPILER},clang)
205207
MY_CMAKE_FLAGS += -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
206208
else ifeq (${COMPILER},gcc)

src/build-scripts/install_homebrew_deps.bash

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ if [ "$LINKSTATIC" == "1" ] ; then
5151
brew install homebrew/dupes/bzip2
5252
brew install yaml-cpp --with-static-lib
5353
fi
54+
if [ "$CLANG_TIDY" != "" ] ; then
55+
# If we are running for the sake of clang-tidy only, we will need
56+
# a modern clang version not just the xcode one.
57+
brew install llvm
58+
fi
59+
5460
echo ""
5561
echo "After brew installs:"
5662
brew list --versions

src/cmake/compiler.cmake

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ option (LINKSTATIC "Link with static external libraries when possible" OFF)
1818
option (CODECOV "Build code coverage tests" OFF)
1919
set (SANITIZE "" CACHE STRING "Build code using sanitizer (address, thread)")
2020
option (CLANG_TIDY "Enable clang-tidy" OFF)
21-
set (CLANG_TIDY_CHECKS "-*" CACHE STRING "clang-tidy checks to perform")
21+
set (CLANG_TIDY_CHECKS "" CACHE STRING "clang-tidy checks to perform (none='-*')")
2222
set (CLANG_TIDY_ARGS "" CACHE STRING "clang-tidy args")
2323
option (CLANG_TIDY_FIX "Have clang-tidy fix source" OFF)
2424
set (CLANG_FORMAT_INCLUDES "src/*.h" "src/*.cpp"
@@ -317,15 +317,31 @@ endif ()
317317

318318
# clang-tidy options
319319
if (CLANG_TIDY)
320-
set (CMAKE_CXX_CLANG_TIDY "clang-tidy;-header-filter=(${PROJECT_NAME})|(${PROJ_NAME})|(${PROJ_NAME_LOWER})|(_pvt.h)")
321-
if (CLANG_TIDY_ARGS)
322-
set (CMAKE_CXX_CLANG_TIDY "${CMAKE_CXX_CLANG_TIDY};${CLANG_TIDY_ARGS}")
323-
endif ()
324-
if (CLANG_TIDY_FIX)
325-
set (CMAKE_CXX_CLANG_TIDY "${CMAKE_CXX_CLANG_TIDY};-fix")
320+
find_program(CLANG_TIDY_EXE NAMES "clang-tidy"
321+
DOC "Path to clang-tidy executable")
322+
message (STATUS "CLANG_TIDY_EXE ${CLANG_TIDY_EXE}")
323+
if (CLANG_TIDY_EXE AND NOT ${CMAKE_VERSION} VERSION_LESS 3.6)
324+
set (CMAKE_CXX_CLANG_TIDY
325+
"${CLANG_TIDY_EXE}"
326+
)
327+
if (CLANG_TIDY_ARGS)
328+
list (APPEND CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_ARGS})
329+
endif ()
330+
if (CLANG_TIDY_CHECKS)
331+
list (APPEND CMAKE_CXX_CLANG_TIDY -checks="${CLANG_TIDY_CHECKS}")
332+
endif ()
333+
execute_process (COMMAND ${CMAKE_CXX_CLANG_TIDY} -list-checks
334+
OUTPUT_VARIABLE tidy_checks
335+
OUTPUT_STRIP_TRAILING_WHITESPACE)
336+
if (CLANG_TIDY_FIX)
337+
list (APPEND CMAKE_CXX_CLANG_TIDY "-fix")
338+
endif ()
339+
message (STATUS "clang-tidy command line is: ${CMAKE_CXX_CLANG_TIDY}")
340+
message (STATUS "${tidy_checks}")
341+
else ()
342+
message (STATUS "Cannot run clang-tidy as requested")
326343
endif ()
327-
set (CMAKE_CXX_CLANG_TIDY "${CMAKE_CXX_CLANG_TIDY};-checks=${CLANG_TIDY_CHECKS}")
328-
message (STATUS "clang-tidy command line is: ${CMAKE_CXX_CLANG_TIDY}")
344+
# Hint: run with CLANG_TIDY_ARGS=-list-checks to list all the checks
329345
endif ()
330346

331347
# clang-format

0 commit comments

Comments
 (0)