OSDN Git Service

* doc/invoke.texi: Remove Chill references.
[pf3gnuchains/gcc-fork.git] / gcc / README.Portability
1 Copyright (C) 2000 Free Software Foundation, Inc.
2
3 This file is intended to contain a few notes about writing C code
4 within GCC so that it compiles without error on the full range of
5 compilers GCC needs to be able to compile on.
6
7 The problem is that many ISO-standard constructs are not accepted by
8 either old or buggy compilers, and we keep getting bitten by them.
9 This knowledge until know has been sparsely spread around, so I
10 thought I'd collect it in one useful place.  Please add and correct
11 any problems as you come across them.
12
13 I'm going to start from a base of the ISO C89 standard, since that is
14 probably what most people code to naturally.  Obviously using
15 constructs introduced after that is not a good idea.
16
17 The first section of this file deals strictly with portability issues,
18 the second with common coding pitfalls.
19
20
21                         Portability Issues
22                         ==================
23
24 Unary +
25 -------
26
27 K+R C compilers and preprocessors have no notion of unary '+'.  Thus
28 the following code snippet contains 2 portability problems.
29
30 int x = +2;  /* int x = 2;  */
31 #if +1       /* #if 1  */
32 #endif
33
34
35 Pointers to void
36 ----------------
37
38 K+R C compilers did not have a void pointer, and used char * as the
39 pointer to anything.  The macro PTR is defined as either void * or
40 char * depending on whether you have a standards compliant compiler or
41 a K+R one.  Thus
42
43   free ((void *) h->value.expansion);
44
45 should be written
46
47   free ((PTR) h->value.expansion);
48
49 Further, an initial investigation indicates that pointers to functions
50 returning void are okay.  Thus the example given by "Calling functions
51 through pointers to functions" below appears not to cause a problem.
52
53
54 String literals
55 ---------------
56
57 Some SGI compilers choke on the parentheses in:-
58
59 const char string[] = ("A string");
60
61 This is unfortunate since this is what the GNU gettext macro N_
62 produces.  You need to find a different way to code it.
63
64 K+R C did not allow concatenation of string literals like
65
66   "This is a " "single string literal".
67
68 Moreover, some compilers like MSVC++ have fairly low limits on the
69 maximum length of a string literal; 509 is the lowest we've come
70 across.  You may need to break up a long printf statement into many
71 smaller ones.
72
73
74 Empty macro arguments
75 ---------------------
76
77 ISO C (6.8.3 in the 1990 standard) specifies the following:
78
79 If (before argument substitution) any argument consists of no
80 preprocessing tokens, the behavior is undefined.
81
82 This was relaxed by ISO C99, but some older compilers emit an error,
83 so code like
84
85 #define foo(x, y) x y
86 foo (bar, )
87
88 needs to be coded in some other way.
89
90
91 signed keyword
92 --------------
93
94 The signed keyword did not exist in K+R compilers; it was introduced
95 in ISO C89, so you cannot use it.  In both K+R and standard C,
96 unqualified char and bitfields may be signed or unsigned.  There is no
97 way to portably declare signed chars or signed bitfields.
98
99 All other arithmetic types are signed unless you use the 'unsigned'
100 qualifier.  For instance, it is safe to write
101
102   short paramc;
103
104 instead of
105
106   signed short paramc;
107
108 If you have an algorithm that depends on signed char or signed
109 bitfields, you must find another way to write it before it can be
110 integrated into GCC.
111
112
113 Function prototypes
114 -------------------
115
116 You need to provide a function prototype for every function before you
117 use it, and functions must be defined K+R style.  The function
118 prototype should use the PARAMS macro, which takes a single argument.
119 Therefore the parameter list must be enclosed in parentheses.  For
120 example,
121
122 int myfunc PARAMS ((double, int *));
123
124 int
125 myfunc (var1, var2)
126         double var1;
127         int *var2;
128 {
129   ...
130 }
131
132 You also need to use PARAMS when referring to function protypes in
133 other circumstances, for example see "Calling functions through
134 pointers to functions" below.
135
136 Variable-argument functions are best described by example:-
137
138 void cpp_ice PARAMS ((cpp_reader *, const char *msgid, ...));
139
140 void
141 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
142 {  
143 #ifndef ANSI_PROTOTYPES
144   cpp_reader *pfile;
145   const char *msgid;
146 #endif
147   va_list ap;
148   
149   VA_START (ap, msgid);
150   
151 #ifndef ANSI_PROTOTYPES
152   pfile = va_arg (ap, cpp_reader *);
153   msgid = va_arg (ap, const char *);
154 #endif
155
156   ...
157   va_end (ap);
158 }
159
160 For the curious, here are the definitions of the above macros.  See
161 ansidecl.h for the definitions of the above macros and more.
162
163 #define PARAMS(paramlist)  paramlist  /* ISO C.  */
164 #define VPARAMS(args)   args
165
166 #define PARAMS(paramlist)  ()         /* K+R C.  */
167 #define VPARAMS(args)   (va_alist) va_dcl
168
169 One aspect of using K+R style function declarations, is you cannot
170 have arguments whose types are char, short, or float, since without
171 prototypes (ie, K+R rules), these types are promoted to int, int, and
172 double respectively.
173
174 Calling functions through pointers to functions
175 -----------------------------------------------
176
177 K+R C compilers require parentheses around the dereferenced function
178 pointer expression in the call, whereas ISO C relaxes the syntax.  For
179 example
180
181 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
182       *p->handler (pfile, p->arg);
183
184 needs to become
185
186       (*p->handler) (pfile, p->arg);
187
188
189 Macros
190 ------
191
192 The rules under K+R C and ISO C for achieving stringification and
193 token pasting are quite different.  Therefore some macros have been
194 defined which will get it right depending upon the compiler.
195
196   CONCAT2(a,b) CONCAT3(a,b,c) and CONCAT4(a,b,c,d)
197
198 will paste the tokens passed as arguments.  You must not leave any
199 space around the commas.  Also,
200
201   STRINGX(x)
202
203 will stringify an argument; to get the same result on K+R and ISO
204 compilers x should not have spaces around it.
205
206
207 Passing structures by value
208 ---------------------------
209
210 Avoid passing structures by value, either to or from functions.  It
211 seems some K+R compilers handle this differently or not at all.
212
213
214 Enums
215 -----
216
217 In K+R C, you have to cast enum types to use them as integers, and
218 some compilers in particular give lots of warnings for using an enum
219 as an array index.
220
221
222 Bitfields
223 ---------
224
225 See also "signed keyword" above.  In K+R C only unsigned int bitfields
226 were defined (i.e. unsigned char, unsigned short, unsigned long.
227 Using plain int/short/long was not allowed).
228
229
230 free and realloc
231 ----------------
232
233 Some implementations crash upon attempts to free or realloc the null
234 pointer.  Thus if mem might be null, you need to write
235
236   if (mem)
237     free (mem);
238
239
240 Reserved Keywords
241 -----------------
242
243 K+R C has "entry" as a reserved keyword, so you should not use it for
244 your variable names.
245
246
247 Type promotions
248 ---------------
249
250 K+R used unsigned-preserving rules for arithmetic expresssions, while
251 ISO uses value-preserving.  This means an unsigned char compared to an
252 int is done as an unsigned comparison in K+R (since unsigned char
253 promotes to unsigned) while it is signed in ISO (since all of the
254 values in unsigned char fit in an int, it promotes to int).
255
256 Trigraphs
257 ---------
258
259 You weren't going to use them anyway, but trigraphs were not defined
260 in K+R C, and some otherwise ISO C compliant compilers do not accept
261 them.
262
263
264 Suffixes on Integer Constants
265 -----------------------------
266
267 K+R C did not accept a 'u' suffix on integer constants.  If you want
268 to declare a constant to be be unsigned, you must use an explicit
269 cast.
270
271 You should never use a 'l' suffix on integer constants ('L' is fine),
272 since it can easily be confused with the number '1'.
273
274
275                         Common Coding Pitfalls
276                         ======================
277
278 errno
279 -----
280
281 errno might be declared as a macro.
282
283
284 Implicit int
285 ------------
286
287 In C, the 'int' keyword can often be omitted from type declarations.
288 For instance, you can write
289
290   unsigned variable;
291
292 as shorthand for
293
294   unsigned int variable;
295
296 There are several places where this can cause trouble.  First, suppose
297 'variable' is a long; then you might think
298
299   (unsigned) variable
300
301 would convert it to unsigned long.  It does not.  It converts to
302 unsigned int.  This mostly causes problems on 64-bit platforms, where
303 long and int are not the same size.
304
305 Second, if you write a function definition with no return type at
306 all:
307
308   operate(a, b)
309       int a, b;
310   {
311     ...
312   }
313
314 that function is expected to return int, *not* void.  GCC will warn
315 about this.  K+R C has no problem with 'void' as a return type, so you
316 need not worry about that.
317
318 Implicit function declarations always have return type int.  So if you
319 correct the above definition to
320
321   void
322   operate(a, b)
323       int a, b;
324   ...
325
326 but operate() is called above its definition, you will get an error
327 about a "type mismatch with previous implicit declaration".  The cure
328 is to prototype all functions at the top of the file, or in an
329 appropriate header.
330
331 Char vs unsigned char vs int
332 ----------------------------
333
334 In C, unqualified 'char' may be either signed or unsigned; it is the
335 implementation's choice.  When you are processing 7-bit ASCII, it does
336 not matter.  But when your program must handle arbitrary binary data,
337 or fully 8-bit character sets, you have a problem.  The most obvious
338 issue is if you have a look-up table indexed by characters.
339
340 For instance, the character '\341' in ISO Latin 1 is SMALL LETTER A
341 WITH ACUTE ACCENT.  In the proper locale, isalpha('\341') will be
342 true.  But if you read '\341' from a file and store it in a plain
343 char, isalpha(c) may look up character 225, or it may look up
344 character -31.  And the ctype table has no entry at offset -31, so
345 your program will crash.  (If you're lucky.)
346
347 It is wise to use unsigned char everywhere you possibly can.  This
348 avoids all these problems.  Unfortunately, the routines in <string.h>
349 take plain char arguments, so you have to remember to cast them back
350 and forth - or avoid the use of strxxx() functions, which is probably
351 a good idea anyway.
352
353 Another common mistake is to use either char or unsigned char to
354 receive the result of getc() or related stdio functions.  They may
355 return EOF, which is outside the range of values representable by
356 char.  If you use char, some legal character value may be confused
357 with EOF, such as '\377' (SMALL LETTER Y WITH UMLAUT, in Latin-1).
358 The correct choice is int.
359
360 A more subtle version of the same mistake might look like this:
361
362   unsigned char pushback[NPUSHBACK];
363   int pbidx;
364   #define unget(c) (assert(pbidx < NPUSHBACK), pushback[pbidx++] = (c))
365   #define get(c) (pbidx ? pushback[--pbidx] : getchar())
366   ...
367   unget(EOF);
368
369 which will mysteriously turn a pushed-back EOF into a SMALL LETTER Y
370 WITH UMLAUT.
371
372
373 Other common pitfalls
374 ---------------------
375
376 o Expecting 'plain' char to be either sign or unsigned extending
377
378 o Shifting an item by a negative amount or by greater than or equal to
379   the number of bits in a type (expecting shifts by 32 to be sensible
380   has caused quite a number of bugs at least in the early days).
381
382 o Expecting ints shifted right to be sign extended.
383
384 o Modifying the same value twice within one sequence point.
385
386 o Host vs. target floating point representation, including emitting NaNs
387   and Infinities in a form that the assembler handles.
388
389 o qsort being an unstable sort function (unstable in the sense that
390   multiple items that sort the same may be sorted in different orders
391   by different qsort functions).
392
393 o Passing incorrect types to fprintf and friends.
394
395 o Adding a function declaration for a module declared in another file to
396   a .c file instead of to a .h file.