Skip to content

Commit

Permalink
coding standard: defining unsigned integer constants using U()
Browse files Browse the repository at this point in the history
Misra C rule 7.2 says:
"A 'u' or 'U' suffix shall be applied to all integer constants that are
represented in an unsigned type"

Besides Misra this makes sense anyway to make the sign and size of the
integer well defined instead of depending on the size of the constant or
version of C standard a compiler is using.

Do this with the U() macro instead of an explicit 'U' at the end to make
the defines usable from assembly too.

Acked-by: Jerome Forissier <jerome@forissier.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
  • Loading branch information
jenswi-linaro authored and jbech-linaro committed May 4, 2021
1 parent 37453fd commit b9c4b28
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions general/coding_standards.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ Variables are initialized according to these general guidelines:
which can be a scalar or a composite type are initialized with
``memset()`` in order to minimize the amount of future headache.

Unsigned integer constants are defined using the ``U()``, ``UL()`` or
``ULL()`` macros, depending on the required width. ``U()`` is a good choice
for 32-bit values. Any of the minimum width cousins
``UINT{8,16,32,64}_C()`` are also accepted for compatibility. This makes
the sign and size of the integer well defined instead of depend on how
large the value is or which compiler is used. For example:

.. code-block:: c
#define MY_UNSIGNED_CONSTANT U(123)
Regarding the checkpatch tool, it is not included directly into this project.
Please use checkpatch.pl from the Linux kernel git in combination with the local
`checkpatch script`_.
Expand Down

0 comments on commit b9c4b28

Please sign in to comment.