style-guide.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. Espressif IoT Development Framework Style Guide
  2. ===============================================
  3. About This Guide
  4. ----------------
  5. Purpose of this style guide is to encourage use of common coding practices within the ESP-IDF.
  6. Style guide is a set of rules which are aimed to help create readable, maintainable, and robust code. By writing code which looks the same way across the code base we help others read and comprehend the code. By using same conventions for spaces and newlines we reduce chances that future changes will produce huge unreadable diffs. By following common patterns for module structure and by using language features consistently we help others understand code behavior.
  7. We try to keep rules simple enough, which means that they can not cover all potential cases. In some cases one has to bend these simple rules to achieve readability, maintainability, or robustness.
  8. When doing modifications to third-party code used in ESP-IDF, follow the way that particular project is written. That will help propose useful changes for merging into upstream project.
  9. C Code Formatting
  10. -----------------
  11. .. highlight:: c
  12. .. _style-guide-naming:
  13. Naming
  14. ^^^^^^
  15. * Any variable or function which is only used in a single source file should be declared ``static``.
  16. * Public names (non-static variables and functions) should be namespaced with a per-component or per-unit prefix, to avoid naming collisions. ie ``esp_vfs_register()`` or ``esp_console_run()``. Starting the prefix with ``esp_`` for Espressif-specific names is optional, but should be consistent with any other names in the same component.
  17. * Static variables should be prefixed with ``s_`` for easy identification. For example, ``static bool s_invert``.
  18. * Avoid unnecessary abbreviations (ie shortening ``data`` to ``dat``), unless the resulting name would otherwise be very long.
  19. Indentation
  20. ^^^^^^^^^^^
  21. Use 4 spaces for each indentation level. Do not use tabs for indentation. Configure the editor to emit 4 spaces each time you press tab key.
  22. Vertical Space
  23. ^^^^^^^^^^^^^^
  24. Place one empty line between functions. Do not begin or end a function with an empty line.
  25. ::
  26. void function1()
  27. {
  28. do_one_thing();
  29. do_another_thing();
  30. // INCORRECT, do not place empty line here
  31. }
  32. // place empty line here
  33. void function2()
  34. {
  35. // INCORRECT, do not use an empty line here
  36. int var = 0;
  37. while (var < SOME_CONSTANT) {
  38. do_stuff(&var);
  39. }
  40. }
  41. The maximum line length is 120 characters as long as it does not seriously affect the readability.
  42. Horizontal Space
  43. ^^^^^^^^^^^^^^^^
  44. Always add single space after conditional and loop keywords::
  45. if (condition) { // correct
  46. // ...
  47. }
  48. switch (n) { // correct
  49. case 0:
  50. // ...
  51. }
  52. for(int i = 0; i < CONST; ++i) { // INCORRECT
  53. // ...
  54. }
  55. Add single space around binary operators. No space is necessary for unary operators. It is okay to drop space around multiply and divide operators::
  56. const int y = y0 + (x - x0) * (y1 - y0) / (x1 - x0); // correct
  57. const int y = y0 + (x - x0)*(y1 - y0)/(x1 - x0); // also okay
  58. int y_cur = -y; // correct
  59. ++y_cur;
  60. const int y = y0+(x-x0)*(y1-y0)/(x1-x0); // INCORRECT
  61. No space is necessary around ``.`` and ``->`` operators.
  62. Sometimes adding horizontal space within a line can help make code more readable. For example, you can add space to align function arguments::
  63. esp_rom_gpio_connect_in_signal(PIN_CAM_D6, I2S0I_DATA_IN14_IDX, false);
  64. esp_rom_gpio_connect_in_signal(PIN_CAM_D7, I2S0I_DATA_IN15_IDX, false);
  65. esp_rom_gpio_connect_in_signal(PIN_CAM_HREF, I2S0I_H_ENABLE_IDX, false);
  66. esp_rom_gpio_connect_in_signal(PIN_CAM_PCLK, I2S0I_DATA_IN15_IDX, false);
  67. Note however that if someone goes to add new line with a longer identifier as first argument (e.g., ``PIN_CAM_VSYNC``), it will not fit. So other lines would have to be realigned, adding meaningless changes to the commit.
  68. Therefore, use horizontal alignment sparingly, especially if you expect new lines to be added to the list later.
  69. Never use TAB characters for horizontal alignment.
  70. Never add trailing whitespace at the end of the line.
  71. Braces
  72. ^^^^^^
  73. - Function definition should have a brace on a separate line::
  74. // This is correct:
  75. void function(int arg)
  76. {
  77. }
  78. // NOT like this:
  79. void function(int arg) {
  80. }
  81. - Within a function, place opening brace on the same line with conditional and loop statements::
  82. if (condition) {
  83. do_one();
  84. } else if (other_condition) {
  85. do_two();
  86. }
  87. Comments
  88. ^^^^^^^^
  89. Use ``//`` for single line comments. For multi-line comments it is okay to use either ``//`` on each line or a ``/* */`` block.
  90. Although not directly related to formatting, here are a few notes about using comments effectively.
  91. - Do not use single comments to disable some functionality::
  92. void init_something()
  93. {
  94. setup_dma();
  95. // load_resources(); // WHY is this thing commented, asks the reader?
  96. start_timer();
  97. }
  98. - If some code is no longer required, remove it completely. If you need it you can always look it up in git history of this file. If you disable some call because of temporary reasons, with an intention to restore it in the future, add explanation on the adjacent line::
  99. void init_something()
  100. {
  101. setup_dma();
  102. // TODO: we should load resources here, but loader is not fully integrated yet.
  103. // load_resources();
  104. start_timer();
  105. }
  106. - Same goes for ``#if 0 ... #endif`` blocks. Remove code block completely if it is not used. Otherwise, add comment explaining why the block is disabled. Do not use ``#if 0 ... #endif`` or comments to store code snippets which you may need in the future.
  107. - Do not add trivial comments about authorship and change date. You can always look up who modified any given line using git. E.g., this comment adds clutter to the code without adding any useful information::
  108. void init_something()
  109. {
  110. setup_dma();
  111. // XXX add 2016-09-01
  112. init_dma_list();
  113. fill_dma_item(0);
  114. // end XXX add
  115. start_timer();
  116. }
  117. Line Endings
  118. ^^^^^^^^^^^^
  119. Commits should only contain files with LF (Unix style) endings.
  120. Windows users can configure git to check out CRLF (Windows style) endings locally and commit LF endings by setting the ``core.autocrlf`` setting. `Github has a document about setting this option <github-line-endings>`.
  121. If you accidentally have some commits in your branch that add LF endings, you can convert them to Unix by running this command in an MSYS2 or Unix terminal (change directory to the IDF working directory and check the correct branch is currently checked out, beforehand):
  122. .. code-block:: bash
  123. git rebase --exec 'git diff-tree --no-commit-id --name-only -r HEAD | xargs dos2unix && git commit -a --amend --no-edit --allow-empty' master
  124. (Note that this line rebases on master, change the branch name at the end to rebase on another branch.)
  125. For updating a single commit, it is possible to run ``dos2unix FILENAME`` and then run ``git commit --amend``
  126. Formatting Your Code
  127. ^^^^^^^^^^^^^^^^^^^^
  128. ESP-IDF uses Astyle to format source code. The configuration is stored in :project_file:`tools/ci/astyle-rules.yml` file.
  129. Initially, all components are excluded from formatting checks. You can enable formatting checks for the component by removing it from ``components_not_formatted_temporary`` list. Then run:
  130. .. code-block:: bash
  131. pre-commit run --files <path_to_files> astyle_py
  132. Alternatively, you can run ``astyle_py`` manually. You can install it with ``pip install astyle_py==VERSION``. Make sure you have the same version installed as the one specified in :project_file:`.pre-commit-config.yaml` file. With ``astyle_py`` installed, run:
  133. .. code-block:: bash
  134. astyle_py --rules=$IDF_PATH/tools/ci/astyle-rules.yml <path-to-file>
  135. Type Definitions
  136. ^^^^^^^^^^^^^^^^
  137. Should be snake_case, ending with _t suffix::
  138. typedef int signed_32_bit_t;
  139. Enum
  140. ^^^^
  141. Enums should be defined through the `typedef` and be namespaced::
  142. typedef enum
  143. {
  144. MODULE_FOO_ONE,
  145. MODULE_FOO_TWO,
  146. MODULE_FOO_THREE
  147. } module_foo_t;
  148. .. _assertions:
  149. Assertions
  150. ^^^^^^^^^^
  151. The standard C ``assert()`` function, defined in ``assert.h`` should be used to check conditions that should be true in source code. In the default configuration, an assert condition that returns ``false`` or 0 will call ``abort()`` and trigger a :doc:`Fatal Error </api-guides/fatal-errors>`.
  152. ``assert()`` should only be used to detect unrecoverable errors due to a serious internal logic bug or corruption, where it is not possible for the program to continue. For recoverable errors, including errors that are possible due to invalid external input, an :doc:`error value should be returned </api-guides/error-handling>`.
  153. .. note::
  154. When asserting a value of type ``esp_err_t`` is equal to ``ESP_OK``, use the :ref:`esp-error-check-macro` instead of an ``assert()``.
  155. It is possible to configure ESP-IDF projects with assertions disabled (see :ref:`CONFIG_COMPILER_OPTIMIZATION_ASSERTION_LEVEL`). Therefore, functions called in an ``assert()`` statement should not have side-effects.
  156. It is also necessary to use particular techniques to avoid "variable set but not used" warnings when assertions are disabled, due to code patterns such as::
  157. int res = do_something();
  158. assert(res == 0);
  159. Once the ``assert`` is optimized out, the ``res`` value is unused and the compiler will warn about this. However the function ``do_something()`` must still be called, even if assertions are disabled.
  160. When the variable is declared and initialized in a single statement, a good strategy is to cast it to ``void`` on a new line. The compiler will not produce a warning, and the variable can still be optimized out of the final binary::
  161. int res = do_something();
  162. assert(res == 0);
  163. (void)res;
  164. If the variable is declared separately, for example if it is used for multiple assertions, then it can be declared with the GCC attribute ``__attribute__((unused))``. The compiler will not produce any unused variable warnings, but the variable can still be optimized out::
  165. int res __attribute__((unused));
  166. res = do_something();
  167. assert(res == 0);
  168. res = do_something_else();
  169. assert(res != 0);
  170. Header File Guards
  171. ------------------
  172. All public facing header files should have preprocessor guards. A pragma is preferred::
  173. #pragma once
  174. over the following pattern::
  175. #ifndef FILE_NAME_H
  176. #define FILE_NAME_H
  177. ...
  178. #endif // FILE_NAME_H
  179. In addition to guard macros, all C header files should have ``extern "C"`` guards to allow the header to be used from C++ code. Note that the following order should be used: ``pragma once``, then any ``#include`` statements, then ``extern "C"`` guards::
  180. #pragma once
  181. #include <stdint.h>
  182. #ifdef __cplusplus
  183. extern "C" {
  184. #endif
  185. /* declarations go here */
  186. #ifdef __cplusplus
  187. }
  188. #endif
  189. Include Statements
  190. ------------------
  191. When writing ``#include`` statements, try to maintain the following order:
  192. * C standard library headers.
  193. * Other POSIX standard headers and common extensions to them (such as ``sys/queue.h``.)
  194. * Common IDF headers (``esp_log.h``, ``esp_system.h``, ``esp_timer.h``, ``esp_sleep.h``, etc.)
  195. * Headers of other components, such as FreeRTOS.
  196. * Public headers of the current component.
  197. * Private headers.
  198. Use angle brackets for C standard library headers and other POSIX headers (``#include <stdio.h>``).
  199. Use double quotes for all other headers (``#include "esp_log.h"``).
  200. C++ Code Formatting
  201. -------------------
  202. The same rules as for C apply. Where they are not enough, apply the following rules.
  203. File Naming
  204. ^^^^^^^^^^^^
  205. C++ Header files have the extension ``.hpp``. C++ source files have the extension ``.cpp``. The latter is important for the compiler to distinguish them from normal C source files.
  206. Naming
  207. ^^^^^^
  208. * **Class and struct** names shall be written in ``CamelCase`` with a capital letter as beginning. Member variables and methods shall be in ``snake_case``. An exception from ``CamelCase`` is if the readability is severely decreased, e.g., in ``GPIOOutput``, then an underscore ``_`` is allowed to make it more readable: ``GPIO_Output``.
  209. * **Namespaces** shall be in lower ``snake_case``.
  210. * **Templates** are specified in the line above the function declaration.
  211. * Interfaces in terms of Object-Oriented Programming shall be named without the suffix ``...Interface``. Later, this makes it easier to extract interfaces from normal classes and vice versa without making a breaking change.
  212. Member Order in Classes
  213. ^^^^^^^^^^^^^^^^^^^^^^^
  214. In order of precedence:
  215. * First put the public members, then the protected, then private ones. Omit public, protected or private sections without any members.
  216. * First put constructors/destructors, then member functions, then member variables.
  217. For example:
  218. ::
  219. class ForExample {
  220. public:
  221. // first constructors, then default constructor, then destructor
  222. ForExample(double example_factor_arg);
  223. ForExample();
  224. ~ForExample();
  225. // then remaining pubic methods
  226. set_example_factor(double example_factor_arg);
  227. // then public member variables
  228. uint32_t public_data_member;
  229. private:
  230. // first private methods
  231. void internal_method();
  232. // then private member variables
  233. double example_factor;
  234. };
  235. Spacing
  236. ^^^^^^^
  237. * Do not indent inside namespaces.
  238. * Put ``public``, ``protected`` and ``private`` labels at the same indentation level as the corresponding ``class`` label.
  239. Simple Example
  240. ^^^^^^^^^^^^^^^
  241. ::
  242. // file spaceship.h
  243. #ifndef SPACESHIP_H_
  244. #define SPACESHIP_H_
  245. #include <cstdlib>
  246. namespace spaceships {
  247. class SpaceShip {
  248. public:
  249. SpaceShip(size_t crew);
  250. size_t get_crew_size() const;
  251. private:
  252. const size_t crew;
  253. };
  254. class SpaceShuttle : public SpaceShip {
  255. public:
  256. SpaceShuttle();
  257. };
  258. class Sojuz : public SpaceShip {
  259. public:
  260. Sojuz();
  261. };
  262. template <typename T>
  263. class CargoShip {
  264. public:
  265. CargoShip(const T &cargo);
  266. private:
  267. T cargo;
  268. };
  269. } // namespace spaceships
  270. #endif // SPACESHIP_H_
  271. // file spaceship.cpp
  272. #include "spaceship.h"
  273. namespace spaceships {
  274. // Putting the curly braces in the same line for constructors is OK if it only initializes
  275. // values in the initializer list
  276. SpaceShip::SpaceShip(size_t crew) : crew(crew) { }
  277. size_t SpaceShip::get_crew_size() const
  278. {
  279. return crew;
  280. }
  281. SpaceShuttle::SpaceShuttle() : SpaceShip(7)
  282. {
  283. // doing further initialization
  284. }
  285. Sojuz::Sojuz() : SpaceShip(3)
  286. {
  287. // doing further initialization
  288. }
  289. template <typename T>
  290. CargoShip<T>::CargoShip(const T &cargo) : cargo(cargo) { }
  291. } // namespace spaceships
  292. CMake Code Style
  293. ----------------
  294. - Indent with four spaces.
  295. - Maximum line length 120 characters. When splitting lines, try to
  296. focus on readability where possible (for example, by pairing up
  297. keyword/argument pairs on individual lines).
  298. - Do not put anything in the optional parentheses after ``endforeach()``, ``endif()``, etc.
  299. - Use lowercase (``with_underscores``) for command, function, and macro names.
  300. - For locally scoped variables, use lowercase (``with_underscores``).
  301. - For globally scoped variables, use uppercase (``WITH_UNDERSCORES``).
  302. - Otherwise follow the defaults of the cmake-lint_ project.
  303. Configuring the Code Style for a Project Using EditorConfig
  304. -----------------------------------------------------------
  305. EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs. The EditorConfig project consists of a file format for defining coding styles and a collection of text editor plugins that enable editors to read the file format and adhere to defined styles. EditorConfig files are easily readable and they work nicely with version control systems.
  306. For more information, see `EditorConfig <https://editorconfig.org>`_ Website.
  307. Third Party Component Code Styles
  308. ---------------------------------
  309. ESP-IDF integrates a number of third party components where these components may have differing code styles.
  310. FreeRTOS
  311. ^^^^^^^^
  312. The code style adopted by FreeRTOS is described in the `FreeRTOS style guide <https://www.freertos.org/FreeRTOS-Coding-Standard-and-Style-Guide.html#StyleGuide>`_. Formatting of FreeRTOS source code is automated using `Uncrustify <https://github.com/uncrustify/uncrustify>`_, thus a copy of the FreeRTOS code style's Uncrustify configuration (``uncrustify.cfg``) is stored within ESP-IDF FreeRTOS component.
  313. If a FreeRTOS source file is modified, the updated file can be formatted again by following the steps below:
  314. 1. Ensure that Uncrustify (v0.69.0) is installed on your system
  315. 2. Run the following command on the update FreeRTOS source file (where ``source.c`` is the path to the source file that requires formatting).
  316. .. code-block:: bash
  317. uncrustify -c $IDF_PATH/components/freertos/FreeRTOS-Kernel/uncrustify.cfg --replace source.c --no-backup
  318. Documenting Code
  319. ----------------
  320. Please see the guide here: :doc:`documenting-code`.
  321. Structure
  322. ---------
  323. To be written.
  324. Language Features
  325. -----------------
  326. To be written.
  327. .. _cmake-lint: https://github.com/richq/cmake-lint