C-style.htm 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Ghostscript C coding guidelines</title>
  5. <!-- $Id: C-style.htm,v 1.55 2005/10/20 19:46:23 ray Exp $ -->
  6. <!-- Originally: c-style.txt -->
  7. <link rel="stylesheet" type="text/css" href="gs.css" title="Ghostscript Style">
  8. </head>
  9. <body>
  10. <!-- [1.0 begin visible header] ============================================ -->
  11. <!-- [1.1 begin headline] ================================================== -->
  12. <h1>Ghostscript C coding guidelines</h1>
  13. <!-- [1.1 end headline] ==================================================== -->
  14. <!-- [1.2 begin table of contents] ========================================= -->
  15. <h2>Table of contents</h2>
  16. <blockquote><ul>
  17. <li><a href="#Introduction">Introduction</a>
  18. <li><a href="#C_language">C language do's and don'ts</a>
  19. <ul>
  20. <li>Preprocessor:
  21. <a href="#Conditionals">Conditionals</a>,
  22. <a href="#Macros">Macros</a>,
  23. <a href="#Preprocessor_other">Other</a>
  24. <li><a href="#Lexical_elements">Lexical elements</a>
  25. <li><a href="#Scoping">Scoping</a>
  26. <li>Data types:
  27. <a href="#Scalars">Scalars</a>,
  28. <a href="#Arrays">Arrays</a>,
  29. <a href="#Typedefs">Typedefs</a>,
  30. <a href="#Structures">Structures</a>,
  31. <a href="#Unions">Unions</a>
  32. <li><a href="#Expressions">Expressions</a>
  33. <li><a href="#Statements">Statements</a>
  34. <li><a href="#Procedures">Procedures</a> (prototypes and definitions)
  35. <li><a href="#Standard_library">Standard library</a>
  36. </ul>
  37. <li><a href="#Language_extensions">Language extensions</a>
  38. <li><a href="#Stylistic_conventions">Stylistic conventions</a>
  39. <ul>
  40. <li>Formatting:
  41. <a href="#Indentation">Indentation</a>,
  42. <a href="#Spaces">Spaces</a>,
  43. <a href="#Parentheses">Parentheses</a>
  44. <li><a href="#Preprocessor_style">Preprocessor</a>
  45. <li><a href="#Naming">Naming</a>
  46. <li><a href="#Types">Types</a>
  47. <li><a href="#Procedures_style">Procedures</a>,
  48. <li>Miscellany:
  49. <a href="#Local_variables">Local variables</a>,
  50. <a href="#Compiler_warnings">Compiler warnings</a>
  51. </ul>
  52. <li><a href="#File_structuring">File structuring and naming</a>
  53. <ul>
  54. <li><a href="#All_files">All files</a>
  55. <li><a href="#Makefiles">Makefiles</a>
  56. <li><a href="#General_C_code">General C Code</a>
  57. <li><a href="#Headers">Headers (<b><tt>.h</tt></b> files)</a>
  58. <li><a href="#Source">Source (<b><tt>.c</tt></b> files)</a>
  59. </ul>
  60. <li><a href="#Conventions">Ghostscript conventions</a>
  61. <ul>
  62. <li><a href="#Specific_names">Specific names</a>:
  63. <a href="#code"><b><tt>code</tt></b></a>,
  64. <a href="#status"><b><tt>status</tt></b></a>
  65. <li><a href="#Structure_type_descriptors">Structure type descriptors</a>
  66. <li><a href="#Objects">"Objects"</a>
  67. <li><a href="#Error_handling">Error handling</a>
  68. </ul>
  69. </ul></blockquote>
  70. <!-- [1.2 end table of contents] =========================================== -->
  71. <!-- [1.3 begin hint] ====================================================== -->
  72. <p>
  73. For other information, see the <a href="Readme.htm">Ghostscript
  74. overview</a>.
  75. <!-- [1.3 end hint] ======================================================== -->
  76. <hr>
  77. <!-- [1.0 end visible header] ============================================== -->
  78. <!-- [2.0 begin contents] ================================================== -->
  79. <h2><a name="Introduction"></a>Introduction</h2>
  80. <p>
  81. This document describes Ghostscript's C coding conventions. It is primarily
  82. <em>prescriptive</em>, documenting what developers should do when writing
  83. new code; the companion developer documentation (<a
  84. href="Develop.htm">Develop.htm</a>) is primarily <em>descriptive</em>,
  85. documenting the way things are.
  86. <p>
  87. We encourage following the general language usage and stylistic rules for
  88. any code that will be integrated with Ghostscript, even if the code doesn't
  89. use Ghostscript's run-time facilities or have anything to do with
  90. PostScript, PDF, or page description languages. Ghostscript itself follows
  91. some additional conventions; these are documented separately under "<a
  92. href="#Conventions">Ghostscript conventions</a>" below.
  93. <hr>
  94. <h2><a name="C_language"></a>C language do's and don'ts</h2>
  95. <p>
  96. There are several different versions of the C language, and even of the ANSI
  97. C standard. Ghostscript versions through 7.0 were designed to compile on
  98. pre-ANSI compilers as well as on many compilers that implemented the ANSI
  99. standard with varying faithfulness. Ghostscript versions since 7.0 do not
  100. cater for pre-ANSI compilers: they must conform to the ANSI 1989 standard
  101. (ANS X3.159-1989), with certain restrictions and a few conventional
  102. additions.
  103. <h3>Preprocessor</h3>
  104. <h4><a name="Conditionals"></a>Conditionals</h4>
  105. Restrictions:
  106. <ul>
  107. <li>Don't assume that <b><tt>#if</tt></b> will treat undefined names as 0.
  108. While the ANSI standard requires this, it may produce a warning.
  109. <li>In <b><tt>.c</tt></b> files, don't use preprocessor conditionals that
  110. test for individual platforms or compilers. Use them only in header files
  111. named xxx<b><tt>_.h</tt></b>.
  112. </ul>
  113. <h4><a name="Macros"></a>Macros</h4>
  114. <p>
  115. Restrictions:
  116. <ul>
  117. <li>Don't redefine a macro, even with the same definition, without using
  118. <b><tt>#undef</tt></b>.
  119. <li><b><tt>CAPITALIZE</tt></b> macro names unless there is a good reason not
  120. to.
  121. <li>Even though the legacy code contains some macros which contain
  122. control flow statments, avoid the use of this in new code and do not
  123. create macros which contain hidden control flow, especially 'return'.
  124. The use of control flow in macros complicates debug significantly
  125. requiring tedious expansion of macros to build a module to be debugged
  126. or resorting to disassembly windows to set breakpoints or to trace
  127. flow.
  128. <li>Don't use a macro call within a macro argument if the call expands to a
  129. token sequence that includes any commas not within parentheses: this
  130. produces different results depending on whether the compiler expands the
  131. inner call before or after the argument is substituted into the macro body.
  132. (The ANSI standard says that calls must be expanded after substitution, but
  133. some compilers do it the other way.)
  134. <li>Don't use macro names, even inadvertently, in string constants. Some
  135. compilers erroneously try to expand them.
  136. <li>Don't use macros to define shorthands for casted pointers. For
  137. instance, avoid
  138. <blockquote><b><tt>
  139. #define fdev ((gx_device_fubar *)dev)
  140. </tt></b></blockquote>
  141. <p>
  142. and instead use
  143. <blockquote><b><tt>
  144. gx_device_fubar * const fdev = (gx_device_fubar *)dev;
  145. </tt></b></blockquote>
  146. <p>
  147. The use of <b><tt>const</tt></b> alerts the reader that this is effectively
  148. a synonym.
  149. <li>If a macro generates anything larger than a single expression (that is,
  150. one or more statements), surround it with <b><tt>BEGIN</tt></b> and
  151. <b><tt>END</tt></b>. These work around the fact that simple statements and
  152. compound statements in C can't be substituted for each other syntactically.
  153. <li>If a macro introduces local variables, only use names that end with an
  154. underscore (<b><tt>_</tt></b>), such as <b><tt>code_</tt></b>. This avoids
  155. clashes both with ordinary variable names (which should never end with an
  156. underscore) and with system-defined names (which may begin with an
  157. underscore).
  158. </ul>
  159. <h3><a name="Preprocessor_other"></a>Other</h3>
  160. <p>
  161. Restrictions:
  162. <ul>
  163. <li>Only use <b><tt>#pragma</tt></b> in files that are explicitly identified
  164. as being platform-dependent. Many compilers complain if this is used at
  165. all, and some complain if they don't recognize the specific pragma being
  166. requested (both incorrect according to the ANSI standard).
  167. </ul>
  168. <h3><a name="Lexical_elements"></a>Lexical elements</h3>
  169. <p>
  170. Do not use:
  171. <ul>
  172. <li>ANSI trigraphs (??x)
  173. <li>Nested comments (/* /* */ */) (not ANSI compliant, but often accepted)
  174. <li>Multi-character character constants ('abc')
  175. <li>Wide-character character or string constants (L'x', L"x")
  176. </ul>
  177. <p>
  178. Restrictions:
  179. <ul>
  180. <li>Procedure and static variable names must be 31 characters or less.
  181. <li>Externally visible procedure and variable names must be unique in the
  182. first 23 characters.
  183. </ul>
  184. <h3><a name="Scoping"></a>Scoping (extern, static, ...)</h3>
  185. <p>
  186. Do not use:
  187. <ul>
  188. <li><b><tt>register</tt></b>
  189. </ul>
  190. <p>
  191. Restrictions:
  192. <ul>
  193. <li>Do not allow a global variable (constant) to have more than one
  194. non-<b><tt>extern</tt></b> definition, even though some ANSI C compilers
  195. allow this. Every global constant should have exactly one definition, in a
  196. <b><tt>.c</tt></b> file, and preferably just one <b><tt>extern</tt></b>
  197. declaration, in a header file.
  198. <li>Use <b><tt>private</tt></b> instead of <b><tt>static</tt></b> for
  199. procedures and variables declared at the outermost scope of a file. This
  200. allows making such constructs either visible or invisible to profilers by
  201. changing a single <b><tt>#define</tt></b>.
  202. <li><b><tt>static</tt></b> or <b><tt>private</tt></b> variables must be
  203. <b><tt>const</tt></b> and initialized: non-<b><tt>const</tt></b> statically
  204. allocated variables are incompatible with reentrancy, and we're in the
  205. process of eliminating all of them.
  206. <li>Do not use <b><tt>extern</tt></b> in <b><tt>.c</tt></b> files, only in
  207. <b><tt>.h</tt></b> files, unless you have a very good reason for it (e.g.,
  208. as in <a href="../src/iconf.c">iconf.c</a>). There are too many such
  209. <b><tt>extern</tt></b>s in the code now: we are eliminating them over time.
  210. <li>Do not declare the same name as both <b><tt>static</tt></b>
  211. (<b><tt>private</tt></b>) and non-<b><tt>static</tt></b> within the same
  212. compilation. (Some compilers complain, some do not.) This is especially a
  213. problem for procedures: it is easy to declare a procedure as
  214. <b><tt>private</tt></b> near the beginning of a file and accidentally not
  215. declare it <b><tt>private</tt></b> where it is defined later in the file.
  216. <li>Even though the ANSI standard allows initialized external declarations
  217. (<b><tt>extern&nbsp;int&nbsp;x&nbsp;=&nbsp;0</tt></b>), don't use them.
  218. </ul>
  219. <h3><a name="Scalars"></a>Scalars</h3>
  220. <p>
  221. Restrictions:
  222. <ul>
  223. <li>Avoid using <b><tt>char</tt></b>, except for <b><tt>char&nbsp;*</tt></b>
  224. for a pointer to a string. Don't assume that <b><tt>char</tt></b> is
  225. signed; also don't assume it is unsigned.
  226. <li>Never cast a <b><tt>float</tt></b> to a <b><tt>double</tt></b>
  227. explicitly. ANSI compilers in their default mode do all floating point
  228. computations in double precision, and handle such casts automatically.
  229. <li>Don't use <b><tt>long long</tt></b>: even though it is in the ANSI
  230. standard, not all compilers support it. Use <b><tt>bits64</tt></b> instead
  231. (see below under "<a href="#Language_extensions">Language extensions</a>").
  232. <li>Don't assume anything about whether <b><tt>sizeof(long)</tt></b> is less
  233. than, equal to, or greater than <b><tt>sizeof(ptr)</tt></b>. (However, you
  234. can make such comparisons in preprocessor conditionals using
  235. <b><tt>ARCH_SIZEOF_LONG</tt></b> and <b><tt>ARCH_SIZEOF_PTR</tt></b>.)
  236. </ul>
  237. <h3><a name="Arrays"></a>Arrays</h3>
  238. <p>
  239. Restrictions:
  240. <ul>
  241. <li>Don't declare arrays of size 0. (The newer ANSI standard allows this,
  242. but the older one doesn't.)
  243. <li>Don't declare an array of size 1 at the end of a structure to indicate
  244. that a variable-size array follows.
  245. <li>Don't declare initialized <b><tt>auto</tt></b> arrays.
  246. </ul>
  247. <h3><a name="Typedefs"></a>Typedefs</h3>
  248. <p>
  249. Restrictions:
  250. <ul>
  251. <li>Don't use <b><tt>typedef</tt></b> for function types, such as
  252. <blockquote>
  253. <b><tt>typedef int proc_xyz_t(double, int *);</tt></b>
  254. </blockquote>
  255. <p>Many compilers don't handle this correctly -- they will give errors, or
  256. do the wrong thing, when declaring variables of type
  257. <b><tt>proc_xyz_t</tt></b> and/or <b><tt>proc_xyz_t *</tt></b>. Instead, do
  258. this:
  259. <blockquote>
  260. <b><tt>#define PROC_XYZ(proc) int proc(double, int *)<br>
  261. PROC_XYZ(some_proc); /* declare a procedure of this type */<br>
  262. typedef PROC_XYZ((*proc_xyz_ptr_t)); /* define a type for procedure ptrs */<br>
  263. <br>
  264. proc_xyz_ptr_t pp; /* pointer to procedure */</tt></b>
  265. </blockquote>
  266. <li>Don't redefine <b><tt>typedef</tt></b>'ed names, even with the same
  267. definition. Some compilers complain about this, and the standard doesn't
  268. allow it.
  269. </ul>
  270. <h3><a name="Structures"></a>Structures</h3>
  271. <p>
  272. Restrictions:
  273. <ul>
  274. <li>Don't use anonymous structures if you can possibly avoid it, except
  275. occasionally as components of other structures. Ideally, use the
  276. <b><tt>struct</tt></b> keyword only for declaring named structure types,
  277. like this:
  278. <blockquote>
  279. <b><tt>typedef struct xxx_s {</tt></b><br>
  280. &nbsp;&nbsp;&nbsp;... members ...<br>
  281. <b><tt>} xxx_t;</tt></b>
  282. </blockquote>
  283. <li>Use <b><tt>struct</tt></b> only when declaring structure types, never
  284. for referring to them (e.g., never declare a variable as type
  285. <b><tt>struct&nbsp;xxx_s&nbsp;*</tt></b>).
  286. <li>Don't assume that the compiler will (or won't) insert padding in
  287. structures to align members for best performance. To preserve alignment,
  288. only declare structure members that are narrower than an <b><tt>int</tt></b>
  289. if there will be a lot of instances of that structure in memory. For such
  290. structures, insert <b><tt>byte</tt></b> and/or <b><tt>short</tt></b> padding
  291. members as necessary to re-establish <b><tt>int</tt></b> alignment.
  292. <li>Don't declare initialized <b><tt>auto</tt></b> structures.
  293. </ul>
  294. <h3><a name="Unions"></a>Unions</h3>
  295. <p>
  296. Restrictions:
  297. <ul>
  298. <li>Use unions only as components of structures, not as typedefs in their
  299. own right.
  300. <li>Don't attempt to initialize unions: not all compilers support this, even
  301. though it is in the 1989 ANSI standard.
  302. </ul>
  303. <h3><a name="Expressions"></a>Expressions</h3>
  304. <p>
  305. Restrictions:
  306. <ul>
  307. <li>Don't assign a larger integer data type to a smaller one without a cast
  308. (<b><tt>int_x&nbsp;=&nbsp;long_y</tt></b>).
  309. <li>It's OK to use the address of a structure or array element
  310. (<b><tt>&p->e</tt></b>, <b><tt>&a[i]</tt></b>) locally, or pass it to a
  311. procedure that won't store it, but don't store such an address in allocated
  312. storage unless you're very sure of what you're doing.
  313. <li>Don't use conditional expressions with structure or union values.
  314. (Pointers to structures or unions are OK.)
  315. <li>For calling a variable or parameter procedure, use
  316. <b><tt>ptr-&gt;func(...)</tt></b>. Some old code uses explicit indirection,
  317. <b><tt>(*ptr-&gt;func)(...)</tt></b>: don't use this in new code.
  318. <li>Don't write expressions that depend on order of evaluation, unless the
  319. order is created explicitly by use of <b><tt>||</tt></b>,
  320. <b><tt>&amp;&amp;</tt></b>, <b><tt>?:</tt></b>, <b><tt>,</tt></b>, or
  321. function nesting (the arguments of a function must be evaluated before the
  322. function is called). In particular, don't assume that the arguments of a
  323. function will be evaluated left-to-right, or that the left side of an
  324. assignment will be evaluated before the right.
  325. <li>Don't mix integer and enumerated types ad lib: treat enumerated types as
  326. distinct from integer types, and use casts to convert between the two.
  327. (Some compilers generate warnings if you do not do this.)
  328. </ul>
  329. <h3><a name="Statements"></a>Statements</h3>
  330. <p>
  331. Restrictions:
  332. <ul>
  333. <li>If you use an expression as a statement, other than an assignment or a
  334. function call with <b><tt>void</tt></b> return value, enclose it explicitly
  335. in <b><tt>DISCARD()</tt></b>.
  336. <li>The type of the operand of a <b><tt>switch</tt></b> must match the type
  337. of the case labels, whether the labels are <b><tt>int</tt></b>s or the
  338. members of an <b><tt>enum</tt></b> type. (Use a cast if necessary.)
  339. <li>It is OK for one case of a switch to "fall through" into another (i.e.,
  340. for the statement just before a case label not to be a control transfer),
  341. but a comment <b><tt>/*&nbsp;falls&nbsp;through&nbsp;*/</tt></b> is
  342. required.
  343. <li>If you are returning an error code specified explicitly (e.g.,
  344. <b><tt>return&nbsp;gs_error_rangecheck</tt></b> or
  345. <b><tt>return&nbsp;e_rangecheck</tt></b>), use
  346. <b><tt>return_error()</tt></b> rather than plain <b><tt>return</tt></b>.
  347. However, if the program is simply propagating an error code generated
  348. elsewhere, as opposed to generating the error, use <b><tt>return</tt></b>
  349. (e.g., <b><tt>if&nbsp;(code&nbsp;<&nbsp;0)&nbsp;return&nbsp;code</tt></b>).
  350. </ul>
  351. <h3><a name="Procedures"></a>Procedures</h3>
  352. <p>
  353. Restrictions:
  354. <ul>
  355. <li>Provide a prototype for every procedure, and make sure the prototype is
  356. available at every call site. If the procedure is local to a file
  357. (<b><tt>private</tt></b>), the prototype should precede the procedure, in
  358. the same file; if the procedure is global, the prototype should be in a
  359. header file.
  360. <li>If a procedure parameter is itself a procedure, do list its parameter
  361. types rather than just using <b><tt>()</tt></b>. For example,
  362. <blockquote><b><tt>
  363. int foo(int (*callback)(int, int));
  364. </tt></b></blockquote>
  365. <p>
  366. rather than just
  367. <blockquote><b><tt>
  368. int foo(int (*callback)());
  369. </tt></b></blockquote>
  370. <li>Don't use the <b><tt>P</tt></b>* macros in new code. (See the
  371. Procedures section of <a href="#Language_extensions">Language extensions</a>
  372. below for more information.)
  373. <li>Always provide an explicit return type for procedures, in both the
  374. prototype and the definition: don't rely on the implicit declaration as
  375. <b><tt>int</tt></b>.
  376. <li>Don't use <b><tt>float</tt></b> as the return type of a procedure,
  377. unless there's a special reason. Floating point hardware typically does
  378. everything in double precision internally and has to do extra work to
  379. convert between double and single precision.
  380. <li>Don't declare parameters as being of type <b><tt>float</tt></b>,
  381. <b><tt>short</tt></b>, or <b><tt>char</tt></b>. If you do this and forget
  382. to include the prototype at a call site, ANSI compilers will generate
  383. incompatible calling sequences. Use <b><tt>floatp</tt></b> (a synonym for
  384. <b><tt>double</tt></b>, mnemonic for "float parameter") instead of
  385. <b><tt>float</tt></b>, and use <b><tt>int</tt></b> or <b><tt>uint</tt></b>
  386. instead of <b><tt>short</tt></b> or <b><tt>char</tt></b>.
  387. </ul>
  388. <h3><a name="Standard_library"></a>Standard library</h3>
  389. <p>
  390. Restrictions:
  391. <ul>
  392. <li>Only use library features that are documented in the established ANSI
  393. standard (e.g., Harbison & Steele's book). Do not use procedures that are
  394. "standards" promulgated by Microsoft (e.g., <b><tt>stricmp</tt></b>), or
  395. originate in BSD Unix (e.g., <b><tt>strcasecmp</tt></b>), or were added in
  396. later versions of the standard such as C 9X.
  397. <li>Do not use any features from <b><tt>stdio.h</tt></b> that assume the
  398. existence of <b><tt>stdin</tt></b>, <b><tt>stdout</tt></b>, or
  399. <b><tt>stderr</tt></b>. See <a href="../src/gsio.h">gsio.h</a> for the full
  400. list. Instead, use <b><tt>gs_stdin</tt></b> et al.
  401. </ul>
  402. <hr>
  403. <h2><a name="Language_extensions"></a>Language extensions</h2>
  404. <h3>Scoping</h3>
  405. <dl>
  406. <dt><b><tt>inline</tt></b>
  407. <dd><b><tt>inline</tt></b> is available even if the compiler does not
  408. support it. Be aware, however, that it may have no effect. In particular,
  409. do not use <b><tt>inline</tt></b> in header files. Instead, use the
  410. <b><tt>extern_inline</tt></b> facility described just below.
  411. <dt><b><tt>extern_inline</tt></b>
  412. <dd>Compilers that do support <b><tt>inline</tt></b> vary in how they decide
  413. whether to (also) compile a closed-code copy of the procedure. Because of
  414. this, putting an <b><tt>inline</tt></b> procedure in a header file may
  415. produce multiple closed copies, causing duplicate name errors at link time.
  416. <b><tt>extern_inline</tt></b> provides a safe way to put
  417. <b><tt>inline</tt></b> procedures in header files, regardless of compiler.
  418. Unfortunately, the only way we've found to make this fully portable involves
  419. a fair amount of boilerplate. For details, please see <a
  420. href="../src/stdpre.h">stdpre.h</a>.
  421. <dt><b><tt>private</tt></b>
  422. <dd><b>Use <tt>private</tt></b> instead of <b><tt>static</tt></b> for all
  423. file-local procedures, and also for file-local variables defined at the
  424. outermost level. However, use <b><tt>static</tt></b>, not
  425. <b><tt>private</tt></b>, for variables defined within a procedure.
  426. <p>
  427. <b><tt>private</tt></b> is normally #define'd as <b><tt>static</tt></b>.
  428. However, it can also be #define'd as empty, which allows profilers to
  429. measure all procedures, and 'nm' to list all interesting statically
  430. allocated variables, not just public ones.
  431. </dl>
  432. <h3>Scalar types</h3>
  433. <dl>
  434. <dt><b><tt>bool, true, false</tt></b>
  435. <dd><b><tt>bool</tt></b> is intended as a Boolean type, with canonical
  436. values <b><tt>true</tt></b> and <b><tt>false</tt></b>. In a more reasonable
  437. language, such as Java, <b><tt>bool</tt></b> is an enumerated type requiring
  438. an explicit cast to or from <b><tt>int</tt></b>; however, because C's
  439. conditionals are defined as producing <b><tt>int</tt></b> values, we can't
  440. even define <b><tt>bool</tt></b> as a C <b><tt>enum</tt></b> without
  441. provoking compiler warnings.
  442. <p>
  443. Even though <b><tt>bool</tt></b> is a synonym for <b><tt>int</tt></b>, treat
  444. them as conceptually different types:
  445. <ul>
  446. <li>Initialize or set <b><tt>bool</tt></b> variables to <b><tt>true</tt></b>
  447. or <b><tt>false</tt></b>, not 0 or 1.
  448. <li>Use the Boolean operators <b><tt>!</tt></b>, <b><tt>&&</tt></b>,
  449. and <b><tt>||</tt></b> only with Booleans. Don't use the idiom
  450. <b><tt>!!x</tt></b> to create a Boolean that is true iff <b><tt>x</tt></b>
  451. != 0: use <b><tt>x != 0</tt></b>.
  452. <li>Use an explicit <b><tt>(int)</tt></b> cast to convert a Boolean to an
  453. integer.
  454. </ul>
  455. <dt><b><tt>byte, ushort, uint, ulong</tt></b>
  456. <dd>These types are simply shorthands for <b><tt>unsigned char, short, int,
  457. long</tt></b>.
  458. <p>
  459. In addition, the use of <b><tt>byte *</tt></b> indicates a
  460. Ghostscript-style string, with explicit length given separately, as
  461. opposed to a null terminated C-style string, which is <b><tt>char
  462. *</tt></b>.
  463. <dt><b><tt>floatp</tt></b>
  464. <dd>This is a synonym for <b><tt>double</tt></b>. It should be used for,
  465. and only for, procedure parameters that would otherwise be
  466. <b><tt>float</tt></b>. (As noted above, procedure parameters should not be
  467. declared as <b><tt>float</tt></b>.)
  468. <dt><b><tt>bits8, bits16, bits32</tt></b>
  469. <dd>These are unsigned integer types of the given width. Use them wherever
  470. the actual width matters: do <em>not</em>, for example, use
  471. <b><tt>short</tt></b> assuming that it is 16 bits wide.
  472. <dt><b><tt>bits64</tt></b>
  473. <dd><strong>****** NOT IMPLEMENTED YET ******</strong>
  474. This is an unsigned 64-bit integer type, but it may not be available on all
  475. platforms. Any code that uses this type should be surrounded by
  476. <b><tt>#if&nbsp;ARCH_HAS_BITS64</tt></b>.
  477. </dl>
  478. <hr>
  479. <h2><a name="Stylistic_conventions"></a>Stylistic conventions</h2>
  480. <p>
  481. Ghostscript's coding rules cover not only the use of the language, but also
  482. many stylistic issues like the choice of names and the use of whitespace.
  483. The stylistic rules are meant to produce code that is easy to read. It's
  484. important to observe them as much as possible in order to maintain a
  485. consistent style, but if you find these rules getting in your way or
  486. producing ugly-looking results once in a while, it's OK to break it.
  487. <h3><a name="Formatting"></a>Formatting</h3>
  488. <h4><a name="Indentation"></a>Indentation</h4>
  489. <p>
  490. We've formatted all of our code using the GNU <b><tt>indent</tt></b> program.
  491. <blockquote><b><tt>
  492. indent&nbsp;-bad&nbsp;-nbap&nbsp;-nsob&nbsp;-br&nbsp;-ce&nbsp;-cli4&nbsp;-npcs&nbsp;-ncs&nbsp;\<br>
  493. &nbsp;&nbsp;&nbsp;-i4&nbsp;-di0&nbsp;-psl&nbsp;-lp&nbsp;-lps&nbsp;somefile.c
  494. </tt></b></blockquote>
  495. <p>
  496. does a 98% accurate job of producing our preferred style. Unfortunately,
  497. there are bugs in all versions of GNU <b><tt>indent</tt></b>, requiring
  498. both pre- and post-processing of the code. The <b><tt>gsindent</tt></b>
  499. script in the Ghostscript fileset contains the necessary workarounds.
  500. <p>
  501. Put indentation points every 4 spaces, with 8 spaces = 1 tab stop.
  502. <p>
  503. Don't indent the initial <b><tt>#</tt></b> of preprocessor commands.
  504. However, for nested preprocessor commands, do use indentation between the
  505. <b><tt>#</tt></b> and the command itself. Use 2 spaces per level of
  506. nesting, e.g.:
  507. <blockquote>
  508. <b><tt>#ifndef&nbsp;xyz</tt></b><br>
  509. <b><tt>#&nbsp;&nbsp;define&nbsp;xyz&nbsp;0</tt></b><br>
  510. <b><tt>#endif</tt></b>
  511. </blockquote>
  512. <p>
  513. For assignments (including chain assignments), put the entire statement on
  514. one line if it will fit; if not, break it after a <b><tt>=</tt></b> and
  515. indent all the following lines. I.e., format like this:
  516. <blockquote>
  517. var1&nbsp;<b><tt>=</tt></b>&nbsp;value<b><tt>;</tt></b><br>
  518. var1&nbsp;<b><tt>=</tt></b>&nbsp;var2&nbsp;<b><tt>=</tt></b>&nbsp;value<b><tt>;</tt></b><br>
  519. var1&nbsp;<b><tt>=</tt></b><br>
  520. &nbsp;&nbsp;&nbsp;&nbsp;value<b><tt>;</tt></b><br>
  521. var1&nbsp;<b><tt>=</tt></b><br>
  522. &nbsp;&nbsp;&nbsp;&nbsp;var2&nbsp;<b><tt>=</tt></b>&nbsp;value<b><tt>;</tt></b><br>
  523. var1&nbsp;<b><tt>=</tt></b>&nbsp;var2&nbsp;<b><tt>=</tt></b><br>
  524. &nbsp;&nbsp;&nbsp;&nbsp;value<b><tt>;</tt></b>
  525. </blockquote>
  526. <p>
  527. But not like this:
  528. <blockquote>
  529. var1&nbsp;<b><tt>=</tt></b><br>
  530. var2&nbsp;<b><tt>=</tt></b> value<b><tt>;</tt></b>
  531. </blockquote>
  532. <p>
  533. Indent in-line blocks thus:
  534. <blockquote>
  535. <b><tt>{</tt></b><br>
  536. &nbsp;&nbsp;&nbsp;... declarations ...<br>
  537. &nbsp;&nbsp;&nbsp;{{ blank line if any declarations above }}<br>
  538. &nbsp;&nbsp;&nbsp;... statements ...<br>
  539. <b><tt>}</tt></b>
  540. </blockquote>
  541. <p>
  542. Similarly, indent procedures thus:
  543. <blockquote>
  544. return_type<br>
  545. proc_name(... arguments ...)<br>
  546. <b><tt>{</tt></b><br>
  547. &nbsp;&nbsp;&nbsp;... declarations ...<br>
  548. &nbsp;&nbsp;&nbsp;{{ blank line if any declarations above }}<br>
  549. &nbsp;&nbsp;&nbsp;... statements ...<br>
  550. <b><tt>}</tt></b>
  551. </blockquote>
  552. <p>
  553. If a control construct (<b><tt>if</tt></b>, <b><tt>do</tt></b>,
  554. <b><tt>while</tt></b>, or <b><tt>for</tt></b>) has a one-line body, use
  555. this:
  556. <blockquote>
  557. ... control construct ...<br>
  558. &nbsp;&nbsp;&nbsp;... subordinate simple statement ...
  559. </blockquote>
  560. <p>
  561. If it has a multi-line body, use this:
  562. <blockquote>
  563. ... control construct ... <b><tt>{</tt></b><br>
  564. &nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  565. <b><tt>}</tt></b>
  566. </blockquote>
  567. <p>
  568. If the subordinate code has declarations, see blocks above.
  569. <p>
  570. For if-else statements, do this:
  571. <blockquote>
  572. <b><tt>if (</tt></b> ...<b><tt> ) {</tt></b><br>
  573. &nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  574. <b><tt>} else if (</tt></b> ...<b><tt> ) {</tt></b><br>
  575. &nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  576. <b><tt>} else {</tt></b><br>
  577. &nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  578. <b><tt>}</tt></b>
  579. </blockquote>
  580. <p>
  581. When there are more than two alternatives, as in the example above, use the
  582. above ("parallel") syntax rather than the following ("nested") syntax:
  583. <blockquote>
  584. <b><tt>if (</tt></b> ...<b><tt> ) {</tt></b><br>
  585. &nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  586. <b><tt>} else {</tt></b><br>
  587. <b><tt>&nbsp;&nbsp;&nbsp;if (</tt></b> ...<b><tt> ) {</tt></b><br>
  588. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  589. <b><tt>&nbsp;&nbsp;&nbsp;} else {</tt></b><br>
  590. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  591. <b><tt>&nbsp;&nbsp;&nbsp;}</tt></b><br>
  592. <b><tt>}</tt></b>
  593. </blockquote>
  594. <p>
  595. Similarly, for do-while statements, do this:
  596. <blockquote>
  597. <b><tt>do {</tt></b><br>
  598. &nbsp;&nbsp;&nbsp;... body ...<br>
  599. <b><tt>} while (</tt></b> ... condition ... <b><tt>);</tt></b>
  600. </blockquote>
  601. <h4><a name="Spaces"></a>Spaces</h4>
  602. <p>
  603. Do put a space:
  604. <ul>
  605. <li>after every comma and semicolon, unless it ends a line;
  606. <li>around every binary operator other than "<b><tt>-&gt;</tt></b>" and
  607. "<b><tt>.</tt></b>", although you can omit the spaces around the innermost
  608. operator in a nested expression if you like;
  609. <li>on both sides of the parentheses of an <b><tt>if</tt></b>, <b><tt>for</tt></b>, or <b><tt>while</tt></b>.
  610. </ul>
  611. <p>
  612. Don't put a space:
  613. <ul>
  614. <li>at the end of a line;
  615. <li>before a comma or semicolon;
  616. <li>after unary prefix operators;
  617. <li>before the parenthesis of a macro or procedure call.
  618. </ul>
  619. <h4><a name="Parentheses"></a>Parentheses</h4>
  620. <p>
  621. Parentheses are important in only a few places:
  622. <ul>
  623. <li>Around the inner subexpressions in expressions that mix
  624. <b><tt>&amp;&amp;</tt></b> and <b><tt>||</tt></b>, even if they are not
  625. required by precedence, for example
  626. <blockquote><b><tt>
  627. (xx &amp;&amp; yy) || zz
  628. </tt></b></blockquote>
  629. <li>Similarly around inner subexpressions in expressions that mix
  630. <b><tt>&amp;</tt></b>, <b><tt>|</tt></b>, or shifts, especially if mixing
  631. these with other operators, for instance
  632. <blockquote><b><tt>
  633. (x &lt;&lt; 3) | (y &gt;&gt; 5)
  634. </tt></b></blockquote>
  635. <li>In macro definitions around every use of an argument that logically
  636. could be an expression, for example
  637. <blockquote><b><tt>
  638. ((x) * (x) + (y) * (y))
  639. </tt></b></blockquote>
  640. </ul>
  641. <p>
  642. Anywhere else, given the choice, use fewer parentheses.
  643. <p>
  644. For stylistic consistency with the existing Ghostscript code, put
  645. parentheses around conditional expressions even if they aren't
  646. syntactically required, unless you really dislike doing this. Note that
  647. the parentheses should go around the entire expression, not the condition.
  648. For instance, instead of
  649. <blockquote><b><tt>
  650. hpgl_add_point_to_path(pgls, arccoord_x, arccoord_y,<br>
  651. &nbsp;&nbsp;&nbsp;(pgls-&gt;g.pen_down) ? gs_lineto : gs_moveto);
  652. </tt></b></blockquote>
  653. <p>
  654. use
  655. <blockquote><b><tt>
  656. hpgl_add_point_to_path(pgls, arccoord_x, arccoord_y,<br>
  657. &nbsp;&nbsp;&nbsp;(pgls-&gt;g.pen_down ? gs_lineto : gs_moveto));
  658. </tt></b></blockquote>
  659. <h3><a name="Preprocessor_style"></a>Preprocessor</h3>
  660. <h4>Conditionals</h4>
  661. <p>
  662. Using preprocessor conditionals can easily lead to unreadable code, since
  663. the eye really wants to read linearly rather than having to parse the
  664. conditionals just to figure out what code is relevant. It's OK to use
  665. conditionals that have small scope and that don't change the structure or
  666. logic of the program (typically, they select between different sets of
  667. values depending on some configuration parameter), but where possible, break
  668. up source modules rather than use conditionals that affect the structure or
  669. logic.
  670. <h4>Macros</h4>
  671. <p>
  672. Ghostscript code uses macros heavily to effectively extend the rather
  673. weak abstraction capabilities of the C language, specifically in the area of
  674. memory management and garbage collection: in order to read Ghostscript code
  675. effectively, you simply have to learn some of these macros as though they
  676. were part of the language. The current code also uses macros heavily for
  677. other purposes, but we are trying to phase these out as rapidly as possible,
  678. because they make the code harder to read and debug, and to use the
  679. rules that follow consistently in new code.
  680. <p>
  681. Define macros in the smallest scope you can manage (procedure, file, or
  682. <b><tt>.h</tt></b> file), and <b><tt>#undef</tt></b> them at the end of
  683. that scope: that way, someone reading the code can see the definitions
  684. easily when reading the uses. If that isn't appropriate, define them in as
  685. large a scope as possible, so that they effectively become part of the
  686. language. This places an additional burden on the reader, but it can be
  687. amortized over reading larger amounts of code.
  688. <p>
  689. Try hard to use procedures instead of macros. Use "<b><tt>inline</tt></b>"
  690. if you really think the extra speed is needed, but only within a
  691. <b><tt>.c</tt></b> file: don't put inline procedures in <b><tt>.h</tt></b>
  692. files, because most compilers don't honor "<b><tt>inline</tt></b>" and
  693. you'll wind up with a copy of the procedure in every <b><tt>.c</tt></b>
  694. file that includes the <b><tt>.h</tt></b> file.
  695. <p>
  696. If you define a macro that looks like a procedure, make sure it will work
  697. wherever a procedure will work. In particular, put parentheses around every
  698. use of an argument within the macro body, so that the macro will parse
  699. correctly if some of the arguments are expressions, and put parentheses
  700. around the entire macro body. (This is still subject to the problem that an
  701. argument may be evaluated more than once, but there is no way around this in
  702. C, since C doesn't provide for local variables within expressions.)
  703. <p>
  704. If you define macros for special loop control structures, make their uses
  705. look somewhat like ordinary loops, for instance:
  706. <blockquote>
  707. <b><tt>BEGIN_RECT(xx, yy) {</tt></b><br>
  708. &nbsp;&nbsp;... body indented one position ...<br>
  709. <b><tt>} END_RECT(xx, yy);</tt></b>
  710. </blockquote>
  711. <p>
  712. If at all possible, don't use free variables in macros -- that is, variables
  713. that aren't apparent as arguments of the macro. If you must use free
  714. variables, list them all in a comment at the point where the macro is
  715. defined.
  716. <p>
  717. If you define new macros or groups of macros, especially if they aren't
  718. simply inline procedures or named constant values, put some extra effort
  719. into documenting them, to compensate for the fact that macros are
  720. intrinsically harder to understand than procedures.
  721. <h3><a name="Comments"></a>Comments</h3>
  722. <p>
  723. The most important descriptive comments are ones in header files that
  724. describe structures, including invariants; but every procedure or structure
  725. declaration, or group of other declarations, should have a comment. Don't
  726. spend a lot of time commenting executable code unless something unusual or
  727. subtle is going on.
  728. <h3><a name="Naming"></a>Naming</h3>
  729. <p>
  730. Use fully spelled-out English words in names, rather than contractions.
  731. This is most important for procedure and macro names, global variables and
  732. constants, values of <b><tt>#define</tt></b> and <b><tt>enum</tt></b>,
  733. <b><tt>struct</tt></b> and other <b><tt>typedef</tt></b> names, and
  734. structure member names, and for argument and variable names which have
  735. uninformative types like <b><tt>int</tt></b>. It's not very important for
  736. arguments or local variables of distinctive types, or for local index or
  737. count variables.
  738. <p>
  739. Avoid names that run English words together:
  740. "<b><tt>hpgl_compute_arc_center</tt></b>" is better than
  741. "<b><tt>hpgl_compute_arccenter</tt></b>". However, for terms drawn from
  742. some predefined source, like the names of PostScript operators, use a term
  743. in its well-known form (for instance, <b><tt>gs_setlinewidth</tt></b>
  744. rather than <b><tt>gs_set_line_width</tt></b>).
  745. <p>
  746. Procedures, variables, and structures visible outside a single
  747. <b><tt>.c</tt></b> file should generally have prefixes that indicate what
  748. subsystem they belong to (in the case of Ghostscript, <b><tt>gs_</tt></b>
  749. or <b><tt>gx_</tt></b>). This rule isn't followed very consistently.
  750. <h3><a name="Types"></a>Types</h3>
  751. <p>
  752. Many older structure names don't have <b><tt>_t</tt></b> on the end, but
  753. this suffix should be used in all new code. (The <b><tt>_s</tt></b>
  754. structure name is needed only to satisfy some debuggers. No code other than
  755. the structure declaration should refer to it.)
  756. <p>
  757. Declare structure types that contain pointers to other instances of
  758. themselves like this:
  759. <blockquote>
  760. <b><tt>typedef struct xxx_s xxx_t;</tt></b><br>
  761. <b><tt>struct xxx_s {</tt></b><br>
  762. &nbsp;&nbsp;&nbsp;... members ...<br>
  763. &nbsp;&nbsp;&nbsp;<b><tt>xxx_t *</tt></b>ptr_member_name;<br>
  764. &nbsp;&nbsp;&nbsp;... members ...<br>
  765. <b><tt>};</tt></b>
  766. </blockquote>
  767. <p>
  768. If, to maintain data abstraction and avoid including otherwise unnecessary
  769. header files, you find that you want the type <b><tt>xxx_t</tt></b> to be
  770. available in a header file that doesn't include the definition of the
  771. structure <b><tt>xxx_s</tt></b>, use this approach:
  772. <blockquote>
  773. <b><tt>#ifndef xxx_DEFINED</tt></b><br>
  774. <b><tt>#&nbsp;&nbsp;define xxx_DEFINED</tt></b><br>
  775. <b><tt>typedef struct xxx_s xxx_t;</tt></b><br>
  776. <b><tt>#endif</tt></b><br>
  777. <b><tt>struct xxx_s {</tt></b><br>
  778. &nbsp;&nbsp;&nbsp;... members ...<br>
  779. <b><tt>};</tt></b>
  780. </blockquote>
  781. <p>
  782. You can then copy the first 4 lines in other header files. (Don't ever
  783. include them in an executable code file.)
  784. <p>
  785. Don't bother using <b><tt>const</tt></b> for anything other than with
  786. pointers as described below. However, in those places where it is necessary
  787. to cast a pointer of type <b><tt>const&nbsp;T&nbsp;*</tt></b> to type
  788. <b><tt>T&nbsp;*</tt></b>, always include a comment that explains why you are
  789. "breaking const".
  790. <h4>Pointers</h4>
  791. <p>
  792. Use <b><tt>const</tt></b> for pointer referents (that is,
  793. <b><tt>const&nbsp;T&nbsp;*</tt></b>) wherever possible and appropriate.
  794. <p>
  795. If you find yourself wanting to use <b><tt>void&nbsp;*</tt></b>, try to
  796. find an alternative using unions or (in the case of super- and subclasses)
  797. casts, unless you're writing something like a memory manager that really
  798. treats memory as opaque.
  799. <h3><a name="Procedures_style"></a>Procedures</h3>
  800. <p>
  801. In general, don't create procedures that are private and only called from
  802. one place. However, if a compound statement (especially an arm of a
  803. conditional) is too long for the eye easily to match its enclosing braces
  804. "<b><tt>{...}</tt></b>" -- that is, longer than 10 or 15 lines -- and it
  805. doesn't use or set a lot of state held in outer local variables, it may be
  806. more readable if you put it in a procedure.
  807. <h3>Miscellany</h3>
  808. <h4><a name="Local_variables"></a>Local variables</h4>
  809. <p>
  810. Don't assign new values to procedure parameters. It makes debugging very
  811. confusing when the parameter values printed for a procedure are not the
  812. ones actually supplied by the caller. Instead use a separate local
  813. variable initialized to the value of the parameter.
  814. <p>
  815. If a local variable is only assigned a value once, assign it that value at
  816. its declaration, if possible. For example,
  817. <blockquote>
  818. <b><tt>int x = </tt></b>some expression <b><tt>;</tt></b>
  819. </blockquote>
  820. <p>
  821. rather than
  822. <blockquote>
  823. <b><tt>int x;</tt></b><br>
  824. ...<br>
  825. <b><tt>x = </tt></b> some expression <b><tt>;</tt></b>
  826. </blockquote>
  827. <p>
  828. Use a local pointer variable like this to "narrow" pointer types:
  829. <blockquote>
  830. <b><tt>int</tt></b><br>
  831. someproc(... <b><tt>gx_device *dev</tt></b> ...)<br>
  832. <b><tt>{<br>
  833. &nbsp;&nbsp;&nbsp;gx_device_printer *const pdev = (gx_device_printer *)dev;</tt></b><br>
  834. &nbsp;&nbsp;&nbsp;...<br>
  835. <b><tt>}</tt></b>
  836. </blockquote>
  837. <p>
  838. Don't "shadow" a local variable or procedure parameter with an inner local
  839. variable of the same name. I.e., don't do this:
  840. <blockquote>
  841. <b><tt>int</tt></b><br>
  842. someproc(... <b><tt>int x</tt></b> ...)<br>
  843. <b><tt>{</tt></b><br>
  844. &nbsp;&nbsp;&nbsp;...<br>
  845. <b><tt>&nbsp;&nbsp;&nbsp;int x;</tt></b><br>
  846. &nbsp;&nbsp;&nbsp;...<br>
  847. <b><tt>}</tt></b>
  848. </blockquote>
  849. <h4><a name="Compiler_warnings"></a>Compiler warnings</h4>
  850. <p>
  851. The following section refers to the warnings produced by <b><tt>gcc</tt></b>:
  852. your compiler may produce different ones.
  853. <p>
  854. It's OK if compilation produces the following warnings:
  855. <ul>
  856. <li><b><tt>&lt;name&gt; might be used uninitialized in this function</tt></b>
  857. <li><b><tt>cast discards `const' from pointer target type</tt></b>
  858. </ul>
  859. <p>
  860. The first of these often occurs because the compiler isn't aware of control
  861. flow restrictions that guarantee the variable will be initialized before
  862. use: if it occurs in new code, check the code carefully, but don't worry
  863. about the message. The second is often unavoidable in code that initializes
  864. or frees a structure that is otherwise <b><tt>const</tt></b> during its
  865. lifetime.
  866. <p>
  867. Do work hard to eliminate all warnings other than these,
  868. since they often indicate the possible presence of coding errors.
  869. In particular, get rid of warnings about parameter passing or
  870. initialization that discards <b><tt>const</tt></b>,
  871. by using explicit casts.
  872. <hr>
  873. <h2><a name="File_structuring"></a>File structuring</h2>
  874. <h3><a name="All_files"></a>All files</h3>
  875. <p>
  876. Keep file names within the "8.3" format for portability:
  877. <ul>
  878. <li>Use only letters, digits, dash, and underscore in file names.
  879. <li>Don't assume upper and lower case letters are distinct.
  880. <li>Put no more than 8 characters before the ".", if any.
  881. <li>If there is a ".", put between 1 and 3 characters after the ".".
  882. </ul>
  883. <p>
  884. For files other than documentation files, use only lower case letters
  885. in the names; for HTML documentation files, capitalize the first letter.
  886. <p>
  887. Every code file should start with comments containing
  888. <ol>
  889. <li>a copyright notice,
  890. <li>the name of the file in the form of an RCS Id:
  891. <blockquote><b><tt>
  892. /* $<!-- -->Id: filename.ext $*/
  893. </tt></b></blockquote>
  894. <p>
  895. (using the comment convention appropriate to the language of the file), and
  896. <li>a summary, no more than one line, of what the file contains.
  897. </ol>
  898. <p>
  899. If you create a file by copying the beginning of another file, be sure to
  900. update the copyright year and change the file name.
  901. <h3><a name="Makefiles"></a>Makefiles</h3>
  902. <p>
  903. Use the extension <b><tt>.mak</tt></b> for makefiles.
  904. <p>
  905. For each
  906. <blockquote><b><tt>
  907. #include "xxx.h"
  908. </tt></b></blockquote>
  909. <p>
  910. make sure there is a dependency on <b><tt>$(xxx_h)</tt></b> in the
  911. makefile. If xxx ends with a "<b><tt>_</tt></b>", this rule still holds,
  912. so that if you code
  913. <blockquote><b><tt>
  914. #include "math_.h"
  915. </tt></b></blockquote>
  916. <p>
  917. the makefile must contain a dependency on "<b><tt>$(math__h)</tt></b>"
  918. (note the two underscores "<b><tt>__</tt></b>").
  919. <p>
  920. List the dependencies bottom-to-top, like the <b><tt>#include</tt></b>
  921. statements themselves; within each level, list them alphabetically. Do
  922. this also with <b><tt>#include</tt></b> statements themselves whenever
  923. possible (but sometimes there are inter-header dependencies that require
  924. bending this rule).
  925. <p>
  926. For compatibility with the build utilities on OpenVMS, always put a space
  927. before the colon that separates the target(s) of a rule from the dependents.
  928. <h3><a name="General_C_code"></a>General C code</h3>
  929. <p>
  930. List <b><tt>#include</tt></b> statements from "bottom" to "top", that is,
  931. in the following order:
  932. <blockquote><ol>
  933. <li>System includes (<b><tt>"xxx_.h"</tt></b>)
  934. <li><b><tt>gs*.h</tt></b>
  935. <li><b><tt>gx*.h</tt></b> (yes, <b><tt>gs</tt></b> and <b><tt>gx</tt></b>
  936. are in the wrong order.)
  937. <li><b><tt>s*.h</tt></b>
  938. <li><b><tt>i*.h</tt></b> (or other interpreter headers that don't start
  939. with "<b><tt>i</tt></b>")
  940. </ol></blockquote>
  941. <h3><a name="Headers"></a>Headers (<b><tt>.h</tt></b> files)</h3>
  942. <p>
  943. In header files, always use the following at the beginning of a header file
  944. to prevent double inclusion:
  945. <blockquote>
  946. {{ Copyright notice etc. }}<br><br>
  947. <b><tt>#ifndef </tt></b>&lt;filename&gt;<b><tt>_INCLUDED</tt></b><br>
  948. <b><tt>#define </tt></b>&lt;filename&gt;<b><tt>_INCLUDED</tt></b><br><br>
  949. {{ The contents of the file }}<br><br>
  950. <b><tt>#endif /* </tt></b>&lt;filename&gt;<b><tt>_INCLUDED */</tt></b>
  951. </blockquote>
  952. <p>
  953. The header file is the first place that a reader goes for
  954. information about procedures, structures, constants, etc., so ensure that
  955. every procedure and structure has a comment that says what it does. Divide
  956. procedures into meaningful groups set off by some distinguished form of
  957. comment.
  958. <h3><a name="Source"></a>Source (<b><tt>.c</tt></b> files)</h3>
  959. <p>
  960. After the initial comments, arrange C files in the following order:
  961. <blockquote><ol>
  962. <li><b><tt>#include</tt></b> statements
  963. <li>Exported data declarations
  964. <li>Explicit externs (if necessary)
  965. <li>Forward declarations of procedures
  966. <li>Private data declarations
  967. <li>Exported procedures
  968. <li>Private procedures
  969. </ol></blockquote>
  970. <p>
  971. Be flexible about the order of the declarations if necessary to improve
  972. readability. Many older files don't follow this order, often without good
  973. reason.
  974. <hr>
  975. <h2><a name="Conventions"></a>Ghostscript conventions</h2>
  976. <h3><a name="Specific_names"></a>Specific names</h3>
  977. <p>
  978. The Ghostscript code uses certain names consistently for certain kinds of
  979. values. Some of the commonest and least obvious are these two:
  980. <h4><a name="code"></a><b><tt>code</tt></b></h4>
  981. <blockquote>
  982. A value to be returned from a procedure:
  983. <table cellpadding=0 cellspacing=0>
  984. <tr valign=top> <td align=right>&lt; 0
  985. <td>&nbsp;&nbsp;&nbsp;&nbsp;
  986. <td>An error code defined in
  987. <a href="../src/gserrors.h">gserrors.h</a>
  988. (or <a href="../src/ierrors.h">ierrors.h</a>)
  989. <tr valign=top> <td align=right>0
  990. <td>&nbsp;
  991. <td>Normal return
  992. <tr valign=top> <td align=right>&gt; 0
  993. <td>&nbsp;
  994. <td>A non-standard but successful return (which must be documented, preferably with the procedure's prototype)
  995. </table>
  996. </blockquote>
  997. <h4><a name="status"></a><b><tt>status</tt></b></h4>
  998. <blockquote>
  999. A value returned from a stream procedure:
  1000. <table cellpadding=0 cellspacing=0>
  1001. <tr valign=top> <td align=right>&lt; 0
  1002. <td>&nbsp;&nbsp;&nbsp;&nbsp;
  1003. <td>An exceptional condition as defined in
  1004. <a href="../src/scommon.h">scommon.h</a>
  1005. <tr valign=top> <td align=right>0
  1006. <td>&nbsp;
  1007. <td>Normal return (or, from the "<b><tt>process</tt></b>" procedure, means that more input is needed)
  1008. <tr valign=top> <td align=right>1
  1009. <td>&nbsp;
  1010. <td>More output space is needed (from the "<b><tt>process</tt></b>" procedure)
  1011. </table>
  1012. </blockquote>
  1013. <h3><a name="Structure_type_descriptors"></a>Structure type descriptors</h3>
  1014. <p>
  1015. The Ghostscript memory manager requires run-time type information for every
  1016. structure. (We don't document this in detail here: see the <a
  1017. href="Develop.htm#Structure_descriptors">Structure descriptors</a> section
  1018. of the developer documentation for details.) Putting the descriptor for a
  1019. structure next to the structure definition will help keep the two
  1020. consistent, so immediately after the definition of a structure
  1021. <b><tt>xxx_s</tt></b>, define its structure descriptor:
  1022. <blockquote>
  1023. <b><tt>struct xxx_s {</tt></b><br>
  1024. &nbsp;&nbsp;&nbsp;... members ...<br>
  1025. <b><tt>};</tt></b><br>
  1026. <b><tt>#define private_st_xxx()&nbsp;&nbsp;/* in </tt></b>&lt;filename&gt;<tt><b>.c */\</tt></b><br>
  1027. <b><tt>&nbsp;&nbsp;gs_private_st_</tt></b>&lt;whatever&gt;<b><tt>(st_xxx, xxx_t,\</tt></b><br>
  1028. <b><tt>&nbsp;&nbsp;&nbsp;&nbsp;"xxx_t", xxx_enum_ptrs, xxx_reloc_ptrs,\</tt></b><br>
  1029. <b><tt>&nbsp;&nbsp;&nbsp;&nbsp;</tt></b>... additional parameters as needed ...<b><tt>)</tt></b>
  1030. </blockquote>
  1031. <p>
  1032. The file that implements operations on this structure
  1033. (&lt;filename&gt;<b><tt>.c</tt></b>) should then include, near the
  1034. beginning, the line:
  1035. <blockquote>
  1036. <b><tt>private_st_xxx();</tt></b>
  1037. </blockquote>
  1038. <p>
  1039. In much existing code, structure descriptors are declared as
  1040. <b><tt>public</tt></b>, which allows clients to allocate instances of the
  1041. structure directly. We now consider this bad design. Instead, structure
  1042. descriptors should always be <b><tt>private</tt></b>; the implementation
  1043. file should provide one or more procedures for allocating instances, e.g.,
  1044. <blockquote>
  1045. <b><tt>xxx_t *gs_xxx_alloc(P1(gs_memory_t *mem));</tt></b>
  1046. </blockquote>
  1047. <p>
  1048. If it is necessary to make a structure descriptor public, it should be
  1049. declared in its clients as
  1050. <blockquote>
  1051. <b><tt>extern_st(st_xxx);</tt></b>
  1052. </blockquote>
  1053. <h3><a name="Objects"></a>"Objects"</h3>
  1054. <p>
  1055. Ghostscript makes heavy use of object-oriented constructs, including
  1056. analogues of classes, instances, subclassing, and class-associated
  1057. procedures. However, these constructs are implemented in C rather than C++,
  1058. for two reasons:
  1059. <ul>
  1060. <li>The first Ghostscript code was written in 1986, long before C++ was
  1061. codified or was well supported by tools. Even today, C++ tools rarely
  1062. support C++ as well as C tools support C.
  1063. <li>C++ imposes its own implementations for virtual procedures, inheritance,
  1064. run-time type information, and (to some extent) memory management.
  1065. Ghostscript requires use of its own memory manager, and also sometimes
  1066. requires the ability to change the virtual procedures of an object
  1067. dynamically.
  1068. </ul>
  1069. <h4>Classes</h4>
  1070. <p>
  1071. The source code representation of a class is simply a
  1072. <b><tt>typedef</tt></b> for a C <b><tt>struct</tt></b>. See <a
  1073. href="C-style.htm#Structures">Structures</a>, above, for details.
  1074. <h4>Procedures</h4>
  1075. <p>
  1076. Ghostscript has no special construct for non-virtual procedures associated
  1077. with a class. In some cases, the <b><tt>typedef</tt></b> for the class is
  1078. in a header file but the <b><tt>struct</tt></b> declaration is in the
  1079. implementation code file: this provides an extra level of opaqueness, since
  1080. clients then can't see the representation and must make all accesses through
  1081. procedures. You should use this approach in new code, if it doesn't
  1082. increase the size of the code too much or require procedure calls for very
  1083. heavily used accesses.
  1084. <p>
  1085. Ghostscript uses three different approaches for storing and accessing
  1086. virtual procedures, plus a fourth one that is recommended but not currently
  1087. used. For exposition, we assume the class (type) is named
  1088. <b><tt>xxx_t</tt></b>, it has a virtual procedure
  1089. <b><tt>void&nbsp;(*virtu)(P1(xxx_t&nbsp;*))</tt></b>, and we have a variable
  1090. declared as <b><tt>xxx_t&nbsp;*pxx</tt></b>.
  1091. <ol>
  1092. <li>The procedures are stored in a separate, constant structure of type
  1093. <b><tt>xxx_procs</tt></b>, of which <b><tt>virtu</tt></b> is a member. The
  1094. structure definition of <b><tt>xxx_t</tt></b> includes a member defined as
  1095. <b><tt>const&nbsp;xxx_procs&nbsp;*procs</tt></b> (always named
  1096. <b><tt>procs</tt></b>). The construct for calling the virtual procedure is
  1097. <b><tt>pxx->procs->virtu(pxx)</tt></b>.
  1098. <li>The procedures are defined in a structure of type
  1099. <b><tt>xxx_procs</tt></b> as above. The structure definition of
  1100. <b><tt>xxx_t</tt></b> includes a member defined as
  1101. <b><tt>xxx_procs&nbsp;procs</tt></b> (always named <b><tt>procs</tt></b>).
  1102. The construct for calling the virtual procedure is
  1103. <b><tt>pxx->procs.virtu(pxx)</tt></b>.
  1104. <li>The procedures are not defined in a separate structure: each procedure
  1105. is a separate member of <b><tt>xxx_t</tt></b>. The construct for calling
  1106. the virtual procedure is <b><tt>pxx->virtu(pxx)</tt></b>.
  1107. <li>The procedures are defined in a structure of type
  1108. <b><tt>xxx_procs</tt></b> as above. The structure definition of
  1109. <b><tt>xxx_t</tt></b> includes a member defined as
  1110. <b><tt>xxx_procs&nbsp;procs[1]</tt></b> (always named
  1111. <b><tt>procs</tt></b>). The construct for calling the virtual procedure is
  1112. again <b><tt>pxx->procs->virtu(pxx)</tt></b>.
  1113. </ol>
  1114. <p>
  1115. Note that in approach 1, the procedures are in a shared constant structure;
  1116. in approaches 2 - 4, they are in a per-instance structure that can be
  1117. changed dynamically, which is sometimes important.
  1118. <p>
  1119. In the present Ghostscript code, approach 1 is most common, followed by 2
  1120. and 3; 4 is not used at all. For new code, you should use 1 or 4: that way,
  1121. all virtual procedure calls have the same form, regardless of whether the
  1122. procedures are shared and constant or per-instance and mutable.
  1123. <h4>Subclassing</h4>
  1124. <p>
  1125. Ghostscript's class mechanism allows for subclasses that can add data
  1126. members, or can add procedure members if approach 1 or 3 (above) is used.
  1127. Since C doesn't support subclassing, we use a convention to accomplish it.
  1128. In the example below, <b><tt>gx_device</tt></b> is the root class; it has a
  1129. subclass <b><tt>gx_device_forward</tt></b>, which in turn has a subclass
  1130. <b><tt>gx_device_null</tt></b>. First we define a macro for all the members
  1131. of the root class, and the root class type. (As for structures in general,
  1132. classes need a structure descriptor, as discussed in <a
  1133. href="#Structures">Structures</a> above: we include these in the examples
  1134. below.)
  1135. <blockquote><b><tt>
  1136. #define gx_device_common\<br>
  1137. &nbsp;&nbsp;&nbsp;&nbsp;type1 member1;\<br>
  1138. &nbsp;&nbsp;&nbsp;&nbsp;</tt></b>...<b><tt><br>
  1139. &nbsp;&nbsp;&nbsp;&nbsp;typeN memberN<br>
  1140. <br>
  1141. typedef struct gx_device_s {<br>
  1142. &nbsp;&nbsp;&nbsp;&nbsp;gx_device_common;<br>
  1143. } gx_device;<br>
  1144. <br>
  1145. #define private_st_gx_device()&nbsp;&nbsp;/* in gsdevice.c */\<br>
  1146. &nbsp;&nbsp;gs_private_st_</tt></b>&lt;whatever&gt;<b><tt>(st_gx_device, gx_device,\<br>
  1147. &nbsp;&nbsp;&nbsp;&nbsp;"gx_device", device_enum_ptrs, device_reloc_ptrs,\<br>
  1148. &nbsp;&nbsp;&nbsp;&nbsp;</tt></b>... additional parameters as needed ...<b><tt>)</tt></b>
  1149. </tt></b></blockquote>
  1150. <p>
  1151. We then define a similar macro and type for the subclass.
  1152. <blockquote><b><tt>
  1153. #define gx_device_forward_common\<br>
  1154. &nbsp;&nbsp;&nbsp;&nbsp;gx_device_common;\<br>
  1155. &nbsp;&nbsp;&nbsp;&nbsp;gx_device *target<br>
  1156. <br>
  1157. typedef struct gx_device_forward_s {<br>
  1158. &nbsp;&nbsp;&nbsp;&nbsp;gx_device_forward_common;<br>
  1159. } gx_device_forward;<br>
  1160. <br>
  1161. #define private_st_device_forward()&nbsp;&nbsp;/* in gsdevice.c */\<br>
  1162. &nbsp;&nbsp;gs_private_st_suffix_add1(st_device_forward, gx_device_forward,\<br>
  1163. &nbsp;&nbsp;&nbsp;&nbsp;"gx_device_forward", device_forward_enum_ptrs, device_forward_reloc_ptrs,\<br>
  1164. &nbsp;&nbsp;&nbsp;&nbsp;gx_device, target)
  1165. </tt></b></blockquote>
  1166. <p>
  1167. Finally, we define a leaf class, which doesn't need a macro because we don't
  1168. currently subclass it. (We can create the macro later if needed, with no
  1169. changes anywhere else.) In this particular case, the leaf class has no
  1170. additional data members, but it could have some.
  1171. <blockquote><b><tt>
  1172. typedef struct gx_device_null_s {<br>
  1173. &nbsp;&nbsp;&nbsp;&nbsp;gx_device_forward_common;<br>
  1174. };<br>
  1175. <br>
  1176. #define private_st_device_null()&nbsp;&nbsp;/* in gsdevice.c */\<br>
  1177. &nbsp;&nbsp;gs_private_st_suffix_add0_local(st_device_null, gx_device_null,\<br>
  1178. &nbsp;&nbsp;&nbsp;&nbsp;"gx_device_null", device_null_enum_ptrs, device_null_reloc_ptrs,\<br>
  1179. &nbsp;&nbsp;&nbsp;&nbsp;gx_device_forward)
  1180. </tt></b></blockquote>
  1181. <p>
  1182. Note that the above example is <strong>not</strong> the actual definition of
  1183. the <b><tt>gx_device</tt></b> structure type: the actual type has some
  1184. additional complications because it has a finalization procedure. See <a
  1185. href="../src/gxdevcli.h">src/gxdevcli.h</a> for the details.
  1186. <p>
  1187. If you add members to a root class (such as <b><tt>gx_device</tt></b> in
  1188. this example), or change existing members, do this in the
  1189. <b><tt>gx_device_common</tt></b> macro, not the <b><tt>gx_device</tt></b>
  1190. structure definition. Similarly, to change the
  1191. <b><tt>gx_device_forward</tt></b> class, modify the
  1192. <b><tt>gx_device_forward_common</tt></b> macro, not the structure
  1193. definition. Only change the structure definition if the class is a leaf
  1194. class (one with no <b><tt>_common</tt></b> macro and no possibility of
  1195. subclassing), like <b><tt>gx_device_null</tt></b>.
  1196. <h3><a name="Error_handling"></a>Error handling</h3>
  1197. <p>
  1198. Every caller should check for error returns and, in general, propagate them
  1199. to <b>its</b> callers. By convention, nearly every procedure returns an
  1200. <b><tt>int</tt></b> to indicate the outcome of the call:
  1201. <blockquote><table cellpadding=0 cellspacing=0>
  1202. <tr valign=top> <td align=right>&lt; 0
  1203. <td>&nbsp;&nbsp;&nbsp;&nbsp;
  1204. <td>Error return
  1205. <tr valign=top> <td align=right>0
  1206. <td>&nbsp;
  1207. <td>Normal return
  1208. <tr valign=top> <td align=right>&gt; 0
  1209. <td>&nbsp;
  1210. <td>Non-error return other than the normal case
  1211. </table></blockquote>
  1212. <p>
  1213. To make a procedure generate an error and return it, as opposed to
  1214. propagating an error generated by a lower procedure, you should use
  1215. <blockquote>
  1216. <b><tt>return_error(</tt></b><em>error_number</em><b><tt>);</tt></b>
  1217. </blockquote>
  1218. <p>
  1219. Sometimes it is more convenient to generate the error in one place and
  1220. return it in another. In this case, you should use
  1221. <blockquote>
  1222. <b><tt>code = gs_note_error(</tt></b><em>error_number</em><b><tt>);</tt></b><br>
  1223. ...<br>
  1224. <b><tt>return code;</tt></b>
  1225. </blockquote>
  1226. <p>
  1227. In executables built for debugging, the <b><tt>-E</tt></b> (or
  1228. <b><tt>-Z#</tt></b>) command line switch causes <b><tt>return_error</tt></b>
  1229. and <b><tt>gs_note_error</tt></b> to print the error number and the source
  1230. file and line: this is often helpful for identifying the original cause of
  1231. an error.
  1232. <p>
  1233. See the file <a href="../src/gserrors.h">src/gserrors.h</a> for the error
  1234. return codes used by the graphics library, most of which correspond directly
  1235. to PostScript error conditions.
  1236. <!-- [2.0 end contents] ==================================================== -->
  1237. <!-- [3.0 begin visible trailer] =========================================== -->
  1238. <hr>
  1239. <p>
  1240. <small>Copyright &copy; 1996, 1997, 1998 Aladdin Enterprises.
  1241. Copyright &copy; 2001 artofcode LLC.
  1242. All rights reserved.</small>
  1243. <p>
  1244. <small>This file is part of AFPL Ghostscript. See the <a
  1245. href="Public.htm">Aladdin Free Public License</a> (the "License") for full
  1246. details of the terms of using, copying, modifying, and redistributing AFPL
  1247. Ghostscript.</small>
  1248. <p>
  1249. <small>Ghostscript version 8.53, 20 October 2005
  1250. <!-- [3.0 end visible trailer] ============================================= -->
  1251. </body>
  1252. </html>