HACKING 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 1. Coding conventions
  2. 1.1. Details
  3. Use tor_malloc, tor_free, tor_snprintf, tor_strdup, and tor_gettimeofday
  4. instead of their generic equivalents. (They always succeed or exit.)
  5. You can get a full list of the compatibility functions that Tor provides
  6. by looking through src/common/util.h and src/common/compat.h.
  7. Use 'INLINE' instead of 'inline', so that we work properly on Windows.
  8. 1.2. Calling and naming conventions
  9. Whenever possible, functions should return -1 on error and 0 on success.
  10. For multi-word identifiers, use lowercase words combined with
  11. underscores. (e.g., "multi_word_identifier"). Use ALL_CAPS for macros and
  12. constants.
  13. Typenames should end with "_t".
  14. Function names should be prefixed with a module name or object name. (In
  15. general, code to manipulate an object should be a module with the same
  16. name as the object, so it's hard to tell which convention is used.)
  17. Functions that do things should have imperative-verb names
  18. (e.g. buffer_clear, buffer_resize); functions that return booleans should
  19. have predicate names (e.g. buffer_is_empty, buffer_needs_resizing).
  20. 1.3. What To Optimize
  21. Don't optimize anything if it's not in the critical path. Right now,
  22. the critical path seems to be AES, logging, and the network itself.
  23. Feel free to do your own profiling to determine otherwise.
  24. 1.4. Log conventions
  25. Log convention: use only these four log severities.
  26. ERR is if something fatal just happened.
  27. WARN if something bad happened, but we're still running. The
  28. bad thing is either a bug in the code, an attack or buggy
  29. protocol/implementation of the remote peer, etc. The operator should
  30. examine the bad thing and try to correct it.
  31. NOTICE if it's something the operator will want to know about.
  32. (No error or warning messages should be expected during normal OR or OP
  33. operation. I expect most people to run on -l notice eventually. If a
  34. library function is currently called such that failure always means
  35. ERR, then the library function should log WARN and let the caller
  36. log ERR.)
  37. INFO means something happened (maybe bad, maybe ok), but there's nothing
  38. you need to (or can) do about it.
  39. DEBUG is for everything louder than INFO.
  40. [XXX Proposed convention: every message of severity INFO or higher should
  41. either (A) be intelligible to end-users who don't know the Tor source; or
  42. (B) somehow inform the end-users that they aren't expected to understand
  43. the message (perhaps with a string like "internal error"). Option (A) is
  44. to be preferred to option (B). -NM]
  45. 1.5. Doxygen
  46. We use the 'doxygen' utility to generate documentation from our
  47. source code. Here's how to use it:
  48. 1. Begin every file that should be documented with
  49. /**
  50. * \file filename.c
  51. * \brief Short desccription of the file.
  52. **/
  53. (Doxygen will recognize any comment beginning with /** as special.)
  54. 2. Before any function, structure, #define, or variable you want to
  55. document, add a comment of the form:
  56. /** Describe the function's actions in imperative sentences.
  57. *
  58. * Use blank lines for paragraph breaks
  59. * - and
  60. * - hyphens
  61. * - for
  62. * - lists.
  63. *
  64. * Write <b>argument_names</b> in boldface.
  65. *
  66. * \code
  67. * place_example_code();
  68. * between_code_and_endcode_commands();
  69. * \endcode
  70. */
  71. 3. Make sure to escape the characters "<", ">", "\", "%" and "#" as "\<",
  72. "\>", "\\", "\%", and "\#".
  73. 4. To document structure members, you can use two forms:
  74. struct foo {
  75. /** You can put the comment before an element; */
  76. int a;
  77. int b; /**< Or use the less-than symbol to put the comment
  78. * after the element. */
  79. };
  80. 5. To generate documentation from the Tor source code, type:
  81. $ doxygen -g
  82. To generate a file called 'Doxyfile'. Edit that file and run
  83. 'doxygen' to generate the API documentation.
  84. 6. See the Doxygen manual for more information; this summary just
  85. scratches the surface.