File: HACKING.md

package info (click to toggle)
redland 1.0.17-1.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 8,780 kB
  • ctags: 4,263
  • sloc: ansic: 37,638; sh: 12,115; perl: 2,590; xml: 807; makefile: 587
file content (342 lines) | stat: -rw-r--r-- 8,955 bytes parent folder | download | duplicates (3)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
Hacking Redland librdf
======================

2011-08-23

[Dave Beckett](http://www.dajobe.org/)


Commits
-------

Should be:

  * Licensed with the same license as Redland librdf.
  * Patches in GNU `diff -u` unifed context format preferred.
  * Include tests if they add new features.


Code Style
----------

Do not make large commits that only change code style unless you have
previously had this agreed or it is in code under major refactoring
where a diff is not a large concern.  There is always `diff -b` when
large code style (whitespace) changes are made.


### Indenting

2 spaces.  No tabs.

All code must be wrapped to 80 chars as far as is possible.  Function
definitions or calls should indent the parameters to the left `(`.

Redland libraries use very long function names following the naming
convention which can make linebreaking very hard.  In this case,
indent function parameters on new lines 4 spaces after the function
name like this:

    var = function_name_with_very_long_name_that_is_hard_to_wrap_args(
              argument1_with_very_long_name_or_expression,
              argument2,
              ..)

Use no space between a keyword followed by braces argument.
For example, use `if(cond)` rather than `if (cond)` (ditto for
while, do etc.) and
`functionname(...)` rather than `functionname (...)` in
both definition and calls of functions.

There is nothing wrong with introducing a variable to break up a very
long function call argument.


### Expressions

Put spaces around operators in expressions, assignments, tests, conditions

GOOD:

  * `a += 2 * x`
  * `if(a < 2)`

BAD:

  * `a+=2*x`
  * `if(a<2)`

When comparing to `0` or a `NULL` pointer, use the idiomatic form
that has no comparison.

GOOD:

  * `if(!ptr)`
  * `if(!index)`

BAD:

  * `if(ptr == NULL)`
  * `if(index == 0)`
  * `if(0 == index)`

When comparing a variable to a constant, the code has currently used
`if(var == constant)` rather than the slightly safer, and easier to
compile check, `if(constant == var)`.


### Blocks

In general add {}s around blocks in if else chains when one of the blocks
has more than 1 line of code.  Try not to mix, but the final case if it
is one line, can be braceless.

    if(var == 1) {
      ... multiple lines of code ...
    } else {
      ... multiple lines of code ...
    }

or

    if(var == 1)
      ... one line of code
    else
      ... one line of code

or

    if(var == 1) {
      ... multiple lines of code ...
    } else if(var == 2) {
      ... multiple lines of code and / or more if conditions ...
    } else
      ... one line of code ...


### Switches

If using if else chains on an enumeration, don't do that, use a
`switch()` which GCC can use to find missing cases when they get added.

    switch(enum_var) {
      case ENUM_1:
        ... code ...
        break;
    
      case ENUM_2:
        ... code ...
        break;
    
      case ENUM_DONT_CARE:
      default:
        ... code ...
        break;
    }

There should ALWAYS be a `default:` case.


### Functions

Declare functions in this format:

    returntype
    functionname(type1 param1, type2 param2, ...)
    {
      type3 var1;
      type4 var2;
      
      ... first line of code ...
      
      tidy:
        ... cleanup code...
      
      return value;
     }

Notes:

   * Declare one variable per line
   * Declare all variables at the top of the function (K&R C style)
   * You may declare variables in inner `{}` blocks.  The
     form `if(1) { ... var decls ...; more code }` may be used but
     a code rewrite is preferable.
   * If a label is used it *MUST* be used only for cleanup, and going
     forward in the code to the end of the function.
   * Multiple `return` are allowed but for obvious error or result
     returns.  Do not twist the code to enable a single return.
   * `goto` may be used for resource cleanup and result return 
     where control flow only goes forward.

### C Pre-Processor (CPP) Macros

Always define macros for internal constants and name the macros with
the library prefix followed by a descriptive name in ALL CAPS such as:

    #define LIBRDF_FOOBAR_BUFFER_SIZE 1234

When evaluating macro symbols that may be undefined, always check the
symbol is defined first.  Like this:

    #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 42
	   ... do complex debugging stuff ...
	#endif

This is not needed for macros that are known to be defined, such as those
checked by `configure` e.g.

    #if RAPTOR_VERSION_DECIMAL > 20100
	   ... do stuff that requires a raptor2 version 2.1.0 or newer ...
	#endif
	
since the above would be checked implicitly by `configure` using
`pkg-config(1)` to validate Raptor 2 is present before getting to the
code that tries to evaluate the value from `raptor2.h`.

The debug macros that are used for printing out values when debugging
is enabled do not need protection by `#if` or `#ifdef` and should be
used like this:

    LIBRDF_DEBUG1("Something wonderful happened\n");

    LIBRDF_DEBUG2("Something %s happened\n", happening);


### Memory allocation

Allocating a zeroed out block of memory or a set of objects (calloc)

    var = LIBRDF_CALLOC(type, count, size)

 Prefering when `count` = 1 this form:

    var = LIBRDF_CALLOC(type, 1, sizeof(*var))

Allocating a block of memory:
    
    var = LIBRDF_MALLOC(type, size)

Freeing memory:    

    LIBRDF_FREE(type, var)
    
The reasoning here is to make allocs mostly fit into 1 line without
too much boilerplate and duplication of types.

The macro names vary by library such as `RAPTOR_CALLOC` and
`RASQAL_CALLOC` for Raptor and Rasqal respectively.


### Documentation

Public functions, types, enumerations and defines must have autodocs -
the structured comment block before the definition.  This is read by
`gtk-doc(1)` to generate reference API documentation.

Format:

    /**
     * functionname:
     * @param1: Description of first parameter
     * @param2: Description of second parameter (or NULL)
     * ... more params ...
     *
     * Short Description
     *
     * Long Description.
     *
     * Return value: return value
     */
     returntype
     functionname(...)
     {
       ... body ...
     }

The _Short Description_ have several commmon forms:

  * Constructor - creates a FOO object
  * Destructor - destroys a FOO object
  * Short description of regular function or method.
  * INTERNAL - short description

The latter is used for autodocs for internal functions either as
internal documentation or for APIs that may one day be public.

The _(or NULL)_ phrase is used for pointer parameters that may be
omitted.  This is usually tested by the function as an assertion.
In some functions there are more complex conditions on which optional
parameters are allowed, these are described in the _Long Description_.

The long description may also include a deprecation statement such as:

    * @Deprecated: Use new_function() with foo = BAR

This must be indented to the left and will be used by the
`gtk-doc(1)` document generator to provide a link to the replacement
function and usage.


Commit Messages
---------------

The general standard for Redland libraries using GIT is a merge
of the GIT standards format and GNU ChangeLog

    First line summaries what commit does - this goes into the GIT short log
    
    (function1, function2): what changed
    
    (function3): Added, deprecating function4()
    
    (function4): Deleted, replaced by function3()
    
    struct foo gains field ...
    
    struct bar loses field ...
    
    enum blah gains new value BLAH_2 which ...

Use `name()` in the description for references to functions.
Make sure to do (function1, function2) NOT (function1,function2) as
it makes things easier to format later.

Sometimes it's short enough (good) that it all can be done in the first
line, pretty much only if it's a small change to a single function.

If the change is trivial or a typo and (this is *IMPORTANT*) NOT a
commit to code files, then the commit can start with '#'.  This may
get filtered out of commit log message notifications and ChangeLog.

e.g.  `#spelling`  or `#ws`
the latter is whitespace changes for some reason

The changes will semi-automatically be added to the ChangeLog files
following the GNU style, indented and word wrapped, and adding the list
of files at the start.  So the commit message above ends up looking
something like:

    2010-08-23  User Name <user@example.org>
    
            * dir/file1.c, dir2/file2.c: First line summaries what commit
              does - this goes into the GIT short log

              (function1, function2): what changed

              (function3): Added, deprecating function4()

              (function4): Deleted, replaced by function3()

              struct foo gains field ...

              struct bar loses field ...

              enum blah gains new value BLAH_2 which ...


<!--
Local variables:
mode: markdown
End:
-->