The following defines the common C source file format for linux-wlan.
Most of the C-code formatting rules come from the linux kernel
document CodingStyle
.
For all source files, we'll stick to the US character set and avoid all trigraphs.
All indentation will be done using tab characters which are mapped to a spacing of eight characters.
Braces will be placed according to the format originally established in Kernighan and Ritchie's book "The C Programming Language". Here are some example statements:
for ( i= 0; i < N; i++) { . . . } if ( a < b ) { . . . } else { . . . } do { . . . } while ( i >> 0 ); |
All elements defined via the C preprocessor (constants and macros) are named using all capital letters. An exception is for macros that are either wrapping function calls for portability or for macros that are inline replacements for code that would normally be in a function.
All programmer defined types must have single word type names defined using the
typedefstatement. All type names should be identified with an
_tsuffix. This is particularly important for function pointers that are members of structures or arguments to functions.
Anonymous types are not allowed. All struct, union, and enum types shall be named and typedef'd.
Each file should be layed out using a common format. The following shows a complete file with all its major sections.
Each major section within the file is begun with a comment of the form:
/*================================================================*/ /* [Section Name] */
Subsections within a major section are denoted using:
/*----------------------------------------------------------------*/ /* [Subsection Name] */
/* [filename]: [one line description of the file] * -------------------------------------------------------------------- * * [Project Name] * * [License Statement] * * [Warranty Statement] * * [Initial Author Statement] * * -------------------------------------------------------------------- * * [Initial Author Contact] * * -------------------------------------------------------------------- * * [File Description] * * [Implementation and Usage Notes] * * -------------------------------------------------------------------- */ /*================================================================*/ /* System Includes */ /*================================================================*/ /* Project Includes */ /*================================================================*/ /* Local Constants */ /*================================================================*/ /* Local Macros */ /*----------------------------------------------------------------*/ /* [A subsection] */ /*================================================================*/ /* Local Types */ /*================================================================*/ /* Local Static Definitions */ /*================================================================*/ /* Local Function Declarations */ /*================================================================*/ /* Function Definitions */ |
Preprocessor #include
statements that are including
system includes shall be placed in this section. System
includes are those include files that are not part of the
managed source for this project.
Preprocessor #include
statements that are including
project includes shall be placed in this section. Project
includes are those include files that are a part of the
managed source for this project.
Preprocessor "manifest" constants that are local to this file shall be placed in this section. "Manifest" constants are preprocessor macros that take no arguments.
Proprocessor macros that accept arguments shall be placed in this section.
Programmer defined types that are only used within the scope of this file shall be defined in this section. Programmer defined types that are used in more than one source file should be defined in a header file.
Variables with static extent that are defined within this file shall
be placed in this section. Whether a variable has scope beyond this
file will be apparent based on the presence or absence of the
static
keyword in the declaration. If a variable is
declared without the static
keyword, there should be an
extern
declaration for that variable in a header file.
Functions that are only used within this file should be declared
(prototyped) in this section. Additionally, these functions should be
declared using the static
keyword. This avoids polluting
the global namespace with function names that need not be
extern
.
Any functions defined in this file that are called from outside this file should be declared (prototyped) in a header file.
This section contains the definitions of the functions in this file. Each function (or group of strongly related functions) will be preceded by a function header comment (see below).
Each source file will have a header comment containing information about the file as a whole. That comment shall be formatted:
/* [filename]: [one line description of the file] * -------------------------------------------------------------------- * * [Project Name] * * [License Statement] * * [Warranty Statement] * * [Initial Author Statement] * * -------------------------------------------------------------------- * * [Initial Author Contact] * * -------------------------------------------------------------------- * * [File Description] * * [Implementation and Usage Notes] * * -------------------------------------------------------------------- */ |
Each function (or group of closely related functions) will be preceded
by a function comment header. The Side effects
and
Call context
sections are optional.
/*---------------------------------------------------------------- * [function name] * * [description] * * Arguments: * [argument list] * * Returns: * [return value list] * * Side effects: * [description of function side effects] * * Call context: * [description of calling context] ----------------------------------------------------------------*/ |