aboutsummaryrefslogtreecommitdiff
path: root/doc/HACKING
blob: 66e030b90ca6d6a556ae743cdb199212903cbeda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
0. The buildbot.

  http://tor-buildbot.freehaven.net:8010/

1. Coding conventions

1.0. Whitespace and C conformance

  Invoke "make check-spaces" from time to time, so it can tell you about
  deviations from our C whitespace style.  Generally, we use:
    - Unix-style line endings
    - K&R-style indentation
    - No space before newlines
    - A blank line at the end of each file
    - Never more than one blank line in a row
    - Always spaces, never tabs
    - No more than 79-columns per line.
    - Two spaces per indent.
    - A space between control keywords and their corresponding paren
      "if (x)", "while (x)", and "switch (x)", never "if(x)", "while(x)", or
      "switch(x)".
    - A space between anything and an open brace.
    - No space between a function name and an opening paren. "puts(x)", not
      "puts (x)".
    - Function declarations at the start of the line.

  We try hard to build without warnings everywhere.  In particular, if you're
  using gcc, you should invoke the configure script with the option
  "--enable-gcc-warnings".  This will give a bunch of extra warning flags to
  the compiler, and help us find divergences from our preferred C style.

1.1. Details

  Use tor_malloc, tor_free, tor_strdup, and tor_gettimeofday instead of their
  generic equivalents.  (They always succeed or exit.)

  You can get a full list of the compatibility functions that Tor provides
  by looking through src/common/util.h and src/common/compat.h.

  Use 'INLINE' instead of 'inline', so that we work properly on Windows.

1.2. Calling and naming conventions

  Whenever possible, functions should return -1 on error and 0 on success.

  For multi-word identifiers, use lowercase words combined with
  underscores. (e.g., "multi_word_identifier").  Use ALL_CAPS for macros and
  constants.

  Typenames should end with "_t".

  Function names should be prefixed with a module name or object name.  (In
  general, code to manipulate an object should be a module with the same
  name as the object, so it's hard to tell which convention is used.)

  Functions that do things should have imperative-verb names
  (e.g. buffer_clear, buffer_resize); functions that return booleans should
  have predicate names (e.g. buffer_is_empty, buffer_needs_resizing).

1.3. What To Optimize

  Don't optimize anything if it's not in the critical path.  Right now,
  the critical path seems to be AES, logging, and the network itself.
  Feel free to do your own profiling to determine otherwise.

1.4. Log conventions

  http://wiki.noreply.org/noreply/TheOnionRouter/TorFAQ#LogLevels

  No error or warning messages should be expected during normal OR or OP
  operation.

  If a library function is currently called such that failure always
  means ERR, then the library function should log WARN and let the caller
  log ERR.

  [XXX Proposed convention: every message of severity INFO or higher should
  either (A) be intelligible to end-users who don't know the Tor source; or
  (B) somehow inform the end-users that they aren't expected to understand
  the message (perhaps with a string like "internal error").  Option (A) is
  to be preferred to option (B). -NM]

1.5. Doxygen

  We use the 'doxygen' utility to generate documentation from our
  source code. Here's how to use it:

  1. Begin every file that should be documented with
         /**
          * \file filename.c
          * \brief Short desccription of the file.
          **/

     (Doxygen will recognize any comment beginning with /** as special.)

  2. Before any function, structure, #define, or variable you want to
     document, add a comment of the form:

        /** Describe the function's actions in imperative sentences.
         *
         * Use blank lines for paragraph breaks
         *   - and
         *   - hyphens
         *   - for
         *   - lists.
         *
         * Write <b>argument_names</b> in boldface.
         *
         * \code
         *     place_example_code();
         *     between_code_and_endcode_commands();
         * \endcode
         */

  3. Make sure to escape the characters "<", ">", "\", "%" and "#" as "\<",
     "\>", "\\", "\%", and "\#".

  4. To document structure members, you can use two forms:

       struct foo {
         /** You can put the comment before an element; */
         int a;
         int b; /**< Or use the less-than symbol to put the comment
                 * after the element. */
       };

  5. To generate documentation from the Tor source code, type:

     $ doxygen -g

     To generate a file called 'Doxyfile'.  Edit that file and run
     'doxygen' to generate the API documentation.

  6. See the Doxygen manual for more information; this summary just
     scratches the surface.