Skip to content

Commit

Permalink
feat(tool): use ast-grep to lint code base
Browse files Browse the repository at this point in the history
  • Loading branch information
suda-morris authored and hfudev committed Apr 9, 2024
1 parent 96c81c8 commit c4c8965
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .gitlab/ci/pre_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ check_version:
check_api_usage:
extends: .pre_check_template
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_examples_extra_component_dirs.sh

Expand Down
8 changes: 4 additions & 4 deletions components/hal/sdio_slave_hal.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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
for (int i = 0; i < hal->send_queue_size + 1; i++) {
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);
if (ret != ESP_OK) return ret;
Expand All @@ -202,7 +202,7 @@ static esp_err_t init_send_queue(sdio_slave_context_t *hal)
last = NULL;
//clear the queue
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
sdio_ringbuf_return(buf, (uint8_t *) first);
return ESP_OK;
Expand Down Expand Up @@ -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_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);
desc->owner = 1;
STAILQ_INSERT_TAIL(queue, desc, qe);
Expand Down
2 changes: 2 additions & 0 deletions sgconfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ruleDirs:
- ./tools/ci/sg_rules
10 changes: 0 additions & 10 deletions tools/ci/check_api_violation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
exit 1
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
21 changes: 0 additions & 21 deletions tools/ci/check_examples_rom_header.sh

This file was deleted.

1 change: 1 addition & 0 deletions tools/ci/exclude_check_tools_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ tools/ci/python_packages/idf_http_server_test/**/*
tools/ci/python_packages/idf_iperf_test_util/**/*
tools/esp_prov/**/*
tools/ci/sort_yaml.py
tools/ci/sg_rules/*
1 change: 0 additions & 1 deletion tools/ci/executable-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ tools/ci/check_codeowners.py
tools/ci/check_deprecated_kconfigs.py
tools/ci/check_esp_memory_utils_headers.sh
tools/ci/check_examples_extra_component_dirs.sh
tools/ci/check_examples_rom_header.sh
tools/ci/check_executables.py
tools/ci/check_idf_version.sh
tools/ci/check_kconfigs.py
Expand Down
32 changes: 32 additions & 0 deletions tools/ci/sg_rules/no_private_rom_api_in_examples.yml
Original file line number Diff line number Diff line change
@@ -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: ''
35 changes: 35 additions & 0 deletions tools/ci/sg_rules/no_std_assert_in_hal_component.yml
Original file line number Diff line number Diff line change
@@ -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: ''

0 comments on commit c4c8965

Please sign in to comment.