codingstyle.rst 4.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Coding Style
============

Mesa is over 20 years old and the coding style has evolved over time.
Some old parts use a style that's a bit out of date. Different sections
of mesa can use different coding style as set in the local EditorConfig
(.editorconfig) and/or Emacs (.dir-locals.el) file. Alternatively the
following is applicable. If the guidelines below don't cover something,
try following the format of existing, neighboring code.

Basic formatting guidelines

-  3-space indentation, no tabs.
-  Limit lines to 78 or fewer characters. The idea is to prevent line
   wrapping in 80-column editors and terminals. There are exceptions,
   such as if you're defining a large, static table of information.
-  Opening braces go on the same line as the if/for/while statement. For
   example:

Erik Faye-Lund's avatar
Erik Faye-Lund committed
20
   .. code-block:: c
21
22
23
24
25
26
27
28
29
30
31
32

      if (condition) {
         foo;
      } else {
         bar;
      }

-  Put a space before/after operators. For example, ``a = b + c;`` and
   not ``a=b+c;``
-  This GNU indent command generally does the right thing for
   formatting:

Erik Faye-Lund's avatar
Erik Faye-Lund committed
33
   .. code-block:: console
34
35
36
37
38
39
40
41
42
43

      indent -br -i3 -npcs --no-tabs infile.c -o outfile.c

-  Use comments wherever you think it would be helpful for other
   developers. Several specific cases and style examples follow. Note
   that we roughly follow `Doxygen <http://www.doxygen.nl>`__
   conventions.

   Single-line comments:

Erik Faye-Lund's avatar
Erik Faye-Lund committed
44
   .. code-block:: c
45
46
47
48
49
50

      /* null-out pointer to prevent dangling reference below */
      bufferObj = NULL;

   Or,

Erik Faye-Lund's avatar
Erik Faye-Lund committed
51
   .. code-block:: c
52
53
54
55
56

      bufferObj = NULL;  /* prevent dangling reference below */

   Multi-line comment:

Erik Faye-Lund's avatar
Erik Faye-Lund committed
57
   .. code-block:: c
58
59
60
61
62
63
64

      /* If this is a new buffer object id, or one which was generated but
       * never used before, allocate a buffer object now.
       */

   We try to quote the OpenGL specification where prudent:

Erik Faye-Lund's avatar
Erik Faye-Lund committed
65
   .. code-block:: c
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

      /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
       *
       *     "An INVALID_OPERATION error is generated for any of the following
       *     conditions:
       *
       *     * <length> is zero."
       *
       * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
       * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
       * either.
       */

   Function comment example:

Erik Faye-Lund's avatar
Erik Faye-Lund committed
81
   .. code-block:: c
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

      /**
       * Create and initialize a new buffer object.  Called via the
       * ctx->Driver.CreateObject() driver callback function.
       * \param  name  integer name of the object
       * \param  type  one of GL_FOO, GL_BAR, etc.
       * \return  pointer to new object or NULL if error
       */
      struct gl_object *
      _mesa_create_object(GLuint name, GLenum type)
      {
         /* function body */
      }

-  Put the function return type and qualifiers on one line and the
   function name and parameters on the next, as seen above. This makes
   it easy to use ``grep ^function_name dir/*`` to find function
   definitions. Also, the opening brace goes on the next line by itself
   (see above.)
-  Function names follow various conventions depending on the type of
   function:

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
   +---------------------+------------------------------------------+
   | Convention          | Explanation                              |
   +=====================+==========================================+
   | ``glFooBar()``      | a public GL entry point (in              |
   |                     | :file:`glapi_dispatch.c`)                |
   +---------------------+------------------------------------------+
   | ``_mesa_FooBar()``  | the internal immediate mode function     |
   +---------------------+------------------------------------------+
   | ``save_FooBar()``   | retained mode (display list) function in |
   |                     | :file:`dlist.c`                          |
   +---------------------+------------------------------------------+
   | ``foo_bar()``       | a static (private) function              |
   +---------------------+------------------------------------------+
   | ``_mesa_foo_bar()`` | an internal non-static Mesa function     |
   +---------------------+------------------------------------------+
119
120
121
122

-  Constants, macros and enum names are ``ALL_UPPERCASE``, with \_
   between words.
-  Mesa usually uses camel case for local variables (Ex:
Erik Faye-Lund's avatar
Erik Faye-Lund committed
123
   ``localVarname``) while Gallium typically uses underscores (Ex:
124
125
126
127
128
129
130
   ``local_var_name``).
-  Global variables are almost never used because Mesa should be
   thread-safe.
-  Booleans. Places that are not directly visible to the GL API should
   prefer the use of ``bool``, ``true``, and ``false`` over
   ``GLboolean``, ``GL_TRUE``, and ``GL_FALSE``. In C code, this may
   mean that ``#include <stdbool.h>`` needs to be added. The
131
132
   ``try_emit_*`` method ``src/mesa/state_tracker/st_glsl_to_tgsi.cpp``
   can serve as an example.