Skip to content

Commit

Permalink
Move cppcheck to cmake target (Exawind#300)
Browse files Browse the repository at this point in the history
* Move cppcheck to cmake target and update CI accordingly. Add cppcheck problem-matcher. Reduce a few clang-tidy checks.

* Generate compile_commands.json by default.

* Enable problem-matcher for clang-tidy.
  • Loading branch information
jrood-nrel authored Dec 24, 2020
1 parent b1f1b63 commit 2f5e4a7
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 38 deletions.
4 changes: 1 addition & 3 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
#Checks: '*,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-readability-braces-around-statements,-fuchsia-default-arguments,-fuchsia-default-arguments-calls,-fuchsia-overloaded-operator,-fuchsia-statically-constructed-objects,-google-runtime-references,-hicpp-no-assembler,-llvm-include-order,-llvmlibc-callee-namespace,-llvmlibc-implementation-in-namespace,-llvmlibc-restrict-system-libc-headers'
Checks: 'clang-diagnostic-*,clang-analyzer-*,corecppguidelines-*,modernize-*,readability-*'
Checks: 'clang-diagnostic-*,clang-analyzer-*,corecppguidelines-*,modernize-*,readability-*,-readability-magic-numbers,-modernize-avoid-c-arrays,-modernize-use-trailing-return-type,-readability-named-parameter,-readability-implicit-bool-conversion,-readability-isolate-declaration''
WarningsAsErrors: ''
#HeaderFilterRegex: '^.*/(AMReX)/.*\.H$'
HeaderFilterRegex: '^((?!/amrex/Src/|/googletest/).)*$'
AnalyzeTemporaryDtors: false
FormatStyle: none
Expand Down
18 changes: 18 additions & 0 deletions .github/problem-matchers/cppcheck.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"__comment": "Modified from vscode-cpptools's Extension/package.json gcc rule",
"problemMatcher": [
{
"owner": "cppcheck-problem-matcher",
"pattern": [
{
"regexp": "^(.*):(\\d+):(\\d+):\\s+(?:fatal\\s+)?(error|performance|portability|style|warning):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
]
}
]
}
45 changes: 15 additions & 30 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,19 @@ jobs:
-DAMR_WIND_ENABLE_TESTS:BOOL=ON \
-DAMR_WIND_TEST_WITH_FCOMPARE:BOOL=OFF \
-DAMR_WIND_ENABLE_MASA:BOOL=OFF \
-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON \
-DAMR_WIND_ENABLE_CPPCHECK:BOOL=ON \
${{github.workspace}}
- name: Check
working-directory: build-cppcheck
run: |
# Using a working directory for cppcheck makes analysis faster
mkdir cppcheck-wd
# Cppcheck ignores -isystem directories, so we change them to regular -I include directories (with no spaces either)
sed -i '' -e 's/isystem /I/g' compile_commands.json
cppcheck --inline-suppr \
--suppress=unmatchedSuppression --suppress=syntaxError --suppress=internalAstError \
--suppress=unusedFunction --std=c++14 --language=c++ --enable=all --project=compile_commands.json \
-j ${{env.NPROCS}} --cppcheck-build-dir=cppcheck-wd -i ${{github.workspace}}/submods/amrex/Src \
-i ${{github.workspace}}/submods/googletest --output-file=cppcheck.txt
# Warnings in header files are unavoidable, so we filter out submodule headers after analysis
# Might be wrong to assume cppcheck always reports issues using 3 lines
awk -v nlines=2 '/submods\/amrex/ || /submods\/googletest/ {for (i=0; i<nlines; i++) {getline}; next} 1' \
< cppcheck.txt > cppcheck-warnings.txt
- name: Short report
working-directory: build-cppcheck
run: |
cat cppcheck-warnings.txt | egrep 'error:|performance:|portability:|style:|warning:' | sort | \
awk 'BEGIN{i=0}{print $0}{i++}END{print "Warnings: "i}'
run: make cppcheck-ci
- name: Full report
working-directory: build-cppcheck
run: cat cppcheck/cppcheck-full-report.txt
- name: Short report
working-directory: build-cppcheck
run: |
echo "::add-matcher::.github/problem-matchers/gcc.json"
cat cppcheck-warnings.txt
echo "::add-matcher::.github/problem-matchers/cppcheck.json"
cat cppcheck-ci-report.txt
clang-tidy:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -99,18 +84,18 @@ jobs:
-DAMR_WIND_ENABLE_MASA:BOOL=OFF \
-DAMR_WIND_ENABLE_ALL_WARNINGS:BOOL=OFF \
-DAMR_WIND_ENABLE_CLANG_TIDY:BOOL=ON \
-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON \
${{github.workspace}}
- name: Check
working-directory: build-clang-tidy
run: cmake --build . -- -j ${{env.NPROCS}} 2> clang-tidy-warnings.txt
- name: Short report
working-directory: build-clang-tidy
run: |
cat clang-tidy-warnings.txt | grep "warning:" | sort | \
awk 'BEGIN{i=0}{print $0}{i++}END{print "Warnings: "i}'
cmake --build . --parallel ${{env.NPROCS}} 2> clang-tidy-full-report.txt
cat clang-tidy-full-report.txt | grep "warning:" | grep -v "submods" | sort | uniq | sort -nr | \
awk 'BEGIN{i=0}{print $0}{i++}END{print "Warnings: "i}' > clang-tidy-ci-report.txt
- name: Full report
working-directory: build-clang-tidy
run: cat clang-tidy-full-report.txt
- name: Short report
working-directory: build-clang-tidy
run: |
#echo "::add-matcher::.github/problem-matchers/gcc.json"
cat clang-tidy-warnings.txt
echo "::add-matcher::.github/problem-matchers/gcc.json"
cat clang-tidy-ci-report.txt
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

cmake_minimum_required (VERSION 3.14 FATAL_ERROR)
project(AMR-Wind CXX C)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
Expand All @@ -13,6 +14,7 @@ include(amr-wind-utils)
option(AMR_WIND_ENABLE_ALL_WARNINGS "Show most warnings for most compilers" ON)
option(AMR_WIND_ENABLE_WERROR "Treat compiler warnings as errors" OFF)
option(AMR_WIND_ENABLE_CLANG_TIDY "Compile with clang-tidy static analysis" OFF)
option(AMR_WIND_ENABLE_CPPCHECK "Enable cppcheck static analysis target" OFF)
option(AMR_WIND_ENABLE_FCOMPARE "Enable building fcompare when not testing" OFF)

#Enabling tests overrides the executable options
Expand Down Expand Up @@ -77,7 +79,7 @@ message(STATUS "CMAKE_CXX_COMPILER_ID = ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "CMAKE_CXX_COMPILER_VERSION = ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")

include(${CMAKE_SOURCE_DIR}/cmake/set_rpath.cmake)
include(set_rpath)

#Create target names
set(amr_wind_lib_name "amrwind_obj")
Expand All @@ -90,13 +92,13 @@ add_library(${amr_wind_lib_name} OBJECT)
add_library(${aw_api_lib} SHARED)
add_executable(${amr_wind_exe_name})

init_clang_tidy()
init_code_checks()
if(CLANG_TIDY_EXE)
set_target_properties(${amr_wind_lib_name} ${aw_api_lib} ${amr_wind_exe_name}
PROPERTIES CXX_CLANG_TIDY ${CLANG_TIDY_EXE})
endif()

include(${CMAKE_SOURCE_DIR}/cmake/set_compile_flags.cmake)
include(set_compile_flags)

if (AMR_WIND_ENABLE_NETCDF)
set(CMAKE_PREFIX_PATH ${NETCDF_DIR} ${CMAKE_PREFIX_PATH})
Expand Down
43 changes: 41 additions & 2 deletions cmake/amr-wind-utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ macro(init_amrex)
endif()
endmacro(init_amrex)

macro(init_clang_tidy)
macro(init_code_checks)
if(AMR_WIND_ENABLE_CLANG_TIDY)
find_program(CLANG_TIDY_EXE NAMES "clang-tidy")
if(CLANG_TIDY_EXE)
Expand All @@ -70,4 +70,43 @@ macro(init_clang_tidy)
message(WARNING "clang-tidy not found.")
endif()
endif()
endmacro(init_clang_tidy)

if(AMR_WIND_ENABLE_CPPCHECK)
find_program(CPPCHECK_EXE NAMES "cppcheck")
if(CPPCHECK_EXE)
message(STATUS "cppcheck found: ${CPPCHECK_EXE}")
include(ProcessorCount)
ProcessorCount(NP)
if(NP EQUAL 0)
set(NP 1)
endif()
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/cppcheck)
add_custom_target(cppcheck
COMMAND ${CMAKE_COMMAND} -E echo "Running cppcheck on project using ${NP} cores..."
COMMAND ${CMAKE_COMMAND} -E make_directory cppcheck/cppcheck-wd
# cppcheck ignores -isystem directories, so we change them to regular -I include directories (with no spaces either)
COMMAND sed "s/isystem /I/g" ${CMAKE_BINARY_DIR}/compile_commands.json > cppcheck_compile_commands.json
COMMAND ${CPPCHECK_EXE} --template=gcc --inline-suppr --suppress=internalAstError --suppress=unusedFunction --std=c++14 --language=c++ --enable=all --project=cppcheck_compile_commands.json --cppcheck-build-dir=cppcheck-wd -i ${CMAKE_SOURCE_DIR}/submods/amrex/Src -i ${CMAKE_SOURCE_DIR}/submods/googletest --output-file=cppcheck-full-report.txt -j ${NP}
COMMENT "Run cppcheck on project compile_commands.json"
BYPRODUCTS cppcheck-full-report.txt
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/cppcheck
VERBATIM USES_TERMINAL
)
add_custom_target(cppcheck-ci
# Filter out submodule source files after analysis
COMMAND awk -v nlines=2 "/submods/ {for (i=0; i<nlines; i++) {getline}; next} 1" < cppcheck/cppcheck-full-report.txt > cppcheck/cppcheck-short-report.txt
COMMAND cat cppcheck/cppcheck-short-report.txt | egrep "information:|error:|performance:|portability:|style:|warning:" | sort > cppcheck-ci-report.txt
COMMAND printf "Warnings: " >> cppcheck-ci-report.txt
COMMAND cat cppcheck-ci-report.txt | awk "END{print NR-1}" >> cppcheck-ci-report.txt
COMMENT "Filter cppcheck results to only AMR-Wind files with results in cppcheck-ci-report.txt"
DEPENDS cppcheck
BYPRODUCTS cppcheck-ci-report.txt
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
VERBATIM
)

else()
message(WARNING "cppcheck not found.")
endif()
endif()
endmacro(init_code_checks)

0 comments on commit 2f5e4a7

Please sign in to comment.