Coding style

From Diablo

This document contains coding guidelines for Diablo contributors. It is expected that all new code contibuted to Diablo will follow this standard.

Note: A lot of people have contributed to Diablo before these guidelines came into existence so some of the code currently in Diablo might not conform to these guidelines. As time permits, we will make the code conform when we visit source files where changes are made.

Naming conventions

Function names should have the first letter of each component of the name capitalized.

All global functions should be prefixed with the data type of the main data they are working on. For example, all functions working on basic blocks should be prefixed with Bbl. Whenever possible, the first argument should be this data.

Defines should have all capital letters with underscores between components of the name.
unless they are to be treated as function calls in which case they should be named as functions.

Spaces

* Use a single space after flow-control key words (if, for, do, while), before the opening parentheses
* Use a single space after function names, before the opening parentheses, unless the function is macro-like (i.e. all uppercase letters and underscores)
* Use a single space between components inside parentheses (after the comma)
* For pointer declarations, do not use spaces between the asterisk and the variable.

Indentation

Use spaces to indent, not tabs. Diablo uses two spaces for indentation.

Use Allman brace style, where the opening brace is placed on the next line as the source code line preceding it.

This is correct:

if (response) 
{
    /* do something */
}

Always use braces--even for single statement blocks

Comments

Use Ansi-C style (/* ... */) comments. C++ style comments are not recommended. Some C compilers do not support C++ style comments.

Declarations

In a function declaration the
storage class (if specified) and data type are placed on a line by themselves directly preceding the name of the function.

Local variables are declared at the beginning of a block

C99 allows you to declare variables anywhere in a function, but a lot of compilers still do not support C99.

Example

Here's an example that puts this all together:

void
BblDoSomething (t_bbl * bbl, int j)
{
  int foo, bar;

  if ((foo == 0) && (bar == 0)) 
  {
      t_ins * ins = InsGet (foo, bar);
  }
}