You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

400 lines
16 KiB

  1. /** @page styleguide Style Guides
  2. The goals for each of these guides are:
  3. - to produce correct code that appears clean, consistent, and readable,
  4. - to allow developers to create patches that conform to a standard, and
  5. - to eliminate these issues as points of future contention.
  6. Some of these rules may be ignored in the spirit of these stated goals;
  7. however, such exceptions should be fairly rare.
  8. The following style guides describe a formatting, naming, and other
  9. conventions that should be followed when writing or changing the OpenOCD
  10. code:
  11. - @subpage styletcl
  12. - @subpage stylec
  13. - @subpage styleperl
  14. - @subpage styleautotools
  15. In addition, the following style guides provide information for
  16. providing documentation, either as part of the C code or stand-alone.
  17. - @subpage styledoxygen
  18. - @subpage styletexinfo
  19. - @subpage stylelatex
  20. Feedback would be welcome to improve the OpenOCD guidelines.
  21. */
  22. /** @page styletcl TCL Style Guide
  23. OpenOCD needs to expand its Jim/TCL Style Guide.
  24. Many of the guidelines listed on the @ref stylec page should apply to
  25. OpenOCD's Jim/TCL code as well.
  26. */
  27. /** @page stylec C Style Guide
  28. This page contains guidelines for writing new C source code for the
  29. OpenOCD project.
  30. @section styleformat Formatting Guide
  31. - remove any trailing white space at the end of lines.
  32. - use TAB characters for indentation; do NOT use spaces.
  33. - displayed TAB width is 4 characters.
  34. - use Unix line endings ('\\n'); do NOT use DOS endings ('\\r\\n')
  35. - limit adjacent empty lines to at most two (2).
  36. - remove any trailing empty lines at the end of source files
  37. - do not "comment out" code from the tree; instead, one should either:
  38. -# remove it entirely (git can retrieve the old version), or
  39. -# use an @c \#if/\#endif block.
  40. Finally, try to avoid lines of code that are longer than than 72-80 columns:
  41. - long lines frequently indicate other style problems:
  42. - insufficient use of static functions, macros, or temporary variables
  43. - poor flow-control structure; "inverted" logical tests
  44. - a few lines may be wider than this limit (typically format strings), but:
  45. - all C compilers will concatenate series of string constants.
  46. - all long string constants should be split across multiple lines.
  47. @section stylenames Naming Rules
  48. - most identifiers must use lower-case letters (and digits) only.
  49. - macros must use upper-case letters (and digits) only.
  50. - OpenOCD identifiers should NEVER use @c MixedCaps.
  51. - structure names must end with the '_s' suffix.
  52. - typedef names must end with the '_t' suffix.
  53. - use underline characters between consecutive words in identifiers
  54. (e.g. @c more_than_one_word).
  55. @section stylec99 C99 Rules
  56. - inline functions
  57. - @c // comments -- in new code, prefer these for single-line comments
  58. - trailing comma allowed in enum declarations
  59. - designated initializers (@{ .field = value @})
  60. - variables declarations may be mixed with code
  61. - new block scopes for selection and iteration statements
  62. @section styletypes Type Guidelines
  63. - use native types (@c int or @c unsigned) if the type is not important
  64. - if size matters, use the types from \<stdint.h\> or \<inttypes.h\>:
  65. - @c int8_t, @c int16_t, @c int32_t, or @c int64_t: signed types of specified size
  66. - @c uint8_t, @c uint16_t, @c uint32_t, or @c uint64_t: unsigned types of specified size
  67. - do @b NOT redefine @c uN types from "types.h"
  68. @section stylefunc Functions
  69. - static inline functions should be prefered over macros:
  70. @code
  71. /** do NOT define macro-like functions like this... */
  72. #define CUBE(x) ((x) * (x) * (x))
  73. /** instead, define the same expression using a C99 inline function */
  74. static inline int cube(int x) { return x * x * x; }
  75. @endcode
  76. - Functions should be declared static unless required by other modules
  77. - define static functions before first usage to avoid forward declarations.
  78. - Functions should have no space between its name and its parameter list:
  79. @code
  80. int f(int x1, int x2)
  81. {
  82. ...
  83. int y = f(x1, x2 - x1);
  84. ...
  85. }
  86. @endcode
  87. - Separate assignment and logical test statements. In other words, you
  88. should write statements like the following:
  89. @code
  90. // separate statements should be preferred
  91. result = foo();
  92. if (ERROR_OK != result)
  93. ...
  94. @endcode
  95. More directly, do @b not combine these kinds of statements:
  96. @code
  97. // Combined statements should be avoided
  98. if (ERROR_OK != (result = foo()))
  99. return result;
  100. @endcode
  101. */
  102. /** @page styledoxygen Doxygen Style Guide
  103. The following sections provide guidelines for OpenOCD developers
  104. who wish to write Doxygen comments in the code or this manual.
  105. For an introduction to Doxygen documentation,
  106. see the @ref primerdoxygen.
  107. @section styledoxyblocks Doxygen Block Selection
  108. Several different types of Doxygen comments can be used; often,
  109. one style will be the most appropriate for a specific context.
  110. The following guidelines provide developers with heuristics for
  111. selecting an appropriate form and writing consistent documentation
  112. comments.
  113. -# use @c /// to for one-line documentation of instances.
  114. -# for documentation requiring multiple lines, use a "block" style:
  115. @verbatim
  116. /**
  117. * @brief First sentence is short description. Remaining text becomes
  118. * the full description block, where "empty" lines start new paragraphs.
  119. *
  120. * One can make text appear in @a italics, @b bold, @c monospace, or
  121. * in blocks such as the one in which this example appears in the Style
  122. * Guide. See the Doxygen Manual for the full list of commands.
  123. *
  124. * @param foo For a function, describe the parameters (e.g. @a foo).
  125. * @returns The value(s) returned, or possible error conditions.
  126. */
  127. @endverbatim
  128. -# The block should start on the line following the opening @c /**.
  129. -# The end of the block, \f$*/\f$, should also be on its own line.
  130. -# Every line in the block should have a @c '*' in-line with its start:
  131. - A leading space is required to align the @c '*' with the @c /** line.
  132. - A single "empty" line should separate the function documentation
  133. from the block of parameter and return value descriptions.
  134. - Except to separate paragraphs of documentation, other extra
  135. "empty" lines should be removed from the block.
  136. -# Only single spaces should be used; do @b not add mid-line indentation.
  137. -# If the total line length will be less than 72-80 columns, then
  138. - The @c /**< form can be used on the same line.
  139. - This style should be used sparingly; the best use is for fields:
  140. @code int field; /**< field description */ @endcode
  141. @section styledoxyall Doxygen Style Guide
  142. The following guidelines apply to all Doxygen comment blocks:
  143. -# Use the @c '\@cmd' form for all doxygen commands (do @b not use @c '\\cmd').
  144. -# Use symbol names such that Doxygen automatically creates links:
  145. -# @c function_name() can be used to reference functions
  146. (e.g. flash_set_dirty()).
  147. -# @c struct_name::member_name should be used to reference structure
  148. fields in the documentation (e.g. @c flash_driver_s::name).
  149. -# URLS get converted to markup automatically, without any extra effort.
  150. -# new pages can be linked into the heirarchy by using the @c \@subpage
  151. command somewhere the page(s) under which they should be linked:
  152. -# use @c \@ref in other contexts to create links to pages and sections.
  153. -# Use good Doxygen mark-up:
  154. -# '\@a' (italics) should be used to reference parameters (e.g. <i>foo</i>).
  155. -# '\@b' (bold) should be used to emphasizing <b>single</b> words.
  156. -# '\@c' (monospace) should be used with <code>file names</code> and
  157. <code>code symbols</code>, so they appear visually distinct from
  158. surrounding text.
  159. -# To mark-up multiple words, the HTML alternatives must be used.
  160. -# Two spaces should be used when nesting lists; do @b not use '\\t' in lists.
  161. -# Code examples provided in documentation must conform to the Style Guide.
  162. @section styledoxytext Doxygen Text Inputs
  163. In addition to the guidelines in the preceding sections, the following
  164. additional style guidelines should be considered when writing
  165. documentation as part of standalone text files:
  166. -# Text files must contain Doxygen at least one comment block:
  167. -# Documentation should begin in the first column (except for nested lists).
  168. -# Do NOT use the @c '*' convention that must be used in the source code.
  169. -# Each file should contain at least one @c \@page block.
  170. -# Each new page should be listed as a \@subpage in the \@page block
  171. of the page that should serve as its parent.
  172. -# Large pages should be structure in parts using meaningful \@section
  173. and \@subsection commands.
  174. -# Include a @c \@file block at the end of each Doxygen @c .txt file to
  175. document its contents:
  176. - Doxygen creates such pages for files automatically, but no content
  177. will appear on them for those that only contain manual pages.
  178. - The \@file block should provide useful meta-documentation to assist
  179. techincal writers; typically, a list of the pages that it contains.
  180. - For example, the @ref styleguide exists in @c doc/manual/style.txt,
  181. which contains a reference back to itself.
  182. -# The \@file and \@page commands should begin on the same line as
  183. the start of the Doxygen comment:
  184. @verbatim
  185. /** @page pagename Page Title
  186. Documentation for the page.
  187. */
  188. /** @file
  189. This file contains the @ref pagename page.
  190. */
  191. @endverbatim
  192. For an example, the Doxygen source for this Style Guide can be found in
  193. @c doc/manual/style.txt, alongside other parts of The Manual.
  194. */
  195. /** @page styletexinfo Texinfo Style Guide
  196. The User's Guide is there to provide two basic kinds of information. It
  197. is a guide for how and why to use each feature or mechanism of OpenOCD.
  198. It is also the reference manual for all commands and options involved
  199. in using them, including interface, flash, target, and other drivers.
  200. At this time, it is the only user-targetted documentation; everything
  201. else is addressing OpenOCD developers.
  202. There are two key audiences for the User's Guide, both developer based.
  203. The primary audience is developers using OpenOCD as a tool in their
  204. work, or who may be starting to use it that way. A secondary audience
  205. includes developers who are supporting those users by packaging or
  206. customizing it for their hardware, installing it as part of some software
  207. distribution, or by evolving OpenOCD itself. There is some crossover
  208. between those audiences. We encourage contributions from users as the
  209. fundamental way to evolve and improve OpenOCD. In particular, creating
  210. a board or target specific configuration file is something that many
  211. users will end up doing at some point, and we like to see such files
  212. become part of the mainline release.
  213. General documentation rules to remember include:
  214. - Be concise and clear. It's work to remove those extra words and
  215. sentences, but such "noise" doesn't help readers.
  216. - Make it easy to skim and browse. "Tell what you're going to say,
  217. then say it". Help readers decide whether to dig in now, or
  218. leave it for later.
  219. - Make sure the chapters flow well. Presentations should not jump
  220. around, and should move easily from overview down to details.
  221. - Avoid using the passive voice.
  222. - Address the reader to clarify roles ("your config file", "the board you
  223. are debugging", etc.); "the user" (etc) is artificial.
  224. - Use good English grammar and spelling. Remember also that English
  225. will not be the first language for many readers. Avoid complex or
  226. idiomatic usage that could create needless barriers.
  227. - Use examples to highlight fundamental ideas and common idioms.
  228. - Don't overuse list constructs. This is not a slide presentation;
  229. prefer paragraphs.
  230. When presenting features and mechanisms of OpenOCD:
  231. - Explain key concepts before presenting commands using them.
  232. - Tie examples to common developer tasks.
  233. - When giving instructions, you can \@enumerate each step both
  234. to clearly delineate the steps, and to highlight that this is
  235. not explanatory text.
  236. - When you provide "how to use it" advice or tutorials, keep it
  237. in separate sections from the reference material.
  238. - Good indexing is something of a black art. Use \@cindex for important
  239. concepts, but don't overuse it. In particular, rely on the \@deffn
  240. indexing, and use \@cindex primarily with significant blocks of text
  241. such as \@subsection. The \@dfn of a key term may merit indexing.
  242. - Use \@xref (and \@anchor) with care. Hardcopy versions, from the PDF,
  243. must make sense without clickable links (which don't work all that well
  244. with Texinfo in any case). If you find you're using many links,
  245. read that as a symptom that the presentation may be disjointed and
  246. confusing.
  247. - Avoid font tricks like \@b, but use \@option, \@file, \@dfn, \@emph
  248. and related mechanisms where appropriate.
  249. For technical reference material:
  250. - It's OK to start sections with explanations and end them with
  251. detailed lists of the relevant commands.
  252. - Use the \@deffn style declarations to define all commands and drivers.
  253. These will automatically appear in the relevant index, and those
  254. declarations help promote consistent presentation and style.
  255. - It's a "Command" if it can be used interactively.
  256. - Else it's a "Config Command" if it must be used before the
  257. configuration stage completes.
  258. - For a "Driver", list its name.
  259. - Use BNF style regular expressions to define parameters:
  260. brackets around zero-or-one choices, parentheses around
  261. exactly-one choices.
  262. - Use \@option, \@file, \@var and other mechanisms where appropriate.
  263. - Say what output it displays, and what value it returns to callers.
  264. - Explain clearly what the command does. Sometimes you will find
  265. that it can't be explained clearly. That usually means the command
  266. is poorly designed; replace it with something better, if you can.
  267. - Be complete: document all commands, except as part of a strategy
  268. to phase something in or out.
  269. - Be correct: review the documentation against the code, and
  270. vice versa.
  271. - Alphabetize the \@defn declarations for all commands in each
  272. section.
  273. - Keep the per-command documentation focussed on exactly what that
  274. command does, not motivation, advice, suggestions, or big examples.
  275. When commands deserve such expanded text, it belongs elsewhere.
  276. Solutions might be using a \@section explaining a cluster of related
  277. commands, or acting as a mini-tutorial.
  278. - Details for any given driver should be grouped together.
  279. The User's Guide is the first place most users will start reading,
  280. after they begin using OpenOCD. Make that investment of their time
  281. be as productive as possible. Needing to look at OpenOCD source code,
  282. to figure out how to use it is a bad sign, though it's OK to need to
  283. look at the User's guide to figure out what a config script is doing.
  284. */
  285. /** @page stylelatex LaTeX Style Guide
  286. This page needs to provide style guidelines for using LaTeX, the
  287. typesetting language used by The References for OpenOCD Hardware.
  288. Likewise, the @ref primerlatex for using this guide needs to be completed.
  289. */
  290. /** @page styleperl Perl Style Guide
  291. This page provides some style guidelines for using Perl, a scripting
  292. language used by several small tools in the tree:
  293. -# Ensure all Perl scripts use the proper suffix (@c .pl for scripts, and
  294. @c .pm for modules)
  295. -# Pass files as script parameters or piped as input:
  296. - Do NOT code paths to files in the tree, as this breaks out-of-tree builds.
  297. - If you must, then you must also use an automake rule to create the script.
  298. -# use @c '#!/usr/bin/perl' as the first line of Perl scripts.
  299. -# always <code>use strict</code> and <code>use warnings</code>
  300. -# invoke scripts indirectly in Makefiles or other scripts:
  301. @code
  302. perl script.pl
  303. @endcode
  304. Maintainers must also be sure to follow additional guidelines:
  305. -# Ensure that Perl scripts are committed as executables:
  306. Use "<code>chmod +x script.pl</code>"
  307. @a before using "<code>git add script.pl</code>"
  308. */
  309. /** @page styleautotools Autotools Style Guide
  310. This page contains style guidelines for the OpenOCD autotools scripts.
  311. The following guidelines apply to the @c configure.in file:
  312. - Better guidelines need to be developed, but until then...
  313. - Use good judgement.
  314. The following guidelines apply to @c Makefile.am files:
  315. -# When assigning variables with long lists of items:
  316. -# Separate the values on each line to make the files "patch friendly":
  317. @code
  318. VAR = \
  319. value1 \
  320. value2 \
  321. ...
  322. value9 \
  323. value10
  324. @endcode
  325. */
  326. /** @file
  327. This file contains the @ref styleguide pages. The @ref styleguide pages
  328. include the following Style Guides for their respective code and
  329. documentation languages:
  330. - @ref styletcl
  331. - @ref stylec
  332. - @ref styledoxygen
  333. - @ref styletexinfo
  334. - @ref stylelatex
  335. - @ref styleperl
  336. - @ref styleautotools
  337. */