STYLE 6.9 KB

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