Skip to content

github: Add format check. #175

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
121 changes: 121 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# SPDX-License-Identifier: Apache-2.0
#
# Note: The list of ForEachMacros can be obtained using:
#
# git grep -h '^#define [^[:space:]]*FOR_EACH[^[:space:]]*(' include/ \
# | sed "s,^#define \([^[:space:]]*FOR_EACH[^[:space:]]*\)(.*$, - '\1'," \
# | sort | uniq
#
# References:
# - https://clang.llvm.org/docs/ClangFormatStyleOptions.html

---
BasedOnStyle: LLVM
AlignOperands: Align
BreakBeforeTernaryOperators: false
EmptyLineBeforeAccessModifier: LogicalBlock
AlignConsecutiveMacros: AcrossComments
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AttributeMacros:
- __aligned
- __deprecated
- __packed
- __printf_like
- __syscall
- __syscall_always_inline
- __subsystem
BitFieldColonSpacing: After
BreakBeforeBraces: Attach
ColumnLimit: 100
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
ForEachMacros:
- 'ARRAY_FOR_EACH'
- 'ARRAY_FOR_EACH_PTR'
- 'FOR_EACH'
- 'FOR_EACH_FIXED_ARG'
- 'FOR_EACH_IDX'
- 'FOR_EACH_IDX_FIXED_ARG'
- 'FOR_EACH_NONEMPTY_TERM'
- 'FOR_EACH_FIXED_ARG_NONEMPTY_TERM'
- 'RB_FOR_EACH'
- 'RB_FOR_EACH_CONTAINER'
- 'SYS_DLIST_FOR_EACH_CONTAINER'
- 'SYS_DLIST_FOR_EACH_CONTAINER_SAFE'
- 'SYS_DLIST_FOR_EACH_NODE'
- 'SYS_DLIST_FOR_EACH_NODE_SAFE'
- 'SYS_SEM_LOCK'
- 'SYS_SFLIST_FOR_EACH_CONTAINER'
- 'SYS_SFLIST_FOR_EACH_CONTAINER_SAFE'
- 'SYS_SFLIST_FOR_EACH_NODE'
- 'SYS_SFLIST_FOR_EACH_NODE_SAFE'
- 'SYS_SLIST_FOR_EACH_CONTAINER'
- 'SYS_SLIST_FOR_EACH_CONTAINER_SAFE'
- 'SYS_SLIST_FOR_EACH_NODE'
- 'SYS_SLIST_FOR_EACH_NODE_SAFE'
- '_WAIT_Q_FOR_EACH'
- 'Z_FOR_EACH'
- 'Z_FOR_EACH_ENGINE'
- 'Z_FOR_EACH_EXEC'
- 'Z_FOR_EACH_FIXED_ARG'
- 'Z_FOR_EACH_FIXED_ARG_EXEC'
- 'Z_FOR_EACH_IDX'
- 'Z_FOR_EACH_IDX_EXEC'
- 'Z_FOR_EACH_IDX_FIXED_ARG'
- 'Z_FOR_EACH_IDX_FIXED_ARG_EXEC'
- 'Z_GENLIST_FOR_EACH_CONTAINER'
- 'Z_GENLIST_FOR_EACH_CONTAINER_SAFE'
- 'Z_GENLIST_FOR_EACH_NODE'
- 'Z_GENLIST_FOR_EACH_NODE_SAFE'
- 'STRUCT_SECTION_FOREACH'
- 'STRUCT_SECTION_FOREACH_ALTERNATE'
- 'TYPE_SECTION_FOREACH'
- 'K_SPINLOCK'
- 'COAP_RESOURCE_FOREACH'
- 'COAP_SERVICE_FOREACH'
- 'COAP_SERVICE_FOREACH_RESOURCE'
- 'HTTP_RESOURCE_FOREACH'
- 'HTTP_SERVER_CONTENT_TYPE_FOREACH'
- 'HTTP_SERVICE_FOREACH'
- 'HTTP_SERVICE_FOREACH_RESOURCE'
- 'I3C_BUS_FOR_EACH_I3CDEV'
- 'I3C_BUS_FOR_EACH_I2CDEV'
- 'MIN_HEAP_FOREACH'
IfMacros:
- 'CHECKIF'
IncludeCategories:
- Regex: '^".*\.h"$'
Priority: 0
- Regex: '^<(assert|complex|ctype|errno|fenv|float|inttypes|limits|locale|math|setjmp|signal|stdarg|stdbool|stddef|stdint|stdio|stdlib|string|tgmath|time|wchar|wctype)\.h>$'
Priority: 1
- Regex: '^\<zephyr/.*\.h\>$'
Priority: 2
- Regex: '.*'
Priority: 3
IndentCaseLabels: false
IndentGotoLabels: true
IndentAccessModifiers: false
AccessModifierOffset: -4
IndentWidth: 4
InsertBraces: true
InsertNewlineAtEOF: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SortIncludes: Never
SeparateDefinitionBlocks: Always
UseTab: ForContinuationAndIndentation
TabWidth: 4
WhitespaceSensitiveMacros:
- COND_CODE_0
- COND_CODE_1
- IF_DISABLED
- IF_ENABLED
- LISTIFY
- STRINGIFY
- Z_STRINGIFY
- DT_FOREACH_PROP_ELEM_SEP
31 changes: 0 additions & 31 deletions .github/workflows/checkpatch.yml

This file was deleted.

35 changes: 35 additions & 0 deletions .github/workflows/commit_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: 'Check Commit Message'

on:
pull_request:
types:
- opened
- edited
- reopened
- synchronize
branches:
- 'main'

jobs:
check-commit-messages:
runs-on: ubuntu-latest
steps:
- name: 'Check commit messages format'
uses: gsactions/commit-message-checker@v2
with:
pattern: '^[^!]+: [A-Za-z]+.+ .+$'
flags: 'gm'
error: 'Commit subject line must match the following pattern: <scope>: <description>'
excludeTitle: 'false'
excludeDescription: 'true'
checkAllCommitMessages: 'true'
accessToken: ${{ secrets.GITHUB_TOKEN }}
- name: 'Check commit messages length'
uses: gsactions/commit-message-checker@v2
with:
pattern: '^[^#].{10,80}$'
error: 'Commit subject line maximum line length of 80 characters is exceeded.'
excludeTitle: 'false'
excludeDescription: 'true'
checkAllCommitMessages: 'true'
accessToken: ${{ secrets.GITHUB_TOKEN }}
77 changes: 77 additions & 0 deletions .github/workflows/format_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: 'Format Check'

on:
push:
branches:
- 'main'
paths:
- '**/*.c'
- '**/*.cpp'
- '**/*.h'
- '**/*.hpp'

pull_request:
types:
- opened
- edited
- reopened
- synchronize
branches:
- 'main'
paths:
- '**/*.c'
- '**/*.cpp'
- '**/*.h'
- '**/*.hpp'

workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'

jobs:
format-check:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: false
persist-credentials: false

- name: Install clang-format
run: |
sudo apt-get update
sudo apt-get install -y clang-format colordiff

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v46
with:
files: |
cores/arduino/**/*.{c,cpp,cc,cxx,h,hpp}
!cores/arduino/api/**
loader/**/*.{c,cpp,cc,cxx,h,hpp}
!loader/llext_exports.c
libraries/**/*.{c,cpp,cc,cxx,h,hpp}
!libraries/ea_malloc/**

- name: Run clang-format check
if: steps.changed-files.outputs.any_changed == 'true'
run: |
clang-format --version
exit_code=0

for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
file_fmt="${file}.tmp"
clang-format ${file} > ${file_fmt}
diff -q -u ${file} ${file_fmt} >> /dev/null 2>&1 || {
echo "❌ Formatting issues found in: $file"
colordiff -u ${file} ${file_fmt} || true
exit_code=1
}
done

exit $exit_code
60 changes: 33 additions & 27 deletions cores/arduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

/* Check all pins are defined only once */
#define DIGITAL_PIN_CHECK_UNIQUE(i, _) \
((DT_FOREACH_PROP_ELEM_SEP_VARGS( \
DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, (+), \
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), digital_pin_gpios, i)), \
((DT_FOREACH_PROP_ELEM_SEP_VARGS( \
DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, (+), \
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), digital_pin_gpios, i)), \
DT_PHA_BY_IDX(DT_PATH(zephyr_user), digital_pin_gpios, i, pin))) == 1)

#if !LISTIFY(DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios), DIGITAL_PIN_CHECK_UNIQUE, (&&))
Expand All @@ -44,36 +44,37 @@
(DIGITAL_PIN_EXISTS(n, p, i, dev, num) ? i : 0)

/* Only matched pin returns non-zero value, so the sum is matched pin's index */
#define DIGITAL_PIN_GPIOS_FIND_PIN(dev, pin) \
DT_FOREACH_PROP_ELEM_SEP_VARGS(DT_PATH(zephyr_user), digital_pin_gpios, \
LED_BUILTIN_INDEX_BY_REG_AND_PINNUM, (+), dev, pin)
#define DIGITAL_PIN_GPIOS_FIND_PIN(dev, pin) \
DT_FOREACH_PROP_ELEM_SEP_VARGS(DT_PATH(zephyr_user), digital_pin_gpios, \
LED_BUILTIN_INDEX_BY_REG_AND_PINNUM, (+), dev, pin)

#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), builtin_led_gpios) && \
(DT_PROP_LEN(DT_PATH(zephyr_user), builtin_led_gpios) > 0)

#if !(DT_FOREACH_PROP_ELEM_SEP_VARGS( \
DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, (+), \
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0)), \
DT_PHA_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0, pin)) > 0)
#if !(DT_FOREACH_PROP_ELEM_SEP_VARGS( \
DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, (+), \
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0)), \
DT_PHA_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0, pin)) > 0)
#warning "pin not found in digital_pin_gpios"
#else
#define LED_BUILTIN \
DIGITAL_PIN_GPIOS_FIND_PIN( \
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0)), \
DIGITAL_PIN_GPIOS_FIND_PIN( \
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0)), \
DT_PHA_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0, pin))
#endif

/* If digital-pin-gpios is not defined, tries to use the led0 alias */
#elif DT_NODE_EXISTS(DT_ALIAS(led0))

#if !(DT_FOREACH_PROP_ELEM_SEP_VARGS(DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, \
(+), DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_ALIAS(led0), gpios, 0)), \
DT_PHA_BY_IDX(DT_ALIAS(led0), gpios, 0, pin)) > 0)
#if !(DT_FOREACH_PROP_ELEM_SEP_VARGS(DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, \
(+), \
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_ALIAS(led0), gpios, 0)), \
DT_PHA_BY_IDX(DT_ALIAS(led0), gpios, 0, pin)) > 0)
#warning "pin not found in digital_pin_gpios"
#else
#define LED_BUILTIN \
DIGITAL_PIN_GPIOS_FIND_PIN(DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_ALIAS(led0), gpios, 0)), \
DT_PHA_BY_IDX(DT_ALIAS(led0), gpios, 0, pin))
DIGITAL_PIN_GPIOS_FIND_PIN(DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_ALIAS(led0), gpios, 0)), \
DT_PHA_BY_IDX(DT_ALIAS(led0), gpios, 0, pin))
#endif

#endif // builtin_led_gpios
Expand All @@ -95,11 +96,13 @@ enum digitalPins {

#ifdef CONFIG_ADC

#define AN_ENUMS(n, p, i) A ## i = DIGITAL_PIN_GPIOS_FIND_PIN( \
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), p, i)), \
DT_PHA_BY_IDX(DT_PATH(zephyr_user), p, i, pin)),
enum analogPins { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user),
adc_pin_gpios, AN_ENUMS) };
#define AN_ENUMS(n, p, i) \
A##i = DIGITAL_PIN_GPIOS_FIND_PIN(DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), p, i)), \
DT_PHA_BY_IDX(DT_PATH(zephyr_user), p, i, pin)),

enum analogPins {
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), adc_pin_gpios, AN_ENUMS)
};

// We provide analogReadResolution APIs
void analogReadResolution(int bits);
Expand All @@ -112,8 +115,11 @@ void analogReadResolution(int bits);
#undef DAC1
#undef DAC2
#undef DAC3
#define DAC_ENUMS(n, p, i) DAC ## i = i,
enum dacPins { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), dac_channels, DAC_ENUMS) NUM_OF_DACS };
#define DAC_ENUMS(n, p, i) DAC##i = i,

enum dacPins {
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), dac_channels, DAC_ENUMS) NUM_OF_DACS
};

#endif

Expand All @@ -122,10 +128,10 @@ void noInterrupts(void);

int digitalPinToInterrupt(pin_size_t pin);

#define digitalPinToPort(x) (x)
#define digitalPinToPort(x) (x)
#define digitalPinToBitMask(x) (x)
#define portOutputRegister(x) (x)
#define portInputRegister(x) (x)
#define portOutputRegister(x) (x)
#define portInputRegister(x) (x)

void analogReadResolution(int bits);
void analogWriteResolution(int bits);
Expand Down
Loading