md_doc_c-style-guide.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  6. <meta name="generator" content="Doxygen 1.8.17"/>
  7. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  8. <title>GRPC C++: GRPC C STYLE GUIDE</title>
  9. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  10. <script type="text/javascript" src="jquery.js"></script>
  11. <script type="text/javascript" src="dynsections.js"></script>
  12. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  13. <script type="text/javascript" src="search/searchdata.js"></script>
  14. <script type="text/javascript" src="search/search.js"></script>
  15. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  16. </head>
  17. <body>
  18. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  19. <div id="titlearea">
  20. <table cellspacing="0" cellpadding="0">
  21. <tbody>
  22. <tr style="height: 56px;">
  23. <td id="projectalign" style="padding-left: 0.5em;">
  24. <div id="projectname">GRPC C++
  25. &#160;<span id="projectnumber">1.36.1</span>
  26. </div>
  27. </td>
  28. </tr>
  29. </tbody>
  30. </table>
  31. </div>
  32. <!-- end header part -->
  33. <!-- Generated by Doxygen 1.8.17 -->
  34. <script type="text/javascript">
  35. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  36. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  37. /* @license-end */
  38. </script>
  39. <script type="text/javascript" src="menudata.js"></script>
  40. <script type="text/javascript" src="menu.js"></script>
  41. <script type="text/javascript">
  42. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  43. $(function() {
  44. initMenu('',true,false,'search.php','Search');
  45. $(document).ready(function() { init_search(); });
  46. });
  47. /* @license-end */</script>
  48. <div id="main-nav"></div>
  49. <!-- window showing the filter options -->
  50. <div id="MSearchSelectWindow"
  51. onmouseover="return searchBox.OnSearchSelectShow()"
  52. onmouseout="return searchBox.OnSearchSelectHide()"
  53. onkeydown="return searchBox.OnSearchSelectKey(event)">
  54. </div>
  55. <!-- iframe showing the search results (closed by default) -->
  56. <div id="MSearchResultsWindow">
  57. <iframe src="javascript:void(0)" frameborder="0"
  58. name="MSearchResults" id="MSearchResults">
  59. </iframe>
  60. </div>
  61. </div><!-- top -->
  62. <div class="PageDoc"><div class="header">
  63. <div class="headertitle">
  64. <div class="title">GRPC C STYLE GUIDE </div> </div>
  65. </div><!--header-->
  66. <div class="contents">
  67. <div class="textblock"><h1><a class="anchor" id="autotoc_md33"></a>
  68. Background</h1>
  69. <p>Here we document style rules for C usage in the gRPC Core library.</p>
  70. <h1><a class="anchor" id="autotoc_md34"></a>
  71. General</h1>
  72. <ul>
  73. <li>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 <a href="../tools/distrib/clang_format_code.sh">tools/distrib/clang_format_code.sh</a>.</li>
  74. </ul>
  75. <h1><a class="anchor" id="autotoc_md35"></a>
  76. Header Files</h1>
  77. <ul>
  78. <li>Public header files (those in the include/grpc tree) should compile as pedantic C89.</li>
  79. <li>Public header files should be includable from C++ programs. That is, they should include the following: <div class="fragment"><div class="line"><span class="preprocessor">#ifdef __cplusplus</span></div>
  80. <div class="line"><span class="keyword">extern</span> <span class="stringliteral">&quot;C&quot;</span> {</div>
  81. <div class="line"><span class="preprocessor"># endif</span></div>
  82. <div class="line"> </div>
  83. <div class="line"><span class="comment">/* ... body of file ... */</span></div>
  84. <div class="line"> </div>
  85. <div class="line"><span class="preprocessor">#ifdef __cplusplus</span></div>
  86. <div class="line">}</div>
  87. <div class="line"><span class="preprocessor"># endif</span></div>
  88. </div><!-- fragment --></li>
  89. <li>Header files should be self-contained and end in .h.</li>
  90. <li><p class="startli">All header files should have a <code>#define</code> guard to prevent multiple inclusion. To guarantee uniqueness they should be based on the file's path.</p>
  91. <p class="startli">For public headers: <code><a class="el" href="grpc_8h.html">include/grpc/grpc.h</a></code> → <code>GRPC_GRPC_H</code></p>
  92. <p class="startli">For private headers: <code>src/core/lib/channel/channel_stack.h</code> → <code>GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_H</code></p>
  93. </li>
  94. </ul>
  95. <h1><a class="anchor" id="autotoc_md36"></a>
  96. Variable Initialization</h1>
  97. <p>When declaring a (non-static) pointer variable, always initialize it to <code>NULL</code>. Even in the case of static pointer variables, it's recommended to explicitly initialize them to <code>NULL</code>.</p>
  98. <h1><a class="anchor" id="autotoc_md37"></a>
  99. C99 Features</h1>
  100. <ul>
  101. <li>Variable sized arrays are not allowed.</li>
  102. <li>Do not use the 'inline' keyword.</li>
  103. <li>Flexible array members are allowed (<a href="https://en.wikipedia.org/wiki/Flexible_array_member">https://en.wikipedia.org/wiki/Flexible_array_member</a>).</li>
  104. </ul>
  105. <h1><a class="anchor" id="autotoc_md38"></a>
  106. Comments</h1>
  107. <p>Within public header files, only <code>/* */</code> comments are allowed.</p>
  108. <p>Within implementation files and private headers, either single line <code>//</code> or multi line <code>/* */</code> 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).</p>
  109. <h1><a class="anchor" id="autotoc_md39"></a>
  110. Symbol Names</h1>
  111. <ul>
  112. <li>Non-static functions must be prefixed by <code>grpc_</code></li>
  113. <li>Static functions must <em>not</em> be prefixed by <code>grpc_</code></li>
  114. <li>Typenames of <code>struct</code>s , <code>union</code>s, and <code>enum</code>s must be prefixed by <code>grpc_</code> if they are declared in a header file. They must not be prefixed by <code>grpc_</code> if they are declared in a source file.</li>
  115. <li>Enumeration values and <code>#define</code> names must be uppercase. All other values must be lowercase.</li>
  116. <li>Enumeration values or <code>#define</code> names defined in a header file must be prefixed with <code>GRPC_</code> (except for <code>#define</code> macros that are being used to substitute functions; those should follow the general rules for functions). Enumeration values or <code>#define</code>s defined in source files must not be prefixed with <code>GRPC_</code>.</li>
  117. <li>Multiple word identifiers use underscore as a delimiter, <em>never</em> camel case. E.g. <code>variable_name</code>.</li>
  118. </ul>
  119. <h1><a class="anchor" id="autotoc_md40"></a>
  120. Functions</h1>
  121. <ul>
  122. <li>The use of <a href="http://man7.org/linux/man-pages/man3/atexit.3.html"><code>atexit()</code></a> is in forbidden in libgrpc. </li>
  123. </ul>
  124. </div></div><!-- contents -->
  125. </div><!-- PageDoc -->
  126. <!-- start footer part -->
  127. <hr class="footer"/><address class="footer"><small>
  128. Generated on Wed Mar 3 2021 19:17:22 for GRPC C++ by &#160;<a href="http://www.doxygen.org/index.html">
  129. <img class="footer" src="doxygen.png" alt="doxygen"/>
  130. </a> 1.8.17
  131. </small></address>
  132. </body>
  133. </html>