CODING_STYLE 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. QEMU Coding Style
  2. =================
  3. Please use the script checkpatch.pl in the scripts directory to check
  4. patches before submitting.
  5. 1. Whitespace
  6. Of course, the most important aspect in any coding style is whitespace.
  7. Crusty old coders who have trouble spotting the glasses on their noses
  8. can tell the difference between a tab and eight spaces from a distance
  9. of approximately fifteen parsecs. Many a flamewar have been fought and
  10. lost on this issue.
  11. QEMU indents are four spaces. Tabs are never used, except in Makefiles
  12. where they have been irreversibly coded into the syntax.
  13. Spaces of course are superior to tabs because:
  14. - You have just one way to specify whitespace, not two. Ambiguity breeds
  15. mistakes.
  16. - The confusion surrounding 'use tabs to indent, spaces to justify' is gone.
  17. - Tab indents push your code to the right, making your screen seriously
  18. unbalanced.
  19. - Tabs will be rendered incorrectly on editors who are misconfigured not
  20. to use tab stops of eight positions.
  21. - Tabs are rendered badly in patches, causing off-by-one errors in almost
  22. every line.
  23. - It is the QEMU coding style.
  24. Do not leave whitespace dangling off the ends of lines.
  25. 2. Line width
  26. Lines are 80 characters; not longer.
  27. Rationale:
  28. - Some people like to tile their 24" screens with a 6x4 matrix of 80x24
  29. xterms and use vi in all of them. The best way to punish them is to
  30. let them keep doing it.
  31. - Code and especially patches is much more readable if limited to a sane
  32. line length. Eighty is traditional.
  33. - It is the QEMU coding style.
  34. 3. Naming
  35. Variables are lower_case_with_underscores; easy to type and read. Structured
  36. type names are in CamelCase; harder to type but standing out. Enum type
  37. names and function type names should also be in CamelCase. Scalar type
  38. names are lower_case_with_underscores_ending_with_a_t, like the POSIX
  39. uint64_t and family. Note that this last convention contradicts POSIX
  40. and is therefore likely to be changed.
  41. When wrapping standard library functions, use the prefix qemu_ to alert
  42. readers that they are seeing a wrapped version; otherwise avoid this prefix.
  43. 4. Block structure
  44. Every indented statement is braced; even if the block contains just one
  45. statement. The opening brace is on the line that contains the control
  46. flow statement that introduces the new block; the closing brace is on the
  47. same line as the else keyword, or on a line by itself if there is no else
  48. keyword. Example:
  49. if (a == 5) {
  50. printf("a was 5.\n");
  51. } else if (a == 6) {
  52. printf("a was 6.\n");
  53. } else {
  54. printf("a was something else entirely.\n");
  55. }
  56. Note that 'else if' is considered a single statement; otherwise a long if/
  57. else if/else if/.../else sequence would need an indent for every else
  58. statement.
  59. An exception is the opening brace for a function; for reasons of tradition
  60. and clarity it comes on a line by itself:
  61. void a_function(void)
  62. {
  63. do_something();
  64. }
  65. Rationale: a consistent (except for functions...) bracing style reduces
  66. ambiguity and avoids needless churn when lines are added or removed.
  67. Furthermore, it is the QEMU coding style.