Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLANG Tidy as a CI step #1241

Merged
merged 14 commits into from
Aug 15, 2024
37 changes: 35 additions & 2 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
# Settings file automatically used by clang-tidy
# Heavily adapted from the deal.II clang-tidy file
#
# See ./contrib/utilities/run_clang_tidy.sh for details

# We disable performance-inefficient-string-concatenation because we don't care about "a"+to_string(5)+...
#
# Rationale for disabling warnings:
#
# - selected modernize-* warnings:
# Some of these produce a lot of noise for limited utility.
#
# - performance-inefficient-string-concatenation:
# We don't care about "a"+to_string(5)+...
#
# - performance-no-automatic-move:
# All modern compilers perform the return value optimization and we prefer
# to keep things const.
#

Checks: "-*,cppcoreguidelines-pro-type-static-cast-downcast,google-readability-casting,modernize-*,-modernize-pass-by-value,-modernize-raw-string-literal,-modernize-use-auto,-modernize-use-override,-modernize-use-default-member-init,-modernize-use-transparent-functors,use-emplace,mpi-*,performance-*,-performance-inefficient-string-concatenation"
Checks: >
-*,
cppcoreguidelines-pro-type-static-cast-downcast,
modernize-*,
-modernize-avoid-c-arrays,
-modernize-concat-nested-namespaces,
-modernize-pass-by-value,
-modernize-raw-string-literal,
-modernize-return-braced-init-list,
-modernize-use-auto,
-modernize-use-default-member-init,
-modernize-use-nodiscard,
-modernize-use-override,
-modernize-use-trailing-return-type,
-modernize-use-transparent-functors,
mpi-*,
performance-*,
-performance-inefficient-string-concatenation,
-performance-no-automatic-move,
-performance-avoid-endl,
readability-qualified-auto

WarningsAsErrors: '*'
69 changes: 69 additions & 0 deletions .github/workflows/clang_tidy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#Heavily inspired by the deal.II version of this workflow
#https://github.com/dealii/dealii

name: Clang tidy

on:
push:
# Runs on every push on master branch
branches:
- master
# Runs on every push on master branch. If a push contains multiple commits, it will be ran on the latest one.
pull_request:
paths-ignore:
- 'doc/**'
- 'contrib/**'

# Cancels running instances when a pull request is updated by a commit
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref || github.run_id }}
cancel-in-progress: true

env:
COMPILE_JOBS: 4

jobs:
tidy:
name: tidy
runs-on: [ubuntu-22.04]

strategy:
fail-fast: false
matrix:
dealii_version: ["master"]

# Run steps in container of dealii's master branch
#container:
# image: dealii/dealii:${{ matrix.dealii_version }}-focal
# options: --user root

steps:
- name: Setup
run: |
# Since dealii image doesn't include Node.js, we'll install it
sudo apt-get install -y software-properties-common
sudo add-apt-repository ppa:ginggs/deal.ii-9.5.1-backports
sudo apt-get update
sudo apt-get install -y --no-install-recommends nodejs \
clang-12 \
clang-tidy-12 \
numdiff \
libboost-all-dev \
libcgal-dev \
libp4est-dev \
trilinos-all-dev \
petsc-dev \
libmetis-dev \
libhdf5-mpi-dev \
deal.ii

# Checks-out Lethe with branch of triggering commit
- name: Checkout code
uses: actions/checkout@v2

- name: Tidy
run: |
mkdir build
cd build
export PATH=/usr/lib/llvm-12/share/clang/:$PATH
../contrib/utilities/run_clang_tidy.sh ..
79 changes: 79 additions & 0 deletions contrib/utilities/run_clang_tidy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash
## ------------------------------------------------------------------------
##
## SPDX-License-Identifier: LGPL-2.1-or-later
## Copyright (C) 2018 - 2020 by the deal.II authors
##
## This file is part of the deal.II library.
##
## Part of the source code is dual licensed under Apache-2.0 WITH
## LLVM-exception OR LGPL-2.1-or-later. Detailed license information
## governing the source code and code contributions can be found in
## LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
##
## ------------------------------------------------------------------------

#
# This script runs the clang-tidy tool on the deal.II code base.
#
#
# Usage:
# /contrib/utilities/run_clang_tidy.sh SRC_DIR OPTIONAL_CMAKE_ARGS
# with:
# SRC_DIR points to a deal.II source directory
# OPTIONAL_CMAKE_ARGS are optional arguments to pass to CMake
# make sure to run this script in an empty build directory
#
# Requirements:
# Clang 5.0.1+ and have clang, clang++, and run-clang-tidy.py in
# your path.

# grab first argument and make relative path an absolute one:
SRC=$1
SRC=$(cd "$SRC";pwd)
shift

if test ! -d "$SRC/source" -o ! -d "$SRC/include" -o ! -d "$SRC/examples" -o ! -f "$SRC/CMakeLists.txt" ; then
echo "Usage:"
echo " run_clang_tidy.sh /path/to/dealII"
exit 1
fi
echo "SRC-DIR=$SRC"

# enable MPI (to get MPI warnings)
# export compile commands (so that run-clang-tidy.py works)
ARGS=("-D" "CMAKE_EXPORT_COMPILE_COMMANDS=ON" "-D" "CMAKE_BUILD_TYPE=Debug" "$@")

# for a list of checks, see /.clang-tidy
cat "$SRC/.clang-tidy"

if ! [ -x "$(command -v run-clang-tidy.py)" ] || ! [ -x "$(command -v clang++)" ]; then
echo "make sure clang, clang++, and run-clang-tidy.py (part of clang) are in the path"
exit 2
fi

CC=clang CXX=clang++ cmake "${ARGS[@]}" "$SRC" || (echo "cmake failed!"; false) || exit 2

cmake --build . -j4

# generate allheaders.h
#(cd include; find . -name '*.h'; cd $SRC/include/; find . -name '*.h') | grep -v allheaders.h | grep -v undefine_macros.h | sed 's|^./|#include <|' | sed 's|$|>|' >include/deal.II/allheaders.h

# finally run clang-tidy on lethe
#
# pipe away stderr (just contains nonsensical "x warnings generated")
# pipe output to output.txt
run-clang-tidy.py -p . -quiet -header-filter "$SRC/include/*" -extra-arg='-DCLANG_TIDY' 2>error.txt >output.txt

# grep interesting errors and make sure we remove duplicates:
grep -E '(warning|error): ' output.txt | sort | uniq >clang-tidy.log

# if we have errors, report them and set exit status to failure
if [ -s clang-tidy.log ]; then
cat clang-tidy.log
exit 4
fi

echo "OK"
exit 0

Loading