Skip to content

Code Style and Formatting

Armando Faz edited this page Nov 20, 2023 · 3 revisions

Guidelines

The following are (non-exhuastive) guidelines for code style taken from the Kernel coding style and the Clang kernel documentation.

  • 8chars identation and 80 columns.
  • Pointer is attached to name, so int *x.
  • Use C99 fixed-size types.
  • Avoid the use of typedef on structs and standard types.
  • Comment Style
/*
 * lorem ipsum dolor sit amet
 * lorem ipsum dolor sit amet
 * lorem ipsum dolor sit amet
 */
  • Defines are ALLCAPS and consecutive defines are aligned:
#define CONSTANT_LARGE_NAME_SIZE 2
#define CONSTANT_MEDIUM_SIZE     3
#define CONSTANT_SMALL           4

Formatting

To format files. run

$ make -C zeta format FILES="/crypto/sha256_generic.c /crypto/sha512_generic.c"

Pass in FILES the files to be formatted. Note the leading / (slash), which indicates the position of the files with respect to the kernel source code.

This will invoke astyle followed by clang-format commands. AStyle is used to prepare the files for clang-format, as the latter does not catch issues that astyle does.

Linting

Zeta contains a linter script to be used as a reference to spot coding errors. The cpplinter could be verbose as it targets C++, however it helps to remove minor issues.

$ make -C zeta lint FILES="/crypto/sha256_generic.c /crypto/sha512_generic.c"

Pass in FILES the files to be linted. Note the leading / (slash), which indicates the position of the files with respect to the kernel source code.

License Identifiers

Include SPDX identifiers in code. See full reference for more details. For example, for code under dual license GPL OR MIT, it should be included the following lines:

For header (.h) files: /* SPDX-License-Identifier: GPL-2.0 OR MIT */

For source (.c) files: // SPDX-License-Identifier: GPL-2.0 OR MIT

For module files: MODULE_LICENSE("Dual MIT/GPL");