CodingStandards.txt 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. Coding conventions for Tor
  2. --------------------------
  3. tl;dr:
  4. * Run configure with '--enable-gcc-warnings'
  5. * Run 'make check-spaces' to catch whitespace errors
  6. * Document your functions
  7. * Write unit tests
  8. * Add a file in 'changes' for your branch.
  9. Patch checklist
  10. ~~~~~~~~~~~~~~~
  11. If possible, send your patch as one of these (in descending order of
  12. preference)
  13. - A git branch we can pull from
  14. - Patches generated by git format-patch
  15. - A unified diff
  16. Did you remember...
  17. - To build your code while configured with --enable-gcc-warnings?
  18. - To run "make check-spaces" on your code?
  19. - To run "make check-docs" to see whether all new options are on
  20. the manpage?
  21. - To write unit tests, as possible?
  22. - To base your code on the appropriate branch?
  23. - To include a file in the "changes" directory as appropriate?
  24. How we use Git branches
  25. -----------------------
  26. Each main development series (like 0.2.1, 0.2.2, etc) has its main work
  27. applied to a single branch. At most one series can be the development series
  28. at a time; all other series are maintenance series that get bug-fixes only.
  29. The development series is built in a git branch called "master"; the
  30. maintenance series are built in branches called "maint-0.2.0", "maint-0.2.1",
  31. and so on. We regularly merge the active maint branches forward.
  32. For all series except the development series, we also have a "release" branch
  33. (as in "release-0.2.1"). The release series is based on the corresponding
  34. maintenance series, except that it deliberately lags the maint series for
  35. most of its patches, so that bugfix patches are not typically included in a
  36. maintenance release until they've been tested for a while in a development
  37. release. Occasionally, we'll merge an urgent bugfix into the release branch
  38. before it gets merged into maint, but that's rare.
  39. If you're working on a bugfix for a bug that occurs in a particular version,
  40. base your bugfix branch on the "maint" branch for the first supported series
  41. that has that bug. (As of June 2013, we're supporting 0.2.3 and later.) If
  42. you're working on a new feature, base it on the master branch.
  43. How we log changes
  44. ------------------
  45. When you do a commit that needs a ChangeLog entry, add a new file to
  46. the "changes" toplevel subdirectory. It should have the format of a
  47. one-entry changelog section from the current ChangeLog file, as in
  48. o Major bugfixes:
  49. - Fix a potential buffer overflow. Fixes bug 99999; bugfix on
  50. 0.3.1.4-beta.
  51. To write a changes file, first categorize the change. Some common categories
  52. are: Minor bugfixes, Major bugfixes, Minor features, Major features, Code
  53. simplifications and refactoring. Then say what the change does. If
  54. it's a bugfix, mention what bug it fixes and when the bug was
  55. introduced. To find out which Git tag the change was introduced in,
  56. you can use "git describe --contains <sha1 of commit>".
  57. If at all possible, try to create this file in the same commit where you are
  58. making the change. Please give it a distinctive name that no other branch will
  59. use for the lifetime of your change. To verify the format of the changes file,
  60. you can use "make check-changes".
  61. When we go to make a release, we will concatenate all the entries
  62. in changes to make a draft changelog, and clear the directory. We'll
  63. then edit the draft changelog into a nice readable format.
  64. What needs a changes file?::
  65. A not-exhaustive list: Anything that might change user-visible
  66. behavior. Anything that changes internals, documentation, or the build
  67. system enough that somebody could notice. Big or interesting code
  68. rewrites. Anything about which somebody might plausibly wonder "when
  69. did that happen, and/or why did we do that" 6 months down the line.
  70. Why use changes files instead of Git commit messages?::
  71. Git commit messages are written for developers, not users, and they
  72. are nigh-impossible to revise after the fact.
  73. Why use changes files instead of entries in the ChangeLog?::
  74. Having every single commit touch the ChangeLog file tended to create
  75. zillions of merge conflicts.
  76. Whitespace and C conformance
  77. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  78. Invoke "make check-spaces" from time to time, so it can tell you about
  79. deviations from our C whitespace style. Generally, we use:
  80. - Unix-style line endings
  81. - K&R-style indentation
  82. - No space before newlines
  83. - A blank line at the end of each file
  84. - Never more than one blank line in a row
  85. - Always spaces, never tabs
  86. - No more than 79-columns per line.
  87. - Two spaces per indent.
  88. - A space between control keywords and their corresponding paren
  89. "if (x)", "while (x)", and "switch (x)", never "if(x)", "while(x)", or
  90. "switch(x)".
  91. - A space between anything and an open brace.
  92. - No space between a function name and an opening paren. "puts(x)", not
  93. "puts (x)".
  94. - Function declarations at the start of the line.
  95. We try hard to build without warnings everywhere. In particular, if you're
  96. using gcc, you should invoke the configure script with the option
  97. "--enable-gcc-warnings". This will give a bunch of extra warning flags to
  98. the compiler, and help us find divergences from our preferred C style.
  99. Functions to use
  100. ~~~~~~~~~~~~~~~~
  101. We have some wrapper functions like tor_malloc, tor_free, tor_strdup, and
  102. tor_gettimeofday; use them instead of their generic equivalents. (They
  103. always succeed or exit.)
  104. You can get a full list of the compatibility functions that Tor provides by
  105. looking through src/common/util.h and src/common/compat.h. You can see the
  106. available containers in src/common/containers.h. You should probably
  107. familiarize yourself with these modules before you write too much code, or
  108. else you'll wind up reinventing the wheel.
  109. Use 'INLINE' instead of 'inline', so that we work properly on Windows.
  110. Calling and naming conventions
  111. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  112. Whenever possible, functions should return -1 on error and 0 on success.
  113. For multi-word identifiers, use lowercase words combined with
  114. underscores. (e.g., "multi_word_identifier"). Use ALL_CAPS for macros and
  115. constants.
  116. Typenames should end with "_t".
  117. Function names should be prefixed with a module name or object name. (In
  118. general, code to manipulate an object should be a module with the same name
  119. as the object, so it's hard to tell which convention is used.)
  120. Functions that do things should have imperative-verb names
  121. (e.g. buffer_clear, buffer_resize); functions that return booleans should
  122. have predicate names (e.g. buffer_is_empty, buffer_needs_resizing).
  123. If you find that you have four or more possible return code values, it's
  124. probably time to create an enum. If you find that you are passing three or
  125. more flags to a function, it's probably time to create a flags argument that
  126. takes a bitfield.
  127. What To Optimize
  128. ~~~~~~~~~~~~~~~~
  129. Don't optimize anything if it's not in the critical path. Right now, the
  130. critical path seems to be AES, logging, and the network itself. Feel free to
  131. do your own profiling to determine otherwise.
  132. Log conventions
  133. ~~~~~~~~~~~~~~~
  134. https://www.torproject.org/docs/faq#LogLevel
  135. No error or warning messages should be expected during normal OR or OP
  136. operation.
  137. If a library function is currently called such that failure always means ERR,
  138. then the library function should log WARN and let the caller log ERR.
  139. Every message of severity INFO or higher should either (A) be intelligible
  140. to end-users who don't know the Tor source; or (B) somehow inform the
  141. end-users that they aren't expected to understand the message (perhaps
  142. with a string like "internal error"). Option (A) is to be preferred to
  143. option (B).
  144. Doxygen comment conventions
  145. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  146. Say what functions do as a series of one or more imperative sentences, as
  147. though you were telling somebody how to be the function. In other words, DO
  148. NOT say:
  149. /** The strtol function parses a number.
  150. *
  151. * nptr -- the string to parse. It can include whitespace.
  152. * endptr -- a string pointer to hold the first thing that is not part
  153. * of the number, if present.
  154. * base -- the numeric base.
  155. * returns: the resulting number.
  156. */
  157. long strtol(const char *nptr, char **nptr, int base);
  158. Instead, please DO say:
  159. /** Parse a number in radix <b>base</b> from the string <b>nptr</b>,
  160. * and return the result. Skip all leading whitespace. If
  161. * <b>endptr</b> is not NULL, set *<b>endptr</b> to the first character
  162. * after the number parsed.
  163. **/
  164. long strtol(const char *nptr, char **nptr, int base);
  165. Doxygen comments are the contract in our abstraction-by-contract world: if
  166. the functions that call your function rely on it doing something, then your
  167. function should mention that it does that something in the documentation. If
  168. you rely on a function doing something beyond what is in its documentation,
  169. then you should watch out, or it might do something else later.