STYLE 6.9 KB

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