code_style_guidelines.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. .. _doc_code_style_guidelines:
  2. Code style guidelines
  3. =====================
  4. .. highlight:: shell
  5. When contributing to Godot's source code, you will be expected to follow the
  6. style guidelines outlined below. Some of them are checked via the Continuous
  7. Integration process and reviewers will ask you to fix potential issues, so
  8. best setup your system as outlined below to ensure all your commits follow the
  9. guidelines.
  10. C++ and Objective-C
  11. -------------------
  12. There are no written guidelines, but the code style agreed upon by the
  13. developers is enforced via the `clang-format <http://clang.llvm.org/docs/ClangFormat.html>`__
  14. code beautifier, which takes care for you of all our conventions.
  15. To name a few:
  16. - Indentation and alignment are both tab based (respectively one and two tabs)
  17. - One space around math and assignments operators as well as after commas
  18. - Pointer and reference operators are affixed to the variable identifier, not
  19. to the type name
  20. - See further down regarding header includes
  21. The rules used by clang-format are outlined in the
  22. `.clang-format <https://github.com/godotengine/godot/blob/master/.clang-format>`__
  23. file of the Godot repository.
  24. As long as you ensure that your style matches the surrounding code and that you
  25. not introducing trailing whitespace or space-based indentation, you should be
  26. fine. If you plan to contribute regularly however, we strongly advise that you
  27. setup clang-format locally to check and automatically fix all your commits.
  28. .. warning:: Godot's code style should *not* be applied to thirdparty code,
  29. i.e. that is included in Godot's source tree but was not written
  30. specifically for our project. Such code usually come from
  31. different upstream projects with their own style guides (or lack
  32. thereof), and don't want to introduce differences that would make
  33. syncing with upstream repositories harder.
  34. Thirdparty code is usually included in the ``thirdparty/`` folder
  35. and can thus easily be excluded from formatting scripts. For the
  36. rare cases where a thirdparty code snippet needs to be included
  37. directly within a Godot file, you can use
  38. ``/* clang-format off */`` and ``/* clang-format on */`` to tell
  39. clang-format to ignore a chunk of code.
  40. Using clang-format locally
  41. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  42. First of all, you will need to install clang-format. As of now, you need to use
  43. **clang-format 8.x** to be compatible with Godot's format. Later versions might
  44. be suitable, but previous versions had bugs that will cause formatting changes
  45. to the current code base.
  46. Installation
  47. ^^^^^^^^^^^^
  48. Here's how to install clang-format:
  49. - Linux: It will usually be available out-of-the-box with the clang toolchain
  50. packaged by your distribution. If your distro version is not the required one,
  51. you can download a pre-compiled version from the
  52. `LLVM website <http://releases.llvm.org/download.html>`__, or if you are on
  53. a Debian derivative, use the `upstream repos <http://apt.llvm.org/>`__.
  54. - macOS and Windows: You can download precompiled binaries from the
  55. `LLVM website <http://releases.llvm.org/download.html>`__. You may need to add
  56. the path to the binary's folder to your system's ``PATH`` environment
  57. variable to be able to call ``clang-format`` out of the box.
  58. You then have different possibilities to apply clang-format to your changes:
  59. Manual usage
  60. ^^^^^^^^^^^^
  61. You can apply clang-format manually one or more files with the following
  62. command:
  63. ::
  64. clang-format -i <path/to/file(s)>
  65. - ``-i`` means that the changes should be written directly to the file (by
  66. default clang-format would only output the fixed version to the terminal).
  67. - The path can point to several files, either one after the other or using
  68. wildcards like in a typical Unix shell. Be careful when globbing so that
  69. you don't run clang-format on compiled objects (.o and .a files) that are
  70. in Godot's tree. So better use ``core/*.{cpp,h}`` than ``core/*``.
  71. Pre-commit hook
  72. ^^^^^^^^^^^^^^^
  73. For ease of use, we provide a pre-commit hook for Git that will run
  74. clang-format automatically on all your commits to check them, and let you apply
  75. its changes in the final commit.
  76. This "hook" is a script which can be found in ``misc/hooks``, refer to that
  77. folder's README.md for installation instructions.
  78. If your clang-format is not in the ``PATH``, you may have to edit the
  79. ``pre-commit-clang-format`` to point to the correct binary for it to work.
  80. The hook was tested on Linux and macOS, but should also work in the Git Shell
  81. on Windows.
  82. IDE plugin
  83. ^^^^^^^^^^
  84. Most IDEs or code editors have beautifier plugins that can be configured to run
  85. clang-format automatically, for example each time you save a file.
  86. Here is a non-exhaustive list of beautifier plugins for some IDEs:
  87. - Qt Creator: `Beautifier plugin <http://doc.qt.io/qtcreator/creator-beautifier.html>`__
  88. - Visual Studio Code: `Clang-Format <https://marketplace.visualstudio.com/items?itemName=xaver.clang-format>`__
  89. - Visual Studio: `ClangFormat <https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.ClangFormat>`__
  90. - vim: `vim-clang-format <https://github.com/rhysd/vim-clang-format>`__
  91. (Pull requests welcome to extend this list with tested plugins.)
  92. Header includes
  93. ~~~~~~~~~~~~~~~
  94. When adding new C++ or Objective-C files or including new headers in existing
  95. ones, the following rules should be followed:
  96. - The first lines in the file should be Godot's copyright header and MIT
  97. license, copy-pasted from another file. Make sure to adjust the filename.
  98. - In a ``.h`` header, include guards should be used with the form
  99. ``FILENAME_H``.
  100. - In a ``.cpp`` file (e.g. ``filename.cpp``), the first include should be the
  101. one where the class is declared (e.g. ``#include "filename.h"``), followed by
  102. an empty line for separation.
  103. - Then come headers from Godot's own code base, included in alphabetical order
  104. (enforced by ``clang-format``) with paths relative to the root folder. Those
  105. includes should be done with quotes, e.g. ``#include "core/object.h"``. The
  106. block of Godot header includes should then be followed by an empty line for
  107. separation.
  108. - Finally, thirdparty headers (either from ``thirdparty`` or from the system's
  109. include paths) come next and should be included with the < and > symbols, e.g.
  110. ``#include <png.h>``. The block of thirdparty headers should also be followed
  111. by an empty line for separation.
  112. - Godot and thirdparty headers should be included in the file that requires
  113. them, i.e. in the `.h` header if used in the declarative code or in the `.cpp`
  114. if used only in the imperative code.
  115. Example:
  116. .. code:: cpp
  117. /*************************************************************************/
  118. /* my_new_file.h */
  119. /*************************************************************************/
  120. /* This file is part of: */
  121. /* GODOT ENGINE */
  122. /* https://godotengine.org */
  123. /*************************************************************************/
  124. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  125. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  126. /* */
  127. /* Permission is hereby granted, free of charge, to any person obtaining */
  128. /* a copy of this software and associated documentation files (the */
  129. /* "Software"), to deal in the Software without restriction, including */
  130. /* without limitation the rights to use, copy, modify, merge, publish, */
  131. /* distribute, sublicense, and/or sell copies of the Software, and to */
  132. /* permit persons to whom the Software is furnished to do so, subject to */
  133. /* the following conditions: */
  134. /* */
  135. /* The above copyright notice and this permission notice shall be */
  136. /* included in all copies or substantial portions of the Software. */
  137. /* */
  138. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  139. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  140. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  141. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  142. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  143. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  144. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  145. /*************************************************************************/
  146. #ifndef MY_NEW_FILE_H
  147. #define MY_NEW_FILE_H
  148. #include "core/hash_map.h"
  149. #include "core/list.h"
  150. #include "scene/gui/control.h
  151. #include <png.h>
  152. ...
  153. #endif // MY_NEW_FILE_H
  154. .. code:: cpp
  155. /*************************************************************************/
  156. /* my_new_file.cpp */
  157. /*************************************************************************/
  158. /* This file is part of: */
  159. /* GODOT ENGINE */
  160. /* https://godotengine.org */
  161. /*************************************************************************/
  162. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  163. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  164. /* */
  165. /* Permission is hereby granted, free of charge, to any person obtaining */
  166. /* a copy of this software and associated documentation files (the */
  167. /* "Software"), to deal in the Software without restriction, including */
  168. /* without limitation the rights to use, copy, modify, merge, publish, */
  169. /* distribute, sublicense, and/or sell copies of the Software, and to */
  170. /* permit persons to whom the Software is furnished to do so, subject to */
  171. /* the following conditions: */
  172. /* */
  173. /* The above copyright notice and this permission notice shall be */
  174. /* included in all copies or substantial portions of the Software. */
  175. /* */
  176. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  177. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  178. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  179. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  180. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  181. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  182. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  183. /*************************************************************************/
  184. #include "my_new_file.h"
  185. #include "core/math/math_funcs.h"
  186. #include "scene/gui/line_edit.h
  187. #include <zlib.h>
  188. #include <zstd.h>
  189. Java
  190. ----
  191. Godot's Java code (mostly in ``platform/android``) is also enforced via
  192. ``clang-format``, so see the instructions above to set it up. Keep in mind that
  193. this style guide only applies to code written and maintained by Godot, not
  194. thirdparty code such as the ``java/src/com/google`` subfolder.
  195. Python
  196. ------
  197. Godot's SCons buildsystem is written in Python, and various scripts included
  198. in the source tree are also using Python.
  199. For those, we follow the `PEP-8 style guide <https://www.python.org/dev/peps/pep-0008/>`__,
  200. this is however not as strongly enforced as for the C++ code. If you are so
  201. inclined, you can check and format your Python changes using
  202. `autopep8 <https://pypi.org/project/autopep8/>`__.