1bf215546Sopenharmony_ciCoding Style
2bf215546Sopenharmony_ci============
3bf215546Sopenharmony_ci
4bf215546Sopenharmony_ciMesa is over 20 years old and the coding style has evolved over time.
5bf215546Sopenharmony_ciSome old parts use a style that's a bit out of date. Different sections
6bf215546Sopenharmony_ciof mesa can use different coding style as set in the local EditorConfig
7bf215546Sopenharmony_ci(.editorconfig) and/or Emacs (.dir-locals.el) file. Alternatively the
8bf215546Sopenharmony_cifollowing is applicable. If the guidelines below don't cover something,
9bf215546Sopenharmony_citry following the format of existing, neighboring code.
10bf215546Sopenharmony_ci
11bf215546Sopenharmony_ciBasic formatting guidelines
12bf215546Sopenharmony_ci
13bf215546Sopenharmony_ci-  3-space indentation, no tabs.
14bf215546Sopenharmony_ci-  Limit lines to 78 or fewer characters. The idea is to prevent line
15bf215546Sopenharmony_ci   wrapping in 80-column editors and terminals. There are exceptions,
16bf215546Sopenharmony_ci   such as if you're defining a large, static table of information.
17bf215546Sopenharmony_ci-  Opening braces go on the same line as the if/for/while statement. For
18bf215546Sopenharmony_ci   example:
19bf215546Sopenharmony_ci
20bf215546Sopenharmony_ci   .. code-block:: c
21bf215546Sopenharmony_ci
22bf215546Sopenharmony_ci      if (condition) {
23bf215546Sopenharmony_ci         foo;
24bf215546Sopenharmony_ci      } else {
25bf215546Sopenharmony_ci         bar;
26bf215546Sopenharmony_ci      }
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci-  Put a space before/after operators. For example, ``a = b + c;`` and
29bf215546Sopenharmony_ci   not ``a=b+c;``
30bf215546Sopenharmony_ci-  This GNU indent command generally does the right thing for
31bf215546Sopenharmony_ci   formatting:
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci   .. code-block:: console
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci      indent -br -i3 -npcs --no-tabs infile.c -o outfile.c
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci-  Use comments wherever you think it would be helpful for other
38bf215546Sopenharmony_ci   developers. Several specific cases and style examples follow. Note
39bf215546Sopenharmony_ci   that we roughly follow `Doxygen <http://www.doxygen.nl>`__
40bf215546Sopenharmony_ci   conventions.
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci   Single-line comments:
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   .. code-block:: c
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci      /* null-out pointer to prevent dangling reference below */
47bf215546Sopenharmony_ci      bufferObj = NULL;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   Or,
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   .. code-block:: c
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci      bufferObj = NULL;  /* prevent dangling reference below */
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci   Multi-line comment:
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   .. code-block:: c
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci      /* If this is a new buffer object id, or one which was generated but
60bf215546Sopenharmony_ci       * never used before, allocate a buffer object now.
61bf215546Sopenharmony_ci       */
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci   We try to quote the OpenGL specification where prudent:
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   .. code-block:: c
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci      /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
68bf215546Sopenharmony_ci       *
69bf215546Sopenharmony_ci       *     "An INVALID_OPERATION error is generated for any of the following
70bf215546Sopenharmony_ci       *     conditions:
71bf215546Sopenharmony_ci       *
72bf215546Sopenharmony_ci       *     * <length> is zero."
73bf215546Sopenharmony_ci       *
74bf215546Sopenharmony_ci       * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
75bf215546Sopenharmony_ci       * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
76bf215546Sopenharmony_ci       * either.
77bf215546Sopenharmony_ci       */
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci   Function comment example:
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   .. code-block:: c
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci      /**
84bf215546Sopenharmony_ci       * Create and initialize a new buffer object.  Called via the
85bf215546Sopenharmony_ci       * ctx->Driver.CreateObject() driver callback function.
86bf215546Sopenharmony_ci       * \param  name  integer name of the object
87bf215546Sopenharmony_ci       * \param  type  one of GL_FOO, GL_BAR, etc.
88bf215546Sopenharmony_ci       * \return  pointer to new object or NULL if error
89bf215546Sopenharmony_ci       */
90bf215546Sopenharmony_ci      struct gl_object *
91bf215546Sopenharmony_ci      _mesa_create_object(GLuint name, GLenum type)
92bf215546Sopenharmony_ci      {
93bf215546Sopenharmony_ci         /* function body */
94bf215546Sopenharmony_ci      }
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci-  Put the function return type and qualifiers on one line and the
97bf215546Sopenharmony_ci   function name and parameters on the next, as seen above. This makes
98bf215546Sopenharmony_ci   it easy to use ``grep ^function_name dir/*`` to find function
99bf215546Sopenharmony_ci   definitions. Also, the opening brace goes on the next line by itself
100bf215546Sopenharmony_ci   (see above.)
101bf215546Sopenharmony_ci-  Function names follow various conventions depending on the type of
102bf215546Sopenharmony_ci   function:
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   +---------------------+------------------------------------------+
105bf215546Sopenharmony_ci   | Convention          | Explanation                              |
106bf215546Sopenharmony_ci   +=====================+==========================================+
107bf215546Sopenharmony_ci   | ``glFooBar()``      | a public GL entry point (in              |
108bf215546Sopenharmony_ci   |                     | :file:`glapi_dispatch.c`)                |
109bf215546Sopenharmony_ci   +---------------------+------------------------------------------+
110bf215546Sopenharmony_ci   | ``_mesa_FooBar()``  | the internal immediate mode function     |
111bf215546Sopenharmony_ci   +---------------------+------------------------------------------+
112bf215546Sopenharmony_ci   | ``save_FooBar()``   | retained mode (display list) function in |
113bf215546Sopenharmony_ci   |                     | :file:`dlist.c`                          |
114bf215546Sopenharmony_ci   +---------------------+------------------------------------------+
115bf215546Sopenharmony_ci   | ``foo_bar()``       | a static (private) function              |
116bf215546Sopenharmony_ci   +---------------------+------------------------------------------+
117bf215546Sopenharmony_ci   | ``_mesa_foo_bar()`` | an internal non-static Mesa function     |
118bf215546Sopenharmony_ci   +---------------------+------------------------------------------+
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci-  Constants, macros and enum names are ``ALL_UPPERCASE``, with \_
121bf215546Sopenharmony_ci   between words.
122bf215546Sopenharmony_ci-  Mesa usually uses camel case for local variables (Ex:
123bf215546Sopenharmony_ci   ``localVarname``) while Gallium typically uses underscores (Ex:
124bf215546Sopenharmony_ci   ``local_var_name``).
125bf215546Sopenharmony_ci-  Global variables are almost never used because Mesa should be
126bf215546Sopenharmony_ci   thread-safe.
127bf215546Sopenharmony_ci-  Booleans. Places that are not directly visible to the GL API should
128bf215546Sopenharmony_ci   prefer the use of ``bool``, ``true``, and ``false`` over
129bf215546Sopenharmony_ci   ``GLboolean``, ``GL_TRUE``, and ``GL_FALSE``. In C code, this may
130bf215546Sopenharmony_ci   mean that ``#include <stdbool.h>`` needs to be added. The
131bf215546Sopenharmony_ci   ``try_emit_*`` method ``src/mesa/state_tracker/st_glsl_to_tgsi.cpp``
132bf215546Sopenharmony_ci   can serve as an example.
133