tcl/target/nrf52: Use 'error' instead of 'echo'
[openocd.git] / doc / manual / style.txt
bloba1e6b8f0164e177c23f114fa6a0d602ca16fda0b
1 /** @page styleguide Style Guides
3 The goals for each of these guides are:
4 - to produce correct code that appears clean, consistent, and readable,
5 - to allow developers to create patches that conform to a standard, and
6 - to eliminate these issues as points of future contention.
8 Some of these rules may be ignored in the spirit of these stated goals;
9 however, such exceptions should be fairly rare.
11 The following style guides describe a formatting, naming, and other
12 conventions that should be followed when writing or changing the OpenOCD
13 code:
15 - @subpage styletcl
16 - @subpage stylec
17 - @subpage styleperl
18 - @subpage styleautotools
20 In addition, the following style guides provide information for
21 providing documentation, either as part of the C code or stand-alone.
23 - @subpage styledoxygen
24 - @subpage styletexinfo
25 - @subpage stylelatex
27 Feedback would be welcome to improve the OpenOCD guidelines.
29  */
30 /** @page styletcl TCL Style Guide
32 OpenOCD needs to expand its Jim/TCL Style Guide.
34 Many of the guidelines listed on the @ref stylec page should apply to
35 OpenOCD's Jim/TCL code as well.
37  */
38 /** @page stylec C Style Guide
40 This page contains guidelines for writing new C source code for the
41 OpenOCD project.
43 @section styleformat Formatting Guide
45 - remove any trailing white space at the end of lines.
46 - use TAB characters for indentation; do NOT use spaces.
47 - displayed TAB width is 4 characters.
48 - use Unix line endings ('\\n'); do NOT use DOS endings ('\\r\\n')
49 - limit adjacent empty lines to at most two (2).
50 - remove any trailing empty lines at the end of source files
51 - do not "comment out" code from the tree nor put it within a block
52   @code
53   #if 0
54   ...
55   #endif
56   @endcode
57   otherwise it would never be checked at compile time and when new
58   patches get merged it could be not compilable anymore.
59   Code that is not fully working nor ready for submission should
60   instead be removed entirely (git can retrieve the old version).
61   For exceptional cases that require keeping some unused code, let
62   the compiler check it by putting it in a block
63   @code
64   if (false) {
65       /* explain why this code should be kept here */
66       ...
67   }
68   @endcode
69 - in a @c switch statement align the @c switch with the @c case label
70   @code
71   switch (dev_id) {
72   case 0x0123:
73       size = 0x10000;
74       break;
75   case 0x0412:
76       size = 0x20000;
77       break;
78   default:
79       size = 0x40000;
80       break;
81   }
82   @endcode
83 - in an <tt> if / then / else </tt> statement, if only one of the conditions
84   require curly brackets due to multi-statement block, put the curly brackets
85   also to the other condition
86   @code
87   if (x > 0)
88       a = 12 + x;
89   else
90       a = 24;
91   @endcode
92   @code
93   if (x > 0) {
94       a = 12 + x;
95   } else {
96       a = 24;
97       x = 0;
98   }
99   @endcode
100 - on <tt> if </tt> statements where the condition is split among multiple
101   lines, increase the indentation of the condition to prevent it to match
102   to the indentation of the <tt> then </tt> block due to length of 'if ('
103   being same of the TAB width of 4 characters. Use:
104   @code
105   if (CMD_ARGC < 3
106           || CMD_ARGC > 8)
107       return ERROR_COMMAND_SYNTAX_ERROR;
108   @endcode
109   instead of:
110   @code
111   if (CMD_ARGC < 3 ||
112       CMD_ARGC > 8)
113       return ERROR_COMMAND_SYNTAX_ERROR;
114   @endcode
116 Finally, try to avoid lines of code that are longer than 72-80 columns:
118 - long lines frequently indicate other style problems:
119   - insufficient use of static functions, macros, or temporary variables
120   - poor flow-control structure; "inverted" logical tests
121 - a few lines may be wider than this limit (typically format strings), but:
122   - all C compilers will concatenate series of string constants.
123   - all long string constants should be split across multiple lines.
124   - do never exceed 120 columns.
126 @section stylenames Naming Rules
128 - most identifiers must use lower-case letters (and digits) only.
129   - macros and enumerators must use upper-case letters (and digits) only.
130   - OpenOCD identifiers should NEVER use @c MixedCaps, aka @c CamelCase.
131 - @c typedef names must end with the '_t' suffix.
132   - This should be reserved for types that should be passed by value.
133   - Do @b not mix the typedef keyword with @c struct.
134 - use underline characters between consecutive words in identifiers
135   (e.g. @c more_than_one_word).
137 @section style_include_guards Include Guards
139 Every header file should have a unique include guard to prevent multiple
140 inclusion.
141 To guarantee uniqueness, an include guard should be based on the filename and
142 the full path in the project source tree.
144 For the header file src/helper/jim-nvp.h, the include guard would look like
145 this:
147 @code
148 #ifndef OPENOCD_HELPER_JIM_NVP_H
149 #define OPENOCD_HELPER_JIM_NVP_H
151 /* Your code here. */
153 #endif /* OPENOCD_HELPER_JIM_NVP_H */
154 @endcode
156 @section stylec99 C99 Rules
158 - inline functions
159 - @c // comments -- in new code, prefer these for single-line comments
160 - trailing comma allowed in enum declarations
161 - designated initializers ( .field = value )
162 - variables declarations should occur at the point of first use
163 - new block scopes for selection and iteration statements
164 - use malloc() to create dynamic arrays. Do @b not use @c alloca
165 or variable length arrays on the stack. non-MMU hosts(uClinux) and
166 pthreads require modest and predictable stack usage.
168 @section styletypes Type Guidelines
169 - use native types (@c int or <tt> unsigned int </tt>) if the type is not important
170   - if size matters, use the types from \<stdint.h\> or \<inttypes.h\>:
171     - @c int8_t, @c int16_t, @c int32_t, or @c int64_t: signed types of specified size
172     - @c uint8_t, @c uint16_t, @c uint32_t, or @c uint64_t: unsigned types of specified size
173     - use the associated @c printf and @c scanf formatting strings for these types
174       (e.g. @c PRId8, PRIx16, SCNu8, ...)
175   - do @b NOT redefine @c uN types from "types.h"
176   - use type @c target_addr_t for target's address values
177   - prefer type <tt> unsigned int </tt> to type @c unsigned
179 @section stylefunc Functions
181 - static inline functions should be preferred over macros:
182 @code
183 /* do NOT define macro-like functions like this... */
184 #define CUBE(x) ((x) * (x) * (x))
185 /* instead, define the same expression using a C99 inline function */
186 static inline int cube(int x) { return x * x * x; }
187 @endcode
188 - Functions should be declared static unless required by other modules
189   - define static functions before first usage to avoid forward declarations.
190 - Functions should have no space between its name and its parameter list:
191 @code
192 int f(int x1, int x2)
194         ...
195         int y = f(x1, x2 - x1);
196         ...
198 @endcode
199 - Separate assignment and logical test statements.  In other words, you
200 should write statements like the following:
201 @code
202 // separate statements should be preferred
203 result = foo();
204 if (result != ERROR_OK)
205         ...
206 @endcode
207 More directly, do @b not combine these kinds of statements:
208 @code
209 // Combined statements should be avoided
210 if ((result = foo()) != ERROR_OK)
211         return result;
212 @endcode
213 - Do not compare @c bool values with @c true or @c false but use the
214   value directly
215 @code
216 if (!is_enabled)
217     ...
218 @endcode
219 - Avoid comparing pointers with @c NULL
220 @code
221 buf = malloc(buf_size);
222 if (!buf) {
223     LOG_ERROR("Out of memory");
224     return ERROR_FAIL;
226 @endcode
228  */
229 /** @page styledoxygen Doxygen Style Guide
231 The following sections provide guidelines for OpenOCD developers
232 who wish to write Doxygen comments in the code or this manual.
233 For an introduction to Doxygen documentation,
234 see the @ref primerdoxygen.
236 @section styledoxyblocks Doxygen Block Selection
238 Several different types of Doxygen comments can be used; often,
239 one style will be the most appropriate for a specific context.
240 The following guidelines provide developers with heuristics for
241 selecting an appropriate form and writing consistent documentation
242 comments.
244 -# use @c /// to for one-line documentation of instances.
245 -# for documentation requiring multiple lines, use a "block" style:
246 @verbatim
248  * @brief First sentence is short description.  Remaining text becomes
249  * the full description block, where "empty" lines start new paragraphs.
251  * One can make text appear in @a italics, @b bold, @c monospace, or
252  * in blocks such as the one in which this example appears in the Style
253  * Guide.  See the Doxygen Manual for the full list of commands.
255  * @param foo For a function, describe the parameters (e.g. @a foo).
256  * @returns The value(s) returned, or possible error conditions.
257  */
258 @endverbatim
259   -# The block should start on the line following the opening @c /\**.
260   -# The end of the block, @c *&zwj;/, should also be on its own line.
261   -# Every line in the block should have a @c '*' in-line with its start:
262     - A leading space is required to align the @c '*' with the @c /\** line.
263     - A single "empty" line should separate the function documentation
264       from the block of parameter and return value descriptions.
265     - Except to separate paragraphs of documentation, other extra
266       "empty" lines should be removed from the block.
267   -# Only single spaces should be used; do @b not add mid-line indentation.
268 -# If the total line length will be less than 72-80 columns, then
269   - The @c /\**< form can be used on the same line.
270   - This style should be used sparingly; the best use is for fields:
271     @verbatim int field; /**< field description */ @endverbatim
273 @section styledoxyall Doxygen Style Guide
275 The following guidelines apply to all Doxygen comment blocks:
277 -# Use the @c '\@cmd' form for all doxygen commands (do @b not use @c '\\cmd').
278 -# Use symbol names such that Doxygen automatically creates links:
279   -# @c function_name() can be used to reference functions
280     (e.g. flash_set_dirty()).
281   -# @c struct_name::member_name should be used to reference structure
282     fields in the documentation (e.g. @c flash_driver::name).
283   -# URLS get converted to markup automatically, without any extra effort.
284   -# new pages can be linked into the hierarchy by using the @c \@subpage
285     command somewhere the page(s) under which they should be linked:
286   -# use @c \@ref in other contexts to create links to pages and sections.
287 -# Use good Doxygen mark-up:
288   -# '\@a' (italics) should be used to reference parameters (e.g. <i>foo</i>).
289   -# '\@b' (bold) should be used to emphasizing <b>single</b> words.
290   -# '\@c' (monospace) should be used with <code>file names</code> and
291   <code>code symbols</code>, so they appear visually distinct from
292   surrounding text.
293   -# To mark-up multiple words, the HTML alternatives must be used.
294 -# Two spaces should be used when nesting lists; do @b not use '\\t' in lists.
295 -# Code examples provided in documentation must conform to the Style Guide.
297 @section styledoxytext Doxygen Text Inputs
299 In addition to the guidelines in the preceding sections, the following
300 additional style guidelines should be considered when writing
301 documentation as part of standalone text files:
303 -# Text files must contain Doxygen at least one comment block:
304   -# Documentation should begin in the first column (except for nested lists).
305   -# Do NOT use the @c '*' convention that must be used in the source code.
306 -# Each file should contain at least one @c \@page block.
307   -# Each new page should be listed as a \@subpage in the \@page block
308   of the page that should serve as its parent.
309   -# Large pages should be structure in parts using meaningful \@section
310   and \@subsection commands.
311 -# Include a @c \@file block at the end of each Doxygen @c .txt file to
312   document its contents:
313   - Doxygen creates such pages for files automatically, but no content
314     will appear on them for those that only contain manual pages.
315   - The \@file block should provide useful meta-documentation to assist
316     technical writers; typically, a list of the pages that it contains.
317   - For example, the @ref styleguide exists in @c doc/manual/style.txt,
318     which contains a reference back to itself.
319 -# The \@file and \@page commands should begin on the same line as
320    the start of the Doxygen comment:
321 @verbatim
322 /** @page pagename Page Title
324 Documentation for the page.
326  */
327 /** @file
329 This file contains the @ref pagename page.
331  */
332 @endverbatim
334 For an example, the Doxygen source for this Style Guide can be found in
335 @c doc/manual/style.txt, alongside other parts of The Manual.
337  */
338 /** @page styletexinfo Texinfo Style Guide
340 The User's Guide is there to provide two basic kinds of information.  It
341 is a guide for how and why to use each feature or mechanism of OpenOCD.
342 It is also the reference manual for all commands and options involved
343 in using them, including interface, flash, target, and other drivers.
344 At this time, it is the only documentation for end users; everything
345 else is addressing OpenOCD developers.
347 There are two key audiences for the User's Guide, both developer based.
348 The primary audience is developers using OpenOCD as a tool in their
349 work, or who may be starting to use it that way.  A secondary audience
350 includes developers who are supporting those users by packaging or
351 customizing it for their hardware, installing it as part of some software
352 distribution, or by evolving OpenOCD itself.  There is some crossover
353 between those audiences.  We encourage contributions from users as the
354 fundamental way to evolve and improve OpenOCD.  In particular, creating
355 a board or target specific configuration file is something that many
356 users will end up doing at some point, and we like to see such files
357 become part of the mainline release.
359 General documentation rules to remember include:
361 - Be concise and clear.  It's work to remove those extra words and
362   sentences, but such "noise" doesn't help readers.
363 - Make it easy to skim and browse.  "Tell what you're going to say,
364   then say it".  Help readers decide whether to dig in now, or
365   leave it for later.
366 - Make sure the chapters flow well.  Presentations should not jump
367   around, and should move easily from overview down to details.
368 - Avoid using the passive voice.
369 - Address the reader to clarify roles ("your config file", "the board you
370   are debugging", etc.); "the user" (etc) is artificial.
371 - Use good English grammar and spelling.  Remember also that English
372   will not be the first language for many readers.  Avoid complex or
373   idiomatic usage that could create needless barriers.
374 - Use examples to highlight fundamental ideas and common idioms.
375 - Don't overuse list constructs.  This is not a slide presentation;
376   prefer paragraphs.
378 When presenting features and mechanisms of OpenOCD:
380 - Explain key concepts before presenting commands using them.
381 - Tie examples to common developer tasks.
382 - When giving instructions, you can \@enumerate each step both
383   to clearly delineate the steps, and to highlight that this is
384   not explanatory text.
385 - When you provide "how to use it" advice or tutorials, keep it
386   in separate sections from the reference material.
387 - Good indexing is something of a black art.  Use \@cindex for important
388   concepts, but don't overuse it.  In particular, rely on the \@deffn
389   indexing, and use \@cindex primarily with significant blocks of text
390   such as \@subsection.  The \@dfn of a key term may merit indexing.
391 - Use \@xref (and \@anchor) with care.  Hardcopy versions, from the PDF,
392   must make sense without clickable links (which don't work all that well
393   with Texinfo in any case).  If you find you're using many links,
394   read that as a symptom that the presentation may be disjointed and
395   confusing.
396 - Avoid font tricks like \@b, but use \@option, \@file, \@dfn, \@emph
397   and related mechanisms where appropriate.
399 For technical reference material:
401 - It's OK to start sections with explanations and end them with
402   detailed lists of the relevant commands.
403 - Use the \@deffn style declarations to define all commands and drivers.
404   These will automatically appear in the relevant index, and those
405   declarations help promote consistent presentation and style.
406    - It's a "Command" if it can be used interactively.
407    - Else it's a "Config Command" if it must be used before the
408      configuration stage completes.
409    - For a "Driver", list its name.
410    - Use EBNF style regular expressions to define parameters:
411      brackets around zero-or-one choices, parentheses around
412      exactly-one choices.
413    - Use \@option, \@file, \@var and other mechanisms where appropriate.
414    - Say what output it displays, and what value it returns to callers.
415    - Explain clearly what the command does.  Sometimes you will find
416      that it can't be explained clearly.  That usually means the command
417      is poorly designed; replace it with something better, if you can.
418    - Be complete:  document all commands, except as part of a strategy
419      to phase something in or out.
420    - Be correct:  review the documentation against the code, and
421      vice versa.
422 - Alphabetize the \@defn declarations for all commands in each
423   section.
424 - Keep the per-command documentation focused on exactly what that
425   command does, not motivation, advice, suggestions, or big examples.
426   When commands deserve such expanded text, it belongs elsewhere.
427   Solutions might be using a \@section explaining a cluster of related
428   commands, or acting as a mini-tutorial.
429 - Details for any given driver should be grouped together.
431 The User's Guide is the first place most users will start reading,
432 after they begin using OpenOCD.  Make that investment of their time
433 be as productive as possible.  Needing to look at OpenOCD source code,
434 to figure out how to use it is a bad sign, though it's OK to need to
435 look at the User's guide to figure out what a config script is doing.
437  */
438 /** @page stylelatex LaTeX Style Guide
440 This page needs to provide style guidelines for using LaTeX, the
441 typesetting language used by The References for OpenOCD Hardware.
442 Likewise, the @ref primerlatex for using this guide needs to be completed.
444  */
445 /** @page styleperl Perl Style Guide
447 This page provides some style guidelines for using Perl, a scripting
448 language used by several small tools in the tree:
450 -# Ensure all Perl scripts use the proper suffix (@c .pl for scripts, and
451    @c .pm for modules)
452 -# Pass files as script parameters or piped as input:
453   - Do NOT code paths to files in the tree, as this breaks out-of-tree builds.
454   - If you must, then you must also use an automake rule to create the script.
455 -# use @c '#!/usr/bin/perl' as the first line of Perl scripts.
456 -# always <code>use strict</code> and <code>use warnings</code>
457 -# invoke scripts indirectly in Makefiles or other scripts:
458 @code
459 perl script.pl
460 @endcode
462 Maintainers must also be sure to follow additional guidelines:
463 -# Ensure that Perl scripts are committed as executables:
464     Use "<code>chmod +x script.pl</code>"
465     @a before using "<code>git add script.pl</code>"
467  */
468 /** @page styleautotools Autotools Style Guide
470 This page contains style guidelines for the OpenOCD autotools scripts.
472 The following guidelines apply to the @c configure.ac file:
473 - Better guidelines need to be developed, but until then...
474 - Use good judgement.
476 The following guidelines apply to @c Makefile.am files:
477 -# When assigning variables with long lists of items:
478   -# Separate the values on each line to make the files "patch friendly":
479 @code
480 VAR = \
481         value1 \
482         value2 \
483         ...
484         value9 \
485         value10
486 @endcode
487  */
488 /** @file
490 This file contains the @ref styleguide pages.  The @ref styleguide pages
491 include the following Style Guides for their respective code and
492 documentation languages:
494 - @ref styletcl
495 - @ref stylec
496 - @ref styledoxygen
497 - @ref styletexinfo
498 - @ref stylelatex
499 - @ref styleperl
500 - @ref styleautotools
502  */