From c6184f84b7227e1548947e42bca3ff3ddb7e379c Mon Sep 17 00:00:00 2001
From: Brian Paul
-Mesa's code style has changed over the years. Here's the latest.
+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.
+If the guidelines below don't cover something, try following the format of
+existing, neighboring code.
-Comment your code! It's extremely important that open-source code be
-well documented. Also, strive to write clean, easily understandable code.
+Basic formatting guidelines
-3-space indentation
-
-If you use tabs, set them to 8 columns
-
-Line width: the preferred width to fill comments and code in Mesa is 78
-columns. Exceptions are sometimes made for clarity (e.g. tabular data is
-sometimes filled to a much larger width so that extraneous carriage returns
-don't obscure the table).
-
-Brace example:
-
-Here's the GNU indent command which will best approximate my preferred style:
-(Note that it won't format switch statements in the preferred way)
-
-Local variable name example: localVarName (no underscores)
-
-Constants and macros are ALL_UPPERCASE, with _ between words
-
-Global variables are not allowed.
-
-Function name examples:
-
-Places that are not directly visible to the GL API should prefer the use
-of bool, true, and
+Coding Style
+
+ if (condition) {
+ foo;
+ } else {
+ bar;
+ }
+
-
- if (condition) {
- foo;
- }
- else {
- bar;
- }
-
- switch (condition) {
- case 0:
- foo();
- break;
-
- case 1: {
- ...
- break;
- }
-
- default:
- ...
- break;
- }
+ indent -br -i3 -npcs --no-tabs infile.c -o outfile.c
-
+
+Single-line comments:
- indent -br -i3 -npcs --no-tabs infile.c -o outfile.c
+ /* null-out pointer to prevent dangling reference below */
+ bufferObj = NULL;
+
+Or,
+
+ bufferObj = NULL; /* prevent dangling reference below */
+
+Multi-line comment:
+
+ /* 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:
+
+ /* 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:
+ *
+ * *
+Function comment example:
+
+ /**
+ * 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 */
+ }
+grep ^function_name dir/*
to find function definitions. Also,
+the opening brace goes on the next line by itself (see above.)
-
- glFooBar() - a public GL entry point (in glapi_dispatch.c)
- _mesa_FooBar() - the internal immediate mode function
- save_FooBar() - retained mode (display list) function in dlist.c
- foo_bar() - a static (private) function
- _mesa_foo_bar() - an internal non-static Mesa function
+ glFooBar() - a public GL entry point (in glapi_dispatch.c)
+ _mesa_FooBar() - the internal immediate mode function
+ save_FooBar() - retained mode (display list) function in dlist.c
+ foo_bar() - a static (private) function
+ _mesa_foo_bar() - an internal non-static Mesa function
-