1Coding Style for the Xen Hypervisor
2===================================
3
4The Xen coding style described below is the coding style used by the
5Xen hypervisor itself (xen/*) as well as various associated low-level
6libraries (e.g. tools/libxc/*).
7
8An exception is made for files which are imported from an external
9source. In these cases the prevailing coding style of the upstream
10source is generally used (commonly the Linux coding style).
11
12Other parts of the code base may use other coding styles, sometimes
13explicitly (e.g. tools/libxl/CODING_STYLE) but often implicitly (Linux
14coding style is fairly common). In general you should copy the style
15of the surrounding code. If you are unsure please ask.
16
17Indentation
18-----------
19
20Indenting uses spaces, not tabs - in contrast to Linux.  An indent
21level consists of four spaces.  Code within blocks is indented by one
22extra indent level.  The enclosing braces of a block are indented the
23same as the code _outside_ the block.  e.g.
24
25void fun(void)
26{
27    /* One level of indent. */
28
29    {
30        /* A second level of indent. */
31    }
32}
33
34White space
35-----------
36
37Space characters are used to spread out logical statements, such as in
38the condition of an if or while.  Spaces are placed between the
39keyword and the brackets surrounding the condition, between the
40brackets and the condition itself, and around binary operators (except
41the structure access operators, '.' and '->'). e.g.
42
43if ( (wibble & wombat) == 42 )
44{
45    ...
46
47There should be no trailing white space at the end of lines (including
48after the opening /* of a comment block).
49
50Line Length
51-----------
52
53Lines should be less than 80 characters in length.  Long lines should
54be split at sensible places and the trailing portions indented.
55
56User visible strings (e.g., printk() messages) should not be split so
57they can searched for more easily.
58
59Bracing
60-------
61
62Braces ('{' and '}') are usually placed on a line of their own, except
63for the do/while loop.  This is unlike the Linux coding style and
64unlike K&R.  do/while loops are an exception. e.g.:
65
66if ( condition )
67{
68    /* Do stuff. */
69}
70else
71{
72    /* Other stuff. */
73}
74
75while ( condition )
76{
77    /* Do stuff. */
78}
79
80do {
81    /* Do stuff. */
82} while ( condition );
83
84etc.
85
86Braces should be omitted for blocks with a single statement. e.g.,
87
88if ( condition )
89    single_statement();
90
91Comments
92--------
93
94Only C style /* ... */ comments are to be used.  C++ style // comments
95should not be used.  Multi-word comments should begin with a capital
96letter.  Comments containing a single sentence may end with a full
97stop; comments containing several sentences must have a full stop
98after each sentence.
99
100Multi-line comment blocks should start and end with comment markers on
101separate lines and each line should begin with a leading '*'.
102
103/*
104 * Example, multi-line comment block.
105 *
106 * Note beginning and end markers on separate lines and leading '*'.
107 */
108
109Emacs local variables
110---------------------
111
112A comment block containing local variables for emacs is permitted at
113the end of files.  It should be:
114
115/*
116 * Local variables:
117 * mode: C
118 * c-file-style: "BSD"
119 * c-basic-offset: 4
120 * indent-tabs-mode: nil
121 * End:
122 */
123