Skip to content

Commit

Permalink
Added the CI for clang-tidy in github workflows.
Browse files Browse the repository at this point in the history
  • Loading branch information
msiddhu committed Jul 11, 2024
1 parent eb2b975 commit 091efdd
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/clang-tidy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: clang-tidy

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
clang-tidy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Setup Environment
env:
PROTOBUF_VERSION: '23.3'
ABSEIL_CPP_VERSION: '20230125.3'
CXX_STANDARD: '14'
run: |
sudo apt update -y
sudo apt install -y --no-install-recommends --no-install-suggests \
build-essential \
clang-18
sudo -E ./ci/setup_cmake.sh
sudo -E ./ci/setup_ci_environment.sh
sudo -E ./ci/setup_googletest.sh
sudo -E ./ci/install_abseil.sh
sudo -E ./ci/install_protobuf.sh
sudo -E ./ci/setup_grpc.sh -m -p protobuf -p abseil-cpp
# WITH_OPENTRACING_SHIM and WITH_ETW is not present and will be excluded.
- name: Prepare CMake
run: |
TOPDIR=$(pwd)
mkdir -p build && cd build
echo "Running cmake..."
CC="clang" CXX="clang++" cmake .. \
-DCMAKE_CXX_STANDARD=14 \
-DWITH_STL=CXX14 \
-DWITH_OTLP_HTTP=ON \
-DWITH_OTLP_FILE=ON \
-DWITH_PROMETHEUS=ON \
-DWITH_ZIPKIN=ON \
-DWITH_ABSEIL=ON \
-DWITH_ELASTICSEARCH=ON \
-DWITH_OTLP_HTTP_COMPRESSION=ON \
-DWITH_EXAMPLES=ON \
-DWITH_EXAMPLES_HTTP=ON \
-DBUILD_W3CTRACECONTEXT_TEST=ON \
-DWITH_METRICS_EXEMPLAR_PREVIEW=ON \
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DWITH_OTLP_GRPC=ON \
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=ON
cd ..
- name: Run Clang-Tidy
run: |
chmod +x tools/clang-tidy.sh
./tools/clang-tidy.sh | tee clang-tidy-output.log
- uses: actions/upload-artifact@v4
if: success() || failure()
with:
name: Logs (clang-tidy)
path: clang-tidy-output.log

- name: Count Warnings
run: |
COUNT=$(grep -c "warning:" clang-tidy-output.log)
echo "clang-tidy reported ${COUNT} warning(s)"
60 changes: 60 additions & 0 deletions tools/clang-tidy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

# Define the find command to exclude certain directories and find relevant files

# This command will exclude the following directories: same as format.sh but removing the opentelemetry-shim as well.
# - third_party
# - tools
# - .git
# - _deps
# - build
# - out
# - .vs
# - opentelemetry_logo.png
# - TraceLoggingDynamic.h:
# - opentelemetry-shim

FIND="find . -name third_party -prune -o -name opentracing-shim -prune -o -name tools -prune -o -name .git -prune -o -name _deps -prune -o -name build -prune -o -name out -prune -o -name .vs -prune -o -name opentelemetry_logo.png -prune -o -name TraceLoggingDynamic.h -prune -o -name '*.h' -o -name '*.cc' -print"

# Define the path to the compile_commands.json
COMPILE_COMMANDS_PATH="./build/compile_commands.json"

# Define the log file name
LOG_FILE="clang-tidy-output.log"


# Don't require this in the log file as the clang-tidy just want to go into system libraries and produce those warnings as well.
# This should be probably avioded with header-filter
FILTER_PATTERN="Suppressed [0-9]* warnings.*|Use -header-filter=.*|Use -system-headers.* | warnings generated."

# Check if the log file is empty or does not exist
rm -f $LOG_FILE

run_clang_tidy() {
clang-tidy --dump-config
file=$1
echo "Running clang-tidy on $file" >> $LOG_FILE
clang-tidy -p=$COMPILE_COMMANDS_PATH "$file" 2>&1 | grep -Ev "$FILTER_PATTERN" | tee -a $LOG_FILE
}

NUM_PROCESSORS=$(nproc)


export -f run_clang_tidy
export COMPILE_COMMANDS_PATH
export LOG_FILE
export FILTER_PATTERN

# Find all relevant files and store them in a variable
FILES=$(eval $FIND)
NUM_FILES=$(echo "$FILES" | wc -l)


echo "Number of processors available: $NUM_PROCESSORS"
echo "Number of files to process: $NUM_FILES"

# Run clang-tidy in parallel on each file found by the find command
echo "$FILES" | xargs -P $NUM_PROCESSORS -n 1 bash -c 'run_clang_tidy "$@"' _

0 comments on commit 091efdd

Please sign in to comment.