File Structure
Summary
Issue
Decision
Status
Details
Assumption
Clang-Format is used as the formatter based on Google style.
Constraints
Positions
This record generally follows the Google C++ Style Guide and Linux Kernel Coding Style, and will refer to them later as “Google Guide” and “Linux Guide” respectively.
The structure of source file (.c) should be as follows:
#include
directives that follow;Order of includes from the Google Guide
Note
Since Clang-Format will remove blink lines between includes, use comments to separate includes form different categories.
Avoid transitive includes as mentioned in the Google Guide
#define
directives for macros and constantsType fordward declarations only for types that will be defined later in the same file
Typedefs
Type declarations in the following order;
Enums
Structs
Unions
Static function declarations
External variables
Static variables
Function definitions
Static function definitions
With the exception of conditional compilation, which should be grouped together for readability, refer to the Linux Guide to write code with conditional compilation better.
Within a module, group global variables into a single context struct
MODULE_NAME_ctx
. Initialize its member in a static initializer if
possible and define a MODULE_NAME_init()
function to initialize other
members unable to be initialized in the static initializer.