STYLE 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. Code style
  2. ==========
  3. In general, FreeBSD style(9) should be followed unless it is irrelevant
  4. (e.g., $FreeBSD$ tags).
  5. Functions with external linkage are declared like this:
  6. /**
  7. * module_func(arg1, arg2):
  8. * Description of what the function does, referring to arguments as
  9. * ${arg1} or suchlike.
  10. */
  11. int module_func(void *, int);
  12. The identical comment appears in the C file where the function is defined.
  13. Static functions may have the above form of comment, or simply a
  14. /* Brief description of what the function does. */
  15. line before the function.
  16. "Unrewrappable" comments starting in the first column should be
  17. /**
  18. * Written like this.
  19. *
  20. * Because (some of) the line-breaks are important.
  21. */
  22. whereas when such comments are indented, they should be
  23. /*-
  24. * Written like this.
  25. *
  26. * Because (some of) the line-breaks are important.
  27. */
  28. Line lengths should generally be 78 characters, and not more than 80
  29. characters.
  30. In general, functions should return (int)(-1) or NULL to indicate error.
  31. Errors should be printed via warnp (if errno is relevant) or warn0 (if errno
  32. is not relevant) when they are first detected and also at higher levels where
  33. useful. As an exception to this, malloc failures (i.e., errno = ENOMEM) can
  34. result in failure being passed back up the call chain without being printed
  35. immediately. (Naturally, other errors can be passed back where a function
  36. definition so specifies; e.g., ENOENT in cases where a file not existing is
  37. not erroneous.)
  38. The first statement in main(), after variable declarations, should be
  39. "WARNP_INIT;" in order to set the program name used for printing warnings.
  40. In general, functions should be structured with one return statement per
  41. status, e.g., one return() for success and one return() for failure. Errors
  42. should be handled by using goto to enter the error return path, e.g.,
  43. int
  44. foo(int bar)
  45. {
  46. if (something fails)
  47. goto err0;
  48. /* ... */
  49. if (something else fails)
  50. goto err1;
  51. /* ... */
  52. if (yet another operation fails)
  53. goto err2;
  54. /* Success! */
  55. return (0);
  56. err2:
  57. /* Clean up something. */
  58. err1:
  59. /* Clean up something else. */
  60. err0:
  61. /* Failure! */
  62. return (-1);
  63. }
  64. As an exception to the above, if there is only one way for the function to
  65. fail, the idioms
  66. return (baz(bar));
  67. and
  68. int rc;
  69. rc = baz(bar);
  70. /* ... cleanup code here ... */
  71. return (rc);
  72. are allowed; furthermore, in cases such as foo_free(), the idiom
  73. if (we shouldn't do anything)
  74. return;
  75. is preferred over
  76. if (we shouldn't do anything)
  77. goto done;
  78. at the start of a function.
  79. Headers should be included in the following groups, with a blank line after
  80. each (non-empty) group:
  81. 1. <sys/*.h>, with <sys/types.h> first followed by others alphabetically.
  82. 2. <net/*.h>, in alphabetical order.
  83. 3. <*.h>, in alphabetical order.
  84. 4. header files from /lib/, in alphabetical order.
  85. 5. header files from the program being built, in alphabetical order.
  86. 6. header files (usually just one) defining the interface for this C file.
  87. If ssize_t is needed, <unistd.h> should be included to provide it.
  88. If size_t is needed, <stddef.h> should be included to provide it unless
  89. <stdio.h>, <stdlib.h>, <string.h>, or <unistd.h> is already required.
  90. If the C99 integer types (uint8_t, int64_t, etc.) are required, <stdint.h>
  91. should be included to provide them unless <inttypes.h> is already required.
  92. The type 'char' should only be used to represent human-readable characters
  93. (input from users, output to users, pathnames, et cetera). The type
  94. 'char *' should normally be a NUL-terminated string. The types 'signed
  95. char' and 'unsigned char' should never be used; C99 integer types should
  96. be used instead.
  97. When a variable is declared to have a pointer type, there should be a space
  98. between the '*' and the variable name, e.g.,
  99. int
  100. main(int argc, char * argv[])
  101. {
  102. char * opt_p = NULL;
  103. Note that this is inconsistent with FreeBSD style(9). When used as a unary
  104. operator, '*' is not separated from its argument, e.g.,
  105. while (*p != '\0')
  106. p++;
  107. When a struct is referenced, the idiom
  108. /* Opaque types. */
  109. struct foo;
  110. struct bar * bar_from_foo(struct foo *);
  111. is preferable to
  112. #include "foo.h" /* needed for struct foo */
  113. struct bar * bar_from_foo(struct foo *);
  114. unless there is some reason why the internal layout of struct foo is needed
  115. (e.g., if struct bar contains a struct foo rather than a struct foo *). Such
  116. struct declarations should be sorted alphabetically.
  117. The file foo.c should only export symbols of the following forms:
  118. foo_* -- most symbols should be of this form.
  119. FOO_* / BAR_FOO_*
  120. -- allowed in cases where FOO or BAR_FOO is idiomatic (e.g.,
  121. MD5, HMAC_SHA256).
  122. foo() / defoo() / unfoo()
  123. -- where "foo" is a verb and this improves code clarity.
  124. Functions named foo_free should return void, and foo_free(NULL) should have
  125. no effect. The right way to spell a comment about this is
  126. /* Behave consistently with free(NULL). */
  127. If static variables need to be initialized to 0 (or NULL) then they should be
  128. explicitly declared that way; implicit initialization should not be used.
  129. In non-trivial code, comments should be included which describe in English
  130. what is being done by the surrounding code with sufficient detail that if the
  131. code were removed, it could be replaced based on reading the comments without
  132. requiring any significant creativity.
  133. Comments and documentation should be written in en-GB-oed; i.e., with
  134. the 'u' included in words such as "honour", "colour", and "neighbour",
  135. and the ending '-ize' in words such as "organize" and "realize". The
  136. Oxford (aka. serial) comma should be used in lists. Quotation marks
  137. should be placed logically, i.e., not including punctuation marks which
  138. do not form a logical part of the quoted text. Two spaces should be used
  139. after a period which ends a sentence.
  140. The first local variable declaration in cookie-using functions should be
  141. struct foo * bar = cookie;
  142. When versions of functions are written to exploit special CPU features
  143. (using the cpusupport framework), that code should be placed into a
  144. separate file (e.g., crypto_aes_aesni.c) so that it can be compiled with
  145. different compiler flags. Such a file should start with
  146. #include "cpusupport.h"
  147. #ifdef CPUSUPPORT_FOO_BAR
  148. /**
  149. * CPUSUPPORT CFLAGS: FOO_BAR FOO_BAZ
  150. */
  151. and end with
  152. #endif /* CPUSUPPORT_FOO_BAR */
  153. For example, we could have
  154. #if defined(CPUSUPPORT_X86_SHANI) && defined(CPUSUPPORT_X86_SSSE3)
  155. /**
  156. * CPUSUPPORT CFLAGS: X86_SHANI X86_SSSE3
  157. */
  158. Functions for which special CPU-feature-exploiting variants exist should
  159. take the form
  160. {
  161. /* Variable declarations here. */
  162. /* Asserts here, if any. */
  163. #ifdef CPUSUPPORT_FOO_BAR
  164. if (/* We've decided we can use the variant code */) {
  165. /* Call variant code and return. */
  166. }
  167. #endif
  168. /* Normal implementation of the function. */
  169. }
  170. If there are multiple CPU-feature-exploiting variants, the `if` could instead
  171. be a `switch` which invokes the appropriate variant function.