HACKING 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. http://wiki.noreply.org/noreply/TheOnionRouter/TorFAQ#LogLevels
  26. No error or warning messages should be expected during normal OR or OP
  27. operation.
  28. If a library function is currently called such that failure always
  29. means ERR, then the library function should log WARN and let the caller
  30. log ERR.
  31. [XXX Proposed convention: every message of severity INFO or higher should
  32. either (A) be intelligible to end-users who don't know the Tor source; or
  33. (B) somehow inform the end-users that they aren't expected to understand
  34. the message (perhaps with a string like "internal error"). Option (A) is
  35. to be preferred to option (B). -NM]
  36. 1.5. Doxygen
  37. We use the 'doxygen' utility to generate documentation from our
  38. source code. Here's how to use it:
  39. 1. Begin every file that should be documented with
  40. /**
  41. * \file filename.c
  42. * \brief Short desccription of the file.
  43. **/
  44. (Doxygen will recognize any comment beginning with /** as special.)
  45. 2. Before any function, structure, #define, or variable you want to
  46. document, add a comment of the form:
  47. /** Describe the function's actions in imperative sentences.
  48. *
  49. * Use blank lines for paragraph breaks
  50. * - and
  51. * - hyphens
  52. * - for
  53. * - lists.
  54. *
  55. * Write <b>argument_names</b> in boldface.
  56. *
  57. * \code
  58. * place_example_code();
  59. * between_code_and_endcode_commands();
  60. * \endcode
  61. */
  62. 3. Make sure to escape the characters "<", ">", "\", "%" and "#" as "\<",
  63. "\>", "\\", "\%", and "\#".
  64. 4. To document structure members, you can use two forms:
  65. struct foo {
  66. /** You can put the comment before an element; */
  67. int a;
  68. int b; /**< Or use the less-than symbol to put the comment
  69. * after the element. */
  70. };
  71. 5. To generate documentation from the Tor source code, type:
  72. $ doxygen -g
  73. To generate a file called 'Doxyfile'. Edit that file and run
  74. 'doxygen' to generate the API documentation.
  75. 6. See the Doxygen manual for more information; this summary just
  76. scratches the surface.