Debian Codegraph @

README.6coding

raw
1 (This file was generated by makeinfo and splitinfo.gawk.)
2 (Released under the old-style GNU documentation license;
3 see sources or other output files for full text.)
4
5 8 Coding conventions
6 ********************
7
8 Ideally, building all of TeX Live with '--enable-compiler-warnings=max'
9 should produce no (GCC) compiler warnings at all. In spite of
10 considerable efforts into that direction we are still far from that goal
11 and there are reasons that we may never fully reach it. Below are some
12 rules about declarations of functions or variables and the use of
13 'const'. These rules should be applied to the code maintained in the
14 TeX Live tree and for other packages whose maintainers are willing to
15 accept patches.
16
17 8.1 Declarations and definitions
18 ================================
19
20 C standards
21 ...........
22
23 The TeX Live build system no longer supports pre-ANSI C compilers. Thus
24 all function prototypes and definitions must conform to the ANSI C
25 standard (including 'void' in the declaration of C functions with no
26 parameters). On the other hand, TL is built for a wide variety of
27 systems, not all of which support the C99 standard. Therefore using C99
28 features should be avoided if that can easily be done. In particular, C
29 code must not contain declarations after statements or C++-style
30 comments.
31
32 If some C99 (or later) constructs must be used, the module should
33 verify that they are available and otherwise provide an alternative.
34 For example, the module 'texk/chktex' uses the C99 function 'stpcpy()'
35 that may or may not be available on a particular system. It uses
36 'AC_CHECK_DECLS([stpcpy])' in 'configure.ac' to test this, and provides
37 a perhaps less efficient alternative (in the file 'Utility.h'):
38
39 #if !(defined HAVE_DECL_STPCPY && HAVE_DECL_STPCPY)
40 static inline char *stpcpy(char *dest, const char *src)
41 {
42 return strcpy(dest, src) + strlen(src);
43 }
44 #endif
45
46 Static functions
47 ................
48
49 Functions used in only one file should be declared 'static'; they
50 require no prototype except in forward declarations.
51
52 Extern functions
53 ................
54
55 Functions not declared 'static', usually because they are used in
56 several files, require an ('extern') prototype in exactly one header
57 file, which is included in the file defining the function and in all
58 files using that function--this is the only way to guarantee consistency
59 between definition and use. There should be no 'extern' declarations
60 sprinkled throughout the C code (with or without comments as to where
61 that function is defined).
62
63 Variable declarations
64 .....................
65
66 The declaration of global variables follows analogous rules: they are
67 either declared 'static' if used in only one file or declared 'extern'
68 in exactly one header and instantiated in exactly one file.
69
70 8.2 Const
71 =========
72
73 The 'const' feature of C is valuable, but easy to mis-use.
74
75 Function parameters
76 ...................
77
78 Ideally, a function parameter not modified by the function should be
79 declared as 'const'. This is important in particular for strings
80 ('char*') because the actual arguments are often string literals. It is
81 perfectly legitimate and safe to use a type 'char*' value for a type
82 'const char*' variable (in an assignment, as initializer, as function
83 argument, or as return value). It is equally safe to use a type
84 'char**' value for a type 'const char*const*' variable, but not for a
85 type 'const char**' variable since that might cause modification of a
86 quantity supposed to be constant.
87
88 Getting all 'const' qualifiers right can get quite involved but can
89 almost always be done. There are only a couple notable exceptions: the
90 X11 headers are full of declarations that ought to use 'const' but do
91 not; at one time, 'libfreetype' also did not fully specify 'const', but
92 this has not been checked recently.
93
94 What must be avoided with 'const'
95 .................................
96
97 The GCC compiler warnings "assignment discards qualifiers..." and
98 analogous warnings for "initialization", "passing arg", or "return" must
99 be strenuously avoided in our own code. The only exception is when they
100 are caused by X11 declarations or other third party code.
101
102 What should be avoided with 'const'
103 ...................................
104
105 A type cast, e.g., from 'const char*' to 'char*' does not solve any
106 problems; depending on warning options, it may only hide them.
107 Therefore such casts should be avoided whenever possible and otherwise
108 must be carefully analyzed to make sure that they cannot cause the
109 modification of quantities supposed to be constant.
110