1 | /* $Id: VBox-CodingGuidelines.cpp 62912 2016-08-03 12:38:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox - Coding Guidelines.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /** @page pg_vbox_guideline VBox Coding Guidelines
|
---|
19 | *
|
---|
20 | * The VBox Coding guidelines are followed by all of VBox with the exception of
|
---|
21 | * qemu. Qemu is using whatever the frenchman does.
|
---|
22 | *
|
---|
23 | * There are a few compulsory rules and a bunch of optional ones. The following
|
---|
24 | * sections will describe these in details. In addition there is a section of
|
---|
25 | * Subversion 'rules'.
|
---|
26 | *
|
---|
27 | *
|
---|
28 | *
|
---|
29 | * @section sec_vbox_guideline_compulsory Compulsory
|
---|
30 | *
|
---|
31 | *
|
---|
32 | * - The indentation size is 4 chars.
|
---|
33 | *
|
---|
34 | * - Tabs are only ever used in makefiles.
|
---|
35 | *
|
---|
36 | * - Use RT and VBOX types.
|
---|
37 | *
|
---|
38 | * - Use Runtime functions.
|
---|
39 | *
|
---|
40 | * - Use the standard bool, uintptr_t, intptr_t and [u]int[1-9+]_t types.
|
---|
41 | *
|
---|
42 | * - Avoid using plain unsigned and int.
|
---|
43 | *
|
---|
44 | * - Use static wherever possible. This makes the namespace less polluted
|
---|
45 | * and avoids nasty name clash problems which can occur, especially on
|
---|
46 | * Unix-like systems. (1) It also simplifies locating callers when
|
---|
47 | * changing it (single source file vs entire VBox tree).
|
---|
48 | *
|
---|
49 | * - Public names are of the form Domain[Subdomain[]]Method, using mixed
|
---|
50 | * casing to mark the words. The main domain is all uppercase.
|
---|
51 | * (Think like java, mapping domain and subdomain to packages/classes.)
|
---|
52 | *
|
---|
53 | * - Public names are always declared using the appropriate DECL macro. (2)
|
---|
54 | *
|
---|
55 | * - Internal names starts with a lowercased main domain.
|
---|
56 | *
|
---|
57 | * - Defines are all uppercase and separate words with underscore.
|
---|
58 | * This applies to enum values too.
|
---|
59 | *
|
---|
60 | * - Typedefs are all uppercase and contain no underscores to distinguish
|
---|
61 | * them from defines.
|
---|
62 | *
|
---|
63 | * - Pointer typedefs start with 'P'. If pointer to const then 'PC'.
|
---|
64 | *
|
---|
65 | * - Function typedefs start with 'FN'. If pointer to FN then 'PFN'.
|
---|
66 | *
|
---|
67 | * - All files are case sensitive.
|
---|
68 | *
|
---|
69 | * - Slashes are unix slashes ('/') runtime converts when necessary.
|
---|
70 | *
|
---|
71 | * - char strings are UTF-8.
|
---|
72 | *
|
---|
73 | * - Strings from any external source must be treated with utmost care as
|
---|
74 | * they do not have to be valid UTF-8. Only trust internal strings.
|
---|
75 | *
|
---|
76 | * - All functions return VBox status codes. There are three general
|
---|
77 | * exceptions from this:
|
---|
78 | * -# Predicate functions. These are function which are boolean in
|
---|
79 | * nature and usage. They return bool. The function name will
|
---|
80 | * include 'Has', 'Is' or similar.
|
---|
81 | * -# Functions which by nature cannot possibly fail.
|
---|
82 | * These return void.
|
---|
83 | * -# "Get"-functions which return what they ask for.
|
---|
84 | * A get function becomes a "Query" function if there is any
|
---|
85 | * doubt about getting what is ask for.
|
---|
86 | *
|
---|
87 | * - VBox status codes have three subdivisions:
|
---|
88 | * -# Errors, which are VERR_ prefixed and negative.
|
---|
89 | * -# Warnings, which are VWRN_ prefixed and positive.
|
---|
90 | * -# Informational, which are VINF_ prefixed and positive.
|
---|
91 | *
|
---|
92 | * - Platform/OS operation are generalized and put in the IPRT.
|
---|
93 | *
|
---|
94 | * - Other useful constructs are also put in the IPRT.
|
---|
95 | *
|
---|
96 | * - The code shall not cause compiler warnings. Check this on ALL
|
---|
97 | * the platforms.
|
---|
98 | *
|
---|
99 | * - The use of symbols leading with single or double underscores is
|
---|
100 | * forbidden as that intrudes on reserved compiler/system namespace. (3)
|
---|
101 | *
|
---|
102 | * - All files have file headers with $Id and a file tag which describes
|
---|
103 | * the file in a sentence or two.
|
---|
104 | * Note: Use the svn-ps.cmd/svn-ps.sh utility with the -a option to add
|
---|
105 | * new sources with keyword expansion and exporting correctly
|
---|
106 | * configured.
|
---|
107 | *
|
---|
108 | * - All public functions are fully documented in Doxygen style using the
|
---|
109 | * javadoc dialect (using the 'at' instead of the 'slash' as
|
---|
110 | * commandprefix.)
|
---|
111 | *
|
---|
112 | * - All structures in header files are described, including all their
|
---|
113 | * members. (Doxygen style, of course.)
|
---|
114 | *
|
---|
115 | * - All modules have a documentation '\@page' in the main source file
|
---|
116 | * which describes the intent and actual implementation.
|
---|
117 | *
|
---|
118 | * - Code which is doing things that are not immediately comprehensible
|
---|
119 | * shall include explanatory comments.
|
---|
120 | *
|
---|
121 | * - Documentation and comments are kept up to date.
|
---|
122 | *
|
---|
123 | * - Headers in /include/VBox shall not contain any slash-slash C++
|
---|
124 | * comments, only ANSI C comments!
|
---|
125 | *
|
---|
126 | * - Comments on \#else indicates what begins while the comment on a
|
---|
127 | * \#endif indicates what ended. Only add these when there are more than
|
---|
128 | * a few lines (6-10) of \#ifdef'ed code, otherwise they're just clutter.
|
---|
129 | *
|
---|
130 | *
|
---|
131 | * (1) It is common practice on Unix to have a single symbol namespace for an
|
---|
132 | * entire process. If one is careless symbols might be resolved in a
|
---|
133 | * different way that one expects, leading to weird problems.
|
---|
134 | *
|
---|
135 | * (2) This is common practice among most projects dealing with modules in
|
---|
136 | * shared libraries. The Windows / PE __declspect(import) and
|
---|
137 | * __declspect(export) constructs are the main reason for this.
|
---|
138 | * OTOH, we do perhaps have a bit too detailed graining of this in VMM...
|
---|
139 | *
|
---|
140 | * (3) There are guys out there grepping public sources for symbols leading with
|
---|
141 | * single and double underscores as well as gotos and other things
|
---|
142 | * considered bad practice. They'll post statistics on how bad our sources
|
---|
143 | * are on some mailing list, forum or similar.
|
---|
144 | *
|
---|
145 | *
|
---|
146 | * @subsection sec_vbox_guideline_compulsory_sub64 64-bit and 32-bit
|
---|
147 | *
|
---|
148 | * Here are some amendments which address 64-bit vs. 32-bit portability issues.
|
---|
149 | *
|
---|
150 | * Some facts first:
|
---|
151 | *
|
---|
152 | * - On 64-bit Windows the type long remains 32-bit. On nearly all other
|
---|
153 | * 64-bit platforms long is 64-bit.
|
---|
154 | *
|
---|
155 | * - On all 64-bit platforms we care about, int is 32-bit, short is 16 bit
|
---|
156 | * and char is 8-bit.
|
---|
157 | * (I don't know about any platforms yet where this isn't true.)
|
---|
158 | *
|
---|
159 | * - size_t, ssize_t, uintptr_t, ptrdiff_t and similar are all 64-bit on
|
---|
160 | * 64-bit platforms. (These are 32-bit on 32-bit platforms.)
|
---|
161 | *
|
---|
162 | * - There is no inline assembly support in the 64-bit Microsoft compilers.
|
---|
163 | *
|
---|
164 | *
|
---|
165 | * Now for the guidelines:
|
---|
166 | *
|
---|
167 | * - Never, ever, use int, long, ULONG, LONG, DWORD or similar to cast a
|
---|
168 | * pointer to integer. Use uintptr_t or intptr_t. If you have to use
|
---|
169 | * NT/Windows types, there is the choice of ULONG_PTR and DWORD_PTR.
|
---|
170 | *
|
---|
171 | * - RT_OS_WINDOWS is defined to indicate Windows. Do not use __WIN32__,
|
---|
172 | * __WIN64__ and __WIN__ because they are all deprecated and scheduled
|
---|
173 | * for removal (if not removed already). Do not use the compiler
|
---|
174 | * defined _WIN32, _WIN64, or similar either. The bitness can be
|
---|
175 | * determined by testing ARCH_BITS.
|
---|
176 | * Example:
|
---|
177 | * @code
|
---|
178 | * #ifdef RT_OS_WINDOWS
|
---|
179 | * // call win32/64 api.
|
---|
180 | * #endif
|
---|
181 | * #ifdef RT_OS_WINDOWS
|
---|
182 | * # if ARCH_BITS == 64
|
---|
183 | * // call win64 api.
|
---|
184 | * # else // ARCH_BITS == 32
|
---|
185 | * // call win32 api.
|
---|
186 | * # endif // ARCH_BITS == 32
|
---|
187 | * #else // !RT_OS_WINDOWS
|
---|
188 | * // call posix api
|
---|
189 | * #endif // !RT_OS_WINDOWS
|
---|
190 | * @endcode
|
---|
191 | *
|
---|
192 | * - There are RT_OS_xxx defines for each OS, just like RT_OS_WINDOWS
|
---|
193 | * mentioned above. Use these defines instead of any predefined
|
---|
194 | * compiler stuff or defines from system headers.
|
---|
195 | *
|
---|
196 | * - RT_ARCH_X86 is defined when compiling for the x86 the architecture.
|
---|
197 | * Do not use __x86__, __X86__, __[Ii]386__, __[Ii]586__, or similar
|
---|
198 | * for this purpose.
|
---|
199 | *
|
---|
200 | * - RT_ARCH_AMD64 is defined when compiling for the AMD64 architecture.
|
---|
201 | * Do not use __AMD64__, __amd64__ or __x64_86__.
|
---|
202 | *
|
---|
203 | * - Take care and use size_t when you have to, esp. when passing a pointer
|
---|
204 | * to a size_t as a parameter.
|
---|
205 | *
|
---|
206 | * - Be wary of type promotion to (signed) integer. For example the
|
---|
207 | * following will cause u8 to be promoted to int in the shift, and then
|
---|
208 | * sign extended in the assignment 64-bit:
|
---|
209 | * @code
|
---|
210 | * uint8_t u8 = 0xfe;
|
---|
211 | * uint64_t u64 = u8 << 24;
|
---|
212 | * // u64 == 0xfffffffffe000000
|
---|
213 | * @endcode
|
---|
214 | *
|
---|
215 | *
|
---|
216 | * @subsection sec_vbox_guideline_compulsory_cppmain C++ guidelines for Main
|
---|
217 | *
|
---|
218 | * Main is currently (2009) full of hard-to-maintain code that uses complicated
|
---|
219 | * templates. The new mid-term goal for Main is to have less custom templates
|
---|
220 | * instead of more for the following reasons:
|
---|
221 | *
|
---|
222 | * - Template code is harder to read and understand. Custom templates create
|
---|
223 | * territories which only the code writer understands.
|
---|
224 | *
|
---|
225 | * - Errors in using templates create terrible C++ compiler messages.
|
---|
226 | *
|
---|
227 | * - Template code is really hard to look at in a debugger.
|
---|
228 | *
|
---|
229 | * - Templates slow down the compiler a lot.
|
---|
230 | *
|
---|
231 | * In particular, the following bits should be considered deprecated and should
|
---|
232 | * NOT be used in new code:
|
---|
233 | *
|
---|
234 | * - everything in include/iprt/cpputils.h (auto_ref_ptr, exception_trap_base,
|
---|
235 | * char_auto_ptr and friends)
|
---|
236 | *
|
---|
237 | * Generally, in many cases, a simple class with a proper destructor can achieve
|
---|
238 | * the same effect as a 1,000-line template include file, and the code is
|
---|
239 | * much more accessible that way.
|
---|
240 | *
|
---|
241 | * Using standard STL templates like std::list, std::vector and std::map is OK.
|
---|
242 | * Exceptions are:
|
---|
243 | *
|
---|
244 | * - Guest Additions because we don't want to link against libstdc++ there.
|
---|
245 | *
|
---|
246 | * - std::string should not be used because we have iprt::MiniString and
|
---|
247 | * com::Utf8Str which can convert efficiently with COM's UTF-16 strings.
|
---|
248 | *
|
---|
249 | * - std::auto_ptr<> in general; that part of the C++ standard is just broken.
|
---|
250 | * Write a destructor that calls delete.
|
---|
251 | *
|
---|
252 | *
|
---|
253 | * @subsection sec_vbox_guideline_compulsory_cppqtgui C++ guidelines for the Qt GUI
|
---|
254 | *
|
---|
255 | * The Qt GUI is currently (2010) on its way to become more compatible to the
|
---|
256 | * rest of VirtualBox coding style wise. From now on, all the coding style
|
---|
257 | * rules described in this file are also mandatory for the Qt GUI. Additionally
|
---|
258 | * the following rules should be respected:
|
---|
259 | *
|
---|
260 | * - GUI classes which correspond to GUI tasks should be prefixed by UI (no VBox anymore)
|
---|
261 | *
|
---|
262 | * - Classes which extents some of the Qt classes should be prefix by QI
|
---|
263 | *
|
---|
264 | * - General task classes should be prefixed by C
|
---|
265 | *
|
---|
266 | * - Slots are prefixed by slt -> sltName
|
---|
267 | *
|
---|
268 | * - Signals are prefixed by sig -> sigName
|
---|
269 | *
|
---|
270 | * - Use Qt classes for lists, strings and so on, the use of STL classes should
|
---|
271 | * be avoided
|
---|
272 | *
|
---|
273 | * - All files like .cpp, .h, .ui, which belong together are located in the
|
---|
274 | * same directory and named the same
|
---|
275 | *
|
---|
276 | *
|
---|
277 | * @subsection sec_vbox_guideline_compulsory_xslt XSLT
|
---|
278 | *
|
---|
279 | * XSLT (eXtensible Stylesheet Language Transformations) is used quite a bit in
|
---|
280 | * the Main API area of VirtualBox to generate sources and bindings to that API.
|
---|
281 | * There are a couple of common pitfalls worth mentioning:
|
---|
282 | *
|
---|
283 | * - Never do repeated //interface[@name=...] and //enum[@name=...] lookups
|
---|
284 | * because they are expensive. Instead delcare xsl:key elements for these
|
---|
285 | * searches and do the lookup using the key() function. xsltproc uses
|
---|
286 | * (per current document) hash tables for each xsl:key, i.e. very fast.
|
---|
287 | *
|
---|
288 | * - When output type is 'text' make sure to call xsltprocNewlineOutputHack
|
---|
289 | * from typemap-shared.inc.xsl every few KB of output, or xsltproc will
|
---|
290 | * end up wasting all the time reallocating the output buffer.
|
---|
291 | *
|
---|
292 | *
|
---|
293 | * @subsection sec_vbox_guideline_compulsory_doxygen Doxygen Comments
|
---|
294 | *
|
---|
295 | * As mentioned above, we shall use doxygen/javadoc style commenting of public
|
---|
296 | * functions, typedefs, classes and such. It is preferred to use this style in
|
---|
297 | * as many places as possible.
|
---|
298 | *
|
---|
299 | * A couple of hints on how to best write doxygen comments:
|
---|
300 | *
|
---|
301 | * - A good class, method, function, structure or enum doxygen comment
|
---|
302 | * starts with a one line sentence giving a brief description of the
|
---|
303 | * item. Details comes in a new paragraph (after blank line).
|
---|
304 | *
|
---|
305 | * - Except for list generators like \@todo, \@cfgm, \@gcfgm and others,
|
---|
306 | * all doxygen comments are related to things in the code. So, for
|
---|
307 | * instance you DO NOT add a doxygen \@note comment in the middle of a
|
---|
308 | * because you've got something important to note, you add a normal
|
---|
309 | * comment like 'Note! blah, very importan blah!'
|
---|
310 | *
|
---|
311 | * - We do NOT use TODO/XXX/BUGBUG or similar markers in the code to flag
|
---|
312 | * things needing fixing later, we always use \@todo doxygen comments.
|
---|
313 | *
|
---|
314 | * - There is no colon after the \@todo. And it is ALWAYS in a doxygen
|
---|
315 | * comment.
|
---|
316 | *
|
---|
317 | * - The \@retval tag is used to explain status codes a method/function may
|
---|
318 | * returns. It is not used to describe output parameters, that is done
|
---|
319 | * using the \@param or \@param[out] tag.
|
---|
320 | *
|
---|
321 | * See https://www.stack.nl/~dimitri/doxygen/manual/index.html for the official
|
---|
322 | * doxygen documention.
|
---|
323 | *
|
---|
324 | *
|
---|
325 | * @section sec_vbox_guideline_optional Optional
|
---|
326 | *
|
---|
327 | * First part is the actual coding style and all the prefixes. The second part
|
---|
328 | * is a bunch of good advice.
|
---|
329 | *
|
---|
330 | *
|
---|
331 | * @subsection sec_vbox_guideline_optional_layout The code layout
|
---|
332 | *
|
---|
333 | * - Max line length is 130 chars. Exceptions are table-like
|
---|
334 | * code/initializers and Log*() statements (don't waste unnecessary
|
---|
335 | * vertical space on debug logging).
|
---|
336 | *
|
---|
337 | * - Comments should try stay within the usual 80 columns as these are
|
---|
338 | * denser and too long lines may be harder to read.
|
---|
339 | *
|
---|
340 | * - Curly brackets are not indented. Example:
|
---|
341 | * @code
|
---|
342 | * if (true)
|
---|
343 | * {
|
---|
344 | * Something1();
|
---|
345 | * Something2();
|
---|
346 | * }
|
---|
347 | * else
|
---|
348 | * {
|
---|
349 | * SomethingElse1().
|
---|
350 | * SomethingElse2().
|
---|
351 | * }
|
---|
352 | * @endcode
|
---|
353 | *
|
---|
354 | * - Space before the parentheses when it comes after a C keyword.
|
---|
355 | *
|
---|
356 | * - No space between argument and parentheses. Exception for complex
|
---|
357 | * expression. Example:
|
---|
358 | * @code
|
---|
359 | * if (PATMR3IsPatchGCAddr(pVM, GCPtr))
|
---|
360 | * @endcode
|
---|
361 | *
|
---|
362 | * - The else of an if is always the first statement on a line. (No curly
|
---|
363 | * stuff before it!)
|
---|
364 | *
|
---|
365 | * - else and if go on the same line if no { compound statement }
|
---|
366 | * follows the if. Example:
|
---|
367 | * @code
|
---|
368 | * if (fFlags & MYFLAGS_1)
|
---|
369 | * fFlags &= ~MYFLAGS_10;
|
---|
370 | * else if (fFlags & MYFLAGS_2)
|
---|
371 | * {
|
---|
372 | * fFlags &= ~MYFLAGS_MASK;
|
---|
373 | * fFlags |= MYFLAGS_5;
|
---|
374 | * }
|
---|
375 | * else if (fFlags & MYFLAGS_3)
|
---|
376 | * @endcode
|
---|
377 | *
|
---|
378 | *
|
---|
379 | * - Slightly complex boolean expressions are split into multiple lines,
|
---|
380 | * putting the operators first on the line and indenting it all according
|
---|
381 | * to the nesting of the expression. The purpose is to make it as easy as
|
---|
382 | * possible to read. Example:
|
---|
383 | * @code
|
---|
384 | * if ( RT_SUCCESS(rc)
|
---|
385 | * || (fFlags & SOME_FLAG))
|
---|
386 | * @endcode
|
---|
387 | *
|
---|
388 | * - When 'if' or 'while' statements gets long, the closing parentheses
|
---|
389 | * goes right below the opening parentheses. This may be applied to
|
---|
390 | * sub-expression. Example:
|
---|
391 | * @code
|
---|
392 | * if ( RT_SUCCESS(rc)
|
---|
393 | * || ( fSomeStuff
|
---|
394 | * && fSomeOtherStuff
|
---|
395 | * && fEvenMoreStuff
|
---|
396 | * )
|
---|
397 | * || SomePredicateFunction()
|
---|
398 | * )
|
---|
399 | * {
|
---|
400 | * ...
|
---|
401 | * }
|
---|
402 | * @endcode
|
---|
403 | *
|
---|
404 | * - The case is indented from the switch (to avoid having the braces for
|
---|
405 | * the 'case' at the same level as the 'switch' statement).
|
---|
406 | *
|
---|
407 | * - If a case needs curly brackets they contain the entire case, are not
|
---|
408 | * indented from the case, and the break or return is placed inside them.
|
---|
409 | * Example:
|
---|
410 | * @code
|
---|
411 | * switch (pCur->eType)
|
---|
412 | * {
|
---|
413 | * case PGMMAPPINGTYPE_PAGETABLES:
|
---|
414 | * {
|
---|
415 | * unsigned iPDE = pCur->GCPtr >> PGDIR_SHIFT;
|
---|
416 | * unsigned iPT = (pCur->GCPtrEnd - pCur->GCPtr) >> PGDIR_SHIFT;
|
---|
417 | * while (iPT-- > 0)
|
---|
418 | * if (pPD->a[iPDE + iPT].n.u1Present)
|
---|
419 | * return VERR_HYPERVISOR_CONFLICT;
|
---|
420 | * break;
|
---|
421 | * }
|
---|
422 | * }
|
---|
423 | * @endcode
|
---|
424 | *
|
---|
425 | * - In a do while construction, the while is on the same line as the
|
---|
426 | * closing "}" if any are used.
|
---|
427 | * Example:
|
---|
428 | * @code
|
---|
429 | * do
|
---|
430 | * {
|
---|
431 | * stuff;
|
---|
432 | * i--;
|
---|
433 | * } while (i > 0);
|
---|
434 | * @endcode
|
---|
435 | *
|
---|
436 | * - Comments are in C style. C++ style comments are used for temporary
|
---|
437 | * disabling a few lines of code.
|
---|
438 | *
|
---|
439 | * - No unnecessary parentheses in expressions (just don't over do this
|
---|
440 | * so that gcc / msc starts bitching). Find a correct C/C++ operator
|
---|
441 | * precedence table if needed.
|
---|
442 | *
|
---|
443 | * - 'for (;;)' is preferred over 'while (true)' and 'while (1)'.
|
---|
444 | *
|
---|
445 | * - Parameters are indented to the start parentheses when breaking up
|
---|
446 | * function calls, declarations or prototypes. (This is in line with
|
---|
447 | * how 'if', 'for' and 'while' statements are done as well.) Example:
|
---|
448 | * @code
|
---|
449 | * RTPROCESS hProcess;
|
---|
450 | * int rc = RTProcCreateEx(papszArgs[0],
|
---|
451 | * papszArgs,
|
---|
452 | * RTENV_DEFAULT,
|
---|
453 | * fFlags,
|
---|
454 | * NULL, // phStdIn
|
---|
455 | * NULL, // phStdOut
|
---|
456 | * NULL, // phStdErr
|
---|
457 | * NULL, // pszAsUser
|
---|
458 | * NULL, // pszPassword
|
---|
459 | * &hProcess);
|
---|
460 | * @endcode
|
---|
461 | *
|
---|
462 | * - That Dijkstra is dead is no excuse for using gotos.
|
---|
463 | *
|
---|
464 | * - Using do-while-false loops to avoid gotos is considered very bad form.
|
---|
465 | * They create hard to read code. They tend to be either too short (i.e.
|
---|
466 | * pointless) or way to long (split up the function already), making
|
---|
467 | * tracking the state is difficult and prone to bugs. Also, they cause
|
---|
468 | * the compiler to generate suboptimal code, because the break branches
|
---|
469 | * are by preferred over the main code flow (MSC has no branch hinting!).
|
---|
470 | * Instead, do make use the 130 columns (i.e. nested ifs) and split
|
---|
471 | * the code up into more functions!
|
---|
472 | *
|
---|
473 | *
|
---|
474 | * @subsection sec_vbox_guideline_optional_prefix Variable / Member Prefixes
|
---|
475 | *
|
---|
476 | * Prefixes are meant to provide extra context clues to a variable/member, we
|
---|
477 | * therefore avoid using prefixes that just indicating the type if a better
|
---|
478 | * choice is available.
|
---|
479 | *
|
---|
480 | *
|
---|
481 | * The prefixes:
|
---|
482 | *
|
---|
483 | * - The 'g_' (or 'g') prefix means a global variable, either on file or module level.
|
---|
484 | *
|
---|
485 | * - The 's_' (or 's') prefix means a static variable inside a function or
|
---|
486 | * class. This is not used for static variables on file level, use 'g_'
|
---|
487 | * for those (logical, right).
|
---|
488 | *
|
---|
489 | * - The 'm_' (or 'm') prefix means a class data member.
|
---|
490 | *
|
---|
491 | * In new code in Main, use "m_" (and common sense). As an exception,
|
---|
492 | * in Main, if a class encapsulates its member variables in an anonymous
|
---|
493 | * structure which is declared in the class, but defined only in the
|
---|
494 | * implementation (like this: 'class X { struct Data; Data *m; }'), then
|
---|
495 | * the pointer to that struct is called 'm' itself and its members then
|
---|
496 | * need no prefix, because the members are accessed with 'm->member'
|
---|
497 | * already which is clear enough.
|
---|
498 | *
|
---|
499 | * - The 'a_' prefix means a parameter (argument) variable. This is
|
---|
500 | * sometimes written 'a' in parts of the source code that does not use
|
---|
501 | * the array prefix.
|
---|
502 | *
|
---|
503 | * - The 'p' prefix means pointer. For instance 'pVM' is pointer to VM.
|
---|
504 | *
|
---|
505 | * - The 'r' prefix means that something is passed by reference.
|
---|
506 | *
|
---|
507 | * - The 'k' prefix means that something is a constant. For instance
|
---|
508 | * 'enum { kStuff };'. This is usually not used in combination with
|
---|
509 | * 'p', 'r' or any such thing, it's main main use is to make enums
|
---|
510 | * easily identifiable.
|
---|
511 | *
|
---|
512 | * - The 'a' prefix means array. For instance 'aPages' could be read as
|
---|
513 | * array of pages.
|
---|
514 | *
|
---|
515 | * - The 'c' prefix means count. For instance 'cbBlock' could be read,
|
---|
516 | * count of bytes in block. (1)
|
---|
517 | *
|
---|
518 | * - The 'cx' prefix means width (count of 'x' units).
|
---|
519 | *
|
---|
520 | * - The 'cy' prefix means height (count of 'y' units).
|
---|
521 | *
|
---|
522 | * - The 'x', 'y' and 'z' prefix refers to the x-, y- , and z-axis
|
---|
523 | * respectively.
|
---|
524 | *
|
---|
525 | * - The 'off' prefix means offset.
|
---|
526 | *
|
---|
527 | * - The 'i' or 'idx' prefixes usually means index. Although the 'i' one
|
---|
528 | * can sometimes just mean signed integer.
|
---|
529 | *
|
---|
530 | * - The 'i[1-9]+' prefix means a fixed bit size variable. Frequently
|
---|
531 | * used with the int[1-9]+_t types where the width is really important.
|
---|
532 | * In most cases 'i' is more appropriate. [type]
|
---|
533 | *
|
---|
534 | * - The 'e' (or 'enm') prefix means enum.
|
---|
535 | *
|
---|
536 | * - The 'u' prefix usually means unsigned integer. Exceptions follows.
|
---|
537 | *
|
---|
538 | * - The 'u[1-9]+' prefix means a fixed bit size variable. Frequently
|
---|
539 | * used with the uint[1-9]+_t types and with bitfields where the width is
|
---|
540 | * really important. In most cases 'u' or 'b' (byte) would be more
|
---|
541 | * appropriate. [type]
|
---|
542 | *
|
---|
543 | * - The 'b' prefix means byte or bytes. [type]
|
---|
544 | *
|
---|
545 | * - The 'f' prefix means flags. Flags are unsigned integers of some kind
|
---|
546 | * or booleans.
|
---|
547 | *
|
---|
548 | * - TODO: need prefix for real float. [type]
|
---|
549 | *
|
---|
550 | * - The 'rd' prefix means real double and is used for 'double' variables.
|
---|
551 | * [type]
|
---|
552 | *
|
---|
553 | * - The 'lrd' prefix means long real double and is used for 'long double'
|
---|
554 | * variables. [type]
|
---|
555 | *
|
---|
556 | * - The 'ch' prefix means a char, the (signed) char type. [type]
|
---|
557 | *
|
---|
558 | * - The 'wc' prefix means a wide/windows char, the RTUTF16 type. [type]
|
---|
559 | *
|
---|
560 | * - The 'uc' prefix means a Unicode Code point, the RTUNICP type. [type]
|
---|
561 | *
|
---|
562 | * - The 'uch' prefix means unsigned char. It's rarely used. [type]
|
---|
563 | *
|
---|
564 | * - The 'sz' prefix means zero terminated character string (array of
|
---|
565 | * chars). (UTF-8)
|
---|
566 | *
|
---|
567 | * - The 'wsz' prefix means zero terminated wide/windows character string
|
---|
568 | * (array of RTUTF16).
|
---|
569 | *
|
---|
570 | * - The 'usz' prefix means zero terminated Unicode string (array of
|
---|
571 | * RTUNICP).
|
---|
572 | *
|
---|
573 | * - The 'str' prefix means C++ string; either a std::string or, in Main,
|
---|
574 | * a Utf8Str or, in Qt, a QString. When used with 'p', 'r', 'a' or 'c'
|
---|
575 | * the first letter should be capitalized.
|
---|
576 | *
|
---|
577 | * - The 'bstr' prefix, in Main, means a UTF-16 Bstr. When used with 'p',
|
---|
578 | * 'r', 'a' or 'c' the first letter should be capitalized.
|
---|
579 | *
|
---|
580 | * - The 'pfn' prefix means pointer to function. Common usage is 'pfnCallback'
|
---|
581 | * and such like.
|
---|
582 | *
|
---|
583 | * - The 'psz' prefix is a combination of 'p' and 'sz' and thus means
|
---|
584 | * pointer to a zero terminated character string. (UTF-8)
|
---|
585 | *
|
---|
586 | * - The 'pcsz' prefix is used to indicate constant string pointers in
|
---|
587 | * parts of the code. Most code uses 'psz' for const and non-const
|
---|
588 | * string pointers, so please ignore this one.
|
---|
589 | *
|
---|
590 | * - The 'l' prefix means (signed) long. We try avoid using this,
|
---|
591 | * expecially with the 'LONG' types in Main as these are not 'long' on
|
---|
592 | * 64-bit non-Windows platforms and can cause confusion. Alternatives:
|
---|
593 | * 'i' or 'i32'. [type]
|
---|
594 | *
|
---|
595 | * - The 'ul' prefix means unsigned long. We try avoid using this,
|
---|
596 | * expecially with the 'ULONG' types in Main as these are not 'unsigned
|
---|
597 | * long' on 64-bit non-Windows platforms and can cause confusion.
|
---|
598 | * Alternatives: 'u' or 'u32'. [type]
|
---|
599 | *
|
---|
600 | *
|
---|
601 | * (1) Except in the occasional 'pcsz' prefix, the 'c' prefix is never ever
|
---|
602 | * used in the meaning 'const'.
|
---|
603 | *
|
---|
604 | *
|
---|
605 | * @subsection sec_vbox_guideline_optional_misc Misc / Advice / Stuff
|
---|
606 | *
|
---|
607 | * - When writing code think as the reader.
|
---|
608 | *
|
---|
609 | * - When writing code think as the compiler. (2)
|
---|
610 | *
|
---|
611 | * - When reading code think as if it's full of bugs - find them and fix them.
|
---|
612 | *
|
---|
613 | * - Pointer within range tests like:
|
---|
614 | * @code
|
---|
615 | * if ((uintptr_t)pv >= (uintptr_t)pvBase && (uintptr_t)pv < (uintptr_t)pvBase + cbRange)
|
---|
616 | * @endcode
|
---|
617 | * Can also be written as (assuming cbRange unsigned):
|
---|
618 | * @code
|
---|
619 | * if ((uintptr_t)pv - (uintptr_t)pvBase < cbRange)
|
---|
620 | * @endcode
|
---|
621 | * Which is shorter and potentially faster. (1)
|
---|
622 | *
|
---|
623 | * - Avoid unnecessary casting. All pointers automatically cast down to
|
---|
624 | * void *, at least for non class instance pointers.
|
---|
625 | *
|
---|
626 | * - It's very very bad practise to write a function larger than a
|
---|
627 | * screen full (1024x768) without any comprehensibility and explaining
|
---|
628 | * comments.
|
---|
629 | *
|
---|
630 | * - More to come....
|
---|
631 | *
|
---|
632 | *
|
---|
633 | * (1) Important, be very careful with the casting. In particular, note that
|
---|
634 | * a compiler might treat pointers as signed (IIRC).
|
---|
635 | *
|
---|
636 | * (2) "A really advanced hacker comes to understand the true inner workings of
|
---|
637 | * the machine - he sees through the language he's working in and glimpses
|
---|
638 | * the secret functioning of the binary code - becomes a Ba'al Shem of
|
---|
639 | * sorts." (Neal Stephenson "Snow Crash")
|
---|
640 | *
|
---|
641 | *
|
---|
642 | *
|
---|
643 | * @section sec_vbox_guideline_warnings Compiler Warnings
|
---|
644 | *
|
---|
645 | * The code should when possible compile on all platforms and compilers without any
|
---|
646 | * warnings. That's a nice idea, however, if it means making the code harder to read,
|
---|
647 | * less portable, unreliable or similar, the warning should not be fixed.
|
---|
648 | *
|
---|
649 | * Some of the warnings can seem kind of innocent at first glance. So, let's take the
|
---|
650 | * most common ones and explain them.
|
---|
651 | *
|
---|
652 | *
|
---|
653 | * @subsection sec_vbox_guideline_warnings_signed_unsigned_compare Signed / Unsigned Compare
|
---|
654 | *
|
---|
655 | * GCC says: "warning: comparison between signed and unsigned integer expressions"
|
---|
656 | * MSC says: "warning C4018: '<|<=|==|>=|>' : signed/unsigned mismatch"
|
---|
657 | *
|
---|
658 | * The following example will not output what you expect:
|
---|
659 | @code
|
---|
660 | #include <stdio.h>
|
---|
661 | int main()
|
---|
662 | {
|
---|
663 | signed long a = -1;
|
---|
664 | unsigned long b = 2294967295;
|
---|
665 | if (a < b)
|
---|
666 | printf("%ld < %lu: true\n", a, b);
|
---|
667 | else
|
---|
668 | printf("%ld < %lu: false\n", a, b);
|
---|
669 | return 0;
|
---|
670 | }
|
---|
671 | @endcode
|
---|
672 | * If I understood it correctly, the compiler will convert a to an
|
---|
673 | * unsigned long before doing the compare.
|
---|
674 | *
|
---|
675 | *
|
---|
676 | *
|
---|
677 | * @section sec_vbox_guideline_svn Subversion Commit Rules
|
---|
678 | *
|
---|
679 | *
|
---|
680 | * Before checking in:
|
---|
681 | *
|
---|
682 | * - Check Tinderbox and make sure the tree is green across all platforms. If it's
|
---|
683 | * red on a platform, don't check in. If you want, warn in the \#vbox channel and
|
---|
684 | * help make the responsible person fix it.
|
---|
685 | * NEVER CHECK IN TO A BROKEN BUILD.
|
---|
686 | *
|
---|
687 | * - When checking in keep in mind that a commit is atomic and that the Tinderbox and
|
---|
688 | * developers are constantly checking out the tree. Therefore do not split up the
|
---|
689 | * commit unless it's into 100% independent parts. If you need to split it up in order
|
---|
690 | * to have sensible commit comments, make the sub-commits as rapid as possible.
|
---|
691 | *
|
---|
692 | * - If you make a user visible change, such as fixing a reported bug,
|
---|
693 | * make sure you add an entry to doc/manual/user_ChangeLogImpl.xml.
|
---|
694 | *
|
---|
695 | * - If you are adding files make sure set the right attributes.
|
---|
696 | * svn-ps.sh/cmd was created for this purpose, please make use of it.
|
---|
697 | *
|
---|
698 | *
|
---|
699 | * After checking in:
|
---|
700 | *
|
---|
701 | * - After checking-in, you watch Tinderbox until your check-ins clear. You do not
|
---|
702 | * go home. You do not sleep. You do not log out or experiment with drugs. You do
|
---|
703 | * not become unavailable. If you break the tree, add a comment saying that you're
|
---|
704 | * fixing it. If you can't fix it and need help, ask in the \#innotek channel or back
|
---|
705 | * out the change.
|
---|
706 | *
|
---|
707 | * (Inspired by mozilla tree rules.)
|
---|
708 | */
|
---|
709 |
|
---|