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
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
27 Feedback would be welcome to improve the OpenOCD guidelines.
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.
38 /** @page stylec C Style Guide
40 This page contains guidelines for writing new C source code for the
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; instead, one should either:
52 -# remove it entirely (git can retrieve the old version), or
53 -# use an @c \#if/\#endif block.
55 Finally, try to avoid lines of code that are longer than than 72-80 columns:
57 - long lines frequently indicate other style problems:
58 - insufficient use of static functions, macros, or temporary variables
59 - poor flow-control structure; "inverted" logical tests
60 - a few lines may be wider than this limit (typically format strings), but:
61 - all C compilers will concatenate series of string constants.
62 - all long string constants should be split across multiple lines.
64 @section stylenames Naming Rules
66 - most identifiers must use lower-case letters (and digits) only.
67 - macros must use upper-case letters (and digits) only.
68 - OpenOCD identifiers should NEVER use @c MixedCaps.
69 - @c typedef names must end with the '_t' suffix.
70 - This should be reserved for types that should be passed by value.
71 - Do @b not mix the typedef keyword with @c struct.
72 - use underline characters between consecutive words in identifiers
73 (e.g. @c more_than_one_word).
75 @section style_include_guards Include Guards
77 Every header file should have a unique include guard to prevent multiple
79 To guarantee uniqueness, an include guard should be based on the filename and
80 the full path in the project source tree.
82 For the header file src/helper/jim-nvp.h, the include guard would look like
86 #ifndef OPENOCD_HELPER_JIM_NVP_H
87 #define OPENOCD_HELPER_JIM_NVP_H
91 #endif /* OPENOCD_HELPER_JIM_NVP_H */
94 @section stylec99 C99 Rules
97 - @c // comments -- in new code, prefer these for single-line comments
98 - trailing comma allowed in enum declarations
99 - designated initializers ( .field = value )
100 - variables declarations should occur at the point of first use
101 - new block scopes for selection and iteration statements
102 - use malloc() to create dynamic arrays. Do @b not use @c alloca
103 or variable length arrays on the stack. non-MMU hosts(uClinux) and
104 pthreads require modest and predictable stack usage.
106 @section styletypes Type Guidelines
107 - use native types (@c int or @c unsigned) if the type is not important
108 - if size matters, use the types from \<stdint.h\> or \<inttypes.h\>:
109 - @c int8_t, @c int16_t, @c int32_t, or @c int64_t: signed types of specified size
110 - @c uint8_t, @c uint16_t, @c uint32_t, or @c uint64_t: unsigned types of specified size
111 - do @b NOT redefine @c uN types from "types.h"
113 @section stylefunc Functions
115 - static inline functions should be prefered over macros:
117 /** do NOT define macro-like functions like this... */
118 #define CUBE(x) ((x) * (x) * (x))
119 /** instead, define the same expression using a C99 inline function */
120 static inline int cube(int x) { return x * x * x; }
122 - Functions should be declared static unless required by other modules
123 - define static functions before first usage to avoid forward declarations.
124 - Functions should have no space between its name and its parameter list:
126 int f(int x1, int x2)
129 int y = f(x1, x2 - x1);
133 - Separate assignment and logical test statements. In other words, you
134 should write statements like the following:
136 // separate statements should be preferred
138 if (ERROR_OK != result)
141 More directly, do @b not combine these kinds of statements:
143 // Combined statements should be avoided
144 if (ERROR_OK != (result = foo()))
149 /** @page styledoxygen Doxygen Style Guide
151 The following sections provide guidelines for OpenOCD developers
152 who wish to write Doxygen comments in the code or this manual.
153 For an introduction to Doxygen documentation,
154 see the @ref primerdoxygen.
156 @section styledoxyblocks Doxygen Block Selection
158 Several different types of Doxygen comments can be used; often,
159 one style will be the most appropriate for a specific context.
160 The following guidelines provide developers with heuristics for
161 selecting an appropriate form and writing consistent documentation
164 -# use @c /// to for one-line documentation of instances.
165 -# for documentation requiring multiple lines, use a "block" style:
168 * @brief First sentence is short description. Remaining text becomes
169 * the full description block, where "empty" lines start new paragraphs.
171 * One can make text appear in @a italics, @b bold, @c monospace, or
172 * in blocks such as the one in which this example appears in the Style
173 * Guide. See the Doxygen Manual for the full list of commands.
175 * @param foo For a function, describe the parameters (e.g. @a foo).
176 * @returns The value(s) returned, or possible error conditions.
179 -# The block should start on the line following the opening @c /\**.
180 -# The end of the block, @c *‍/, should also be on its own line.
181 -# Every line in the block should have a @c '*' in-line with its start:
182 - A leading space is required to align the @c '*' with the @c /\** line.
183 - A single "empty" line should separate the function documentation
184 from the block of parameter and return value descriptions.
185 - Except to separate paragraphs of documentation, other extra
186 "empty" lines should be removed from the block.
187 -# Only single spaces should be used; do @b not add mid-line indentation.
188 -# If the total line length will be less than 72-80 columns, then
189 - The @c /**< form can be used on the same line.
190 - This style should be used sparingly; the best use is for fields:
191 @code int field; /**< field description */ @endcode
193 @section styledoxyall Doxygen Style Guide
195 The following guidelines apply to all Doxygen comment blocks:
197 -# Use the @c '\@cmd' form for all doxygen commands (do @b not use @c '\\cmd').
198 -# Use symbol names such that Doxygen automatically creates links:
199 -# @c function_name() can be used to reference functions
200 (e.g. flash_set_dirty()).
201 -# @c struct_name::member_name should be used to reference structure
202 fields in the documentation (e.g. @c flash_driver::name).
203 -# URLS get converted to markup automatically, without any extra effort.
204 -# new pages can be linked into the heirarchy by using the @c \@subpage
205 command somewhere the page(s) under which they should be linked:
206 -# use @c \@ref in other contexts to create links to pages and sections.
207 -# Use good Doxygen mark-up:
208 -# '\@a' (italics) should be used to reference parameters (e.g. <i>foo</i>).
209 -# '\@b' (bold) should be used to emphasizing <b>single</b> words.
210 -# '\@c' (monospace) should be used with <code>file names</code> and
211 <code>code symbols</code>, so they appear visually distinct from
213 -# To mark-up multiple words, the HTML alternatives must be used.
214 -# Two spaces should be used when nesting lists; do @b not use '\\t' in lists.
215 -# Code examples provided in documentation must conform to the Style Guide.
217 @section styledoxytext Doxygen Text Inputs
219 In addition to the guidelines in the preceding sections, the following
220 additional style guidelines should be considered when writing
221 documentation as part of standalone text files:
223 -# Text files must contain Doxygen at least one comment block:
224 -# Documentation should begin in the first column (except for nested lists).
225 -# Do NOT use the @c '*' convention that must be used in the source code.
226 -# Each file should contain at least one @c \@page block.
227 -# Each new page should be listed as a \@subpage in the \@page block
228 of the page that should serve as its parent.
229 -# Large pages should be structure in parts using meaningful \@section
230 and \@subsection commands.
231 -# Include a @c \@file block at the end of each Doxygen @c .txt file to
232 document its contents:
233 - Doxygen creates such pages for files automatically, but no content
234 will appear on them for those that only contain manual pages.
235 - The \@file block should provide useful meta-documentation to assist
236 techincal writers; typically, a list of the pages that it contains.
237 - For example, the @ref styleguide exists in @c doc/manual/style.txt,
238 which contains a reference back to itself.
239 -# The \@file and \@page commands should begin on the same line as
240 the start of the Doxygen comment:
242 /** @page pagename Page Title
244 Documentation for the page.
249 This file contains the @ref pagename page.
254 For an example, the Doxygen source for this Style Guide can be found in
255 @c doc/manual/style.txt, alongside other parts of The Manual.
258 /** @page styletexinfo Texinfo Style Guide
260 The User's Guide is there to provide two basic kinds of information. It
261 is a guide for how and why to use each feature or mechanism of OpenOCD.
262 It is also the reference manual for all commands and options involved
263 in using them, including interface, flash, target, and other drivers.
264 At this time, it is the only user-targetted documentation; everything
265 else is addressing OpenOCD developers.
267 There are two key audiences for the User's Guide, both developer based.
268 The primary audience is developers using OpenOCD as a tool in their
269 work, or who may be starting to use it that way. A secondary audience
270 includes developers who are supporting those users by packaging or
271 customizing it for their hardware, installing it as part of some software
272 distribution, or by evolving OpenOCD itself. There is some crossover
273 between those audiences. We encourage contributions from users as the
274 fundamental way to evolve and improve OpenOCD. In particular, creating
275 a board or target specific configuration file is something that many
276 users will end up doing at some point, and we like to see such files
277 become part of the mainline release.
279 General documentation rules to remember include:
281 - Be concise and clear. It's work to remove those extra words and
282 sentences, but such "noise" doesn't help readers.
283 - Make it easy to skim and browse. "Tell what you're going to say,
284 then say it". Help readers decide whether to dig in now, or
286 - Make sure the chapters flow well. Presentations should not jump
287 around, and should move easily from overview down to details.
288 - Avoid using the passive voice.
289 - Address the reader to clarify roles ("your config file", "the board you
290 are debugging", etc.); "the user" (etc) is artificial.
291 - Use good English grammar and spelling. Remember also that English
292 will not be the first language for many readers. Avoid complex or
293 idiomatic usage that could create needless barriers.
294 - Use examples to highlight fundamental ideas and common idioms.
295 - Don't overuse list constructs. This is not a slide presentation;
298 When presenting features and mechanisms of OpenOCD:
300 - Explain key concepts before presenting commands using them.
301 - Tie examples to common developer tasks.
302 - When giving instructions, you can \@enumerate each step both
303 to clearly delineate the steps, and to highlight that this is
304 not explanatory text.
305 - When you provide "how to use it" advice or tutorials, keep it
306 in separate sections from the reference material.
307 - Good indexing is something of a black art. Use \@cindex for important
308 concepts, but don't overuse it. In particular, rely on the \@deffn
309 indexing, and use \@cindex primarily with significant blocks of text
310 such as \@subsection. The \@dfn of a key term may merit indexing.
311 - Use \@xref (and \@anchor) with care. Hardcopy versions, from the PDF,
312 must make sense without clickable links (which don't work all that well
313 with Texinfo in any case). If you find you're using many links,
314 read that as a symptom that the presentation may be disjointed and
316 - Avoid font tricks like \@b, but use \@option, \@file, \@dfn, \@emph
317 and related mechanisms where appropriate.
319 For technical reference material:
321 - It's OK to start sections with explanations and end them with
322 detailed lists of the relevant commands.
323 - Use the \@deffn style declarations to define all commands and drivers.
324 These will automatically appear in the relevant index, and those
325 declarations help promote consistent presentation and style.
326 - It's a "Command" if it can be used interactively.
327 - Else it's a "Config Command" if it must be used before the
328 configuration stage completes.
329 - For a "Driver", list its name.
330 - Use EBNF style regular expressions to define parameters:
331 brackets around zero-or-one choices, parentheses around
333 - Use \@option, \@file, \@var and other mechanisms where appropriate.
334 - Say what output it displays, and what value it returns to callers.
335 - Explain clearly what the command does. Sometimes you will find
336 that it can't be explained clearly. That usually means the command
337 is poorly designed; replace it with something better, if you can.
338 - Be complete: document all commands, except as part of a strategy
339 to phase something in or out.
340 - Be correct: review the documentation against the code, and
342 - Alphabetize the \@defn declarations for all commands in each
344 - Keep the per-command documentation focussed on exactly what that
345 command does, not motivation, advice, suggestions, or big examples.
346 When commands deserve such expanded text, it belongs elsewhere.
347 Solutions might be using a \@section explaining a cluster of related
348 commands, or acting as a mini-tutorial.
349 - Details for any given driver should be grouped together.
351 The User's Guide is the first place most users will start reading,
352 after they begin using OpenOCD. Make that investment of their time
353 be as productive as possible. Needing to look at OpenOCD source code,
354 to figure out how to use it is a bad sign, though it's OK to need to
355 look at the User's guide to figure out what a config script is doing.
358 /** @page stylelatex LaTeX Style Guide
360 This page needs to provide style guidelines for using LaTeX, the
361 typesetting language used by The References for OpenOCD Hardware.
362 Likewise, the @ref primerlatex for using this guide needs to be completed.
365 /** @page styleperl Perl Style Guide
367 This page provides some style guidelines for using Perl, a scripting
368 language used by several small tools in the tree:
370 -# Ensure all Perl scripts use the proper suffix (@c .pl for scripts, and
372 -# Pass files as script parameters or piped as input:
373 - Do NOT code paths to files in the tree, as this breaks out-of-tree builds.
374 - If you must, then you must also use an automake rule to create the script.
375 -# use @c '#!/usr/bin/perl' as the first line of Perl scripts.
376 -# always <code>use strict</code> and <code>use warnings</code>
377 -# invoke scripts indirectly in Makefiles or other scripts:
382 Maintainers must also be sure to follow additional guidelines:
383 -# Ensure that Perl scripts are committed as executables:
384 Use "<code>chmod +x script.pl</code>"
385 @a before using "<code>git add script.pl</code>"
388 /** @page styleautotools Autotools Style Guide
390 This page contains style guidelines for the OpenOCD autotools scripts.
392 The following guidelines apply to the @c configure.ac file:
393 - Better guidelines need to be developed, but until then...
394 - Use good judgement.
396 The following guidelines apply to @c Makefile.am files:
397 -# When assigning variables with long lists of items:
398 -# Separate the values on each line to make the files "patch friendly":
410 This file contains the @ref styleguide pages. The @ref styleguide pages
411 include the following Style Guides for their respective code and
412 documentation languages:
420 - @ref styleautotools