| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |                            RT-Thread Coding StyleThis is an developing instruction for RT-Thread developers. As an open sourcesoftware, RT-Thread is done by the cooperation of different people. Thisdocument is a guide for RT-Thread developers and please obey it if you are.RT-Thread users could also get to know some conventions in the code through itand thus easier to understand the implementations of RT-Thread.1. Directory NamingIn normal conditions, please name directories in lower-case. Directories shouldhave descriptive names. For example, the port of a chip should be composed ofthe name of the chip and the category of the chip. Directories under components/should stand for what the component does.2. File NamingIn normal conditions, please name files in lower-case. If the file isreferencing other places, it can have the original name. To avoid namingcollision, do not use general names or the names that are frequently used.3. Header FilesTo avoid include the same header file for multiple times, you need to define asymbol like this:    #ifndef __FILE_H__    #define __FILE_H__    /* header file content */    #endifThe symbol should begin and end with "__" to avoid naming collision. The wordsof the file name should be connected by "_".4. Header File CommentsIn every header file, there should be copyright information and Change Logrecord like this:    /*     * File      : rtthread.h     * This file is part of RT-Thread RTOS     * COPYRIGHT (C) 2006, RT-Thread Development Team     *     * The license and distribution terms for this file may be     * found in the file LICENSE in this distribution or at     * http://www.rt-thread.org/license/LICENSE.     *     * Change Logs:     * Date           Author       Notes     * 2006-03-18     Bernard      the first version     * 2006-04-26     Bernard      add semaphore APIs     * ...    */5. Structure DefinesPlease name structures in lower-case and connect words with "_". For example:    struct rt_list_node    {        struct rt_list_node *next;        struct rt_list_node *prev;    };Braces should have their own lines and the members should be defines withindent.The names of type defines such like structure types should be the structure nameplus "_t". For example:    typedef struct rt_list_node rt_list_t;In order to be easily referenced, the types of objects in kernel is pointer. Forexample:    typedef struct rt_timer* rt_timer_t;6. MacrosIn RT-Thread, please use upper-case names for macro definitions. Words areconnected by "_". Like:    #define RT_TRUE                         17. Function Naming and DeclarationPlease name functions in lower-case and separate words with "_". API provided toupper application should be declared in header files. If the function don't haveparameters, it should be declared as void:    rt_thread_t rt_thread_self(void);8. CommentingPlease use English to comment. There shouldn't be too much comments and thecomments should describe what does the code do. And it should describe how thecomplicated algorithm works. Comments to statements should be placed before themor right of them. Anther places are illegal.9. IndentPlease use TAB or 4 spaces to indent. It's preferred to use 4 spaces. If noother special meanings, the indent should begin right after "{":    if (condition)    {        /* others */    }The only one exception is switch. In switch-case statements, "case" should bealigned with "switch":    switch (value)    {    case value1:        break;    }"case" is aligned with "switch", the following code block should be indented.10. Braces and SpacesFor ease of reading, it is advised that braces should occupy the whole lineinstead of following other statements. Like:    if (condition)    {        /* others */    }When matching braces have their own lines, the reader would identify the codeblocks easily.There should be a space before parentheses when it's not a function call. Forexample:    if (x <= y)    {        /* others */    }    for (index = 0; index < MAX_NUMBER; index ++)    {        /* others */    }In expressions, there should be a space between most binary and ternaryoperators and the strings. No spaces around(inside) parentheses, like:    if ( x <= y )    {        /* other */    }This is a bad practice.11. trace, log InformationsIn RT-Thread, rt_kprintf is a commonly used logging routine. In RT-Threadrt_kprintf is implemented as a polling, non-interrupting string output. It issuitable in "instant" situations such as interrupt context. The polling methodwould have influence to the timing sequence of the log output.It is not recommended to use rt_kprintf frequently. Unless you are aware of thatit's not a big deal to run slower.Logging should be off by default and can be turned on by a switch(e.g. avariable or a macro). When logging, it should be easy to understand and easy todetermine where the problem is.12. FunctionsFunctions in kernel should be K.I.S.S. If the function is too long, you shouldsplit it into smaller ones and make each of them simplified and easy tounderstand.13. ObjectsThe kernel of RT-Thread uses object-oriented tech in C. The naming conventionis: structure names are the object names, object names + verb phrases are themethod names of objects:    struct rt_timer    {        struct rt_object parent;        /* other fields */    };    typedef struct rt_timer* rt_timer_t;The definition of structure rt_timer stands for the object definition of timerobject.    rt_timer_t rt_timer_create(const char* name,        void (*timeout)(void* parameter), void* parameter,        rt_tick_t time, rt_uint8_t flag);    rt_err_t rt_timer_delete(rt_timer_t timer);    rt_err_t rt_timer_start(rt_timer_t timer);    rt_err_t rt_timer_stop(rt_timer_t timer);rt_timer + verb phrase stands for the method that could be used on timer object.When creating a new object, think twice on memory allocations: whether a staticobject could be created or it could only created dynamically on heap.14. Use astyle to format the code automaticallyparameters: --style=allman            --indent=spaces=4            --indent-preproc-block            --pad-oper            --pad-header            --unpad-paren            --suffix=none            --align-pointer=name            --lineend=linux            --convert-tabs            --verbose
 |