md_doc_c-style-guide.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://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.13"/>
  7. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  8. <title>GRPC Core: 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 Core
  25. &#160;<span id="projectnumber">6.0.0</span>
  26. </div>
  27. </td>
  28. </tr>
  29. </tbody>
  30. </table>
  31. </div>
  32. <!-- end header part -->
  33. <!-- Generated by Doxygen 1.8.13 -->
  34. <script type="text/javascript">
  35. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  36. </script>
  37. <script type="text/javascript" src="menudata.js"></script>
  38. <script type="text/javascript" src="menu.js"></script>
  39. <script type="text/javascript">
  40. $(function() {
  41. initMenu('',true,false,'search.php','Search');
  42. $(document).ready(function() { init_search(); });
  43. });
  44. </script>
  45. <div id="main-nav"></div>
  46. <!-- window showing the filter options -->
  47. <div id="MSearchSelectWindow"
  48. onmouseover="return searchBox.OnSearchSelectShow()"
  49. onmouseout="return searchBox.OnSearchSelectHide()"
  50. onkeydown="return searchBox.OnSearchSelectKey(event)">
  51. </div>
  52. <!-- iframe showing the search results (closed by default) -->
  53. <div id="MSearchResultsWindow">
  54. <iframe src="javascript:void(0)" frameborder="0"
  55. name="MSearchResults" id="MSearchResults">
  56. </iframe>
  57. </div>
  58. </div><!-- top -->
  59. <div class="header">
  60. <div class="headertitle">
  61. <div class="title">GRPC C STYLE GUIDE </div> </div>
  62. </div><!--header-->
  63. <div class="contents">
  64. <div class="textblock"><h2>Background </h2>
  65. <p>Here we document style rules for C usage in the gRPC Core library.</p>
  66. <h2>General </h2>
  67. <ul>
  68. <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>
  69. </ul>
  70. <h2>Header Files </h2>
  71. <ul>
  72. <li>Public header files (those in the include/grpc tree) should compile as pedantic C89.</li>
  73. <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><div class="line"><span class="keyword">extern</span> <span class="stringliteral">&quot;C&quot;</span> {</div><div class="line"><span class="preprocessor"># endif</span></div><div class="line"></div><div class="line"><span class="comment">/* ... body of file ... */</span></div><div class="line"></div><div class="line"><span class="preprocessor">#ifdef __cplusplus</span></div><div class="line">}</div><div class="line"><span class="preprocessor"># endif</span></div></div><!-- fragment --></li>
  74. <li>Header files should be self-contained and end in .h.</li>
  75. <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>
  76. <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>
  77. <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>
  78. </li>
  79. </ul>
  80. <h2>Variable Initialization </h2>
  81. <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>
  82. <h2>C99 Features </h2>
  83. <ul>
  84. <li>Variable sized arrays are not allowed.</li>
  85. <li>Do not use the 'inline' keyword.</li>
  86. <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>
  87. </ul>
  88. <h2>Comments </h2>
  89. <p>Within public header files, only <code>/* */</code> comments are allowed.</p>
  90. <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>
  91. <h2>Symbol Names </h2>
  92. <ul>
  93. <li>Non-static functions must be prefixed by <code>grpc_</code></li>
  94. <li>Static functions must <em>not</em> be prefixed by <code>grpc_</code></li>
  95. <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>
  96. <li>Enumeration values and <code>#define</code> names must be uppercase. All other values must be lowercase.</li>
  97. <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>
  98. <li>Multiple word identifiers use underscore as a delimiter, <em>never</em> camel case. E.g. <code>variable_name</code>.</li>
  99. </ul>
  100. <h2>Functions </h2>
  101. <ul>
  102. <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>
  103. </ul>
  104. </div></div><!-- contents -->
  105. <!-- start footer part -->
  106. <hr class="footer"/><address class="footer"><small>
  107. Generated on Fri Apr 13 2018 15:03:52 for GRPC Core by &#160;<a href="http://www.doxygen.org/index.html">
  108. <img class="footer" src="doxygen.png" alt="doxygen"/>
  109. </a> 1.8.13
  110. </small></address>
  111. </body>
  112. </html>