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