From 5c164735b1b63e18d67a343197c94de86bf4efbf Mon Sep 17 00:00:00 2001 From: Aleksander Kaminski Date: Wed, 14 Jun 2023 10:46:36 +0200 Subject: [PATCH] conding: Update coding guildelines JIRA: RTOS-490 --- coding.md | 60 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/coding.md b/coding.md index 2ec99611..508516a3 100644 --- a/coding.md +++ b/coding.md @@ -79,9 +79,9 @@ Function should be not longer than 200 lines of code and not shorter than 10 lin ## Variables -Variables should be named with one short words without the underline characters. If one word is not enough for variable name then use camelCase. When defining a variable assign it a value, do not assume that isn't value is zero. +Variables should be named with one short words without the underline characters. If one word is not enough for variable name then use camelCase. When defining a variable assign it a value, do not assume that its value is zero. **In the kernel code always initialize global/static variables in runtime.** There's not `.bss` and `.data` initialization in the kernel. -## Local variables +## Local variables - kernel Local variables should be defined before the function code according to ANSI C 89 standard. The stack usage and number of local variables should be minimized. Static local variables are not allowed. @@ -107,12 +107,33 @@ Local variables should be defined before the function code according to ANSI C 8 } ``` -## Global variables +## Local variables - libphoenix, userspace -Global variables should be used only if they're absolutely necessary. You should avoid using globally initialized variables. If they are used, global variables can only be placed in common structures. The structure should be named after the system module that implements it, followed by _common. Example notation is shown below. +Scope of local variables should be minimalized, as the stack usage and number of local variables. Static local variables are allowed. ```c - struct { + void countSheeps(hert_t *herd) + { + static int lastCount = 0; + int count = 0; + + for (int i = 0; i < sizeof(herd.sheeps) / sizeof(herd.sheeps[0]); ++i) { + if (herd.sheeps[i] != NULL) { + ++count; + } + } + + printf("Counted %d sheeps (last time it was %d\n", count, lastCount); + lastCount = count; + } +``` + +## Global variables - kernel + +In the kernel code global variables should be always initialized in runtime. Global variables should be used only if they're absolutely necessary. Global variables should be always initialized in If they are used, global variables can only be placed in common structures. The structure should be named after the system module that implements it, followed by _common. Example notation is shown below. + +```c + static struct { spinlock_t spinlock; } pmap_common; ``` @@ -132,7 +153,7 @@ No space should be used after the following unary operators: The `sizeof` and `typeof`are treated as functions and are to be used in accordance to the following notation: > - sizeof(x) + sizeof(x) typeof(x) In case of increment `++` and decrement `--` operators following rules should be applied. If they are postfix, no space should be used before the operator. If they are prefix, no space should be used after the operator. @@ -164,7 +185,14 @@ A space should be used after a keyword of the conditional instruction. Opening a ## Type definition -New types can only be defined if it is absolutely necessary. +New types can only be defined if it is absolutely necessary. if `typedef` is used for a structure/union, structure/union should be left anonymous if possible: + +```c + typedef struct { + int foo; + int bar; + } foobar_t; +``` ## Comments @@ -186,6 +214,18 @@ All comments should be brief and placed only in essential parts of the code. Com The use of any kind of documentation generator (e.g. doxygen) is strictly forbidden. +## Code disabling + +Leaving disabled, dead code should be avoided, version control should be relied upton to hold obsolete code. However, should it be neccessary, preprocessor should be used: + +```c + releveantCode(); + + #if 0 + obsoleteFunction(); + #endif +``` + ## Preprocessor The header with the `#include" preprocessing directive should be placed after the label. The example header notation is shown below. @@ -227,6 +267,10 @@ Following notation for operating system messages should be applied. Message shou > lib_printf("main: Starting syspage programs (%d) and init\n", syspage->progssz); +## Coding guidelines + +MISRA C:2012 coding guildeline standard should be adhered to in all matters not addressed in this guildeline (advisory rules are optional). + ## Miscellaneous The `goto` statement shall not be used. The main goal of this prohibition is the minimalization of the spaghetti code generation and the prevention of the linear programming usage. @@ -239,4 +283,4 @@ In our opinion usage of `goto` increases the chaos in the source code and absolu ## See also -1. [Table of Contents](README.md) \ No newline at end of file +1. [Table of Contents](README.md)