Merge branch 'feat/ast_grep_lint_tool' into 'master'

Use ast-grep to create customized lint rules

See merge request espressif/esp-idf!30050
This commit is contained in:
morris 2024-04-10 10:50:09 +08:00
commit 57696a437d
9 changed files with 76 additions and 37 deletions

View File

@ -25,7 +25,8 @@ check_version:
check_api_usage: check_api_usage:
extends: .pre_check_template extends: .pre_check_template
script: script:
- tools/ci/check_examples_rom_header.sh - python -m pip install ast-grep-cli # use ast-grep to describe customized lint rules
- ast-grep scan
- tools/ci/check_api_violation.sh - tools/ci/check_api_violation.sh
- tools/ci/check_examples_extra_component_dirs.sh - tools/ci/check_examples_extra_component_dirs.sh

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -190,7 +190,7 @@ static esp_err_t init_send_queue(sdio_slave_context_t *hal)
//loop in the ringbuf to link all the desc one after another as a ring //loop in the ringbuf to link all the desc one after another as a ring
for (int i = 0; i < hal->send_queue_size + 1; i++) { for (int i = 0; i < hal->send_queue_size + 1; i++) {
rcv_res = sdio_ringbuf_recv(buf, &last, NULL, RINGBUF_GET_ONE); rcv_res = sdio_ringbuf_recv(buf, &last, NULL, RINGBUF_GET_ONE);
assert (rcv_res == ESP_OK); HAL_ASSERT(rcv_res == ESP_OK);
ret = sdio_ringbuf_send(buf, link_desc_to_last, last); ret = sdio_ringbuf_send(buf, link_desc_to_last, last);
if (ret != ESP_OK) return ret; if (ret != ESP_OK) return ret;
@ -202,7 +202,7 @@ static esp_err_t init_send_queue(sdio_slave_context_t *hal)
last = NULL; last = NULL;
//clear the queue //clear the queue
rcv_res = sdio_ringbuf_recv(buf, &first, &last, RINGBUF_GET_ALL); rcv_res = sdio_ringbuf_recv(buf, &first, &last, RINGBUF_GET_ALL);
assert (rcv_res == ESP_OK); HAL_ASSERT(rcv_res == ESP_OK);
HAL_ASSERT(first == last); //there should be only one desc remain HAL_ASSERT(first == last); //there should be only one desc remain
sdio_ringbuf_return(buf, (uint8_t *) first); sdio_ringbuf_return(buf, (uint8_t *) first);
return ESP_OK; return ESP_OK;
@ -634,7 +634,7 @@ void sdio_slave_hal_recv_flush_one_buffer(sdio_slave_context_t *hal)
{ {
sdio_slave_hal_recv_stailq_t *const queue = &hal->recv_link_list; sdio_slave_hal_recv_stailq_t *const queue = &hal->recv_link_list;
sdio_slave_ll_desc_t *desc = STAILQ_FIRST(queue); sdio_slave_ll_desc_t *desc = STAILQ_FIRST(queue);
assert (desc != NULL && desc->owner == 0); HAL_ASSERT(desc != NULL && desc->owner == 0);
STAILQ_REMOVE_HEAD(queue, qe); STAILQ_REMOVE_HEAD(queue, qe);
desc->owner = 1; desc->owner = 1;
STAILQ_INSERT_TAIL(queue, desc, qe); STAILQ_INSERT_TAIL(queue, desc, qe);

2
sgconfig.yml Normal file
View File

@ -0,0 +1,2 @@
ruleDirs:
- ./tools/ci/sg_rules

View File

@ -20,13 +20,3 @@ if [ $count -gt 0 ]; then
echo "Please try to use the APIs listed in esp_rom/include/esp_rom_xxx.h" echo "Please try to use the APIs listed in esp_rom/include/esp_rom_xxx.h"
exit 1 exit 1
fi fi
# ESP-IDF `hal` component shouldn't call "assert()" directlly
files_to_search=$(git ls-files --full-name 'components/hal/*' | grep -v components/hal/test_apps/)
found_libc_assert=$(grep -E '\W+assert\(' $files_to_search)
if [ -n "$found_libc_assert" ]; then
echo "hal assert violation"
echo $found_libc_assert
echo "Please use HAL_ASSERT() instead of assert() in hal component"
exit 1
fi

View File

@ -1,21 +0,0 @@
#!/usr/bin/env bash
set -uo pipefail
# Examples shouldn't include rom headers directly
output=$(find ${IDF_PATH}/examples -name "*.[chS]" -o -name "*.cpp" -not -path "**/build/**")
files=$(egrep ".*include.*\Wrom\W.*h" ${output} | cut -d ":" -f 1)
found_rom=0
for file in ${files}
do
echo "${file} contains rom headers!"
((found_rom++))
done
if [ $found_rom -eq 0 ]; then
echo "No rom headers found in examples"
exit 0
fi
exit 1

View File

@ -49,3 +49,4 @@ tools/ci/python_packages/idf_http_server_test/**/*
tools/ci/python_packages/idf_iperf_test_util/**/* tools/ci/python_packages/idf_iperf_test_util/**/*
tools/esp_prov/**/* tools/esp_prov/**/*
tools/ci/sort_yaml.py tools/ci/sort_yaml.py
tools/ci/sg_rules/*

View File

@ -55,7 +55,6 @@ tools/ci/check_codeowners.py
tools/ci/check_deprecated_kconfigs.py tools/ci/check_deprecated_kconfigs.py
tools/ci/check_esp_memory_utils_headers.sh tools/ci/check_esp_memory_utils_headers.sh
tools/ci/check_examples_extra_component_dirs.sh tools/ci/check_examples_extra_component_dirs.sh
tools/ci/check_examples_rom_header.sh
tools/ci/check_executables.py tools/ci/check_executables.py
tools/ci/check_idf_version.sh tools/ci/check_idf_version.sh
tools/ci/check_kconfigs.py tools/ci/check_kconfigs.py

View File

@ -0,0 +1,32 @@
# Refer to https://ast-grep.github.io/guide/rule-config.html for Rule Essentials
id: no-private-rom-api-in-c-examples
message: Don't use private ROM APIs in the examples
severity: error # error, warning, info, hint
note: Only APIs prefixed with "esp_rom_" are treated as public.
language: C
files:
- "./examples/**/*"
rule:
kind: preproc_include
has:
field: path
regex: "rom/.*h"
pattern: $N
fix: ''
---
id: no-private-rom-api-in-cpp-examples
message: Don't use private ROM APIs in the examples
severity: error # error, warning, info, hint
note: Only APIs prefixed with "esp_rom_" are treated as public.
language: Cpp
files:
- "./examples/**/*"
rule:
kind: preproc_include
has:
field: path
regex: "rom/.*h"
pattern: $N
fix: ''

View File

@ -0,0 +1,35 @@
# Refer to https://ast-grep.github.io/guide/rule-config.html for Rule Essentials
id: no-std-assert-call-in-hal-component
message: Don't use standard assert function in the hal component
severity: error # error, warning, info, hint
note: The standard assert function depends on newlib(G1) component, but hal is a G0 component
language: C
files:
- "./components/hal/**/*"
ignores:
- "./components/hal/test_apps/**/*"
rule:
kind: expression_statement
pattern: assert($$$ARGS);
fix: HAL_ASSERT($$$ARGS);
---
id: no-std-assert-include-in-hal-component
message: Don't include assert.h in the hal component
severity: error # error, warning, info, hint
note: Please use hal/assert.h to replace assert.h
language: C
files:
- "./components/hal/**/*"
ignores:
- "./components/hal/test_apps/**/*"
rule:
kind: preproc_include
has:
field: path
pattern: $N
constraints:
N:
regex: '^["<]assert' # match "assert.h" or <assert.h>
fix: ''