aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc/c-style-guide.md
blob: 2cfa41dd9ec2976f94de0535466de5198f8a43f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
GRPC C STYLE GUIDE
=====================

Background
----------

Here we document style rules for C usage in the gRPC Core library.

General
-------

- Layout rules are defined by clang-format, and all code should be passed
  through clang-format. A (docker-based) script to do so is included in
  [tools/distrib/clang\_format\_code.sh](../tools/distrib/clang_format_code.sh).

Header Files
------------

- Public header files (those in the include/grpc tree) should compile as
  pedantic C89.
- Public header files should be includable from C++ programs. That is, they
  should include the following:
  ```c
  #ifdef __cplusplus
  extern "C" {
  # endif

  /* ... body of file ... */

  #ifdef __cplusplus
  }
  # endif
  ```
- Header files should be self-contained and end in .h.
- All header files should have a `#define` guard to prevent multiple inclusion.
  To guarantee uniqueness they should be based on the file's path.

  For public headers: `include/grpc/grpc.h` → `GRPC_GRPC_H`

  For private headers:
  `src/core/lib/channel/channel_stack.h` →
  `GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_H`

Variable Initialization
-----------------------

When declaring a (non-static) pointer variable, always initialize it to `NULL`.
Even in the case of static pointer variables, it's recommended to explicitly
initialize them to `NULL`.


C99 Features
------------

- Variable sized arrays are not allowed.
- Do not use the 'inline' keyword.
- Flexible array members are allowed
  (https://en.wikipedia.org/wiki/Flexible_array_member).

Comments
--------

Within public header files, only `/* */` comments are allowed.

Within implementation files and private headers, either single line `//`
or multi line `/* */` comments are allowed. Only one comment style per file is
allowed however (i.e. if single line comments are used anywhere within a file,
ALL comments within that file must be single line comments).

Symbol Names
------------

- Non-static functions must be prefixed by `grpc_`
- Static functions must *not* be prefixed by `grpc_`
- Typenames of `struct`s , `union`s, and `enum`s must be prefixed by `grpc_` if
  they are declared in a header file. They must not be prefixed by `grpc_` if
  they are declared in a source file.
- Enumeration values and `#define` names must be uppercase. All other values
  must be lowercase.
- Enumeration values or `#define` names defined in a header file must be
  prefixed with `GRPC_` (except for `#define` macros that are being used to
  substitute functions; those should follow the general rules for
  functions). Enumeration values or `#define`s defined in source files must not
  be prefixed with `GRPC_`.
- Multiple word identifiers use underscore as a delimiter, *never* camel
  case. E.g. `variable_name`.

Functions
----------

- The use of [`atexit()`](http://man7.org/linux/man-pages/man3/atexit.3.html) is
  in forbidden in libgrpc.