source: rtems-docs/eng/coding-conventions.rst @ ba3f265

Last change on this file since ba3f265 was ba3f265, checked in by Sebastian Huber <sebastian.huber@…>, on 07/24/20 at 05:10:51

eng: Add recommendations for attributes

Fix formatting.

  • Property mode set to 100644
File size: 10.6 KB
Line 
1.. SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. Copyright (C) 2018.
4.. COMMENT: RTEMS Foundation, The RTEMS Documentation Project
5
6.. COMMENT:TBD  - Convert the following to Rest and insert into this file
7.. COMMENT:TBD  - https://devel.rtems.org/wiki/Developer/Coding/Conventions
8
9Coding Conventions
10******************
11
12The style of RTEMS is generally consistent in the core areas.
13This page attempts to capture generally accepted practices.
14When in doubt, consult the code around you or look in cpukit/rtems.
15See the sister page `Doxygen Recommendations <https://devel.rtems.org/wiki/Developer/Coding/Doxygen>`_.
16for examples that illustrate style rules and Doxygen usage.
17
18Source Documentation
19--------------------
20
21* Use Doxygen according to our `Doxygen Recommendations <https://devel.rtems.org/wiki/Developer/Coding/Doxygen>`_..
22* Start each file with a brief description followed by a license.
23  See `Boilerplate File Header <https://devel.rtems.org/wiki/Developer/Coding/Boilerplate_File_Header>`_..
24* Use ``/* */`` comments.
25* Use comments wisely within function bodies, to explain
26  or draw attention without being verbose.
27* Use English prose and strive for good grammar,
28  spelling, and punctuation.
29* Use TODO: with a comment to indicate code that needs improvement.
30  Make it clear what there is to do.
31* Use XXX or FIXME to indicate an error/bug/broken code.
32
33Licenses
34--------
35
36The RTEMS Project has strict requirements on the types of software licenses
37that apply to software it includes and distributes. Submissions will be
38summarily rejected that do not follow the correct license or file header
39requirements.
40
41* Refer to :ref:`LicensingRequirements` for a discussion of the acceptable
42  licenses and the rationale.
43
44* Refer to :ref:`FileHeaderCopyright` for example copyright/license comment
45  blocks for various languages.
46
47Language and Compiler
48---------------------
49
50* Use C99.
51
52* Treat warnings as errors: eliminate them.
53
54* Favor C, but when assembly language is required use inline
55  assembly if possible.
56
57* Do not use compiler extensions.
58
59* Use the RTEMS macros defined in <rtems/score/basedefs.h> for abstracting
60  compiler-specific features.  For using attributes see the
61  `GCC attribute syntax <https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html#Attribute-Syntax>`_.
62  Prefer to place attributes in front of the declarator.  Try to be in line
63  with
64  `C++11 attributes <https://en.cppreference.com/w/cpp/language/attributes>`_
65  and C11 keywords such as
66  `_Noreturn <https://en.cppreference.com/w/c/language/_Noreturn>`_.
67
68* Use NULL for the null pointer, and prefer to use explicit
69  checks against NULL, e.g.,
70
71  .. code-block:: c
72
73      if ( ptr != NULL )
74
75  instead of
76
77  .. code-block:: c
78
79      if ( !ptr )
80
81* Use explicit checks for bits in variables.
82
83   * Example 1: Use
84
85      .. code-block:: c
86
87           if ( XBITS == (var & XBITS) )
88
89     to check for a set of defined bits.
90
91   * Example 2: Use
92
93      .. code-block:: c
94
95          if ( (var & X_FLAGS) != 0) )
96
97     instead of
98
99      .. code-block:: c
100
101          if ( !!(var & X_FLAGS) )
102
103     to check for at least 1 defined bit in a set.
104
105* Use ``(void) unused;`` to mark unused parameters and set-but-unused
106  variables immediately after being set.
107
108* Do not put function prototypes in C source files, any global functions
109  should have a prototype in a header file and any private function
110  should be declared static.
111
112* Declare global variables in exactly one header file.
113  Define global variables in at most one source file.
114  Include the header file declaring the global variable as
115  the first include file if possible to make sure that the
116  compiler checks the declaration and definition and that
117  the header file is self-contained.
118
119* Do not cast arguments to any printf() or printk() variant.
120  Use <inttypes.h> PRI constants for the types supported there.
121  Use <rtems/inttypes.h> for the other POSIX and RTEMS types that
122  have PRI constants defined there. This increases the portability
123  of the printf() format.
124
125* Do not use the register keyword. It is deprecated since C++14.
126
127Formatting
128-----------
129
130* Use spaces instead of tabs.
131* Use two spaces for indentation, four spaces for
132  hanging indentation.
133* Adhere to a limit of `80 characters per line <https://devel.rtems.org/wiki/Developer/Coding/80_characters_per_line>`_..
134* Put function return types and names on one line if they fit.
135* Put function calls on one line if they fit.
136* No space between a function name or function-like macro and
137  the opening parens.
138* Put braces on the same line as and one space after the
139  conditional expression ends.
140* Put the opening brace of a function definition one line after
141  the closing parenthesis of its prototype.
142* Put a single space inside and outside of each parenthesis
143  of a conditional expression.
144  * Exception: never put a space before a closing semi-colon.
145* Put a single space before and after ternary operators.
146* Put a single space before and after binary operators.
147* Put no space between unary operators (e.g. *, &, !, ~, ++, --)
148  and their operands.
149* No spaces around dereferencing operators (-> and .).
150* Do not use more than one blank line in a row.
151* Do not use trailing whitespace at the end of a line.
152
153Readability
154------------
155
156* Understand and follow the `naming rules <https://devel.rtems.org/wiki/Developer/Coding/NamingRules>`_..
157* Use typedef to remove 'struct', but do not use typedef
158  to hide pointers or arrays.
159  * Exception: typedef can be used to simplify function pointer types.
160
161* Do not mix variable declarations and code.
162* Declare variables at the start of a block.
163* Only use primitive initialization of variables at their declarations.
164  Avoid complex initializations or function calls in variable declarations.
165* Do not put unrelated functions or data in a single file.
166* Do not declare functions inside functions.
167* Avoid deep nesting by using early exits e.g. return, break, continue.
168  * Parameter checking should be done first with early error returns.
169  * Avoid allocation and critical sections until error checking is done.
170  * For error checks that require locking, do the checks early after acquiring locks.
171  * Use of 'goto' requires good reason and justification.
172
173* Test and action should stay close together.
174* Avoid complex logic in conditional and loop statements.
175* Put conditional and loop statements on the line after the expression.
176* Favor inline functions to hide
177  `compile-time feature-conditioned compilation <https://devel.rtems.org/wiki/Developer/Coding/Compile-time_feature-conditioned_compilation>`_..
178* Define non-inline functions in a .c source file.
179* Declare all global (non-static) functions in a .h header file.
180* Declare and define inline functions in one place. Usually, this
181  is a *impl.h header file.
182* Declare and define static functions in one place. Usually, this is
183  toward the start of a .c file. Minimize forward declarations of
184  static functions.
185* Function declarations should include variable names.
186* Avoid excess parentheses. Learn the
187  `operator precedence <https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence>`_. rules.
188* Always use parentheses with sizeof. This is an exception to the rule
189  about excess parentheses.
190
191Robustness
192-----------
193
194* Check all return statuses.
195* Validate input parameters.
196* Use debug assertions (assert).
197* Use const when appropriate for read-only function parameters
198  and compile-time constant values.
199* Do not hard code limits such as maximum instances into your code.
200* Prefer to use sizeof(variable) instead of sizeof(type).
201* Favor C automatic variables over global or static variables.
202* Use global variables only when necessary and ensure
203  atomicity of operations.
204* Do not shadow variables.
205* Avoid declaring large buffers or structures on the stack.
206* Avoid using zero (0) as a valid value. Memory often
207  defaults to being zero.
208* Favor mutual exclusion primitives over disabling preemption.
209* Avoid unnecessary dependencies, such as by not calling
210  ''printf()'' on error paths.
211* Avoid inline functions and macros with complicated logic
212  and decision points.
213* Prefer inline functions, enum, and const variables instead of CPP macros.
214* CPP macros should use a leading underscore for parameter
215  names and `avoid macro pitfalls <https://gcc.gnu.org/onlinedocs/cpp/Macro-Pitfalls.html#Macro-Pitfalls>`_..
216
217Portability
218-----------
219
220* Think portable! RTEMS supports a lot of target hardware.
221* For integer primitives, prefer to use precise-width integer
222  types from C99 stdint.h.
223* Write code that is 16-bit, 32-bit, and 64-bit friendly.
224
225Maintainability
226---------------
227
228* Minimize modifications to `third-party code <https://devel.rtems.org/wiki/Developer/Coding/ThirdPartyCode>`_..
229* Keep it simple! Simple code is easier to debug and easier to read than clever code.
230* Share code with other architectures, CPUs, and BSPs where possible.
231* Do not duplicate standard OS or C Library routines.
232
233Performance
234-----------
235
236* Prefer algorithms with the `lowest order of time and space <https://devel.rtems.org/wiki/FAQ/AlgorithmicComplexity>`_.
237  for fast, deterministic execution times with small memory footprints.
238* Understand the constraints of `real-time programming <https://devel.rtems.org/wiki/TBR/Review/Real-Time_Resources>`_..
239  Limit execution times in interrupt contexts and critical sections,
240  such as Interrupt and Timer Service Routines (TSRs).
241* Functions used only through function pointers should be declared
242  'static inline' (RTEMS_INLINE_ROUTINE)
243* Prefer to ++preincrement instead of postincrement++.
244* Avoid using floating point except where absolutely necessary.
245
246Miscellaneous
247-------------
248
249* If you need to temporarily change the execution mode of a
250  task/thread, restore it.
251* If adding code to ''cpukit'' be sure the filename is unique since
252  all files under that directory get merged into a single library.
253
254Layering
255--------
256
257* TBD: add something about the dependencies and header file layering.
258* Understand the `RTEMS Software Architecture <https://devel.rtems.org/wiki/TBR/UserManual/RTEMS_Software_Architecture>'_.
259
260Exceptions to the Rules
261-----------------------
262
263* Minimize reformatting existing code in RTEMS unless the file undergoes
264  substantial non-style changes.
265* `Third-party code <https://devel.rtems.org/wiki/Developer/Coding/ThirdPartyCode>`_.
266  should not be reformatted to fit RTEMS style.
267  Exception: unmaintained third-party code adopted and
268  maintained by RTEMS may be reformatted, subject to the
269  above rules.
270
271Tools
272-----
273
274Some of the above can be assisted by tool support. Feel free to add
275more tools, configurations, etc here.
276
277* `Uncrustify <http://uncrustify.sourceforge.net/>`_.
278  Configuration for RTEMS:
279  `rtems.uncrustify <https://devel.rtems.org/attachment/wiki/Developer/Coding/Conventions/rtems.uncrustify>`_.
Note: See TracBrowser for help on using the repository browser.