Craig Tiller | f0d916a | 2015-12-22 13:39:29 -0800 | [diff] [blame] | 1 | GRPC C STYLE GUIDE |
| 2 | ===================== |
| 3 | |
| 4 | Background |
| 5 | ---------- |
| 6 | |
| 7 | Here we document style rules for C usage in the gRPC Core library. |
| 8 | |
| 9 | General |
| 10 | ------- |
| 11 | |
David Garcia Quintas | fed3e3b | 2016-07-06 14:02:34 -0700 | [diff] [blame] | 12 | - 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 Tiller | f0d916a | 2015-12-22 13:39:29 -0800 | [diff] [blame] | 15 | |
| 16 | Header Files |
| 17 | ------------ |
| 18 | |
David Garcia Quintas | fed3e3b | 2016-07-06 14:02:34 -0700 | [diff] [blame] | 19 | - Public header files (those in the include/grpc tree) should compile as |
David Garcia Quintas | fb81858 | 2016-07-15 08:05:07 -0700 | [diff] [blame] | 20 | pedantic C89. |
David Garcia Quintas | fed3e3b | 2016-07-06 14:02:34 -0700 | [diff] [blame] | 21 | - Public header files should be includable from C++ programs. That is, they |
| 22 | should include the following: |
Craig Tiller | ff298f6 | 2015-12-22 14:06:44 -0800 | [diff] [blame] | 23 | ```c |
| 24 | #ifdef __cplusplus |
| 25 | extern "C" { |
| 26 | # endif |
| 27 | |
| 28 | /* ... body of file ... */ |
| 29 | |
| 30 | #ifdef __cplusplus |
| 31 | } |
| 32 | # endif |
| 33 | ``` |
Craig Tiller | f0d916a | 2015-12-22 13:39:29 -0800 | [diff] [blame] | 34 | - Header files should be self-contained and end in .h. |
Vijay Pai | 4a7fca5 | 2017-09-12 14:31:11 -0700 | [diff] [blame] | 35 | - All header files should have a `#define` guard to prevent multiple inclusion. |
Craig Tiller | f0d916a | 2015-12-22 13:39:29 -0800 | [diff] [blame] | 36 | To guarantee uniqueness they should be based on the file's path. |
| 37 | |
David Garcia Quintas | fed3e3b | 2016-07-06 14:02:34 -0700 | [diff] [blame] | 38 | For public headers: `include/grpc/grpc.h` → `GRPC_GRPC_H` |
Craig Tiller | f0d916a | 2015-12-22 13:39:29 -0800 | [diff] [blame] | 39 | |
David Garcia Quintas | fed3e3b | 2016-07-06 14:02:34 -0700 | [diff] [blame] | 40 | For private headers: |
Vijay Pai | 4a7fca5 | 2017-09-12 14:31:11 -0700 | [diff] [blame] | 41 | `src/core/lib/channel/channel_stack.h` → |
| 42 | `GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_H` |
David Garcia Quintas | fed3e3b | 2016-07-06 14:02:34 -0700 | [diff] [blame] | 43 | |
| 44 | Variable Initialization |
| 45 | ----------------------- |
| 46 | |
| 47 | When declaring a (non-static) pointer variable, always initialize it to `NULL`. |
| 48 | Even in the case of static pointer variables, it's recommended to explicitly |
| 49 | initialize them to `NULL`. |
| 50 | |
Craig Tiller | f0d916a | 2015-12-22 13:39:29 -0800 | [diff] [blame] | 51 | |
| 52 | C99 Features |
| 53 | ------------ |
| 54 | |
David Garcia Quintas | fb81858 | 2016-07-15 08:05:07 -0700 | [diff] [blame] | 55 | - Variable sized arrays are not allowed. |
| 56 | - Do not use the 'inline' keyword. |
David Garcia Quintas | fed3e3b | 2016-07-06 14:02:34 -0700 | [diff] [blame] | 57 | - Flexible array members are allowed |
David Garcia Quintas | fb81858 | 2016-07-15 08:05:07 -0700 | [diff] [blame] | 58 | (https://en.wikipedia.org/wiki/Flexible_array_member). |
Craig Tiller | ff298f6 | 2015-12-22 14:06:44 -0800 | [diff] [blame] | 59 | |
| 60 | Comments |
| 61 | -------- |
| 62 | |
| 63 | Within public header files, only `/* */` comments are allowed. |
| 64 | |
David Garcia Quintas | fed3e3b | 2016-07-06 14:02:34 -0700 | [diff] [blame] | 65 | Within implementation files and private headers, either single line `//` |
Craig Tiller | ff298f6 | 2015-12-22 14:06:44 -0800 | [diff] [blame] | 66 | or multi line `/* */` comments are allowed. Only one comment style per file is |
| 67 | allowed however (i.e. if single line comments are used anywhere within a file, |
| 68 | ALL comments within that file must be single line comments). |
Craig Tiller | f0d916a | 2015-12-22 13:39:29 -0800 | [diff] [blame] | 69 | |
| 70 | Symbol Names |
| 71 | ------------ |
| 72 | |
David Garcia Quintas | fb81858 | 2016-07-15 08:05:07 -0700 | [diff] [blame] | 73 | - Non-static functions must be prefixed by `grpc_` |
| 74 | - Static functions must *not* be prefixed by `grpc_` |
Vijay Pai | 4a7fca5 | 2017-09-12 14:31:11 -0700 | [diff] [blame] | 75 | - 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 Quintas | fb81858 | 2016-07-15 08:05:07 -0700 | [diff] [blame] | 78 | - Enumeration values and `#define` names must be uppercase. All other values |
| 79 | must be lowercase. |
Vijay Pai | 4a7fca5 | 2017-09-12 14:31:11 -0700 | [diff] [blame] | 80 | - 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 Quintas | fb81858 | 2016-07-15 08:05:07 -0700 | [diff] [blame] | 85 | - Multiple word identifiers use underscore as a delimiter, *never* camel |
| 86 | case. E.g. `variable_name`. |
| 87 | |
| 88 | Functions |
| 89 | ---------- |
| 90 | |
| 91 | - The use of [`atexit()`](http://man7.org/linux/man-pages/man3/atexit.3.html) is |
| 92 | in forbidden in libgrpc. |