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

CI: Add Clang tidy #25

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE/new_component.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 50 additions & 0 deletions .github/filter_sarif.py
Original file line number Diff line number Diff line change
@@ -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()
59 changes: 59 additions & 0 deletions .github/workflows/clang-tidy.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dependencies.lock
**/managed_components/**
.vscode/
doxygen/
warnings.txt
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 21 additions & 0 deletions clang_tidy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions clang_tidy/README.md
Original file line number Diff line number Diff line change
@@ -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.
Loading