@@ -24,6 +24,8 @@ examples that you should imitate. The few negative examples
2424are clearly marked with a comment of /* Yuck! * /. Please
2525don't submit code for libdwarf that looks like any of these.
2626
27+ Also see the src/lib/libdwarf/CODINGSTYLE document.
28+
2729Section list:
2830
2931 Indentation
@@ -40,6 +42,7 @@ Section list:
4042 Checking For Overflow
4143 Dwarfdump Flags Data
4244 Never use strcpy strcat strncpy
45+ Static data and functions
4346 Tools that help
4447
4548
@@ -63,7 +66,8 @@ a left parenthesis.
6366
6467### Tab characters
6568
66- The tab character must never appear.
69+ The tab character must never appear except in
70+ a printf string.
6771
6872### Braces
6973Most of the code in libdwarf uses bracing in the style of K&R:
@@ -126,6 +130,10 @@ with blank lines:
126130 ...
127131 };
128132
133+ Having both CamelCase in names and _ in the names
134+ is perhaps unusual, but it's been that way
135+ in the libdwarf source a very long time,
136+ so we sitck with it in most cases.
129137
130138Never use a space before a function-call left parenthesis
131139or a macro-call left parenthesis.
@@ -164,7 +172,6 @@ function definitions in libdwarf should always take the following form:
164172And in dwarfdump, etc, the code tends to follow this form
165173too (but far from always).
166174
167-
168175Function prototypes inside libdwarf headers
169176(as opposed to .c files)
170177usually have the return type
@@ -227,7 +234,6 @@ field in the code (with grep).
227234 unsigned ns_z;
228235 };
229236
230-
231237### Managing nested blocks
232238
233239Long blocks that are deeply nested make the code very hard
@@ -269,7 +275,6 @@ a pattern of handling exceptional cases early and returning:
269275
270276 return DW_DLV_OK;
271277
272-
273278The return statement is often the best thing to use in a
274279pattern like this. If it's not available due to additional
275280nesting above which require some cleanup after the current
@@ -278,7 +283,6 @@ function before using goto.
278283
279284### Test or Loop with Side Effect
280285
281-
282286Never do:
283287
284288 if ((foo = malloc (sizeof (foo_t)))) {
@@ -404,7 +408,6 @@ values without duplicating the error-code-setting.
404408
405409### Dwarfdump Flags Data
406410
407-
408411Dwarfdump has a large number of options and nearly all the
409412option values are recorded as fields in a single global
410413structure glflags (see dwarfdump.c). Option data not in that
@@ -425,7 +428,19 @@ These functions eliminate the need for the three traditional
425428string functions strcpy, strcat, strncpy and do exactly what
426429is needed safely while doing nothing extra.
427430
431+ ### Static data and functions
432+
433+ Any data or function not referenced outside the
434+ defining source file should be declared 'static'.
428435
436+ In the libdwarf library itself static data is
437+ not appropriate in general.
438+ Because multiple Dwarf_Debug
439+ may be open at the same time in a single
440+ program (dwarfdump or user code).
441+
442+ Function names should be all lower case with underbars
443+ with the goal that statements and comments 'read well'.
429444
430445### Tools that help
431446
@@ -454,8 +469,10 @@ The primary commands used (in a source directory) are
454469
455470 cobra -f basic *.h
456471 cobra -f basic *.c
457- cwe *.h
458- cwe *.c
459472 cobra -terse -f stats *.[ch]
460473 cobra -terse -f metrics *.[ch]
461-
474+ # The cwe command generates lots of spurious output as they
475+ # check things we do not care much about.
476+ # But some of it has been useful.
477+ cwe *.h
478+ cwe *.c
0 commit comments