.. _Coding Style:
-============
Coding Style
-============
+############
Suricata uses a fairly strict coding style. This document describes it.
Formatting
-~~~~~~~~~~
+**********
clang-format
-^^^^^^^^^^^^
+============
``clang-format`` is configured to help you with formatting C code.
.. note::
The ``.clang-format`` script requires clang 9 or newer.
Format your Changes
-*******************
+-------------------
Before opening a pull request, please also try to ensure it is formatted
properly. We use ``clang-format`` for this, which has git integration through the
that isolates you from the different versions.
Formatting the most recent commit only
-""""""""""""""""""""""""""""""""""""""
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following command will format only the code changed in the most recent commit:
.. code-block:: bash
commits.
Formatting code in staging
-""""""""""""""""""""""""""
+~~~~~~~~~~~~~~~~~~~~~~~~~~
The following command will format the changes in staging, i.e. files you
``git add``-ed:
$ scripts/clang-format.sh cached --force
Formatting your branch's commits
-""""""""""""""""""""""""""""""""
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In case you have multiple commits on your branch already and forgot to
format them you can fix that up as well.
new commits on ``master`` in case you've updated master since you've branched.
Check formatting
-""""""""""""""""
+~~~~~~~~~~~~~~~~
Check if your branch changes' formatting is correct with:
.. code-block:: bash
change.
Formatting a whole file
-"""""""""""""""""""""""
+~~~~~~~~~~~~~~~~~~~~~~~
+--------------------------------------------------------------------+
| **Note** |
$ clang-format -i {file}
Disabling clang-format
-**********************
+----------------------
There might be times, where the clang-format's formatting might not please.
This might mostly happen with macros, arrays (single or multi-dimensional ones),
/* clang-format on */
Installing clang-format and git-clang-format
-********************************************
+--------------------------------------------
clang-format 9 or newer is required.
On ubuntu 18.04:
Line length
-^^^^^^^^^^^
+===========
Limit line lengths to 100 characters.
Indent
-^^^^^^
+======
We use 4 space indentation.
- UseTab: Never [llvm]_
Braces
-^^^^^^
+======
Functions should have the opening brace on a newline:
- IndentBraces: false
Flow
-~~~~
+****
Don't use conditions and statements on the same line. E.g.
- SplitEmptyRecord: true
Alignment
-~~~~~~~~~
+*********
Pointers
-^^^^^^^^
+========
Pointers shall be right aligned.
.. code-block:: c
- DerivePointerAlignment: false
Declarations and Comments
-^^^^^^^^^^^^^^^^^^^^^^^^^
+=========================
Trailing comments should be aligned for consecutive lines.
.. code-block:: c
- AlignTrailingComments: true
Functions
-~~~~~~~~~
+*********
parameter names
-^^^^^^^^^^^^^^^
+===============
TODO
Function names
-^^^^^^^^^^^^^^
+==============
Function names are NamedLikeThis().
static SCConfNode *SCConfGetNodeOrCreate(char *name, int final)
static vs non-static
-^^^^^^^^^^^^^^^^^^^^
+====================
Functions should be declared static whenever possible.
inline
-^^^^^^
+======
The inlining of functions should be used only in critical paths.
Variables
-~~~~~~~~~
+*********
Names
-^^^^^
+=====
A variable is ``named_like_this`` in all lowercase.
In loop vars, make sure ``i`` is a signed int type.
Scope
-^^^^^
+=====
TODO
Macros
-~~~~~~
+******
Macro names are ALL_CAPS_WITH_UNDERSCORES.
Enclose parameters in parens on each usage inside the macro.
- AlignEscapedNewlines: Right
Comments
-~~~~~~~~
+********
TODO
Function comments
-^^^^^^^^^^^^^^^^^
+=================
We use Doxygen, functions are documented using Doxygen notation:
static SCConfNode *SCConfGetNodeOrCreate(char *name, int final)
General comments
-^^^^^^^^^^^^^^^^
+================
We use ``/* foobar */`` style and try to avoid ``//`` style.
File names
-~~~~~~~~~~
+**********
File names are all lowercase and have a .c. .h or .rs (Rust) extension.
Some cases have a multi-layer prefix, e.g. ``util-mpm-ac.c``
Enums
-~~~~~
+*****
Use a common prefix for all enum values. Value names are ALL_CAPS_WITH_UNDERSCORES.
- AllowShortEnumsOnASingleLine: false [clang11]_
Structures and typedefs
-~~~~~~~~~~~~~~~~~~~~~~~
+***********************
TODO
switch statements
-~~~~~~~~~~~~~~~~~
+*****************
Switch statements are indented like in the following example, so the 'case' is indented from the switch:
- AfterCaseLabel: false (default)
const
-~~~~~
+*****
TODO
goto
-~~~~
+****
Goto statements should be used with care. Generally, we use it primarily for error handling. E.g.:
- IndentGotoLabels: true (default) [clang10]_
Includes
-~~~~~~~~
+********
TODO
- SortIncludes: false
Unittests
-~~~~~~~~~
+*********
When writing unittests that use a data array containing a protocol message, please put an explanatory comment that contain the readable content of the message
static uint8_t welcome_reply[] = { 0x32, 0x32, 0x30, 0x20,
Banned functions
-~~~~~~~~~~~~~~~~
+****************
+------------+---------------+-----------+
| function | replacement | reason |