From cb327f51c5c5c5fe51dea27e7fc610aba7311e96 Mon Sep 17 00:00:00 2001 From: Peter Marcisovsky Date: Wed, 6 Mar 2024 15:47:06 +0100 Subject: [PATCH] refactor: Add clang check --- .../PULL_REQUEST_TEMPLATE/new_component.md | 5 +- .github/filter_sarif.py | 50 ++++++++++++++++ .github/workflows/clang-tidy.yml | 59 +++++++++++++++++++ .gitignore | 1 + README.md | 2 +- clang_tidy/CMakeLists.txt | 21 +++++++ clang_tidy/README.md | 3 + 7 files changed, 138 insertions(+), 3 deletions(-) create mode 100755 .github/filter_sarif.py create mode 100644 .github/workflows/clang-tidy.yml create mode 100644 clang_tidy/CMakeLists.txt create mode 100644 clang_tidy/README.md diff --git a/.github/PULL_REQUEST_TEMPLATE/new_component.md b/.github/PULL_REQUEST_TEMPLATE/new_component.md index 3f60295e..f10b8f00 100644 --- a/.github/PULL_REQUEST_TEMPLATE/new_component.md +++ b/.github/PULL_REQUEST_TEMPLATE/new_component.md @@ -3,8 +3,9 @@ - [ ] Component contains License - [ ] Component contains README.md - [ ] Component contains idf_component.yml file with `url` field defined -- [ ] Component was added to [upload job](https://github.com/espressif/esp-usb/blob/master/.github/workflows/upload_component.yml#L18) -- [ ] Component was added to build job +- [ ] Component was added to [upload job](https://github.com/espressif/esp-usb/blob/master/.github/workflows/upload_component.yml#L34) +- [ ] Component was added to [build job](https://github.com/espressif/esp-usb/blob/master/.idf_build_apps.toml#L2) +- [ ] Component was added to [clang tidy job](https://github.com/espressif/esp-usb/blob/master/clang_tidy/CMakeLists.txt#L7) - [ ] _Optional:_ Component contains unit tests - [ ] CI passing diff --git a/.github/filter_sarif.py b/.github/filter_sarif.py new file mode 100755 index 00000000..add9044f --- /dev/null +++ b/.github/filter_sarif.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 + +import argparse +import copy +import json +import typing + + +def process(in_file: typing.TextIO, out_file: typing.TextIO, include_prefix_list: typing.List[str]) -> None: + in_json = json.load(in_file) + if len(in_json['runs']) != 1: + raise NotImplementedError('Only 1 run is supported') + in_results = in_json['runs'][0]['results'] + out_results = [] + for result in in_results: + locations = result['locations'] + if len(locations) != 1: + raise NotImplementedError('Only 1 location is supported') + artifact_location = locations[0]['physicalLocation']['artifactLocation'] + uri = artifact_location['uri'] + new_uri = None + for include_prefix in include_prefix_list: + if uri.startswith(include_prefix): + new_uri = uri.replace(include_prefix, '') + break + if not new_uri: + continue + new_result = copy.deepcopy(result) + new_result['locations'][0]['physicalLocation']['artifactLocation']['uri'] = new_uri + out_results.append(new_result) + + out_json = copy.deepcopy(in_json) + out_json['runs'][0]['results'] = out_results + json.dump(out_json, out_file, indent=True) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('-o', '--output', type=argparse.FileType('w'), help='Output filtered SARIF file') + parser.add_argument('--include-prefix', required=True, action='append', + help='File prefix for source code to include in analysis') + parser.add_argument('input_file', type=argparse.FileType('r'), help='Input SARIF file') + args = parser.parse_args() + process(args.input_file, args.output, args.include_prefix) + + +if __name__ == '__main__': + main() diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml new file mode 100644 index 00000000..503d1d66 --- /dev/null +++ b/.github/workflows/clang-tidy.yml @@ -0,0 +1,59 @@ +name: Run clang-tidy + +on: + pull_request: + push: + branches: + - master + +jobs: + build: + name: Run clang-tidy + runs-on: ubuntu-20.04 + container: espressif/idf:latest + steps: + - uses: actions/checkout@v3 + with: + submodules: 'true' + - name: Install libtinfo (esp-clang dependency) + run: | + export DEBIAN_FRONTEND=noninteractive + apt update && apt-get install -y libtinfo5 + - name: Install esp-clang + run: | + ${IDF_PATH}/tools/idf_tools.py --non-interactive install esp-clang + - name: Install clang-tidy-sarif + run: | + curl -sSL https://github.com/psastras/sarif-rs/releases/download/clang-tidy-sarif-v0.3.3/clang-tidy-sarif-x86_64-unknown-linux-gnu -o clang-tidy-sarif + chmod +x clang-tidy-sarif + - name: Install pyclang + run: | + . ${IDF_PATH}/export.sh + pip install pyclang~=0.2.0 + - name: Run code analysis + shell: bash + env: + IDF_TOOLCHAIN: clang + IDF_TARGET: esp32s3 + working-directory: clang_tidy + run: | + . ${IDF_PATH}/export.sh + idf.py reconfigure + idf.py clang-check --include-paths $GITHUB_WORKSPACE --exclude-paths $PWD --run-clang-tidy-py run-clang-tidy + cp warnings.txt ../ + - name: Convert clang-tidy results into SARIF output + run: | + export PATH=$PWD:$PATH + ./clang-tidy-sarif -o results.sarif.raw warnings.txt + python3 $GITHUB_WORKSPACE/.github/filter_sarif.py -o results.sarif --include-prefix ${GITHUB_WORKSPACE}/ results.sarif.raw + - uses: actions/upload-artifact@v2 + with: + path: | + warnings.txt + results.sarif + results.sarif.raw + - name: Upload SARIF file + uses: github/codeql-action/upload-sarif@v2 + with: + sarif_file: results.sarif + category: clang-tidy diff --git a/.gitignore b/.gitignore index ab46bd1f..f144b616 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ dependencies.lock **/managed_components/** .vscode/ doxygen/ +warnings.txt diff --git a/README.md b/README.md index 7e496f7a..734cb873 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit) -[![Build and Run Test Application](https://github.com/espressif/esp-usb/actions/workflows/build_and_run_test_app.yml/badge.svg?branch=master)](https://github.com/espressif/esp-usb/actions/workflows/build_and_run_test_app.yml) +[![Build and Run Test Application](https://github.com/espressif/esp-usb/actions/workflows/build_and_run_test_app_usb.yml/badge.svg?branch=master)](https://github.com/espressif/esp-usb/actions/workflows/build_and_run_test_app_usb.yml) [![Clang-Tidy](https://github.com/espressif/esp-usb/actions/workflows/clang-tidy.yml/badge.svg?branch=master)](https://github.com/espressif/esp-usb/security/code-scanning?query=is%3Aopen+branch%3Amaster) # Espressif ESP-USB diff --git a/clang_tidy/CMakeLists.txt b/clang_tidy/CMakeLists.txt new file mode 100644 index 00000000..8e40122b --- /dev/null +++ b/clang_tidy/CMakeLists.txt @@ -0,0 +1,21 @@ +# The following lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) +include($ENV{IDF_PATH}/tools/cmake/version.cmake) + +# Specify components that will be checked with Clang tidy +set(EXTRA_COMPONENT_DIRS + ../device/esp_tinyusb + ../host/class/cdc/esp_modem_usb_dte + ../host/class/cdc/usb_host_cdc_acm + ../host/class/cdc/usb_host_ch34x_vcp + ../host/class/cdc/usb_host_cp210x_vcp + ../host/class/cdc/usb_host_ftdi_vcp + ../host/class/cdc/usb_host_vcp + ../host/class/hid/usb_host_hid + ../host/class/msc/usb_host_msc + ../host/class/uvc/usb_host_uvc +) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(clang_check) \ No newline at end of file diff --git a/clang_tidy/README.md b/clang_tidy/README.md new file mode 100644 index 00000000..b5ee078b --- /dev/null +++ b/clang_tidy/README.md @@ -0,0 +1,3 @@ +# ESP-USB Clang tidy + +This folder aims to store source files for Clang tidy CI job, which is run for this repository in the GitHub CI. \ No newline at end of file