/
C Style and Syntax Standards

C Style and Syntax Standards

We utilize (a slightly modified version of) the Linux Kernel Clang Formatter, which enforces Linux style syntax.

 

Within all C repositories, a GitHub Action is setup to check that all files have been formatted accordingly, and pull requests will not be able to merge unless it passes this check.

Fortunately, this formatter can be run by the user to automatically update their code to abide by these standards.

This process only works for NER applications, not for Embedded Base itself. If/when we get this running for embedded base, the process for running it on a file within that directory will be included below

This purposely will ignore and not format auto generated files like main.c and all HAL files. This is okay and intentional. Dont manually format them.

To setup and run the clang format:

In VScode:

  1. Open VScode from any NER C project. You must do this once for every project!

  2. Open file-->preferences-->settings

  3. Make sure you are on “workspace” settings and not “user” settings

  4. Type clang in the settings search bar

  5. Change C_Cpp: Clang_format_fallback Style to none

  6.  

    1. IF in Cerb, Shep, etc. Change C_Cpp: Clang_format_style to file:${workspaceRoot}/Drivers/Embedded-Base/clang-format

    2. IF in Embedded-Base: Change C_Cpp: Clang_format_style to file:${workspaceRoot}/clang-format

  7. Change editor.formatOnSave to false

 

The linter will not run on save, and will have to be done with Ctr + Shft + i.


In CLI:

 

Debian based Linux/WSL:

sudo apt-get install clang-format clang-format -style=file:./Drivers/Embedded-Base/clang-format -i ./path/to/ur/file.c

Mac:

brew install clang-format clang-format -style=file:./Drivers/Embedded-Base/clang-format -i ./path/to/ur/file.c

 

(obviously use whatever package manager you want for Linux and Mac)

 

Clang Ignore

If ever there is a block of code that needs an exception to this formatter, which does happen on occasion, you can encapsulate that code within these comments:

 

// clang-format off ... // clang-format on

Related content

C Coding Standards
C Coding Standards
More like this
Firmware Onboarding | Embedded Software
Firmware Onboarding | Embedded Software
More like this
Firmware Environment Setup
Firmware Environment Setup
More like this
Github
Github
Read with this
Rust Standards & Tips
Rust Standards & Tips
More like this
Engineering Software
Engineering Software
Read with this