blob: 2cfa41dd9ec2976f94de0535466de5198f8a43f2 [file] [log] [blame] [view]
Craig Tillerf0d916a2015-12-22 13:39:29 -08001GRPC C STYLE GUIDE
2=====================
3
4Background
5----------
6
7Here we document style rules for C usage in the gRPC Core library.
8
9General
10-------
11
David Garcia Quintasfed3e3b2016-07-06 14:02:34 -070012- Layout rules are defined by clang-format, and all code should be passed
13 through clang-format. A (docker-based) script to do so is included in
14 [tools/distrib/clang\_format\_code.sh](../tools/distrib/clang_format_code.sh).
Craig Tillerf0d916a2015-12-22 13:39:29 -080015
16Header Files
17------------
18
David Garcia Quintasfed3e3b2016-07-06 14:02:34 -070019- Public header files (those in the include/grpc tree) should compile as
David Garcia Quintasfb818582016-07-15 08:05:07 -070020 pedantic C89.
David Garcia Quintasfed3e3b2016-07-06 14:02:34 -070021- Public header files should be includable from C++ programs. That is, they
22 should include the following:
Craig Tillerff298f62015-12-22 14:06:44 -080023 ```c
24 #ifdef __cplusplus
25 extern "C" {
26 # endif
27
28 /* ... body of file ... */
29
30 #ifdef __cplusplus
31 }
32 # endif
33 ```
Craig Tillerf0d916a2015-12-22 13:39:29 -080034- Header files should be self-contained and end in .h.
Vijay Pai4a7fca52017-09-12 14:31:11 -070035- All header files should have a `#define` guard to prevent multiple inclusion.
Craig Tillerf0d916a2015-12-22 13:39:29 -080036 To guarantee uniqueness they should be based on the file's path.
37
David Garcia Quintasfed3e3b2016-07-06 14:02:34 -070038 For public headers: `include/grpc/grpc.h` → `GRPC_GRPC_H`
Craig Tillerf0d916a2015-12-22 13:39:29 -080039
David Garcia Quintasfed3e3b2016-07-06 14:02:34 -070040 For private headers:
Vijay Pai4a7fca52017-09-12 14:31:11 -070041 `src/core/lib/channel/channel_stack.h` →
42 `GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_H`
David Garcia Quintasfed3e3b2016-07-06 14:02:34 -070043
44Variable Initialization
45-----------------------
46
47When declaring a (non-static) pointer variable, always initialize it to `NULL`.
48Even in the case of static pointer variables, it's recommended to explicitly
49initialize them to `NULL`.
50
Craig Tillerf0d916a2015-12-22 13:39:29 -080051
52C99 Features
53------------
54
David Garcia Quintasfb818582016-07-15 08:05:07 -070055- Variable sized arrays are not allowed.
56- Do not use the 'inline' keyword.
David Garcia Quintasfed3e3b2016-07-06 14:02:34 -070057- Flexible array members are allowed
David Garcia Quintasfb818582016-07-15 08:05:07 -070058 (https://en.wikipedia.org/wiki/Flexible_array_member).
Craig Tillerff298f62015-12-22 14:06:44 -080059
60Comments
61--------
62
63Within public header files, only `/* */` comments are allowed.
64
David Garcia Quintasfed3e3b2016-07-06 14:02:34 -070065Within implementation files and private headers, either single line `//`
Craig Tillerff298f62015-12-22 14:06:44 -080066or multi line `/* */` comments are allowed. Only one comment style per file is
67allowed however (i.e. if single line comments are used anywhere within a file,
68ALL comments within that file must be single line comments).
Craig Tillerf0d916a2015-12-22 13:39:29 -080069
70Symbol Names
71------------
72
David Garcia Quintasfb818582016-07-15 08:05:07 -070073- Non-static functions must be prefixed by `grpc_`
74- Static functions must *not* be prefixed by `grpc_`
Vijay Pai4a7fca52017-09-12 14:31:11 -070075- Typenames of `struct`s , `union`s, and `enum`s must be prefixed by `grpc_` if
76 they are declared in a header file. They must not be prefixed by `grpc_` if
77 they are declared in a source file.
David Garcia Quintasfb818582016-07-15 08:05:07 -070078- Enumeration values and `#define` names must be uppercase. All other values
79 must be lowercase.
Vijay Pai4a7fca52017-09-12 14:31:11 -070080- Enumeration values or `#define` names defined in a header file must be
81 prefixed with `GRPC_` (except for `#define` macros that are being used to
82 substitute functions; those should follow the general rules for
83 functions). Enumeration values or `#define`s defined in source files must not
84 be prefixed with `GRPC_`.
David Garcia Quintasfb818582016-07-15 08:05:07 -070085- Multiple word identifiers use underscore as a delimiter, *never* camel
86 case. E.g. `variable_name`.
87
88Functions
89----------
90
91- The use of [`atexit()`](http://man7.org/linux/man-pages/man3/atexit.3.html) is
92 in forbidden in libgrpc.