]>
Commit | Line | Data |
---|---|---|
d77de738 ML |
1 | \input texinfo |
2 | @setfilename cpp.info | |
3 | @settitle The C Preprocessor | |
4 | @setchapternewpage off | |
5 | @c @smallbook | |
6 | @c @cropmarks | |
7 | @c @finalout | |
8 | ||
9 | @include gcc-common.texi | |
10 | ||
11 | @copying | |
12 | @c man begin COPYRIGHT | |
74d5206f | 13 | Copyright @copyright{} 1987-2023 Free Software Foundation, Inc. |
d77de738 ML |
14 | |
15 | Permission is granted to copy, distribute and/or modify this document | |
16 | under the terms of the GNU Free Documentation License, Version 1.3 or | |
17 | any later version published by the Free Software Foundation. A copy of | |
18 | the license is included in the | |
19 | @c man end | |
20 | section entitled ``GNU Free Documentation License''. | |
21 | @ignore | |
22 | @c man begin COPYRIGHT | |
23 | man page gfdl(7). | |
24 | @c man end | |
25 | @end ignore | |
26 | ||
27 | @c man begin COPYRIGHT | |
28 | This manual contains no Invariant Sections. The Front-Cover Texts are | |
29 | (a) (see below), and the Back-Cover Texts are (b) (see below). | |
30 | ||
31 | (a) The FSF's Front-Cover Text is: | |
32 | ||
33 | A GNU Manual | |
34 | ||
35 | (b) The FSF's Back-Cover Text is: | |
36 | ||
37 | You have freedom to copy and modify this GNU Manual, like GNU | |
38 | software. Copies published by the Free Software Foundation raise | |
39 | funds for GNU development. | |
40 | @c man end | |
41 | @end copying | |
42 | ||
43 | @c Create a separate index for command line options. | |
44 | @defcodeindex op | |
45 | @syncodeindex vr op | |
46 | ||
47 | @c Used in cppopts.texi and cppenv.texi. | |
48 | @set cppmanual | |
49 | ||
50 | @ifinfo | |
51 | @dircategory Software development | |
52 | @direntry | |
53 | * Cpp: (cpp). The GNU C preprocessor. | |
54 | @end direntry | |
55 | @end ifinfo | |
56 | ||
57 | @titlepage | |
58 | @title The C Preprocessor | |
59 | @versionsubtitle | |
60 | @author Richard M. Stallman, Zachary Weinberg | |
61 | @page | |
62 | @c There is a fill at the bottom of the page, so we need a filll to | |
63 | @c override it. | |
64 | @vskip 0pt plus 1filll | |
65 | @insertcopying | |
66 | @end titlepage | |
67 | @contents | |
68 | @page | |
69 | ||
70 | @ifnottex | |
71 | @node Top | |
72 | @top | |
73 | The C preprocessor implements the macro language used to transform C, | |
74 | C++, and Objective-C programs before they are compiled. It can also be | |
75 | useful on its own. | |
76 | ||
77 | @menu | |
78 | * Overview:: | |
79 | * Header Files:: | |
80 | * Macros:: | |
81 | * Conditionals:: | |
82 | * Diagnostics:: | |
83 | * Line Control:: | |
84 | * Pragmas:: | |
85 | * Other Directives:: | |
86 | * Preprocessor Output:: | |
87 | * Traditional Mode:: | |
88 | * Implementation Details:: | |
89 | * Invocation:: | |
90 | * Environment Variables:: | |
91 | * GNU Free Documentation License:: | |
92 | * Index of Directives:: | |
93 | * Option Index:: | |
94 | * Concept Index:: | |
95 | ||
96 | @detailmenu | |
97 | --- The Detailed Node Listing --- | |
98 | ||
99 | Overview | |
100 | ||
101 | * Character sets:: | |
102 | * Initial processing:: | |
103 | * Tokenization:: | |
104 | * The preprocessing language:: | |
105 | ||
106 | Header Files | |
107 | ||
108 | * Include Syntax:: | |
109 | * Include Operation:: | |
110 | * Search Path:: | |
111 | * Once-Only Headers:: | |
112 | * Alternatives to Wrapper #ifndef:: | |
113 | * Computed Includes:: | |
114 | * Wrapper Headers:: | |
115 | * System Headers:: | |
116 | ||
117 | Macros | |
118 | ||
119 | * Object-like Macros:: | |
120 | * Function-like Macros:: | |
121 | * Macro Arguments:: | |
122 | * Stringizing:: | |
123 | * Concatenation:: | |
124 | * Variadic Macros:: | |
125 | * Predefined Macros:: | |
126 | * Undefining and Redefining Macros:: | |
127 | * Directives Within Macro Arguments:: | |
128 | * Macro Pitfalls:: | |
129 | ||
130 | Predefined Macros | |
131 | ||
132 | * Standard Predefined Macros:: | |
133 | * Common Predefined Macros:: | |
134 | * System-specific Predefined Macros:: | |
135 | * C++ Named Operators:: | |
136 | ||
137 | Macro Pitfalls | |
138 | ||
139 | * Misnesting:: | |
140 | * Operator Precedence Problems:: | |
141 | * Swallowing the Semicolon:: | |
142 | * Duplication of Side Effects:: | |
143 | * Self-Referential Macros:: | |
144 | * Argument Prescan:: | |
145 | * Newlines in Arguments:: | |
146 | ||
147 | Conditionals | |
148 | ||
149 | * Conditional Uses:: | |
150 | * Conditional Syntax:: | |
151 | * Deleted Code:: | |
152 | ||
153 | Conditional Syntax | |
154 | ||
155 | * Ifdef:: | |
156 | * If:: | |
157 | * Defined:: | |
158 | * Else:: | |
159 | * Elif:: | |
160 | ||
161 | Implementation Details | |
162 | ||
163 | * Implementation-defined behavior:: | |
164 | * Implementation limits:: | |
165 | * Obsolete Features:: | |
166 | ||
167 | Obsolete Features | |
168 | ||
169 | * Obsolete Features:: | |
170 | ||
171 | @end detailmenu | |
172 | @end menu | |
173 | ||
174 | @insertcopying | |
175 | @end ifnottex | |
176 | ||
177 | @node Overview | |
178 | @chapter Overview | |
179 | @c man begin DESCRIPTION | |
180 | The C preprocessor, often known as @dfn{cpp}, is a @dfn{macro processor} | |
181 | that is used automatically by the C compiler to transform your program | |
182 | before compilation. It is called a macro processor because it allows | |
183 | you to define @dfn{macros}, which are brief abbreviations for longer | |
184 | constructs. | |
185 | ||
186 | The C preprocessor is intended to be used only with C, C++, and | |
187 | Objective-C source code. In the past, it has been abused as a general | |
188 | text processor. It will choke on input which does not obey C's lexical | |
189 | rules. For example, apostrophes will be interpreted as the beginning of | |
190 | character constants, and cause errors. Also, you cannot rely on it | |
191 | preserving characteristics of the input which are not significant to | |
192 | C-family languages. If a Makefile is preprocessed, all the hard tabs | |
193 | will be removed, and the Makefile will not work. | |
194 | ||
195 | Having said that, you can often get away with using cpp on things which | |
196 | are not C@. Other Algol-ish programming languages are often safe | |
197 | (Ada, etc.) So is assembly, with caution. @option{-traditional-cpp} | |
198 | mode preserves more white space, and is otherwise more permissive. Many | |
199 | of the problems can be avoided by writing C or C++ style comments | |
200 | instead of native language comments, and keeping macros simple. | |
201 | ||
202 | Wherever possible, you should use a preprocessor geared to the language | |
203 | you are writing in. Modern versions of the GNU assembler have macro | |
204 | facilities. Most high level programming languages have their own | |
205 | conditional compilation and inclusion mechanism. If all else fails, | |
206 | try a true general text processor, such as GNU M4. | |
207 | ||
208 | C preprocessors vary in some details. This manual discusses the GNU C | |
209 | preprocessor, which provides a small superset of the features of ISO | |
210 | Standard C@. In its default mode, the GNU C preprocessor does not do a | |
211 | few things required by the standard. These are features which are | |
212 | rarely, if ever, used, and may cause surprising changes to the meaning | |
213 | of a program which does not expect them. To get strict ISO Standard C, | |
214 | you should use the @option{-std=c90}, @option{-std=c99}, | |
215 | @option{-std=c11} or @option{-std=c17} options, depending | |
216 | on which version of the standard you want. To get all the mandatory | |
217 | diagnostics, you must also use @option{-pedantic}. @xref{Invocation}. | |
218 | ||
219 | This manual describes the behavior of the ISO preprocessor. To | |
220 | minimize gratuitous differences, where the ISO preprocessor's | |
221 | behavior does not conflict with traditional semantics, the | |
222 | traditional preprocessor should behave the same way. The various | |
223 | differences that do exist are detailed in the section @ref{Traditional | |
224 | Mode}. | |
225 | ||
226 | For clarity, unless noted otherwise, references to @samp{CPP} in this | |
227 | manual refer to GNU CPP@. | |
228 | @c man end | |
229 | ||
230 | @menu | |
231 | * Character sets:: | |
232 | * Initial processing:: | |
233 | * Tokenization:: | |
234 | * The preprocessing language:: | |
235 | @end menu | |
236 | ||
237 | @node Character sets | |
238 | @section Character sets | |
239 | ||
240 | Source code character set processing in C and related languages is | |
241 | rather complicated. The C standard discusses two character sets, but | |
242 | there are really at least four. | |
243 | ||
244 | The files input to CPP might be in any character set at all. CPP's | |
245 | very first action, before it even looks for line boundaries, is to | |
246 | convert the file into the character set it uses for internal | |
247 | processing. That set is what the C standard calls the @dfn{source} | |
248 | character set. It must be isomorphic with ISO 10646, also known as | |
249 | Unicode. CPP uses the UTF-8 encoding of Unicode. | |
250 | ||
251 | The character sets of the input files are specified using the | |
252 | @option{-finput-charset=} option. | |
253 | ||
254 | All preprocessing work (the subject of the rest of this manual) is | |
255 | carried out in the source character set. If you request textual | |
256 | output from the preprocessor with the @option{-E} option, it will be | |
257 | in UTF-8. | |
258 | ||
259 | After preprocessing is complete, string and character constants are | |
260 | converted again, into the @dfn{execution} character set. This | |
261 | character set is under control of the user; the default is UTF-8, | |
262 | matching the source character set. Wide string and character | |
263 | constants have their own character set, which is not called out | |
264 | specifically in the standard. Again, it is under control of the user. | |
265 | The default is UTF-16 or UTF-32, whichever fits in the target's | |
266 | @code{wchar_t} type, in the target machine's byte | |
267 | order.@footnote{UTF-16 does not meet the requirements of the C | |
268 | standard for a wide character set, but the choice of 16-bit | |
269 | @code{wchar_t} is enshrined in some system ABIs so we cannot fix | |
270 | this.} Octal and hexadecimal escape sequences do not undergo | |
271 | conversion; @t{'\x12'} has the value 0x12 regardless of the currently | |
272 | selected execution character set. All other escapes are replaced by | |
273 | the character in the source character set that they represent, then | |
274 | converted to the execution character set, just like unescaped | |
275 | characters. | |
276 | ||
277 | In identifiers, characters outside the ASCII range can be specified | |
278 | with the @samp{\u} and @samp{\U} escapes or used directly in the input | |
279 | encoding. If strict ISO C90 conformance is specified with an option | |
280 | such as @option{-std=c90}, or @option{-fno-extended-identifiers} is | |
281 | used, then those constructs are not permitted in identifiers. | |
282 | ||
283 | @node Initial processing | |
284 | @section Initial processing | |
285 | ||
286 | The preprocessor performs a series of textual transformations on its | |
287 | input. These happen before all other processing. Conceptually, they | |
288 | happen in a rigid order, and the entire file is run through each | |
289 | transformation before the next one begins. CPP actually does them | |
290 | all at once, for performance reasons. These transformations correspond | |
291 | roughly to the first three ``phases of translation'' described in the C | |
292 | standard. | |
293 | ||
294 | @enumerate | |
d77de738 | 295 | @cindex line endings |
f33d7a88 | 296 | @item |
d77de738 ML |
297 | The input file is read into memory and broken into lines. |
298 | ||
299 | Different systems use different conventions to indicate the end of a | |
300 | line. GCC accepts the ASCII control sequences @kbd{LF}, @kbd{@w{CR | |
301 | LF}} and @kbd{CR} as end-of-line markers. These are the canonical | |
302 | sequences used by Unix, DOS and VMS, and the classic Mac OS (before | |
303 | OSX) respectively. You may therefore safely copy source code written | |
304 | on any of those systems to a different one and use it without | |
305 | conversion. (GCC may lose track of the current line number if a file | |
306 | doesn't consistently use one convention, as sometimes happens when it | |
307 | is edited on computers with different conventions that share a network | |
308 | file system.) | |
309 | ||
310 | If the last line of any input file lacks an end-of-line marker, the end | |
311 | of the file is considered to implicitly supply one. The C standard says | |
312 | that this condition provokes undefined behavior, so GCC will emit a | |
313 | warning message. | |
314 | ||
d77de738 | 315 | @cindex trigraphs |
f33d7a88 | 316 | @item |
d77de738 ML |
317 | @anchor{trigraphs}If trigraphs are enabled, they are replaced by their |
318 | corresponding single characters. By default GCC ignores trigraphs, | |
319 | but if you request a strictly conforming mode with the @option{-std} | |
320 | option, or you specify the @option{-trigraphs} option, then it | |
321 | converts them. | |
322 | ||
323 | These are nine three-character sequences, all starting with @samp{??}, | |
324 | that are defined by ISO C to stand for single characters. They permit | |
325 | obsolete systems that lack some of C's punctuation to use C@. For | |
326 | example, @samp{??/} stands for @samp{\}, so @t{'??/n'} is a character | |
327 | constant for a newline. | |
328 | ||
329 | Trigraphs are not popular and many compilers implement them | |
330 | incorrectly. Portable code should not rely on trigraphs being either | |
331 | converted or ignored. With @option{-Wtrigraphs} GCC will warn you | |
332 | when a trigraph may change the meaning of your program if it were | |
333 | converted. @xref{Wtrigraphs}. | |
334 | ||
335 | In a string constant, you can prevent a sequence of question marks | |
336 | from being confused with a trigraph by inserting a backslash between | |
337 | the question marks, or by separating the string literal at the | |
338 | trigraph and making use of string literal concatenation. @t{"(??\?)"} | |
339 | is the string @samp{(???)}, not @samp{(?]}. Traditional C compilers | |
340 | do not recognize these idioms. | |
341 | ||
342 | The nine trigraphs and their replacements are | |
343 | ||
344 | @smallexample | |
345 | Trigraph: ??( ??) ??< ??> ??= ??/ ??' ??! ??- | |
346 | Replacement: [ ] @{ @} # \ ^ | ~ | |
347 | @end smallexample | |
348 | ||
d77de738 ML |
349 | @cindex continued lines |
350 | @cindex backslash-newline | |
f33d7a88 | 351 | @item |
d77de738 ML |
352 | Continued lines are merged into one long line. |
353 | ||
354 | A continued line is a line which ends with a backslash, @samp{\}. The | |
355 | backslash is removed and the following line is joined with the current | |
356 | one. No space is inserted, so you may split a line anywhere, even in | |
357 | the middle of a word. (It is generally more readable to split lines | |
358 | only at white space.) | |
359 | ||
360 | The trailing backslash on a continued line is commonly referred to as a | |
361 | @dfn{backslash-newline}. | |
362 | ||
363 | If there is white space between a backslash and the end of a line, that | |
364 | is still a continued line. However, as this is usually the result of an | |
365 | editing mistake, and many compilers will not accept it as a continued | |
366 | line, GCC will warn you about it. | |
367 | ||
d77de738 ML |
368 | @cindex comments |
369 | @cindex line comments | |
370 | @cindex block comments | |
f33d7a88 | 371 | @item |
d77de738 ML |
372 | All comments are replaced with single spaces. |
373 | ||
374 | There are two kinds of comments. @dfn{Block comments} begin with | |
375 | @samp{/*} and continue until the next @samp{*/}. Block comments do not | |
376 | nest: | |
377 | ||
378 | @smallexample | |
379 | /* @r{this is} /* @r{one comment} */ @r{text outside comment} | |
380 | @end smallexample | |
381 | ||
382 | @dfn{Line comments} begin with @samp{//} and continue to the end of the | |
383 | current line. Line comments do not nest either, but it does not matter, | |
384 | because they would end in the same place anyway. | |
385 | ||
386 | @smallexample | |
387 | // @r{this is} // @r{one comment} | |
388 | @r{text outside comment} | |
389 | @end smallexample | |
390 | @end enumerate | |
391 | ||
392 | It is safe to put line comments inside block comments, or vice versa. | |
393 | ||
394 | @smallexample | |
395 | @group | |
396 | /* @r{block comment} | |
397 | // @r{contains line comment} | |
398 | @r{yet more comment} | |
399 | */ @r{outside comment} | |
400 | ||
401 | // @r{line comment} /* @r{contains block comment} */ | |
402 | @end group | |
403 | @end smallexample | |
404 | ||
405 | But beware of commenting out one end of a block comment with a line | |
406 | comment. | |
407 | ||
408 | @smallexample | |
409 | @group | |
410 | // @r{l.c.} /* @r{block comment begins} | |
411 | @r{oops! this isn't a comment anymore} */ | |
412 | @end group | |
413 | @end smallexample | |
414 | ||
415 | Comments are not recognized within string literals. | |
416 | @t{@w{"/* blah */"}} is the string constant @samp{@w{/* blah */}}, not | |
417 | an empty string. | |
418 | ||
419 | Line comments are not in the 1989 edition of the C standard, but they | |
420 | are recognized by GCC as an extension. In C++ and in the 1999 edition | |
421 | of the C standard, they are an official part of the language. | |
422 | ||
423 | Since these transformations happen before all other processing, you can | |
424 | split a line mechanically with backslash-newline anywhere. You can | |
425 | comment out the end of a line. You can continue a line comment onto the | |
426 | next line with backslash-newline. You can even split @samp{/*}, | |
427 | @samp{*/}, and @samp{//} onto multiple lines with backslash-newline. | |
428 | For example: | |
429 | ||
430 | @smallexample | |
431 | @group | |
432 | /\ | |
433 | * | |
434 | */ # /* | |
435 | */ defi\ | |
436 | ne FO\ | |
437 | O 10\ | |
438 | 20 | |
439 | @end group | |
440 | @end smallexample | |
441 | ||
442 | @noindent | |
443 | is equivalent to @code{@w{#define FOO 1020}}. All these tricks are | |
444 | extremely confusing and should not be used in code intended to be | |
445 | readable. | |
446 | ||
447 | There is no way to prevent a backslash at the end of a line from being | |
448 | interpreted as a backslash-newline. This cannot affect any correct | |
449 | program, however. | |
450 | ||
451 | @node Tokenization | |
452 | @section Tokenization | |
453 | ||
454 | @cindex tokens | |
455 | @cindex preprocessing tokens | |
456 | After the textual transformations are finished, the input file is | |
457 | converted into a sequence of @dfn{preprocessing tokens}. These mostly | |
458 | correspond to the syntactic tokens used by the C compiler, but there are | |
459 | a few differences. White space separates tokens; it is not itself a | |
460 | token of any kind. Tokens do not have to be separated by white space, | |
461 | but it is often necessary to avoid ambiguities. | |
462 | ||
463 | When faced with a sequence of characters that has more than one possible | |
464 | tokenization, the preprocessor is greedy. It always makes each token, | |
465 | starting from the left, as big as possible before moving on to the next | |
466 | token. For instance, @code{a+++++b} is interpreted as | |
467 | @code{@w{a ++ ++ + b}}, not as @code{@w{a ++ + ++ b}}, even though the | |
468 | latter tokenization could be part of a valid C program and the former | |
469 | could not. | |
470 | ||
471 | Once the input file is broken into tokens, the token boundaries never | |
472 | change, except when the @samp{##} preprocessing operator is used to paste | |
473 | tokens together. @xref{Concatenation}. For example, | |
474 | ||
475 | @smallexample | |
476 | @group | |
477 | #define foo() bar | |
478 | foo()baz | |
479 | @expansion{} bar baz | |
480 | @emph{not} | |
481 | @expansion{} barbaz | |
482 | @end group | |
483 | @end smallexample | |
484 | ||
485 | The compiler does not re-tokenize the preprocessor's output. Each | |
486 | preprocessing token becomes one compiler token. | |
487 | ||
488 | @cindex identifiers | |
489 | Preprocessing tokens fall into five broad classes: identifiers, | |
490 | preprocessing numbers, string literals, punctuators, and other. An | |
491 | @dfn{identifier} is the same as an identifier in C: any sequence of | |
492 | letters, digits, or underscores, which begins with a letter or | |
493 | underscore. Keywords of C have no significance to the preprocessor; | |
494 | they are ordinary identifiers. You can define a macro whose name is a | |
495 | keyword, for instance. The only identifier which can be considered a | |
496 | preprocessing keyword is @code{defined}. @xref{Defined}. | |
497 | ||
498 | This is mostly true of other languages which use the C preprocessor. | |
499 | However, a few of the keywords of C++ are significant even in the | |
500 | preprocessor. @xref{C++ Named Operators}. | |
501 | ||
502 | In the 1999 C standard, identifiers may contain letters which are not | |
503 | part of the ``basic source character set'', at the implementation's | |
504 | discretion (such as accented Latin letters, Greek letters, or Chinese | |
505 | ideograms). This may be done with an extended character set, or the | |
506 | @samp{\u} and @samp{\U} escape sequences. | |
507 | ||
508 | As an extension, GCC treats @samp{$} as a letter. This is for | |
509 | compatibility with some systems, such as VMS, where @samp{$} is commonly | |
510 | used in system-defined function and object names. @samp{$} is not a | |
511 | letter in strictly conforming mode, or if you specify the @option{-$} | |
512 | option. @xref{Invocation}. | |
513 | ||
514 | @cindex numbers | |
515 | @cindex preprocessing numbers | |
516 | A @dfn{preprocessing number} has a rather bizarre definition. The | |
517 | category includes all the normal integer and floating point constants | |
518 | one expects of C, but also a number of other things one might not | |
519 | initially recognize as a number. Formally, preprocessing numbers begin | |
520 | with an optional period, a required decimal digit, and then continue | |
521 | with any sequence of letters, digits, underscores, periods, and | |
522 | exponents. Exponents are the two-character sequences @samp{e+}, | |
523 | @samp{e-}, @samp{E+}, @samp{E-}, @samp{p+}, @samp{p-}, @samp{P+}, and | |
524 | @samp{P-}. (The exponents that begin with @samp{p} or @samp{P} are | |
525 | used for hexadecimal floating-point constants.) | |
526 | ||
527 | The purpose of this unusual definition is to isolate the preprocessor | |
528 | from the full complexity of numeric constants. It does not have to | |
529 | distinguish between lexically valid and invalid floating-point numbers, | |
530 | which is complicated. The definition also permits you to split an | |
531 | identifier at any position and get exactly two tokens, which can then be | |
532 | pasted back together with the @samp{##} operator. | |
533 | ||
534 | It's possible for preprocessing numbers to cause programs to be | |
535 | misinterpreted. For example, @code{0xE+12} is a preprocessing number | |
536 | which does not translate to any valid numeric constant, therefore a | |
537 | syntax error. It does not mean @code{@w{0xE + 12}}, which is what you | |
538 | might have intended. | |
539 | ||
540 | @cindex string literals | |
541 | @cindex string constants | |
542 | @cindex character constants | |
543 | @cindex header file names | |
544 | @c the @: prevents makeinfo from turning '' into ". | |
545 | @dfn{String literals} are string constants, character constants, and | |
546 | header file names (the argument of @samp{#include}).@footnote{The C | |
547 | standard uses the term @dfn{string literal} to refer only to what we are | |
548 | calling @dfn{string constants}.} String constants and character | |
549 | constants are straightforward: @t{"@dots{}"} or @t{'@dots{}'}. In | |
550 | either case embedded quotes should be escaped with a backslash: | |
551 | @t{'\'@:'} is the character constant for @samp{'}. There is no limit on | |
552 | the length of a character constant, but the value of a character | |
553 | constant that contains more than one character is | |
554 | implementation-defined. @xref{Implementation Details}. | |
555 | ||
556 | Header file names either look like string constants, @t{"@dots{}"}, or are | |
557 | written with angle brackets instead, @t{<@dots{}>}. In either case, | |
558 | backslash is an ordinary character. There is no way to escape the | |
559 | closing quote or angle bracket. The preprocessor looks for the header | |
560 | file in different places depending on which form you use. @xref{Include | |
561 | Operation}. | |
562 | ||
563 | No string literal may extend past the end of a line. You may use continued | |
564 | lines instead, or string constant concatenation. | |
565 | ||
566 | @cindex punctuators | |
567 | @cindex digraphs | |
568 | @cindex alternative tokens | |
569 | @dfn{Punctuators} are all the usual bits of punctuation which are | |
570 | meaningful to C and C++. All but three of the punctuation characters in | |
571 | ASCII are C punctuators. The exceptions are @samp{@@}, @samp{$}, and | |
572 | @samp{`}. In addition, all the two- and three-character operators are | |
573 | punctuators. There are also six @dfn{digraphs}, which the C++ standard | |
574 | calls @dfn{alternative tokens}, which are merely alternate ways to spell | |
575 | other punctuators. This is a second attempt to work around missing | |
576 | punctuation in obsolete systems. It has no negative side effects, | |
577 | unlike trigraphs, but does not cover as much ground. The digraphs and | |
578 | their corresponding normal punctuators are: | |
579 | ||
580 | @smallexample | |
581 | Digraph: <% %> <: :> %: %:%: | |
582 | Punctuator: @{ @} [ ] # ## | |
583 | @end smallexample | |
584 | ||
585 | @cindex other tokens | |
586 | Any other single byte is considered ``other'' and passed on to the | |
587 | preprocessor's output unchanged. The C compiler will almost certainly | |
588 | reject source code containing ``other'' tokens. In ASCII, the only | |
589 | ``other'' characters are @samp{@@}, @samp{$}, @samp{`}, and control | |
590 | characters other than NUL (all bits zero). (Note that @samp{$} is | |
591 | normally considered a letter.) All bytes with the high bit set | |
592 | (numeric range 0x7F--0xFF) that were not succesfully interpreted as | |
593 | part of an extended character in the input encoding are also ``other'' | |
594 | in the present implementation. | |
595 | ||
596 | NUL is a special case because of the high probability that its | |
597 | appearance is accidental, and because it may be invisible to the user | |
598 | (many terminals do not display NUL at all). Within comments, NULs are | |
599 | silently ignored, just as any other character would be. In running | |
600 | text, NUL is considered white space. For example, these two directives | |
601 | have the same meaning. | |
602 | ||
603 | @smallexample | |
604 | #define X^@@1 | |
605 | #define X 1 | |
606 | @end smallexample | |
607 | ||
608 | @noindent | |
609 | (where @samp{^@@} is ASCII NUL)@. Within string or character constants, | |
610 | NULs are preserved. In the latter two cases the preprocessor emits a | |
611 | warning message. | |
612 | ||
613 | @node The preprocessing language | |
614 | @section The preprocessing language | |
615 | @cindex directives | |
616 | @cindex preprocessing directives | |
617 | @cindex directive line | |
618 | @cindex directive name | |
619 | ||
620 | After tokenization, the stream of tokens may simply be passed straight | |
621 | to the compiler's parser. However, if it contains any operations in the | |
622 | @dfn{preprocessing language}, it will be transformed first. This stage | |
623 | corresponds roughly to the standard's ``translation phase 4'' and is | |
624 | what most people think of as the preprocessor's job. | |
625 | ||
626 | The preprocessing language consists of @dfn{directives} to be executed | |
627 | and @dfn{macros} to be expanded. Its primary capabilities are: | |
628 | ||
629 | @itemize @bullet | |
630 | @item | |
631 | Inclusion of header files. These are files of declarations that can be | |
632 | substituted into your program. | |
633 | ||
634 | @item | |
635 | Macro expansion. You can define @dfn{macros}, which are abbreviations | |
636 | for arbitrary fragments of C code. The preprocessor will replace the | |
637 | macros with their definitions throughout the program. Some macros are | |
638 | automatically defined for you. | |
639 | ||
640 | @item | |
641 | Conditional compilation. You can include or exclude parts of the | |
642 | program according to various conditions. | |
643 | ||
644 | @item | |
645 | Line control. If you use a program to combine or rearrange source files | |
646 | into an intermediate file which is then compiled, you can use line | |
647 | control to inform the compiler where each source line originally came | |
648 | from. | |
649 | ||
650 | @item | |
651 | Diagnostics. You can detect problems at compile time and issue errors | |
652 | or warnings. | |
653 | @end itemize | |
654 | ||
655 | There are a few more, less useful, features. | |
656 | ||
657 | Except for expansion of predefined macros, all these operations are | |
658 | triggered with @dfn{preprocessing directives}. Preprocessing directives | |
659 | are lines in your program that start with @samp{#}. Whitespace is | |
660 | allowed before and after the @samp{#}. The @samp{#} is followed by an | |
661 | identifier, the @dfn{directive name}. It specifies the operation to | |
662 | perform. Directives are commonly referred to as @samp{#@var{name}} | |
663 | where @var{name} is the directive name. For example, @samp{#define} is | |
664 | the directive that defines a macro. | |
665 | ||
666 | The @samp{#} which begins a directive cannot come from a macro | |
667 | expansion. Also, the directive name is not macro expanded. Thus, if | |
668 | @code{foo} is defined as a macro expanding to @code{define}, that does | |
669 | not make @samp{#foo} a valid preprocessing directive. | |
670 | ||
671 | The set of valid directive names is fixed. Programs cannot define new | |
672 | preprocessing directives. | |
673 | ||
674 | Some directives require arguments; these make up the rest of the | |
675 | directive line and must be separated from the directive name by | |
676 | whitespace. For example, @samp{#define} must be followed by a macro | |
677 | name and the intended expansion of the macro. | |
678 | ||
679 | A preprocessing directive cannot cover more than one line. The line | |
680 | may, however, be continued with backslash-newline, or by a block comment | |
681 | which extends past the end of the line. In either case, when the | |
682 | directive is processed, the continuations have already been merged with | |
683 | the first line to make one long line. | |
684 | ||
685 | @node Header Files | |
686 | @chapter Header Files | |
687 | ||
688 | @cindex header file | |
689 | A header file is a file containing C declarations and macro definitions | |
690 | (@pxref{Macros}) to be shared between several source files. You request | |
691 | the use of a header file in your program by @dfn{including} it, with the | |
692 | C preprocessing directive @samp{#include}. | |
693 | ||
694 | Header files serve two purposes. | |
695 | ||
696 | @itemize @bullet | |
d77de738 | 697 | @cindex system header files |
f33d7a88 | 698 | @item |
d77de738 ML |
699 | System header files declare the interfaces to parts of the operating |
700 | system. You include them in your program to supply the definitions and | |
701 | declarations you need to invoke system calls and libraries. | |
702 | ||
703 | @item | |
704 | Your own header files contain declarations for interfaces between the | |
705 | source files of your program. Each time you have a group of related | |
706 | declarations and macro definitions all or most of which are needed in | |
707 | several different source files, it is a good idea to create a header | |
708 | file for them. | |
709 | @end itemize | |
710 | ||
711 | Including a header file produces the same results as copying the header | |
712 | file into each source file that needs it. Such copying would be | |
713 | time-consuming and error-prone. With a header file, the related | |
714 | declarations appear in only one place. If they need to be changed, they | |
715 | can be changed in one place, and programs that include the header file | |
716 | will automatically use the new version when next recompiled. The header | |
717 | file eliminates the labor of finding and changing all the copies as well | |
718 | as the risk that a failure to find one copy will result in | |
719 | inconsistencies within a program. | |
720 | ||
721 | In C, the usual convention is to give header files names that end with | |
722 | @file{.h}. It is most portable to use only letters, digits, dashes, and | |
723 | underscores in header file names, and at most one dot. | |
724 | ||
725 | @menu | |
726 | * Include Syntax:: | |
727 | * Include Operation:: | |
728 | * Search Path:: | |
729 | * Once-Only Headers:: | |
730 | * Alternatives to Wrapper #ifndef:: | |
731 | * Computed Includes:: | |
732 | * Wrapper Headers:: | |
733 | * System Headers:: | |
734 | @end menu | |
735 | ||
736 | @node Include Syntax | |
737 | @section Include Syntax | |
738 | ||
739 | @findex #include | |
740 | Both user and system header files are included using the preprocessing | |
741 | directive @samp{#include}. It has two variants: | |
742 | ||
743 | @table @code | |
744 | @item #include <@var{file}> | |
745 | This variant is used for system header files. It searches for a file | |
746 | named @var{file} in a standard list of system directories. You can prepend | |
747 | directories to this list with the @option{-I} option (@pxref{Invocation}). | |
748 | ||
749 | @item #include "@var{file}" | |
750 | This variant is used for header files of your own program. It | |
751 | searches for a file named @var{file} first in the directory containing | |
752 | the current file, then in the quote directories and then the same | |
753 | directories used for @code{<@var{file}>}. You can prepend directories | |
754 | to the list of quote directories with the @option{-iquote} option. | |
755 | @end table | |
756 | ||
757 | The argument of @samp{#include}, whether delimited with quote marks or | |
758 | angle brackets, behaves like a string constant in that comments are not | |
759 | recognized, and macro names are not expanded. Thus, @code{@w{#include | |
760 | <x/*y>}} specifies inclusion of a system header file named @file{x/*y}. | |
761 | ||
762 | However, if backslashes occur within @var{file}, they are considered | |
763 | ordinary text characters, not escape characters. None of the character | |
764 | escape sequences appropriate to string constants in C are processed. | |
765 | Thus, @code{@w{#include "x\n\\y"}} specifies a filename containing three | |
766 | backslashes. (Some systems interpret @samp{\} as a pathname separator. | |
767 | All of these also interpret @samp{/} the same way. It is most portable | |
768 | to use only @samp{/}.) | |
769 | ||
770 | It is an error if there is anything (other than comments) on the line | |
771 | after the file name. | |
772 | ||
773 | @node Include Operation | |
774 | @section Include Operation | |
775 | ||
776 | The @samp{#include} directive works by directing the C preprocessor to | |
777 | scan the specified file as input before continuing with the rest of the | |
778 | current file. The output from the preprocessor contains the output | |
779 | already generated, followed by the output resulting from the included | |
780 | file, followed by the output that comes from the text after the | |
781 | @samp{#include} directive. For example, if you have a header file | |
782 | @file{header.h} as follows, | |
783 | ||
784 | @smallexample | |
785 | char *test (void); | |
786 | @end smallexample | |
787 | ||
788 | @noindent | |
789 | and a main program called @file{program.c} that uses the header file, | |
790 | like this, | |
791 | ||
792 | @smallexample | |
793 | int x; | |
794 | #include "header.h" | |
795 | ||
796 | int | |
797 | main (void) | |
798 | @{ | |
799 | puts (test ()); | |
800 | @} | |
801 | @end smallexample | |
802 | ||
803 | @noindent | |
804 | the compiler will see the same token stream as it would if | |
805 | @file{program.c} read | |
806 | ||
807 | @smallexample | |
808 | int x; | |
809 | char *test (void); | |
810 | ||
811 | int | |
812 | main (void) | |
813 | @{ | |
814 | puts (test ()); | |
815 | @} | |
816 | @end smallexample | |
817 | ||
818 | Included files are not limited to declarations and macro definitions; | |
819 | those are merely the typical uses. Any fragment of a C program can be | |
820 | included from another file. The include file could even contain the | |
821 | beginning of a statement that is concluded in the containing file, or | |
822 | the end of a statement that was started in the including file. However, | |
823 | an included file must consist of complete tokens. Comments and string | |
824 | literals which have not been closed by the end of an included file are | |
825 | invalid. For error recovery, they are considered to end at the end of | |
826 | the file. | |
827 | ||
828 | To avoid confusion, it is best if header files contain only complete | |
829 | syntactic units---function declarations or definitions, type | |
830 | declarations, etc. | |
831 | ||
832 | The line following the @samp{#include} directive is always treated as a | |
833 | separate line by the C preprocessor, even if the included file lacks a | |
834 | final newline. | |
835 | ||
836 | @node Search Path | |
837 | @section Search Path | |
838 | ||
839 | By default, the preprocessor looks for header files included by the quote | |
840 | form of the directive @code{@w{#include "@var{file}"}} first relative to | |
841 | the directory of the current file, and then in a preconfigured list | |
842 | of standard system directories. | |
843 | For example, if @file{/usr/include/sys/stat.h} contains | |
844 | @code{@w{#include "types.h"}}, GCC looks for @file{types.h} first in | |
845 | @file{/usr/include/sys}, then in its usual search path. | |
846 | ||
847 | For the angle-bracket form @code{@w{#include <@var{file}>}}, the | |
848 | preprocessor's default behavior is to look only in the standard system | |
849 | directories. The exact search directory list depends on the target | |
850 | system, how GCC is configured, and where it is installed. You can | |
851 | find the default search directory list for your version of CPP by | |
852 | invoking it with the @option{-v} option. For example, | |
853 | ||
854 | @smallexample | |
855 | cpp -v /dev/null -o /dev/null | |
856 | @end smallexample | |
857 | ||
858 | There are a number of command-line options you can use to add | |
859 | additional directories to the search path. | |
860 | The most commonly-used option is @option{-I@var{dir}}, which causes | |
861 | @var{dir} to be searched after the current directory (for the quote | |
862 | form of the directive) and ahead of the standard system directories. | |
863 | You can specify multiple @option{-I} options on the command line, | |
864 | in which case the directories are searched in left-to-right order. | |
865 | ||
866 | If you need separate control over the search paths for the quote and | |
867 | angle-bracket forms of the @samp{#include} directive, you can use the | |
868 | @option{-iquote} and/or @option{-isystem} options instead of @option{-I}. | |
869 | @xref{Invocation}, for a detailed description of these options, as | |
870 | well as others that are less generally useful. | |
871 | ||
872 | If you specify other options on the command line, such as @option{-I}, | |
873 | that affect where the preprocessor searches for header files, the | |
874 | directory list printed by the @option{-v} option reflects the actual | |
875 | search path used by the preprocessor. | |
876 | ||
877 | Note that you can also prevent the preprocessor from searching any of | |
878 | the default system header directories with the @option{-nostdinc} | |
879 | option. This is useful when you are compiling an operating system | |
880 | kernel or some other program that does not use the standard C library | |
881 | facilities, or the standard C library itself. | |
882 | ||
883 | @node Once-Only Headers | |
884 | @section Once-Only Headers | |
885 | @cindex repeated inclusion | |
886 | @cindex including just once | |
887 | @cindex wrapper @code{#ifndef} | |
888 | ||
889 | If a header file happens to be included twice, the compiler will process | |
890 | its contents twice. This is very likely to cause an error, e.g.@: when the | |
891 | compiler sees the same structure definition twice. Even if it does not, | |
892 | it will certainly waste time. | |
893 | ||
894 | The standard way to prevent this is to enclose the entire real contents | |
895 | of the file in a conditional, like this: | |
896 | ||
897 | @smallexample | |
898 | @group | |
899 | /* File foo. */ | |
900 | #ifndef FILE_FOO_SEEN | |
901 | #define FILE_FOO_SEEN | |
902 | ||
903 | @var{the entire file} | |
904 | ||
905 | #endif /* !FILE_FOO_SEEN */ | |
906 | @end group | |
907 | @end smallexample | |
908 | ||
909 | This construct is commonly known as a @dfn{wrapper #ifndef}. | |
910 | When the header is included again, the conditional will be false, | |
911 | because @code{FILE_FOO_SEEN} is defined. The preprocessor will skip | |
912 | over the entire contents of the file, and the compiler will not see it | |
913 | twice. | |
914 | ||
915 | CPP optimizes even further. It remembers when a header file has a | |
916 | wrapper @samp{#ifndef}. If a subsequent @samp{#include} specifies that | |
917 | header, and the macro in the @samp{#ifndef} is still defined, it does | |
918 | not bother to rescan the file at all. | |
919 | ||
920 | You can put comments outside the wrapper. They will not interfere with | |
921 | this optimization. | |
922 | ||
923 | @cindex controlling macro | |
924 | @cindex guard macro | |
925 | The macro @code{FILE_FOO_SEEN} is called the @dfn{controlling macro} or | |
926 | @dfn{guard macro}. In a user header file, the macro name should not | |
927 | begin with @samp{_}. In a system header file, it should begin with | |
928 | @samp{__} to avoid conflicts with user programs. In any kind of header | |
929 | file, the macro name should contain the name of the file and some | |
930 | additional text, to avoid conflicts with other header files. | |
931 | ||
932 | @node Alternatives to Wrapper #ifndef | |
933 | @section Alternatives to Wrapper #ifndef | |
934 | ||
935 | CPP supports two more ways of indicating that a header file should be | |
936 | read only once. Neither one is as portable as a wrapper @samp{#ifndef} | |
937 | and we recommend you do not use them in new programs, with the caveat | |
938 | that @samp{#import} is standard practice in Objective-C. | |
939 | ||
940 | @findex #import | |
941 | CPP supports a variant of @samp{#include} called @samp{#import} which | |
942 | includes a file, but does so at most once. If you use @samp{#import} | |
943 | instead of @samp{#include}, then you don't need the conditionals | |
944 | inside the header file to prevent multiple inclusion of the contents. | |
945 | @samp{#import} is standard in Objective-C, but is considered a | |
946 | deprecated extension in C and C++. | |
947 | ||
948 | @samp{#import} is not a well designed feature. It requires the users of | |
949 | a header file to know that it should only be included once. It is much | |
950 | better for the header file's implementor to write the file so that users | |
951 | don't need to know this. Using a wrapper @samp{#ifndef} accomplishes | |
952 | this goal. | |
953 | ||
954 | In the present implementation, a single use of @samp{#import} will | |
955 | prevent the file from ever being read again, by either @samp{#import} or | |
956 | @samp{#include}. You should not rely on this; do not use both | |
957 | @samp{#import} and @samp{#include} to refer to the same header file. | |
958 | ||
959 | Another way to prevent a header file from being included more than once | |
960 | is with the @samp{#pragma once} directive (@pxref{Pragmas}). | |
961 | @samp{#pragma once} does not have the problems that @samp{#import} does, | |
962 | but it is not recognized by all preprocessors, so you cannot rely on it | |
963 | in a portable program. | |
964 | ||
965 | @node Computed Includes | |
966 | @section Computed Includes | |
967 | @cindex computed includes | |
968 | @cindex macros in include | |
969 | ||
970 | Sometimes it is necessary to select one of several different header | |
971 | files to be included into your program. They might specify | |
972 | configuration parameters to be used on different sorts of operating | |
973 | systems, for instance. You could do this with a series of conditionals, | |
974 | ||
975 | @smallexample | |
976 | #if SYSTEM_1 | |
977 | # include "system_1.h" | |
978 | #elif SYSTEM_2 | |
979 | # include "system_2.h" | |
980 | #elif SYSTEM_3 | |
981 | @dots{} | |
982 | #endif | |
983 | @end smallexample | |
984 | ||
985 | That rapidly becomes tedious. Instead, the preprocessor offers the | |
986 | ability to use a macro for the header name. This is called a | |
987 | @dfn{computed include}. Instead of writing a header name as the direct | |
988 | argument of @samp{#include}, you simply put a macro name there instead: | |
989 | ||
990 | @smallexample | |
991 | #define SYSTEM_H "system_1.h" | |
992 | @dots{} | |
993 | #include SYSTEM_H | |
994 | @end smallexample | |
995 | ||
996 | @noindent | |
997 | @code{SYSTEM_H} will be expanded, and the preprocessor will look for | |
998 | @file{system_1.h} as if the @samp{#include} had been written that way | |
999 | originally. @code{SYSTEM_H} could be defined by your Makefile with a | |
1000 | @option{-D} option. | |
1001 | ||
1002 | You must be careful when you define the macro. @samp{#define} saves | |
1003 | tokens, not text. The preprocessor has no way of knowing that the macro | |
1004 | will be used as the argument of @samp{#include}, so it generates | |
1005 | ordinary tokens, not a header name. This is unlikely to cause problems | |
1006 | if you use double-quote includes, which are close enough to string | |
1007 | constants. If you use angle brackets, however, you may have trouble. | |
1008 | ||
1009 | The syntax of a computed include is actually a bit more general than the | |
1010 | above. If the first non-whitespace character after @samp{#include} is | |
1011 | not @samp{"} or @samp{<}, then the entire line is macro-expanded | |
1012 | like running text would be. | |
1013 | ||
1014 | If the line expands to a single string constant, the contents of that | |
1015 | string constant are the file to be included. CPP does not re-examine the | |
1016 | string for embedded quotes, but neither does it process backslash | |
1017 | escapes in the string. Therefore | |
1018 | ||
1019 | @smallexample | |
1020 | #define HEADER "a\"b" | |
1021 | #include HEADER | |
1022 | @end smallexample | |
1023 | ||
1024 | @noindent | |
1025 | looks for a file named @file{a\"b}. CPP searches for the file according | |
1026 | to the rules for double-quoted includes. | |
1027 | ||
1028 | If the line expands to a token stream beginning with a @samp{<} token | |
1029 | and including a @samp{>} token, then the tokens between the @samp{<} and | |
1030 | the first @samp{>} are combined to form the filename to be included. | |
1031 | Any whitespace between tokens is reduced to a single space; then any | |
1032 | space after the initial @samp{<} is retained, but a trailing space | |
1033 | before the closing @samp{>} is ignored. CPP searches for the file | |
1034 | according to the rules for angle-bracket includes. | |
1035 | ||
1036 | In either case, if there are any tokens on the line after the file name, | |
1037 | an error occurs and the directive is not processed. It is also an error | |
1038 | if the result of expansion does not match either of the two expected | |
1039 | forms. | |
1040 | ||
1041 | These rules are implementation-defined behavior according to the C | |
1042 | standard. To minimize the risk of different compilers interpreting your | |
1043 | computed includes differently, we recommend you use only a single | |
1044 | object-like macro which expands to a string constant. This will also | |
1045 | minimize confusion for people reading your program. | |
1046 | ||
1047 | @node Wrapper Headers | |
1048 | @section Wrapper Headers | |
1049 | @cindex wrapper headers | |
1050 | @cindex overriding a header file | |
1051 | @findex #include_next | |
1052 | ||
1053 | Sometimes it is necessary to adjust the contents of a system-provided | |
1054 | header file without editing it directly. GCC's @command{fixincludes} | |
1055 | operation does this, for example. One way to do that would be to create | |
1056 | a new header file with the same name and insert it in the search path | |
1057 | before the original header. That works fine as long as you're willing | |
1058 | to replace the old header entirely. But what if you want to refer to | |
1059 | the old header from the new one? | |
1060 | ||
1061 | You cannot simply include the old header with @samp{#include}. That | |
1062 | will start from the beginning, and find your new header again. If your | |
1063 | header is not protected from multiple inclusion (@pxref{Once-Only | |
1064 | Headers}), it will recurse infinitely and cause a fatal error. | |
1065 | ||
1066 | You could include the old header with an absolute pathname: | |
1067 | @smallexample | |
1068 | #include "/usr/include/old-header.h" | |
1069 | @end smallexample | |
1070 | @noindent | |
1071 | This works, but is not clean; should the system headers ever move, you | |
1072 | would have to edit the new headers to match. | |
1073 | ||
1074 | There is no way to solve this problem within the C standard, but you can | |
1075 | use the GNU extension @samp{#include_next}. It means, ``Include the | |
1076 | @emph{next} file with this name''. This directive works like | |
1077 | @samp{#include} except in searching for the specified file: it starts | |
1078 | searching the list of header file directories @emph{after} the directory | |
1079 | in which the current file was found. | |
1080 | ||
1081 | Suppose you specify @option{-I /usr/local/include}, and the list of | |
1082 | directories to search also includes @file{/usr/include}; and suppose | |
1083 | both directories contain @file{signal.h}. Ordinary @code{@w{#include | |
1084 | <signal.h>}} finds the file under @file{/usr/local/include}. If that | |
1085 | file contains @code{@w{#include_next <signal.h>}}, it starts searching | |
1086 | after that directory, and finds the file in @file{/usr/include}. | |
1087 | ||
1088 | @samp{#include_next} does not distinguish between @code{<@var{file}>} | |
1089 | and @code{"@var{file}"} inclusion, nor does it check that the file you | |
1090 | specify has the same name as the current file. It simply looks for the | |
1091 | file named, starting with the directory in the search path after the one | |
1092 | where the current file was found. | |
1093 | ||
1094 | The use of @samp{#include_next} can lead to great confusion. We | |
1095 | recommend it be used only when there is no other alternative. In | |
1096 | particular, it should not be used in the headers belonging to a specific | |
1097 | program; it should be used only to make global corrections along the | |
1098 | lines of @command{fixincludes}. | |
1099 | ||
1100 | @node System Headers | |
1101 | @section System Headers | |
1102 | @cindex system header files | |
1103 | ||
1104 | The header files declaring interfaces to the operating system and | |
1105 | runtime libraries often cannot be written in strictly conforming C@. | |
1106 | Therefore, GCC gives code found in @dfn{system headers} special | |
1107 | treatment. All warnings, other than those generated by @samp{#warning} | |
1108 | (@pxref{Diagnostics}), are suppressed while GCC is processing a system | |
1109 | header. Macros defined in a system header are immune to a few warnings | |
1110 | wherever they are expanded. This immunity is granted on an ad-hoc | |
1111 | basis, when we find that a warning generates lots of false positives | |
1112 | because of code in macros defined in system headers. | |
1113 | ||
1114 | Normally, only the headers found in specific directories are considered | |
1115 | system headers. These directories are determined when GCC is compiled. | |
1116 | There are, however, two ways to make normal headers into system headers: | |
1117 | ||
1118 | @itemize @bullet | |
1119 | @item | |
1120 | Header files found in directories added to the search path with the | |
1121 | @option{-isystem} and @option{-idirafter} command-line options are | |
1122 | treated as system headers for the purposes of diagnostics. | |
1123 | ||
d77de738 | 1124 | @findex #pragma GCC system_header |
f33d7a88 | 1125 | @item |
d77de738 ML |
1126 | There is also a directive, @code{@w{#pragma GCC system_header}}, which |
1127 | tells GCC to consider the rest of the current include file a system | |
1128 | header, no matter where it was found. Code that comes before the | |
1129 | @samp{#pragma} in the file is not affected. @code{@w{#pragma GCC | |
1130 | system_header}} has no effect in the primary source file. | |
1131 | @end itemize | |
1132 | ||
1133 | On some targets, such as RS/6000 AIX, GCC implicitly surrounds all | |
1134 | system headers with an @samp{extern "C"} block when compiling as C++. | |
1135 | ||
1136 | @node Macros | |
1137 | @chapter Macros | |
1138 | ||
1139 | A @dfn{macro} is a fragment of code which has been given a name. | |
1140 | Whenever the name is used, it is replaced by the contents of the macro. | |
1141 | There are two kinds of macros. They differ mostly in what they look | |
1142 | like when they are used. @dfn{Object-like} macros resemble data objects | |
1143 | when used, @dfn{function-like} macros resemble function calls. | |
1144 | ||
1145 | You may define any valid identifier as a macro, even if it is a C | |
1146 | keyword. The preprocessor does not know anything about keywords. This | |
1147 | can be useful if you wish to hide a keyword such as @code{const} from an | |
1148 | older compiler that does not understand it. However, the preprocessor | |
1149 | operator @code{defined} (@pxref{Defined}) can never be defined as a | |
1150 | macro, and C++'s named operators (@pxref{C++ Named Operators}) cannot be | |
1151 | macros when you are compiling C++. | |
1152 | ||
1153 | @menu | |
1154 | * Object-like Macros:: | |
1155 | * Function-like Macros:: | |
1156 | * Macro Arguments:: | |
1157 | * Stringizing:: | |
1158 | * Concatenation:: | |
1159 | * Variadic Macros:: | |
1160 | * Predefined Macros:: | |
1161 | * Undefining and Redefining Macros:: | |
1162 | * Directives Within Macro Arguments:: | |
1163 | * Macro Pitfalls:: | |
1164 | @end menu | |
1165 | ||
1166 | @node Object-like Macros | |
1167 | @section Object-like Macros | |
1168 | @cindex object-like macro | |
1169 | @cindex symbolic constants | |
1170 | @cindex manifest constants | |
1171 | ||
1172 | An @dfn{object-like macro} is a simple identifier which will be replaced | |
1173 | by a code fragment. It is called object-like because it looks like a | |
1174 | data object in code that uses it. They are most commonly used to give | |
1175 | symbolic names to numeric constants. | |
1176 | ||
1177 | @findex #define | |
1178 | You create macros with the @samp{#define} directive. @samp{#define} is | |
1179 | followed by the name of the macro and then the token sequence it should | |
1180 | be an abbreviation for, which is variously referred to as the macro's | |
1181 | @dfn{body}, @dfn{expansion} or @dfn{replacement list}. For example, | |
1182 | ||
1183 | @smallexample | |
1184 | #define BUFFER_SIZE 1024 | |
1185 | @end smallexample | |
1186 | ||
1187 | @noindent | |
1188 | defines a macro named @code{BUFFER_SIZE} as an abbreviation for the | |
1189 | token @code{1024}. If somewhere after this @samp{#define} directive | |
1190 | there comes a C statement of the form | |
1191 | ||
1192 | @smallexample | |
1193 | foo = (char *) malloc (BUFFER_SIZE); | |
1194 | @end smallexample | |
1195 | ||
1196 | @noindent | |
1197 | then the C preprocessor will recognize and @dfn{expand} the macro | |
1198 | @code{BUFFER_SIZE}. The C compiler will see the same tokens as it would | |
1199 | if you had written | |
1200 | ||
1201 | @smallexample | |
1202 | foo = (char *) malloc (1024); | |
1203 | @end smallexample | |
1204 | ||
1205 | By convention, macro names are written in uppercase. Programs are | |
1206 | easier to read when it is possible to tell at a glance which names are | |
1207 | macros. | |
1208 | ||
1209 | The macro's body ends at the end of the @samp{#define} line. You may | |
1210 | continue the definition onto multiple lines, if necessary, using | |
1211 | backslash-newline. When the macro is expanded, however, it will all | |
1212 | come out on one line. For example, | |
1213 | ||
1214 | @smallexample | |
1215 | #define NUMBERS 1, \ | |
1216 | 2, \ | |
1217 | 3 | |
1218 | int x[] = @{ NUMBERS @}; | |
1219 | @expansion{} int x[] = @{ 1, 2, 3 @}; | |
1220 | @end smallexample | |
1221 | ||
1222 | @noindent | |
1223 | The most common visible consequence of this is surprising line numbers | |
1224 | in error messages. | |
1225 | ||
1226 | There is no restriction on what can go in a macro body provided it | |
1227 | decomposes into valid preprocessing tokens. Parentheses need not | |
1228 | balance, and the body need not resemble valid C code. (If it does not, | |
1229 | you may get error messages from the C compiler when you use the macro.) | |
1230 | ||
1231 | The C preprocessor scans your program sequentially. Macro definitions | |
1232 | take effect at the place you write them. Therefore, the following input | |
1233 | to the C preprocessor | |
1234 | ||
1235 | @smallexample | |
1236 | foo = X; | |
1237 | #define X 4 | |
1238 | bar = X; | |
1239 | @end smallexample | |
1240 | ||
1241 | @noindent | |
1242 | produces | |
1243 | ||
1244 | @smallexample | |
1245 | foo = X; | |
1246 | bar = 4; | |
1247 | @end smallexample | |
1248 | ||
1249 | When the preprocessor expands a macro name, the macro's expansion | |
1250 | replaces the macro invocation, then the expansion is examined for more | |
1251 | macros to expand. For example, | |
1252 | ||
1253 | @smallexample | |
1254 | @group | |
1255 | #define TABLESIZE BUFSIZE | |
1256 | #define BUFSIZE 1024 | |
1257 | TABLESIZE | |
1258 | @expansion{} BUFSIZE | |
1259 | @expansion{} 1024 | |
1260 | @end group | |
1261 | @end smallexample | |
1262 | ||
1263 | @noindent | |
1264 | @code{TABLESIZE} is expanded first to produce @code{BUFSIZE}, then that | |
1265 | macro is expanded to produce the final result, @code{1024}. | |
1266 | ||
1267 | Notice that @code{BUFSIZE} was not defined when @code{TABLESIZE} was | |
1268 | defined. The @samp{#define} for @code{TABLESIZE} uses exactly the | |
1269 | expansion you specify---in this case, @code{BUFSIZE}---and does not | |
1270 | check to see whether it too contains macro names. Only when you | |
1271 | @emph{use} @code{TABLESIZE} is the result of its expansion scanned for | |
1272 | more macro names. | |
1273 | ||
1274 | This makes a difference if you change the definition of @code{BUFSIZE} | |
1275 | at some point in the source file. @code{TABLESIZE}, defined as shown, | |
1276 | will always expand using the definition of @code{BUFSIZE} that is | |
1277 | currently in effect: | |
1278 | ||
1279 | @smallexample | |
1280 | #define BUFSIZE 1020 | |
1281 | #define TABLESIZE BUFSIZE | |
1282 | #undef BUFSIZE | |
1283 | #define BUFSIZE 37 | |
1284 | @end smallexample | |
1285 | ||
1286 | @noindent | |
1287 | Now @code{TABLESIZE} expands (in two stages) to @code{37}. | |
1288 | ||
1289 | If the expansion of a macro contains its own name, either directly or | |
1290 | via intermediate macros, it is not expanded again when the expansion is | |
1291 | examined for more macros. This prevents infinite recursion. | |
1292 | @xref{Self-Referential Macros}, for the precise details. | |
1293 | ||
1294 | @node Function-like Macros | |
1295 | @section Function-like Macros | |
1296 | @cindex function-like macros | |
1297 | ||
1298 | You can also define macros whose use looks like a function call. These | |
1299 | are called @dfn{function-like macros}. To define a function-like macro, | |
1300 | you use the same @samp{#define} directive, but you put a pair of | |
1301 | parentheses immediately after the macro name. For example, | |
1302 | ||
1303 | @smallexample | |
1304 | #define lang_init() c_init() | |
1305 | lang_init() | |
1306 | @expansion{} c_init() | |
1307 | @end smallexample | |
1308 | ||
1309 | A function-like macro is only expanded if its name appears with a pair | |
1310 | of parentheses after it. If you write just the name, it is left alone. | |
1311 | This can be useful when you have a function and a macro of the same | |
1312 | name, and you wish to use the function sometimes. | |
1313 | ||
1314 | @smallexample | |
1315 | extern void foo(void); | |
1316 | #define foo() /* @r{optimized inline version} */ | |
1317 | @dots{} | |
1318 | foo(); | |
1319 | funcptr = foo; | |
1320 | @end smallexample | |
1321 | ||
1322 | Here the call to @code{foo()} will use the macro, but the function | |
1323 | pointer will get the address of the real function. If the macro were to | |
1324 | be expanded, it would cause a syntax error. | |
1325 | ||
1326 | If you put spaces between the macro name and the parentheses in the | |
1327 | macro definition, that does not define a function-like macro, it defines | |
1328 | an object-like macro whose expansion happens to begin with a pair of | |
1329 | parentheses. | |
1330 | ||
1331 | @smallexample | |
1332 | #define lang_init () c_init() | |
1333 | lang_init() | |
1334 | @expansion{} () c_init()() | |
1335 | @end smallexample | |
1336 | ||
1337 | The first two pairs of parentheses in this expansion come from the | |
1338 | macro. The third is the pair that was originally after the macro | |
1339 | invocation. Since @code{lang_init} is an object-like macro, it does not | |
1340 | consume those parentheses. | |
1341 | ||
1342 | @node Macro Arguments | |
1343 | @section Macro Arguments | |
1344 | @cindex arguments | |
1345 | @cindex macros with arguments | |
1346 | @cindex arguments in macro definitions | |
1347 | ||
1348 | Function-like macros can take @dfn{arguments}, just like true functions. | |
1349 | To define a macro that uses arguments, you insert @dfn{parameters} | |
1350 | between the pair of parentheses in the macro definition that make the | |
1351 | macro function-like. The parameters must be valid C identifiers, | |
1352 | separated by commas and optionally whitespace. | |
1353 | ||
1354 | To invoke a macro that takes arguments, you write the name of the macro | |
1355 | followed by a list of @dfn{actual arguments} in parentheses, separated | |
1356 | by commas. The invocation of the macro need not be restricted to a | |
1357 | single logical line---it can cross as many lines in the source file as | |
1358 | you wish. The number of arguments you give must match the number of | |
1359 | parameters in the macro definition. When the macro is expanded, each | |
1360 | use of a parameter in its body is replaced by the tokens of the | |
1361 | corresponding argument. (You need not use all of the parameters in the | |
1362 | macro body.) | |
1363 | ||
1364 | As an example, here is a macro that computes the minimum of two numeric | |
1365 | values, as it is defined in many C programs, and some uses. | |
1366 | ||
1367 | @smallexample | |
1368 | #define min(X, Y) ((X) < (Y) ? (X) : (Y)) | |
1369 | x = min(a, b); @expansion{} x = ((a) < (b) ? (a) : (b)); | |
1370 | y = min(1, 2); @expansion{} y = ((1) < (2) ? (1) : (2)); | |
1371 | z = min(a + 28, *p); @expansion{} z = ((a + 28) < (*p) ? (a + 28) : (*p)); | |
1372 | @end smallexample | |
1373 | ||
1374 | @noindent | |
1375 | (In this small example you can already see several of the dangers of | |
1376 | macro arguments. @xref{Macro Pitfalls}, for detailed explanations.) | |
1377 | ||
1378 | Leading and trailing whitespace in each argument is dropped, and all | |
1379 | whitespace between the tokens of an argument is reduced to a single | |
1380 | space. Parentheses within each argument must balance; a comma within | |
1381 | such parentheses does not end the argument. However, there is no | |
1382 | requirement for square brackets or braces to balance, and they do not | |
1383 | prevent a comma from separating arguments. Thus, | |
1384 | ||
1385 | @smallexample | |
1386 | macro (array[x = y, x + 1]) | |
1387 | @end smallexample | |
1388 | ||
1389 | @noindent | |
1390 | passes two arguments to @code{macro}: @code{array[x = y} and @code{x + | |
1391 | 1]}. If you want to supply @code{array[x = y, x + 1]} as an argument, | |
1392 | you can write it as @code{array[(x = y, x + 1)]}, which is equivalent C | |
1393 | code. | |
1394 | ||
1395 | All arguments to a macro are completely macro-expanded before they are | |
1396 | substituted into the macro body. After substitution, the complete text | |
1397 | is scanned again for macros to expand, including the arguments. This rule | |
1398 | may seem strange, but it is carefully designed so you need not worry | |
1399 | about whether any function call is actually a macro invocation. You can | |
1400 | run into trouble if you try to be too clever, though. @xref{Argument | |
1401 | Prescan}, for detailed discussion. | |
1402 | ||
1403 | For example, @code{min (min (a, b), c)} is first expanded to | |
1404 | ||
1405 | @smallexample | |
1406 | min (((a) < (b) ? (a) : (b)), (c)) | |
1407 | @end smallexample | |
1408 | ||
1409 | @noindent | |
1410 | and then to | |
1411 | ||
1412 | @smallexample | |
1413 | @group | |
1414 | ((((a) < (b) ? (a) : (b))) < (c) | |
1415 | ? (((a) < (b) ? (a) : (b))) | |
1416 | : (c)) | |
1417 | @end group | |
1418 | @end smallexample | |
1419 | ||
1420 | @noindent | |
1421 | (Line breaks shown here for clarity would not actually be generated.) | |
1422 | ||
1423 | @cindex empty macro arguments | |
1424 | You can leave macro arguments empty; this is not an error to the | |
1425 | preprocessor (but many macros will then expand to invalid code). | |
1426 | You cannot leave out arguments entirely; if a macro takes two arguments, | |
1427 | there must be exactly one comma at the top level of its argument list. | |
1428 | Here are some silly examples using @code{min}: | |
1429 | ||
1430 | @smallexample | |
1431 | min(, b) @expansion{} (( ) < (b) ? ( ) : (b)) | |
1432 | min(a, ) @expansion{} ((a ) < ( ) ? (a ) : ( )) | |
1433 | min(,) @expansion{} (( ) < ( ) ? ( ) : ( )) | |
1434 | min((,),) @expansion{} (((,)) < ( ) ? ((,)) : ( )) | |
1435 | ||
1436 | min() @error{} macro "min" requires 2 arguments, but only 1 given | |
1437 | min(,,) @error{} macro "min" passed 3 arguments, but takes just 2 | |
1438 | @end smallexample | |
1439 | ||
1440 | Whitespace is not a preprocessing token, so if a macro @code{foo} takes | |
1441 | one argument, @code{@w{foo ()}} and @code{@w{foo ( )}} both supply it an | |
1442 | empty argument. Previous GNU preprocessor implementations and | |
1443 | documentation were incorrect on this point, insisting that a | |
1444 | function-like macro that takes a single argument be passed a space if an | |
1445 | empty argument was required. | |
1446 | ||
1447 | Macro parameters appearing inside string literals are not replaced by | |
1448 | their corresponding actual arguments. | |
1449 | ||
1450 | @smallexample | |
1451 | #define foo(x) x, "x" | |
1452 | foo(bar) @expansion{} bar, "x" | |
1453 | @end smallexample | |
1454 | ||
1455 | @node Stringizing | |
1456 | @section Stringizing | |
1457 | @cindex stringizing | |
1458 | @cindex @samp{#} operator | |
1459 | ||
1460 | Sometimes you may want to convert a macro argument into a string | |
1461 | constant. Parameters are not replaced inside string constants, but you | |
1462 | can use the @samp{#} preprocessing operator instead. When a macro | |
1463 | parameter is used with a leading @samp{#}, the preprocessor replaces it | |
1464 | with the literal text of the actual argument, converted to a string | |
1465 | constant. Unlike normal parameter replacement, the argument is not | |
1466 | macro-expanded first. This is called @dfn{stringizing}. | |
1467 | ||
1468 | There is no way to combine an argument with surrounding text and | |
1469 | stringize it all together. Instead, you can write a series of adjacent | |
1470 | string constants and stringized arguments. The preprocessor | |
1471 | replaces the stringized arguments with string constants. The C | |
1472 | compiler then combines all the adjacent string constants into one | |
1473 | long string. | |
1474 | ||
1475 | Here is an example of a macro definition that uses stringizing: | |
1476 | ||
1477 | @smallexample | |
1478 | @group | |
1479 | #define WARN_IF(EXP) \ | |
1480 | do @{ if (EXP) \ | |
1481 | fprintf (stderr, "Warning: " #EXP "\n"); @} \ | |
1482 | while (0) | |
1483 | WARN_IF (x == 0); | |
1484 | @expansion{} do @{ if (x == 0) | |
1485 | fprintf (stderr, "Warning: " "x == 0" "\n"); @} while (0); | |
1486 | @end group | |
1487 | @end smallexample | |
1488 | ||
1489 | @noindent | |
1490 | The argument for @code{EXP} is substituted once, as-is, into the | |
1491 | @code{if} statement, and once, stringized, into the argument to | |
1492 | @code{fprintf}. If @code{x} were a macro, it would be expanded in the | |
1493 | @code{if} statement, but not in the string. | |
1494 | ||
1495 | The @code{do} and @code{while (0)} are a kludge to make it possible to | |
1496 | write @code{WARN_IF (@var{arg});}, which the resemblance of | |
1497 | @code{WARN_IF} to a function would make C programmers want to do; see | |
1498 | @ref{Swallowing the Semicolon}. | |
1499 | ||
1500 | Stringizing in C involves more than putting double-quote characters | |
1501 | around the fragment. The preprocessor backslash-escapes the quotes | |
1502 | surrounding embedded string constants, and all backslashes within string and | |
1503 | character constants, in order to get a valid C string constant with the | |
1504 | proper contents. Thus, stringizing @code{@w{p = "foo\n";}} results in | |
1505 | @t{@w{"p = \"foo\\n\";"}}. However, backslashes that are not inside string | |
1506 | or character constants are not duplicated: @samp{\n} by itself | |
1507 | stringizes to @t{"\n"}. | |
1508 | ||
1509 | All leading and trailing whitespace in text being stringized is | |
1510 | ignored. Any sequence of whitespace in the middle of the text is | |
1511 | converted to a single space in the stringized result. Comments are | |
1512 | replaced by whitespace long before stringizing happens, so they | |
1513 | never appear in stringized text. | |
1514 | ||
1515 | There is no way to convert a macro argument into a character constant. | |
1516 | ||
1517 | If you want to stringize the result of expansion of a macro argument, | |
1518 | you have to use two levels of macros. | |
1519 | ||
1520 | @smallexample | |
1521 | #define xstr(s) str(s) | |
1522 | #define str(s) #s | |
1523 | #define foo 4 | |
1524 | str (foo) | |
1525 | @expansion{} "foo" | |
1526 | xstr (foo) | |
1527 | @expansion{} xstr (4) | |
1528 | @expansion{} str (4) | |
1529 | @expansion{} "4" | |
1530 | @end smallexample | |
1531 | ||
1532 | @code{s} is stringized when it is used in @code{str}, so it is not | |
1533 | macro-expanded first. But @code{s} is an ordinary argument to | |
1534 | @code{xstr}, so it is completely macro-expanded before @code{xstr} | |
1535 | itself is expanded (@pxref{Argument Prescan}). Therefore, by the time | |
1536 | @code{str} gets to its argument, it has already been macro-expanded. | |
1537 | ||
1538 | @node Concatenation | |
1539 | @section Concatenation | |
1540 | @cindex concatenation | |
1541 | @cindex token pasting | |
1542 | @cindex token concatenation | |
1543 | @cindex @samp{##} operator | |
1544 | ||
1545 | It is often useful to merge two tokens into one while expanding macros. | |
1546 | This is called @dfn{token pasting} or @dfn{token concatenation}. The | |
1547 | @samp{##} preprocessing operator performs token pasting. When a macro | |
1548 | is expanded, the two tokens on either side of each @samp{##} operator | |
1549 | are combined into a single token, which then replaces the @samp{##} and | |
1550 | the two original tokens in the macro expansion. Usually both will be | |
1551 | identifiers, or one will be an identifier and the other a preprocessing | |
1552 | number. When pasted, they make a longer identifier. This isn't the | |
1553 | only valid case. It is also possible to concatenate two numbers (or a | |
1554 | number and a name, such as @code{1.5} and @code{e3}) into a number. | |
1555 | Also, multi-character operators such as @code{+=} can be formed by | |
1556 | token pasting. | |
1557 | ||
1558 | However, two tokens that don't together form a valid token cannot be | |
1559 | pasted together. For example, you cannot concatenate @code{x} with | |
1560 | @code{+} in either order. If you try, the preprocessor issues a warning | |
1561 | and emits the two tokens. Whether it puts white space between the | |
1562 | tokens is undefined. It is common to find unnecessary uses of @samp{##} | |
1563 | in complex macros. If you get this warning, it is likely that you can | |
1564 | simply remove the @samp{##}. | |
1565 | ||
1566 | Both the tokens combined by @samp{##} could come from the macro body, | |
1567 | but you could just as well write them as one token in the first place. | |
1568 | Token pasting is most useful when one or both of the tokens comes from a | |
1569 | macro argument. If either of the tokens next to an @samp{##} is a | |
1570 | parameter name, it is replaced by its actual argument before @samp{##} | |
1571 | executes. As with stringizing, the actual argument is not | |
1572 | macro-expanded first. If the argument is empty, that @samp{##} has no | |
1573 | effect. | |
1574 | ||
1575 | Keep in mind that the C preprocessor converts comments to whitespace | |
1576 | before macros are even considered. Therefore, you cannot create a | |
1577 | comment by concatenating @samp{/} and @samp{*}. You can put as much | |
1578 | whitespace between @samp{##} and its operands as you like, including | |
1579 | comments, and you can put comments in arguments that will be | |
1580 | concatenated. However, it is an error if @samp{##} appears at either | |
1581 | end of a macro body. | |
1582 | ||
1583 | Consider a C program that interprets named commands. There probably | |
1584 | needs to be a table of commands, perhaps an array of structures declared | |
1585 | as follows: | |
1586 | ||
1587 | @smallexample | |
1588 | @group | |
1589 | struct command | |
1590 | @{ | |
1591 | char *name; | |
1592 | void (*function) (void); | |
1593 | @}; | |
1594 | @end group | |
1595 | ||
1596 | @group | |
1597 | struct command commands[] = | |
1598 | @{ | |
1599 | @{ "quit", quit_command @}, | |
1600 | @{ "help", help_command @}, | |
1601 | @dots{} | |
1602 | @}; | |
1603 | @end group | |
1604 | @end smallexample | |
1605 | ||
1606 | It would be cleaner not to have to give each command name twice, once in | |
1607 | the string constant and once in the function name. A macro which takes the | |
1608 | name of a command as an argument can make this unnecessary. The string | |
1609 | constant can be created with stringizing, and the function name by | |
1610 | concatenating the argument with @samp{_command}. Here is how it is done: | |
1611 | ||
1612 | @smallexample | |
1613 | #define COMMAND(NAME) @{ #NAME, NAME ## _command @} | |
1614 | ||
1615 | struct command commands[] = | |
1616 | @{ | |
1617 | COMMAND (quit), | |
1618 | COMMAND (help), | |
1619 | @dots{} | |
1620 | @}; | |
1621 | @end smallexample | |
1622 | ||
1623 | @node Variadic Macros | |
1624 | @section Variadic Macros | |
1625 | @cindex variable number of arguments | |
1626 | @cindex macros with variable arguments | |
1627 | @cindex variadic macros | |
1628 | ||
1629 | A macro can be declared to accept a variable number of arguments much as | |
1630 | a function can. The syntax for defining the macro is similar to that of | |
1631 | a function. Here is an example: | |
1632 | ||
1633 | @smallexample | |
1634 | #define eprintf(...) fprintf (stderr, __VA_ARGS__) | |
1635 | @end smallexample | |
1636 | ||
1637 | This kind of macro is called @dfn{variadic}. When the macro is invoked, | |
1638 | all the tokens in its argument list after the last named argument (this | |
1639 | macro has none), including any commas, become the @dfn{variable | |
1640 | argument}. This sequence of tokens replaces the identifier | |
1641 | @code{@w{__VA_ARGS__}} in the macro body wherever it appears. Thus, we | |
1642 | have this expansion: | |
1643 | ||
1644 | @smallexample | |
1645 | eprintf ("%s:%d: ", input_file, lineno) | |
1646 | @expansion{} fprintf (stderr, "%s:%d: ", input_file, lineno) | |
1647 | @end smallexample | |
1648 | ||
1649 | The variable argument is completely macro-expanded before it is inserted | |
1650 | into the macro expansion, just like an ordinary argument. You may use | |
1651 | the @samp{#} and @samp{##} operators to stringize the variable argument | |
1652 | or to paste its leading or trailing token with another token. (But see | |
1653 | below for an important special case for @samp{##}.) | |
1654 | ||
1655 | If your macro is complicated, you may want a more descriptive name for | |
1656 | the variable argument than @code{@w{__VA_ARGS__}}. CPP permits | |
1657 | this, as an extension. You may write an argument name immediately | |
1658 | before the @samp{...}; that name is used for the variable argument. | |
1659 | The @code{eprintf} macro above could be written | |
1660 | ||
1661 | @smallexample | |
1662 | #define eprintf(args...) fprintf (stderr, args) | |
1663 | @end smallexample | |
1664 | ||
1665 | @noindent | |
1666 | using this extension. You cannot use @code{@w{__VA_ARGS__}} and this | |
1667 | extension in the same macro. | |
1668 | ||
1669 | You can have named arguments as well as variable arguments in a variadic | |
1670 | macro. We could define @code{eprintf} like this, instead: | |
1671 | ||
1672 | @smallexample | |
1673 | #define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__) | |
1674 | @end smallexample | |
1675 | ||
1676 | @noindent | |
1677 | This formulation looks more descriptive, but historically it was less | |
1678 | flexible: you had to supply at least one argument after the format | |
1679 | string. In standard C, you could not omit the comma separating the | |
1680 | named argument from the variable arguments. (Note that this | |
1681 | restriction has been lifted in C++20, and never existed in GNU C; see | |
1682 | below.) | |
1683 | ||
1684 | Furthermore, if you left the variable argument empty, you would have | |
1685 | gotten a syntax error, because there would have been an extra comma | |
1686 | after the format string. | |
1687 | ||
1688 | @smallexample | |
1689 | eprintf("success!\n", ); | |
1690 | @expansion{} fprintf(stderr, "success!\n", ); | |
1691 | @end smallexample | |
1692 | ||
1693 | This has been fixed in C++20, and GNU CPP also has a pair of | |
1694 | extensions which deal with this problem. | |
1695 | ||
1696 | First, in GNU CPP, and in C++ beginning in C++20, you are allowed to | |
1697 | leave the variable argument out entirely: | |
1698 | ||
1699 | @smallexample | |
1700 | eprintf ("success!\n") | |
1701 | @expansion{} fprintf(stderr, "success!\n", ); | |
1702 | @end smallexample | |
1703 | ||
1704 | @noindent | |
1705 | Second, C++20 introduces the @code{@w{__VA_OPT__}} function macro. | |
1706 | This macro may only appear in the definition of a variadic macro. If | |
1707 | the variable argument has any tokens, then a @code{@w{__VA_OPT__}} | |
1708 | invocation expands to its argument; but if the variable argument does | |
1709 | not have any tokens, the @code{@w{__VA_OPT__}} expands to nothing: | |
1710 | ||
1711 | @smallexample | |
1712 | #define eprintf(format, ...) \ | |
1713 | fprintf (stderr, format __VA_OPT__(,) __VA_ARGS__) | |
1714 | @end smallexample | |
1715 | ||
1716 | @code{@w{__VA_OPT__}} is also available in GNU C and GNU C++. | |
1717 | ||
1718 | Historically, GNU CPP has also had another extension to handle the | |
1719 | trailing comma: the @samp{##} token paste operator has a special | |
1720 | meaning when placed between a comma and a variable argument. Despite | |
1721 | the introduction of @code{@w{__VA_OPT__}}, this extension remains | |
1722 | supported in GNU CPP, for backward compatibility. If you write | |
1723 | ||
1724 | @smallexample | |
1725 | #define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__) | |
1726 | @end smallexample | |
1727 | ||
1728 | @noindent | |
1729 | and the variable argument is left out when the @code{eprintf} macro is | |
1730 | used, then the comma before the @samp{##} will be deleted. This does | |
1731 | @emph{not} happen if you pass an empty argument, nor does it happen if | |
1732 | the token preceding @samp{##} is anything other than a comma. | |
1733 | ||
1734 | @smallexample | |
1735 | eprintf ("success!\n") | |
1736 | @expansion{} fprintf(stderr, "success!\n"); | |
1737 | @end smallexample | |
1738 | ||
1739 | @noindent | |
1740 | The above explanation is ambiguous about the case where the only macro | |
1741 | parameter is a variable arguments parameter, as it is meaningless to | |
1742 | try to distinguish whether no argument at all is an empty argument or | |
1743 | a missing argument. | |
1744 | CPP retains the comma when conforming to a specific C | |
1745 | standard. Otherwise the comma is dropped as an extension to the standard. | |
1746 | ||
1747 | The C standard | |
1748 | mandates that the only place the identifier @code{@w{__VA_ARGS__}} | |
1749 | can appear is in the replacement list of a variadic macro. It may not | |
1750 | be used as a macro name, macro argument name, or within a different type | |
1751 | of macro. It may also be forbidden in open text; the standard is | |
1752 | ambiguous. We recommend you avoid using it except for its defined | |
1753 | purpose. | |
1754 | ||
1755 | Likewise, C++ forbids @code{@w{__VA_OPT__}} anywhere outside the | |
1756 | replacement list of a variadic macro. | |
1757 | ||
1758 | Variadic macros became a standard part of the C language with C99. | |
1759 | GNU CPP previously supported them | |
1760 | with a named variable argument | |
1761 | (@samp{args...}, not @samp{...} and @code{@w{__VA_ARGS__}}), which | |
1762 | is still supported for backward compatibility. | |
1763 | ||
1764 | @node Predefined Macros | |
1765 | @section Predefined Macros | |
1766 | ||
1767 | @cindex predefined macros | |
1768 | Several object-like macros are predefined; you use them without | |
1769 | supplying their definitions. They fall into three classes: standard, | |
1770 | common, and system-specific. | |
1771 | ||
1772 | In C++, there is a fourth category, the named operators. They act like | |
1773 | predefined macros, but you cannot undefine them. | |
1774 | ||
1775 | @menu | |
1776 | * Standard Predefined Macros:: | |
1777 | * Common Predefined Macros:: | |
1778 | * System-specific Predefined Macros:: | |
1779 | * C++ Named Operators:: | |
1780 | @end menu | |
1781 | ||
1782 | @node Standard Predefined Macros | |
1783 | @subsection Standard Predefined Macros | |
1784 | @cindex standard predefined macros. | |
1785 | ||
1786 | The standard predefined macros are specified by the relevant | |
1787 | language standards, so they are available with all compilers that | |
1788 | implement those standards. Older compilers may not provide all of | |
1789 | them. Their names all start with double underscores. | |
1790 | ||
1791 | @table @code | |
1792 | @item __FILE__ | |
1793 | This macro expands to the name of the current input file, in the form of | |
1794 | a C string constant. This is the path by which the preprocessor opened | |
1795 | the file, not the short name specified in @samp{#include} or as the | |
1796 | input file name argument. For example, | |
1797 | @code{"/usr/local/include/myheader.h"} is a possible expansion of this | |
1798 | macro. | |
1799 | ||
1800 | @item __LINE__ | |
1801 | This macro expands to the current input line number, in the form of a | |
1802 | decimal integer constant. While we call it a predefined macro, it's | |
1803 | a pretty strange macro, since its ``definition'' changes with each | |
1804 | new line of source code. | |
1805 | @end table | |
1806 | ||
1807 | @code{__FILE__} and @code{__LINE__} are useful in generating an error | |
1808 | message to report an inconsistency detected by the program; the message | |
1809 | can state the source line at which the inconsistency was detected. For | |
1810 | example, | |
1811 | ||
1812 | @smallexample | |
1813 | fprintf (stderr, "Internal error: " | |
1814 | "negative string length " | |
1815 | "%d at %s, line %d.", | |
1816 | length, __FILE__, __LINE__); | |
1817 | @end smallexample | |
1818 | ||
1819 | An @samp{#include} directive changes the expansions of @code{__FILE__} | |
1820 | and @code{__LINE__} to correspond to the included file. At the end of | |
1821 | that file, when processing resumes on the input file that contained | |
1822 | the @samp{#include} directive, the expansions of @code{__FILE__} and | |
1823 | @code{__LINE__} revert to the values they had before the | |
1824 | @samp{#include} (but @code{__LINE__} is then incremented by one as | |
1825 | processing moves to the line after the @samp{#include}). | |
1826 | ||
1827 | A @samp{#line} directive changes @code{__LINE__}, and may change | |
1828 | @code{__FILE__} as well. @xref{Line Control}. | |
1829 | ||
1830 | C99 introduced @code{__func__}, and GCC has provided @code{__FUNCTION__} | |
1831 | for a long time. Both of these are strings containing the name of the | |
1832 | current function (there are slight semantic differences; see the GCC | |
1833 | manual). Neither of them is a macro; the preprocessor does not know the | |
1834 | name of the current function. They tend to be useful in conjunction | |
1835 | with @code{__FILE__} and @code{__LINE__}, though. | |
1836 | ||
1837 | @table @code | |
1838 | ||
1839 | @item __DATE__ | |
1840 | This macro expands to a string constant that describes the date on which | |
1841 | the preprocessor is being run. The string constant contains eleven | |
1842 | characters and looks like @code{@w{"Feb 12 1996"}}. If the day of the | |
1843 | month is less than 10, it is padded with a space on the left. | |
1844 | ||
1845 | If GCC cannot determine the current date, it will emit a warning message | |
1846 | (once per compilation) and @code{__DATE__} will expand to | |
1847 | @code{@w{"??? ?? ????"}}. | |
1848 | ||
1849 | @item __TIME__ | |
1850 | This macro expands to a string constant that describes the time at | |
1851 | which the preprocessor is being run. The string constant contains | |
1852 | eight characters and looks like @code{"23:59:01"}. | |
1853 | ||
1854 | If GCC cannot determine the current time, it will emit a warning message | |
1855 | (once per compilation) and @code{__TIME__} will expand to | |
1856 | @code{"??:??:??"}. | |
1857 | ||
1858 | @item __STDC__ | |
1859 | In normal operation, this macro expands to the constant 1, to signify | |
1860 | that this compiler conforms to ISO Standard C@. If GNU CPP is used with | |
1861 | a compiler other than GCC, this is not necessarily true; however, the | |
1862 | preprocessor always conforms to the standard unless the | |
1863 | @option{-traditional-cpp} option is used. | |
1864 | ||
1865 | This macro is not defined if the @option{-traditional-cpp} option is used. | |
1866 | ||
1867 | On some hosts, the system compiler uses a different convention, where | |
1868 | @code{__STDC__} is normally 0, but is 1 if the user specifies strict | |
1869 | conformance to the C Standard. CPP follows the host convention when | |
1870 | processing system header files, but when processing user files | |
1871 | @code{__STDC__} is always 1. This has been reported to cause problems; | |
1872 | for instance, some versions of Solaris provide X Windows headers that | |
1873 | expect @code{__STDC__} to be either undefined or 1. @xref{Invocation}. | |
1874 | ||
1875 | @item __STDC_VERSION__ | |
1876 | This macro expands to the C Standard's version number, a long integer | |
1877 | constant of the form @code{@var{yyyy}@var{mm}L} where @var{yyyy} and | |
1878 | @var{mm} are the year and month of the Standard version. This signifies | |
1879 | which version of the C Standard the compiler conforms to. Like | |
1880 | @code{__STDC__}, this is not necessarily accurate for the entire | |
1881 | implementation, unless GNU CPP is being used with GCC@. | |
1882 | ||
1883 | The value @code{199409L} signifies the 1989 C standard as amended in | |
1884 | 1994, which is the current default; the value @code{199901L} signifies | |
1885 | the 1999 revision of the C standard; the value @code{201112L} | |
1886 | signifies the 2011 revision of the C standard; the value | |
1887 | @code{201710L} signifies the 2017 revision of the C standard (which is | |
1888 | otherwise identical to the 2011 version apart from correction of | |
1889 | defects). An unspecified value larger than @code{201710L} is used for | |
1890 | the experimental @option{-std=c2x} and @option{-std=gnu2x} modes. | |
1891 | ||
1892 | This macro is not defined if the @option{-traditional-cpp} option is | |
1893 | used, nor when compiling C++ or Objective-C@. | |
1894 | ||
1895 | @item __STDC_HOSTED__ | |
1896 | This macro is defined, with value 1, if the compiler's target is a | |
1897 | @dfn{hosted environment}. A hosted environment has the complete | |
1898 | facilities of the standard C library available. | |
1899 | ||
1900 | @item __cplusplus | |
1901 | This macro is defined when the C++ compiler is in use. You can use | |
1902 | @code{__cplusplus} to test whether a header is compiled by a C compiler | |
1903 | or a C++ compiler. This macro is similar to @code{__STDC_VERSION__}, in | |
1904 | that it expands to a version number. Depending on the language standard | |
1905 | selected, the value of the macro is | |
1906 | @code{199711L} for the 1998 C++ standard, | |
1907 | @code{201103L} for the 2011 C++ standard, | |
1908 | @code{201402L} for the 2014 C++ standard, | |
1909 | @code{201703L} for the 2017 C++ standard, | |
1910 | @code{202002L} for the 2020 C++ standard, | |
1911 | or an unspecified value strictly larger than @code{202002L} for the | |
1912 | experimental languages enabled by @option{-std=c++23} and | |
1913 | @option{-std=gnu++23}. | |
1914 | ||
1915 | @item __OBJC__ | |
1916 | This macro is defined, with value 1, when the Objective-C compiler is in | |
1917 | use. You can use @code{__OBJC__} to test whether a header is compiled | |
1918 | by a C compiler or an Objective-C compiler. | |
1919 | ||
1920 | @item __ASSEMBLER__ | |
1921 | This macro is defined with value 1 when preprocessing assembly | |
1922 | language. | |
1923 | ||
1924 | @end table | |
1925 | ||
1926 | @node Common Predefined Macros | |
1927 | @subsection Common Predefined Macros | |
1928 | @cindex common predefined macros | |
1929 | ||
1930 | The common predefined macros are GNU C extensions. They are available | |
1931 | with the same meanings regardless of the machine or operating system on | |
1932 | which you are using GNU C or GNU Fortran. Their names all start with | |
1933 | double underscores. | |
1934 | ||
1935 | @table @code | |
1936 | ||
1937 | @item __COUNTER__ | |
1938 | This macro expands to sequential integral values starting from 0. In | |
1939 | conjunction with the @code{##} operator, this provides a convenient means to | |
1940 | generate unique identifiers. Care must be taken to ensure that | |
1941 | @code{__COUNTER__} is not expanded prior to inclusion of precompiled headers | |
1942 | which use it. Otherwise, the precompiled headers will not be used. | |
1943 | ||
1944 | @item __GFORTRAN__ | |
1945 | The GNU Fortran compiler defines this. | |
1946 | ||
1947 | @item __GNUC__ | |
1948 | @itemx __GNUC_MINOR__ | |
1949 | @itemx __GNUC_PATCHLEVEL__ | |
1950 | These macros are defined by all GNU compilers that use the C | |
1951 | preprocessor: C, C++, Objective-C and Fortran. Their values are the major | |
1952 | version, minor version, and patch level of the compiler, as integer | |
1953 | constants. For example, GCC version @var{x}.@var{y}.@var{z} | |
1954 | defines @code{__GNUC__} to @var{x}, @code{__GNUC_MINOR__} to @var{y}, | |
1955 | and @code{__GNUC_PATCHLEVEL__} to @var{z}. These | |
1956 | macros are also defined if you invoke the preprocessor directly. | |
1957 | ||
1958 | If all you need to know is whether or not your program is being compiled | |
1959 | by GCC, or a non-GCC compiler that claims to accept the GNU C dialects, | |
1960 | you can simply test @code{__GNUC__}. If you need to write code | |
1961 | which depends on a specific version, you must be more careful. Each | |
1962 | time the minor version is increased, the patch level is reset to zero; | |
1963 | each time the major version is increased, the | |
1964 | minor version and patch level are reset. If you wish to use the | |
1965 | predefined macros directly in the conditional, you will need to write it | |
1966 | like this: | |
1967 | ||
1968 | @smallexample | |
1969 | /* @r{Test for GCC > 3.2.0} */ | |
1970 | #if __GNUC__ > 3 || \ | |
1971 | (__GNUC__ == 3 && (__GNUC_MINOR__ > 2 || \ | |
1972 | (__GNUC_MINOR__ == 2 && \ | |
1973 | __GNUC_PATCHLEVEL__ > 0)) | |
1974 | @end smallexample | |
1975 | ||
1976 | @noindent | |
1977 | Another approach is to use the predefined macros to | |
1978 | calculate a single number, then compare that against a threshold: | |
1979 | ||
1980 | @smallexample | |
1981 | #define GCC_VERSION (__GNUC__ * 10000 \ | |
1982 | + __GNUC_MINOR__ * 100 \ | |
1983 | + __GNUC_PATCHLEVEL__) | |
1984 | @dots{} | |
1985 | /* @r{Test for GCC > 3.2.0} */ | |
1986 | #if GCC_VERSION > 30200 | |
1987 | @end smallexample | |
1988 | ||
1989 | @noindent | |
1990 | Many people find this form easier to understand. | |
1991 | ||
1992 | @item __GNUG__ | |
1993 | The GNU C++ compiler defines this. Testing it is equivalent to | |
1994 | testing @code{@w{(__GNUC__ && __cplusplus)}}. | |
1995 | ||
1996 | @item __STRICT_ANSI__ | |
1997 | GCC defines this macro if and only if the @option{-ansi} switch, or a | |
1998 | @option{-std} switch specifying strict conformance to some version of ISO C | |
1999 | or ISO C++, was specified when GCC was invoked. It is defined to @samp{1}. | |
2000 | This macro exists primarily to direct GNU libc's header files to use only | |
2001 | definitions found in standard C. | |
2002 | ||
2003 | @item __BASE_FILE__ | |
2004 | This macro expands to the name of the main input file, in the form | |
2005 | of a C string constant. This is the source file that was specified | |
2006 | on the command line of the preprocessor or C compiler. | |
2007 | ||
2008 | @item __FILE_NAME__ | |
2009 | This macro expands to the basename of the current input file, in the | |
2010 | form of a C string constant. This is the last path component by which | |
2011 | the preprocessor opened the file. For example, processing | |
2012 | @code{"/usr/local/include/myheader.h"} would set this | |
2013 | macro to @code{"myheader.h"}. | |
2014 | ||
2015 | @item __INCLUDE_LEVEL__ | |
2016 | This macro expands to a decimal integer constant that represents the | |
2017 | depth of nesting in include files. The value of this macro is | |
2018 | incremented on every @samp{#include} directive and decremented at the | |
2019 | end of every included file. It starts out at 0, its value within the | |
2020 | base file specified on the command line. | |
2021 | ||
2022 | @item __ELF__ | |
2023 | This macro is defined if the target uses the ELF object format. | |
2024 | ||
2025 | @item __VERSION__ | |
2026 | This macro expands to a string constant which describes the version of | |
2027 | the compiler in use. You should not rely on its contents having any | |
2028 | particular form, but it can be counted on to contain at least the | |
2029 | release number. | |
2030 | ||
2031 | @item __OPTIMIZE__ | |
2032 | @itemx __OPTIMIZE_SIZE__ | |
2033 | @itemx __NO_INLINE__ | |
2034 | These macros describe the compilation mode. @code{__OPTIMIZE__} is | |
2035 | defined in all optimizing compilations. @code{__OPTIMIZE_SIZE__} is | |
2036 | defined if the compiler is optimizing for size, not speed. | |
2037 | @code{__NO_INLINE__} is defined if no functions will be inlined into | |
2038 | their callers (when not optimizing, or when inlining has been | |
2039 | specifically disabled by @option{-fno-inline}). | |
2040 | ||
2041 | These macros cause certain GNU header files to provide optimized | |
2042 | definitions, using macros or inline functions, of system library | |
2043 | functions. You should not use these macros in any way unless you make | |
2044 | sure that programs will execute with the same effect whether or not they | |
2045 | are defined. If they are defined, their value is 1. | |
2046 | ||
2047 | @item __GNUC_GNU_INLINE__ | |
2048 | GCC defines this macro if functions declared @code{inline} will be | |
2049 | handled in GCC's traditional gnu90 mode. Object files will contain | |
2050 | externally visible definitions of all functions declared @code{inline} | |
2051 | without @code{extern} or @code{static}. They will not contain any | |
2052 | definitions of any functions declared @code{extern inline}. | |
2053 | ||
2054 | @item __GNUC_STDC_INLINE__ | |
2055 | GCC defines this macro if functions declared @code{inline} will be | |
2056 | handled according to the ISO C99 or later standards. Object files will contain | |
2057 | externally visible definitions of all functions declared @code{extern | |
2058 | inline}. They will not contain definitions of any functions declared | |
2059 | @code{inline} without @code{extern}. | |
2060 | ||
2061 | If this macro is defined, GCC supports the @code{gnu_inline} function | |
2062 | attribute as a way to always get the gnu90 behavior. | |
2063 | ||
2064 | @item __CHAR_UNSIGNED__ | |
2065 | GCC defines this macro if and only if the data type @code{char} is | |
2066 | unsigned on the target machine. It exists to cause the standard header | |
2067 | file @file{limits.h} to work correctly. You should not use this macro | |
2068 | yourself; instead, refer to the standard macros defined in @file{limits.h}. | |
2069 | ||
2070 | @item __WCHAR_UNSIGNED__ | |
2071 | Like @code{__CHAR_UNSIGNED__}, this macro is defined if and only if the | |
2072 | data type @code{wchar_t} is unsigned and the front-end is in C++ mode. | |
2073 | ||
2074 | @item __REGISTER_PREFIX__ | |
2075 | This macro expands to a single token (not a string constant) which is | |
2076 | the prefix applied to CPU register names in assembly language for this | |
2077 | target. You can use it to write assembly that is usable in multiple | |
2078 | environments. For example, in the @code{m68k-aout} environment it | |
2079 | expands to nothing, but in the @code{m68k-coff} environment it expands | |
2080 | to a single @samp{%}. | |
2081 | ||
2082 | @item __USER_LABEL_PREFIX__ | |
2083 | This macro expands to a single token which is the prefix applied to | |
2084 | user labels (symbols visible to C code) in assembly. For example, in | |
2085 | the @code{m68k-aout} environment it expands to an @samp{_}, but in the | |
2086 | @code{m68k-coff} environment it expands to nothing. | |
2087 | ||
2088 | This macro will have the correct definition even if | |
2089 | @option{-f(no-)underscores} is in use, but it will not be correct if | |
2090 | target-specific options that adjust this prefix are used (e.g.@: the | |
2091 | OSF/rose @option{-mno-underscores} option). | |
2092 | ||
2093 | @item __SIZE_TYPE__ | |
2094 | @itemx __PTRDIFF_TYPE__ | |
2095 | @itemx __WCHAR_TYPE__ | |
2096 | @itemx __WINT_TYPE__ | |
2097 | @itemx __INTMAX_TYPE__ | |
2098 | @itemx __UINTMAX_TYPE__ | |
2099 | @itemx __SIG_ATOMIC_TYPE__ | |
2100 | @itemx __INT8_TYPE__ | |
2101 | @itemx __INT16_TYPE__ | |
2102 | @itemx __INT32_TYPE__ | |
2103 | @itemx __INT64_TYPE__ | |
2104 | @itemx __UINT8_TYPE__ | |
2105 | @itemx __UINT16_TYPE__ | |
2106 | @itemx __UINT32_TYPE__ | |
2107 | @itemx __UINT64_TYPE__ | |
2108 | @itemx __INT_LEAST8_TYPE__ | |
2109 | @itemx __INT_LEAST16_TYPE__ | |
2110 | @itemx __INT_LEAST32_TYPE__ | |
2111 | @itemx __INT_LEAST64_TYPE__ | |
2112 | @itemx __UINT_LEAST8_TYPE__ | |
2113 | @itemx __UINT_LEAST16_TYPE__ | |
2114 | @itemx __UINT_LEAST32_TYPE__ | |
2115 | @itemx __UINT_LEAST64_TYPE__ | |
2116 | @itemx __INT_FAST8_TYPE__ | |
2117 | @itemx __INT_FAST16_TYPE__ | |
2118 | @itemx __INT_FAST32_TYPE__ | |
2119 | @itemx __INT_FAST64_TYPE__ | |
2120 | @itemx __UINT_FAST8_TYPE__ | |
2121 | @itemx __UINT_FAST16_TYPE__ | |
2122 | @itemx __UINT_FAST32_TYPE__ | |
2123 | @itemx __UINT_FAST64_TYPE__ | |
2124 | @itemx __INTPTR_TYPE__ | |
2125 | @itemx __UINTPTR_TYPE__ | |
2126 | These macros are defined to the correct underlying types for the | |
2127 | @code{size_t}, @code{ptrdiff_t}, @code{wchar_t}, @code{wint_t}, | |
2128 | @code{intmax_t}, @code{uintmax_t}, @code{sig_atomic_t}, @code{int8_t}, | |
2129 | @code{int16_t}, @code{int32_t}, @code{int64_t}, @code{uint8_t}, | |
2130 | @code{uint16_t}, @code{uint32_t}, @code{uint64_t}, | |
2131 | @code{int_least8_t}, @code{int_least16_t}, @code{int_least32_t}, | |
2132 | @code{int_least64_t}, @code{uint_least8_t}, @code{uint_least16_t}, | |
2133 | @code{uint_least32_t}, @code{uint_least64_t}, @code{int_fast8_t}, | |
2134 | @code{int_fast16_t}, @code{int_fast32_t}, @code{int_fast64_t}, | |
2135 | @code{uint_fast8_t}, @code{uint_fast16_t}, @code{uint_fast32_t}, | |
2136 | @code{uint_fast64_t}, @code{intptr_t}, and @code{uintptr_t} typedefs, | |
2137 | respectively. They exist to make the standard header files | |
2138 | @file{stddef.h}, @file{stdint.h}, and @file{wchar.h} work correctly. | |
2139 | You should not use these macros directly; instead, include the | |
2140 | appropriate headers and use the typedefs. Some of these macros may | |
2141 | not be defined on particular systems if GCC does not provide a | |
2142 | @file{stdint.h} header on those systems. | |
2143 | ||
2144 | @item __CHAR_BIT__ | |
2145 | Defined to the number of bits used in the representation of the | |
2146 | @code{char} data type. It exists to make the standard header given | |
2147 | numerical limits work correctly. You should not use | |
2148 | this macro directly; instead, include the appropriate headers. | |
2149 | ||
2150 | @item __SCHAR_MAX__ | |
2151 | @itemx __WCHAR_MAX__ | |
2152 | @itemx __SHRT_MAX__ | |
2153 | @itemx __INT_MAX__ | |
2154 | @itemx __LONG_MAX__ | |
2155 | @itemx __LONG_LONG_MAX__ | |
2156 | @itemx __WINT_MAX__ | |
2157 | @itemx __SIZE_MAX__ | |
2158 | @itemx __PTRDIFF_MAX__ | |
2159 | @itemx __INTMAX_MAX__ | |
2160 | @itemx __UINTMAX_MAX__ | |
2161 | @itemx __SIG_ATOMIC_MAX__ | |
2162 | @itemx __INT8_MAX__ | |
2163 | @itemx __INT16_MAX__ | |
2164 | @itemx __INT32_MAX__ | |
2165 | @itemx __INT64_MAX__ | |
2166 | @itemx __UINT8_MAX__ | |
2167 | @itemx __UINT16_MAX__ | |
2168 | @itemx __UINT32_MAX__ | |
2169 | @itemx __UINT64_MAX__ | |
2170 | @itemx __INT_LEAST8_MAX__ | |
2171 | @itemx __INT_LEAST16_MAX__ | |
2172 | @itemx __INT_LEAST32_MAX__ | |
2173 | @itemx __INT_LEAST64_MAX__ | |
2174 | @itemx __UINT_LEAST8_MAX__ | |
2175 | @itemx __UINT_LEAST16_MAX__ | |
2176 | @itemx __UINT_LEAST32_MAX__ | |
2177 | @itemx __UINT_LEAST64_MAX__ | |
2178 | @itemx __INT_FAST8_MAX__ | |
2179 | @itemx __INT_FAST16_MAX__ | |
2180 | @itemx __INT_FAST32_MAX__ | |
2181 | @itemx __INT_FAST64_MAX__ | |
2182 | @itemx __UINT_FAST8_MAX__ | |
2183 | @itemx __UINT_FAST16_MAX__ | |
2184 | @itemx __UINT_FAST32_MAX__ | |
2185 | @itemx __UINT_FAST64_MAX__ | |
2186 | @itemx __INTPTR_MAX__ | |
2187 | @itemx __UINTPTR_MAX__ | |
2188 | @itemx __WCHAR_MIN__ | |
2189 | @itemx __WINT_MIN__ | |
2190 | @itemx __SIG_ATOMIC_MIN__ | |
2191 | Defined to the maximum value of the @code{signed char}, @code{wchar_t}, | |
2192 | @code{signed short}, | |
2193 | @code{signed int}, @code{signed long}, @code{signed long long}, | |
2194 | @code{wint_t}, @code{size_t}, @code{ptrdiff_t}, | |
2195 | @code{intmax_t}, @code{uintmax_t}, @code{sig_atomic_t}, @code{int8_t}, | |
2196 | @code{int16_t}, @code{int32_t}, @code{int64_t}, @code{uint8_t}, | |
2197 | @code{uint16_t}, @code{uint32_t}, @code{uint64_t}, | |
2198 | @code{int_least8_t}, @code{int_least16_t}, @code{int_least32_t}, | |
2199 | @code{int_least64_t}, @code{uint_least8_t}, @code{uint_least16_t}, | |
2200 | @code{uint_least32_t}, @code{uint_least64_t}, @code{int_fast8_t}, | |
2201 | @code{int_fast16_t}, @code{int_fast32_t}, @code{int_fast64_t}, | |
2202 | @code{uint_fast8_t}, @code{uint_fast16_t}, @code{uint_fast32_t}, | |
2203 | @code{uint_fast64_t}, @code{intptr_t}, and @code{uintptr_t} types and | |
2204 | to the minimum value of the @code{wchar_t}, @code{wint_t}, and | |
2205 | @code{sig_atomic_t} types respectively. They exist to make the | |
2206 | standard header given numerical limits work correctly. You should not | |
2207 | use these macros directly; instead, include the appropriate headers. | |
2208 | Some of these macros may not be defined on particular systems if GCC | |
2209 | does not provide a @file{stdint.h} header on those systems. | |
2210 | ||
2211 | @item __INT8_C | |
2212 | @itemx __INT16_C | |
2213 | @itemx __INT32_C | |
2214 | @itemx __INT64_C | |
2215 | @itemx __UINT8_C | |
2216 | @itemx __UINT16_C | |
2217 | @itemx __UINT32_C | |
2218 | @itemx __UINT64_C | |
2219 | @itemx __INTMAX_C | |
2220 | @itemx __UINTMAX_C | |
2221 | Defined to implementations of the standard @file{stdint.h} macros with | |
2222 | the same names without the leading @code{__}. They exist the make the | |
2223 | implementation of that header work correctly. You should not use | |
2224 | these macros directly; instead, include the appropriate headers. Some | |
2225 | of these macros may not be defined on particular systems if GCC does | |
2226 | not provide a @file{stdint.h} header on those systems. | |
2227 | ||
2228 | @item __SCHAR_WIDTH__ | |
2229 | @itemx __SHRT_WIDTH__ | |
2230 | @itemx __INT_WIDTH__ | |
2231 | @itemx __LONG_WIDTH__ | |
2232 | @itemx __LONG_LONG_WIDTH__ | |
2233 | @itemx __PTRDIFF_WIDTH__ | |
2234 | @itemx __SIG_ATOMIC_WIDTH__ | |
2235 | @itemx __SIZE_WIDTH__ | |
2236 | @itemx __WCHAR_WIDTH__ | |
2237 | @itemx __WINT_WIDTH__ | |
2238 | @itemx __INT_LEAST8_WIDTH__ | |
2239 | @itemx __INT_LEAST16_WIDTH__ | |
2240 | @itemx __INT_LEAST32_WIDTH__ | |
2241 | @itemx __INT_LEAST64_WIDTH__ | |
2242 | @itemx __INT_FAST8_WIDTH__ | |
2243 | @itemx __INT_FAST16_WIDTH__ | |
2244 | @itemx __INT_FAST32_WIDTH__ | |
2245 | @itemx __INT_FAST64_WIDTH__ | |
2246 | @itemx __INTPTR_WIDTH__ | |
2247 | @itemx __INTMAX_WIDTH__ | |
2248 | Defined to the bit widths of the corresponding types. They exist to | |
2249 | make the implementations of @file{limits.h} and @file{stdint.h} behave | |
2250 | correctly. You should not use these macros directly; instead, include | |
2251 | the appropriate headers. Some of these macros may not be defined on | |
2252 | particular systems if GCC does not provide a @file{stdint.h} header on | |
2253 | those systems. | |
2254 | ||
2255 | @item __SIZEOF_INT__ | |
2256 | @itemx __SIZEOF_LONG__ | |
2257 | @itemx __SIZEOF_LONG_LONG__ | |
2258 | @itemx __SIZEOF_SHORT__ | |
2259 | @itemx __SIZEOF_POINTER__ | |
2260 | @itemx __SIZEOF_FLOAT__ | |
2261 | @itemx __SIZEOF_DOUBLE__ | |
2262 | @itemx __SIZEOF_LONG_DOUBLE__ | |
2263 | @itemx __SIZEOF_SIZE_T__ | |
2264 | @itemx __SIZEOF_WCHAR_T__ | |
2265 | @itemx __SIZEOF_WINT_T__ | |
2266 | @itemx __SIZEOF_PTRDIFF_T__ | |
2267 | Defined to the number of bytes of the C standard data types: @code{int}, | |
2268 | @code{long}, @code{long long}, @code{short}, @code{void *}, @code{float}, | |
2269 | @code{double}, @code{long double}, @code{size_t}, @code{wchar_t}, @code{wint_t} | |
2270 | and @code{ptrdiff_t}. | |
2271 | ||
2272 | @item __BYTE_ORDER__ | |
2273 | @itemx __ORDER_LITTLE_ENDIAN__ | |
2274 | @itemx __ORDER_BIG_ENDIAN__ | |
2275 | @itemx __ORDER_PDP_ENDIAN__ | |
2276 | @code{__BYTE_ORDER__} is defined to one of the values | |
2277 | @code{__ORDER_LITTLE_ENDIAN__}, @code{__ORDER_BIG_ENDIAN__}, or | |
2278 | @code{__ORDER_PDP_ENDIAN__} to reflect the layout of multi-byte and | |
2279 | multi-word quantities in memory. If @code{__BYTE_ORDER__} is equal to | |
2280 | @code{__ORDER_LITTLE_ENDIAN__} or @code{__ORDER_BIG_ENDIAN__}, then | |
2281 | multi-byte and multi-word quantities are laid out identically: the | |
2282 | byte (word) at the lowest address is the least significant or most | |
2283 | significant byte (word) of the quantity, respectively. If | |
2284 | @code{__BYTE_ORDER__} is equal to @code{__ORDER_PDP_ENDIAN__}, then | |
2285 | bytes in 16-bit words are laid out in a little-endian fashion, whereas | |
2286 | the 16-bit subwords of a 32-bit quantity are laid out in big-endian | |
2287 | fashion. | |
2288 | ||
2289 | You should use these macros for testing like this: | |
2290 | ||
2291 | @smallexample | |
2292 | /* @r{Test for a little-endian machine} */ | |
2293 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ | |
2294 | @end smallexample | |
2295 | ||
2296 | @item __FLOAT_WORD_ORDER__ | |
2297 | @code{__FLOAT_WORD_ORDER__} is defined to one of the values | |
2298 | @code{__ORDER_LITTLE_ENDIAN__} or @code{__ORDER_BIG_ENDIAN__} to reflect | |
2299 | the layout of the words of multi-word floating-point quantities. | |
2300 | ||
2301 | @item __DEPRECATED | |
2302 | This macro is defined, with value 1, when compiling a C++ source file | |
2303 | with warnings about deprecated constructs enabled. These warnings are | |
2304 | enabled by default, but can be disabled with @option{-Wno-deprecated}. | |
2305 | ||
2306 | @item __EXCEPTIONS | |
2307 | This macro is defined, with value 1, when compiling a C++ source file | |
2308 | with exceptions enabled. If @option{-fno-exceptions} is used when | |
2309 | compiling the file, then this macro is not defined. | |
2310 | ||
2311 | @item __GXX_RTTI | |
2312 | This macro is defined, with value 1, when compiling a C++ source file | |
2313 | with runtime type identification enabled. If @option{-fno-rtti} is | |
2314 | used when compiling the file, then this macro is not defined. | |
2315 | ||
2316 | @item __USING_SJLJ_EXCEPTIONS__ | |
2317 | This macro is defined, with value 1, if the compiler uses the old | |
2318 | mechanism based on @code{setjmp} and @code{longjmp} for exception | |
2319 | handling. | |
2320 | ||
2321 | @item __GXX_EXPERIMENTAL_CXX0X__ | |
2322 | This macro is defined when compiling a C++ source file with C++11 features | |
2323 | enabled, i.e., for all C++ language dialects except @option{-std=c++98} | |
2324 | and @option{-std=gnu++98}. This macro is obsolete, but can be used to | |
2325 | detect experimental C++0x features in very old versions of GCC. Since | |
2326 | GCC 4.7.0 the @code{__cplusplus} macro is defined correctly, so most | |
2327 | code should test @code{__cplusplus >= 201103L} instead of using this | |
2328 | macro. | |
2329 | ||
2330 | @item __GXX_WEAK__ | |
2331 | This macro is defined when compiling a C++ source file. It has the | |
2332 | value 1 if the compiler will use weak symbols, COMDAT sections, or | |
2333 | other similar techniques to collapse symbols with ``vague linkage'' | |
2334 | that are defined in multiple translation units. If the compiler will | |
2335 | not collapse such symbols, this macro is defined with value 0. In | |
2336 | general, user code should not need to make use of this macro; the | |
2337 | purpose of this macro is to ease implementation of the C++ runtime | |
2338 | library provided with G++. | |
2339 | ||
2340 | @item __NEXT_RUNTIME__ | |
2341 | This macro is defined, with value 1, if (and only if) the NeXT runtime | |
2342 | (as in @option{-fnext-runtime}) is in use for Objective-C@. If the GNU | |
2343 | runtime is used, this macro is not defined, so that you can use this | |
2344 | macro to determine which runtime (NeXT or GNU) is being used. | |
2345 | ||
2346 | @item __LP64__ | |
2347 | @itemx _LP64 | |
2348 | These macros are defined, with value 1, if (and only if) the compilation | |
2349 | is for a target where @code{long int} and pointer both use 64-bits and | |
2350 | @code{int} uses 32-bit. | |
2351 | ||
2352 | @item __SSP__ | |
2353 | This macro is defined, with value 1, when @option{-fstack-protector} is in | |
2354 | use. | |
2355 | ||
2356 | @item __SSP_ALL__ | |
2357 | This macro is defined, with value 2, when @option{-fstack-protector-all} is | |
2358 | in use. | |
2359 | ||
2360 | @item __SSP_STRONG__ | |
2361 | This macro is defined, with value 3, when @option{-fstack-protector-strong} is | |
2362 | in use. | |
2363 | ||
2364 | @item __SSP_EXPLICIT__ | |
2365 | This macro is defined, with value 4, when @option{-fstack-protector-explicit} is | |
2366 | in use. | |
2367 | ||
2368 | @item __SANITIZE_ADDRESS__ | |
2369 | This macro is defined, with value 1, when @option{-fsanitize=address} | |
2370 | or @option{-fsanitize=kernel-address} are in use. | |
2371 | ||
2372 | @item __SANITIZE_THREAD__ | |
2373 | This macro is defined, with value 1, when @option{-fsanitize=thread} is in use. | |
2374 | ||
2375 | @item __TIMESTAMP__ | |
2376 | This macro expands to a string constant that describes the date and time | |
2377 | of the last modification of the current source file. The string constant | |
2378 | contains abbreviated day of the week, month, day of the month, time in | |
2379 | hh:mm:ss form, year and looks like @code{@w{"Sun Sep 16 01:03:52 1973"}}. | |
2380 | If the day of the month is less than 10, it is padded with a space on the left. | |
2381 | ||
2382 | If GCC cannot determine the current date, it will emit a warning message | |
2383 | (once per compilation) and @code{__TIMESTAMP__} will expand to | |
2384 | @code{@w{"??? ??? ?? ??:??:?? ????"}}. | |
2385 | ||
2386 | @item __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 | |
2387 | @itemx __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 | |
2388 | @itemx __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 | |
2389 | @itemx __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 | |
2390 | @itemx __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16 | |
2391 | These macros are defined when the target processor supports atomic compare | |
2392 | and swap operations on operands 1, 2, 4, 8 or 16 bytes in length, respectively. | |
2393 | ||
2394 | @item __HAVE_SPECULATION_SAFE_VALUE | |
2395 | This macro is defined with the value 1 to show that this version of GCC | |
2396 | supports @code{__builtin_speculation_safe_value}. | |
2397 | ||
2398 | @item __GCC_HAVE_DWARF2_CFI_ASM | |
2399 | This macro is defined when the compiler is emitting DWARF CFI directives | |
2400 | to the assembler. When this is defined, it is possible to emit those same | |
2401 | directives in inline assembly. | |
2402 | ||
2403 | @item __FP_FAST_FMA | |
2404 | @itemx __FP_FAST_FMAF | |
2405 | @itemx __FP_FAST_FMAL | |
2406 | These macros are defined with value 1 if the backend supports the | |
2407 | @code{fma}, @code{fmaf}, and @code{fmal} builtin functions, so that | |
2408 | the include file @file{math.h} can define the macros | |
2409 | @code{FP_FAST_FMA}, @code{FP_FAST_FMAF}, and @code{FP_FAST_FMAL} | |
2410 | for compatibility with the 1999 C standard. | |
2411 | ||
2412 | @item __FP_FAST_FMAF16 | |
2413 | @itemx __FP_FAST_FMAF32 | |
2414 | @itemx __FP_FAST_FMAF64 | |
2415 | @itemx __FP_FAST_FMAF128 | |
2416 | @itemx __FP_FAST_FMAF32X | |
2417 | @itemx __FP_FAST_FMAF64X | |
2418 | @itemx __FP_FAST_FMAF128X | |
2419 | These macros are defined with the value 1 if the backend supports the | |
2420 | @code{fma} functions using the additional @code{_Float@var{n}} and | |
2421 | @code{_Float@var{n}x} types that are defined in ISO/IEC TS | |
2422 | 18661-3:2015. The include file @file{math.h} can define the | |
2423 | @code{FP_FAST_FMAF@var{n}} and @code{FP_FAST_FMAF@var{n}x} macros if | |
2424 | the user defined @code{__STDC_WANT_IEC_60559_TYPES_EXT__} before | |
2425 | including @file{math.h}. | |
2426 | ||
2427 | @item __GCC_IEC_559 | |
2428 | This macro is defined to indicate the intended level of support for | |
2429 | IEEE 754 (IEC 60559) floating-point arithmetic. It expands to a | |
2430 | nonnegative integer value. If 0, it indicates that the combination of | |
2431 | the compiler configuration and the command-line options is not | |
2432 | intended to support IEEE 754 arithmetic for @code{float} and | |
2433 | @code{double} as defined in C99 and C11 Annex F (for example, that the | |
2434 | standard rounding modes and exceptions are not supported, or that | |
2435 | optimizations are enabled that conflict with IEEE 754 semantics). If | |
2436 | 1, it indicates that IEEE 754 arithmetic is intended to be supported; | |
2437 | this does not mean that all relevant language features are supported | |
2438 | by GCC. If 2 or more, it additionally indicates support for IEEE | |
2439 | 754-2008 (in particular, that the binary encodings for quiet and | |
2440 | signaling NaNs are as specified in IEEE 754-2008). | |
2441 | ||
2442 | This macro does not indicate the default state of command-line options | |
2443 | that control optimizations that C99 and C11 permit to be controlled by | |
2444 | standard pragmas, where those standards do not require a particular | |
2445 | default state. It does not indicate whether optimizations respect | |
2446 | signaling NaN semantics (the macro for that is | |
2447 | @code{__SUPPORT_SNAN__}). It does not indicate support for decimal | |
2448 | floating point or the IEEE 754 binary16 and binary128 types. | |
2449 | ||
2450 | @item __GCC_IEC_559_COMPLEX | |
2451 | This macro is defined to indicate the intended level of support for | |
2452 | IEEE 754 (IEC 60559) floating-point arithmetic for complex numbers, as | |
2453 | defined in C99 and C11 Annex G. It expands to a nonnegative integer | |
2454 | value. If 0, it indicates that the combination of the compiler | |
2455 | configuration and the command-line options is not intended to support | |
2456 | Annex G requirements (for example, because @option{-fcx-limited-range} | |
2457 | was used). If 1 or more, it indicates that it is intended to support | |
2458 | those requirements; this does not mean that all relevant language | |
2459 | features are supported by GCC. | |
2460 | ||
2461 | @item __NO_MATH_ERRNO__ | |
2462 | This macro is defined if @option{-fno-math-errno} is used, or enabled | |
2463 | by another option such as @option{-ffast-math} or by default. | |
2464 | ||
2465 | @item __RECIPROCAL_MATH__ | |
2466 | This macro is defined if @option{-freciprocal-math} is used, or enabled | |
2467 | by another option such as @option{-ffast-math} or by default. | |
2468 | ||
2469 | @item __NO_SIGNED_ZEROS__ | |
2470 | This macro is defined if @option{-fno-signed-zeros} is used, or enabled | |
2471 | by another option such as @option{-ffast-math} or by default. | |
2472 | ||
2473 | @item __NO_TRAPPING_MATH__ | |
2474 | This macro is defined if @option{-fno-trapping-math} is used. | |
2475 | ||
2476 | @item __ASSOCIATIVE_MATH__ | |
2477 | This macro is defined if @option{-fassociative-math} is used, or enabled | |
2478 | by another option such as @option{-ffast-math} or by default. | |
2479 | ||
2480 | @item __ROUNDING_MATH__ | |
2481 | This macro is defined if @option{-frounding-math} is used. | |
2482 | ||
2483 | @item __GNUC_EXECUTION_CHARSET_NAME | |
2484 | @itemx __GNUC_WIDE_EXECUTION_CHARSET_NAME | |
2485 | These macros are defined to expand to a narrow string literal of | |
2486 | the name of the narrow and wide compile-time execution character | |
2487 | set used. It directly reflects the name passed to the options | |
2488 | @option{-fexec-charset} and @option{-fwide-exec-charset}, or the defaults | |
2489 | documented for those options (that is, it can expand to something like | |
2490 | @code{"UTF-8"}). @xref{Invocation}. | |
2491 | @end table | |
2492 | ||
2493 | @node System-specific Predefined Macros | |
2494 | @subsection System-specific Predefined Macros | |
2495 | ||
2496 | @cindex system-specific predefined macros | |
2497 | @cindex predefined macros, system-specific | |
2498 | @cindex reserved namespace | |
2499 | ||
2500 | The C preprocessor normally predefines several macros that indicate what | |
2501 | type of system and machine is in use. They are obviously different on | |
2502 | each target supported by GCC@. This manual, being for all systems and | |
2503 | machines, cannot tell you what their names are, but you can use | |
2504 | @command{cpp -dM} to see them all. @xref{Invocation}. All system-specific | |
2505 | predefined macros expand to a constant value, so you can test them with | |
2506 | either @samp{#ifdef} or @samp{#if}. | |
2507 | ||
2508 | The C standard requires that all system-specific macros be part of the | |
2509 | @dfn{reserved namespace}. All names which begin with two underscores, | |
2510 | or an underscore and a capital letter, are reserved for the compiler and | |
2511 | library to use as they wish. However, historically system-specific | |
2512 | macros have had names with no special prefix; for instance, it is common | |
2513 | to find @code{unix} defined on Unix systems. For all such macros, GCC | |
2514 | provides a parallel macro with two underscores added at the beginning | |
2515 | and the end. If @code{unix} is defined, @code{__unix__} will be defined | |
2516 | too. There will never be more than two underscores; the parallel of | |
2517 | @code{_mips} is @code{__mips__}. | |
2518 | ||
2519 | When the @option{-ansi} option, or any @option{-std} option that | |
2520 | requests strict conformance, is given to the compiler, all the | |
2521 | system-specific predefined macros outside the reserved namespace are | |
2522 | suppressed. The parallel macros, inside the reserved namespace, remain | |
2523 | defined. | |
2524 | ||
2525 | We are slowly phasing out all predefined macros which are outside the | |
2526 | reserved namespace. You should never use them in new programs, and we | |
2527 | encourage you to correct older code to use the parallel macros whenever | |
2528 | you find it. We don't recommend you use the system-specific macros that | |
2529 | are in the reserved namespace, either. It is better in the long run to | |
2530 | check specifically for features you need, using a tool such as | |
2531 | @command{autoconf}. | |
2532 | ||
2533 | @node C++ Named Operators | |
2534 | @subsection C++ Named Operators | |
2535 | @cindex named operators | |
2536 | @cindex C++ named operators | |
2537 | @cindex @file{iso646.h} | |
2538 | ||
2539 | In C++, there are eleven keywords which are simply alternate spellings | |
2540 | of operators normally written with punctuation. These keywords are | |
2541 | treated as such even in the preprocessor. They function as operators in | |
2542 | @samp{#if}, and they cannot be defined as macros or poisoned. In C, you | |
2543 | can request that those keywords take their C++ meaning by including | |
2544 | @file{iso646.h}. That header defines each one as a normal object-like | |
2545 | macro expanding to the appropriate punctuator. | |
2546 | ||
2547 | These are the named operators and their corresponding punctuators: | |
2548 | ||
2549 | @multitable {Named Operator} {Punctuator} | |
2550 | @item Named Operator @tab Punctuator | |
2551 | @item @code{and} @tab @code{&&} | |
2552 | @item @code{and_eq} @tab @code{&=} | |
2553 | @item @code{bitand} @tab @code{&} | |
2554 | @item @code{bitor} @tab @code{|} | |
2555 | @item @code{compl} @tab @code{~} | |
2556 | @item @code{not} @tab @code{!} | |
2557 | @item @code{not_eq} @tab @code{!=} | |
2558 | @item @code{or} @tab @code{||} | |
2559 | @item @code{or_eq} @tab @code{|=} | |
2560 | @item @code{xor} @tab @code{^} | |
2561 | @item @code{xor_eq} @tab @code{^=} | |
2562 | @end multitable | |
2563 | ||
2564 | @node Undefining and Redefining Macros | |
2565 | @section Undefining and Redefining Macros | |
2566 | @cindex undefining macros | |
2567 | @cindex redefining macros | |
2568 | @findex #undef | |
2569 | ||
2570 | If a macro ceases to be useful, it may be @dfn{undefined} with the | |
2571 | @samp{#undef} directive. @samp{#undef} takes a single argument, the | |
2572 | name of the macro to undefine. You use the bare macro name, even if the | |
2573 | macro is function-like. It is an error if anything appears on the line | |
2574 | after the macro name. @samp{#undef} has no effect if the name is not a | |
2575 | macro. | |
2576 | ||
2577 | @smallexample | |
2578 | #define FOO 4 | |
2579 | x = FOO; @expansion{} x = 4; | |
2580 | #undef FOO | |
2581 | x = FOO; @expansion{} x = FOO; | |
2582 | @end smallexample | |
2583 | ||
2584 | Once a macro has been undefined, that identifier may be @dfn{redefined} | |
2585 | as a macro by a subsequent @samp{#define} directive. The new definition | |
2586 | need not have any resemblance to the old definition. | |
2587 | ||
2588 | However, if an identifier which is currently a macro is redefined, then | |
2589 | the new definition must be @dfn{effectively the same} as the old one. | |
2590 | Two macro definitions are effectively the same if: | |
2591 | @itemize @bullet | |
2592 | @item Both are the same type of macro (object- or function-like). | |
2593 | @item All the tokens of the replacement list are the same. | |
2594 | @item If there are any parameters, they are the same. | |
2595 | @item Whitespace appears in the same places in both. It need not be | |
2596 | exactly the same amount of whitespace, though. Remember that comments | |
2597 | count as whitespace. | |
2598 | @end itemize | |
2599 | ||
2600 | @noindent | |
2601 | These definitions are effectively the same: | |
2602 | @smallexample | |
2603 | #define FOUR (2 + 2) | |
2604 | #define FOUR (2 + 2) | |
2605 | #define FOUR (2 /* @r{two} */ + 2) | |
2606 | @end smallexample | |
2607 | @noindent | |
2608 | but these are not: | |
2609 | @smallexample | |
2610 | #define FOUR (2 + 2) | |
2611 | #define FOUR ( 2+2 ) | |
2612 | #define FOUR (2 * 2) | |
2613 | #define FOUR(score,and,seven,years,ago) (2 + 2) | |
2614 | @end smallexample | |
2615 | ||
2616 | If a macro is redefined with a definition that is not effectively the | |
2617 | same as the old one, the preprocessor issues a warning and changes the | |
2618 | macro to use the new definition. If the new definition is effectively | |
2619 | the same, the redefinition is silently ignored. This allows, for | |
2620 | instance, two different headers to define a common macro. The | |
2621 | preprocessor will only complain if the definitions do not match. | |
2622 | ||
2623 | @node Directives Within Macro Arguments | |
2624 | @section Directives Within Macro Arguments | |
2625 | @cindex macro arguments and directives | |
2626 | ||
2627 | Occasionally it is convenient to use preprocessor directives within | |
2628 | the arguments of a macro. The C and C++ standards declare that | |
2629 | behavior in these cases is undefined. GNU CPP | |
2630 | processes arbitrary directives within macro arguments in | |
2631 | exactly the same way as it would have processed the directive were the | |
2632 | function-like macro invocation not present. | |
2633 | ||
2634 | If, within a macro invocation, that macro is redefined, then the new | |
2635 | definition takes effect in time for argument pre-expansion, but the | |
2636 | original definition is still used for argument replacement. Here is a | |
2637 | pathological example: | |
2638 | ||
2639 | @smallexample | |
2640 | #define f(x) x x | |
2641 | f (1 | |
2642 | #undef f | |
2643 | #define f 2 | |
2644 | f) | |
2645 | @end smallexample | |
2646 | ||
2647 | @noindent | |
2648 | which expands to | |
2649 | ||
2650 | @smallexample | |
2651 | 1 2 1 2 | |
2652 | @end smallexample | |
2653 | ||
2654 | @noindent | |
2655 | with the semantics described above. | |
2656 | ||
2657 | @node Macro Pitfalls | |
2658 | @section Macro Pitfalls | |
2659 | @cindex problems with macros | |
2660 | @cindex pitfalls of macros | |
2661 | ||
2662 | In this section we describe some special rules that apply to macros and | |
2663 | macro expansion, and point out certain cases in which the rules have | |
2664 | counter-intuitive consequences that you must watch out for. | |
2665 | ||
2666 | @menu | |
2667 | * Misnesting:: | |
2668 | * Operator Precedence Problems:: | |
2669 | * Swallowing the Semicolon:: | |
2670 | * Duplication of Side Effects:: | |
2671 | * Self-Referential Macros:: | |
2672 | * Argument Prescan:: | |
2673 | * Newlines in Arguments:: | |
2674 | @end menu | |
2675 | ||
2676 | @node Misnesting | |
2677 | @subsection Misnesting | |
2678 | ||
2679 | When a macro is called with arguments, the arguments are substituted | |
2680 | into the macro body and the result is checked, together with the rest of | |
2681 | the input file, for more macro calls. It is possible to piece together | |
2682 | a macro call coming partially from the macro body and partially from the | |
2683 | arguments. For example, | |
2684 | ||
2685 | @smallexample | |
2686 | #define twice(x) (2*(x)) | |
2687 | #define call_with_1(x) x(1) | |
2688 | call_with_1 (twice) | |
2689 | @expansion{} twice(1) | |
2690 | @expansion{} (2*(1)) | |
2691 | @end smallexample | |
2692 | ||
2693 | Macro definitions do not have to have balanced parentheses. By writing | |
2694 | an unbalanced open parenthesis in a macro body, it is possible to create | |
2695 | a macro call that begins inside the macro body but ends outside of it. | |
2696 | For example, | |
2697 | ||
2698 | @smallexample | |
2699 | #define strange(file) fprintf (file, "%s %d", | |
2700 | @dots{} | |
2701 | strange(stderr) p, 35) | |
2702 | @expansion{} fprintf (stderr, "%s %d", p, 35) | |
2703 | @end smallexample | |
2704 | ||
2705 | The ability to piece together a macro call can be useful, but the use of | |
2706 | unbalanced open parentheses in a macro body is just confusing, and | |
2707 | should be avoided. | |
2708 | ||
2709 | @node Operator Precedence Problems | |
2710 | @subsection Operator Precedence Problems | |
2711 | @cindex parentheses in macro bodies | |
2712 | ||
2713 | You may have noticed that in most of the macro definition examples shown | |
2714 | above, each occurrence of a macro argument name had parentheses around | |
2715 | it. In addition, another pair of parentheses usually surround the | |
2716 | entire macro definition. Here is why it is best to write macros that | |
2717 | way. | |
2718 | ||
2719 | Suppose you define a macro as follows, | |
2720 | ||
2721 | @smallexample | |
2722 | #define ceil_div(x, y) (x + y - 1) / y | |
2723 | @end smallexample | |
2724 | ||
2725 | @noindent | |
2726 | whose purpose is to divide, rounding up. (One use for this operation is | |
2727 | to compute how many @code{int} objects are needed to hold a certain | |
2728 | number of @code{char} objects.) Then suppose it is used as follows: | |
2729 | ||
2730 | @smallexample | |
2731 | a = ceil_div (b & c, sizeof (int)); | |
2732 | @expansion{} a = (b & c + sizeof (int) - 1) / sizeof (int); | |
2733 | @end smallexample | |
2734 | ||
2735 | @noindent | |
2736 | This does not do what is intended. The operator-precedence rules of | |
2737 | C make it equivalent to this: | |
2738 | ||
2739 | @smallexample | |
2740 | a = (b & (c + sizeof (int) - 1)) / sizeof (int); | |
2741 | @end smallexample | |
2742 | ||
2743 | @noindent | |
2744 | What we want is this: | |
2745 | ||
2746 | @smallexample | |
2747 | a = ((b & c) + sizeof (int) - 1)) / sizeof (int); | |
2748 | @end smallexample | |
2749 | ||
2750 | @noindent | |
2751 | Defining the macro as | |
2752 | ||
2753 | @smallexample | |
2754 | #define ceil_div(x, y) ((x) + (y) - 1) / (y) | |
2755 | @end smallexample | |
2756 | ||
2757 | @noindent | |
2758 | provides the desired result. | |
2759 | ||
2760 | Unintended grouping can result in another way. Consider @code{sizeof | |
2761 | ceil_div(1, 2)}. That has the appearance of a C expression that would | |
2762 | compute the size of the type of @code{ceil_div (1, 2)}, but in fact it | |
2763 | means something very different. Here is what it expands to: | |
2764 | ||
2765 | @smallexample | |
2766 | sizeof ((1) + (2) - 1) / (2) | |
2767 | @end smallexample | |
2768 | ||
2769 | @noindent | |
2770 | This would take the size of an integer and divide it by two. The | |
2771 | precedence rules have put the division outside the @code{sizeof} when it | |
2772 | was intended to be inside. | |
2773 | ||
2774 | Parentheses around the entire macro definition prevent such problems. | |
2775 | Here, then, is the recommended way to define @code{ceil_div}: | |
2776 | ||
2777 | @smallexample | |
2778 | #define ceil_div(x, y) (((x) + (y) - 1) / (y)) | |
2779 | @end smallexample | |
2780 | ||
2781 | @node Swallowing the Semicolon | |
2782 | @subsection Swallowing the Semicolon | |
2783 | @cindex semicolons (after macro calls) | |
2784 | ||
2785 | Often it is desirable to define a macro that expands into a compound | |
2786 | statement. Consider, for example, the following macro, that advances a | |
2787 | pointer (the argument @code{p} says where to find it) across whitespace | |
2788 | characters: | |
2789 | ||
2790 | @smallexample | |
2791 | #define SKIP_SPACES(p, limit) \ | |
2792 | @{ char *lim = (limit); \ | |
2793 | while (p < lim) @{ \ | |
2794 | if (*p++ != ' ') @{ \ | |
2795 | p--; break; @}@}@} | |
2796 | @end smallexample | |
2797 | ||
2798 | @noindent | |
2799 | Here backslash-newline is used to split the macro definition, which must | |
2800 | be a single logical line, so that it resembles the way such code would | |
2801 | be laid out if not part of a macro definition. | |
2802 | ||
2803 | A call to this macro might be @code{SKIP_SPACES (p, lim)}. Strictly | |
2804 | speaking, the call expands to a compound statement, which is a complete | |
2805 | statement with no need for a semicolon to end it. However, since it | |
2806 | looks like a function call, it minimizes confusion if you can use it | |
2807 | like a function call, writing a semicolon afterward, as in | |
2808 | @code{SKIP_SPACES (p, lim);} | |
2809 | ||
2810 | This can cause trouble before @code{else} statements, because the | |
2811 | semicolon is actually a null statement. Suppose you write | |
2812 | ||
2813 | @smallexample | |
2814 | if (*p != 0) | |
2815 | SKIP_SPACES (p, lim); | |
2816 | else @dots{} | |
2817 | @end smallexample | |
2818 | ||
2819 | @noindent | |
2820 | The presence of two statements---the compound statement and a null | |
2821 | statement---in between the @code{if} condition and the @code{else} | |
2822 | makes invalid C code. | |
2823 | ||
2824 | The definition of the macro @code{SKIP_SPACES} can be altered to solve | |
2825 | this problem, using a @code{do @dots{} while} statement. Here is how: | |
2826 | ||
2827 | @smallexample | |
2828 | #define SKIP_SPACES(p, limit) \ | |
2829 | do @{ char *lim = (limit); \ | |
2830 | while (p < lim) @{ \ | |
2831 | if (*p++ != ' ') @{ \ | |
2832 | p--; break; @}@}@} \ | |
2833 | while (0) | |
2834 | @end smallexample | |
2835 | ||
2836 | Now @code{SKIP_SPACES (p, lim);} expands into | |
2837 | ||
2838 | @smallexample | |
2839 | do @{@dots{}@} while (0); | |
2840 | @end smallexample | |
2841 | ||
2842 | @noindent | |
2843 | which is one statement. The loop executes exactly once; most compilers | |
2844 | generate no extra code for it. | |
2845 | ||
2846 | @node Duplication of Side Effects | |
2847 | @subsection Duplication of Side Effects | |
2848 | ||
2849 | @cindex side effects (in macro arguments) | |
2850 | @cindex unsafe macros | |
2851 | Many C programs define a macro @code{min}, for ``minimum'', like this: | |
2852 | ||
2853 | @smallexample | |
2854 | #define min(X, Y) ((X) < (Y) ? (X) : (Y)) | |
2855 | @end smallexample | |
2856 | ||
2857 | When you use this macro with an argument containing a side effect, | |
2858 | as shown here, | |
2859 | ||
2860 | @smallexample | |
2861 | next = min (x + y, foo (z)); | |
2862 | @end smallexample | |
2863 | ||
2864 | @noindent | |
2865 | it expands as follows: | |
2866 | ||
2867 | @smallexample | |
2868 | next = ((x + y) < (foo (z)) ? (x + y) : (foo (z))); | |
2869 | @end smallexample | |
2870 | ||
2871 | @noindent | |
2872 | where @code{x + y} has been substituted for @code{X} and @code{foo (z)} | |
2873 | for @code{Y}. | |
2874 | ||
2875 | The function @code{foo} is used only once in the statement as it appears | |
2876 | in the program, but the expression @code{foo (z)} has been substituted | |
2877 | twice into the macro expansion. As a result, @code{foo} might be called | |
2878 | two times when the statement is executed. If it has side effects or if | |
2879 | it takes a long time to compute, the results might not be what you | |
2880 | intended. We say that @code{min} is an @dfn{unsafe} macro. | |
2881 | ||
2882 | The best solution to this problem is to define @code{min} in a way that | |
2883 | computes the value of @code{foo (z)} only once. The C language offers | |
2884 | no standard way to do this, but it can be done with GNU extensions as | |
2885 | follows: | |
2886 | ||
2887 | @smallexample | |
2888 | #define min(X, Y) \ | |
2889 | (@{ typeof (X) x_ = (X); \ | |
2890 | typeof (Y) y_ = (Y); \ | |
2891 | (x_ < y_) ? x_ : y_; @}) | |
2892 | @end smallexample | |
2893 | ||
2894 | The @samp{(@{ @dots{} @})} notation produces a compound statement that | |
2895 | acts as an expression. Its value is the value of its last statement. | |
2896 | This permits us to define local variables and assign each argument to | |
2897 | one. The local variables have underscores after their names to reduce | |
2898 | the risk of conflict with an identifier of wider scope (it is impossible | |
2899 | to avoid this entirely). Now each argument is evaluated exactly once. | |
2900 | ||
2901 | If you do not wish to use GNU C extensions, the only solution is to be | |
2902 | careful when @emph{using} the macro @code{min}. For example, you can | |
2903 | calculate the value of @code{foo (z)}, save it in a variable, and use | |
2904 | that variable in @code{min}: | |
2905 | ||
2906 | @smallexample | |
2907 | @group | |
2908 | #define min(X, Y) ((X) < (Y) ? (X) : (Y)) | |
2909 | @dots{} | |
2910 | @{ | |
2911 | int tem = foo (z); | |
2912 | next = min (x + y, tem); | |
2913 | @} | |
2914 | @end group | |
2915 | @end smallexample | |
2916 | ||
2917 | @noindent | |
2918 | (where we assume that @code{foo} returns type @code{int}). | |
2919 | ||
2920 | @node Self-Referential Macros | |
2921 | @subsection Self-Referential Macros | |
2922 | @cindex self-reference | |
2923 | ||
2924 | A @dfn{self-referential} macro is one whose name appears in its | |
2925 | definition. Recall that all macro definitions are rescanned for more | |
2926 | macros to replace. If the self-reference were considered a use of the | |
2927 | macro, it would produce an infinitely large expansion. To prevent this, | |
2928 | the self-reference is not considered a macro call. It is passed into | |
2929 | the preprocessor output unchanged. Consider an example: | |
2930 | ||
2931 | @smallexample | |
2932 | #define foo (4 + foo) | |
2933 | @end smallexample | |
2934 | ||
2935 | @noindent | |
2936 | where @code{foo} is also a variable in your program. | |
2937 | ||
2938 | Following the ordinary rules, each reference to @code{foo} will expand | |
2939 | into @code{(4 + foo)}; then this will be rescanned and will expand into | |
2940 | @code{(4 + (4 + foo))}; and so on until the computer runs out of memory. | |
2941 | ||
2942 | The self-reference rule cuts this process short after one step, at | |
2943 | @code{(4 + foo)}. Therefore, this macro definition has the possibly | |
2944 | useful effect of causing the program to add 4 to the value of @code{foo} | |
2945 | wherever @code{foo} is referred to. | |
2946 | ||
2947 | In most cases, it is a bad idea to take advantage of this feature. A | |
2948 | person reading the program who sees that @code{foo} is a variable will | |
2949 | not expect that it is a macro as well. The reader will come across the | |
2950 | identifier @code{foo} in the program and think its value should be that | |
2951 | of the variable @code{foo}, whereas in fact the value is four greater. | |
2952 | ||
2953 | One common, useful use of self-reference is to create a macro which | |
2954 | expands to itself. If you write | |
2955 | ||
2956 | @smallexample | |
2957 | #define EPERM EPERM | |
2958 | @end smallexample | |
2959 | ||
2960 | @noindent | |
2961 | then the macro @code{EPERM} expands to @code{EPERM}. Effectively, it is | |
2962 | left alone by the preprocessor whenever it's used in running text. You | |
2963 | can tell that it's a macro with @samp{#ifdef}. You might do this if you | |
2964 | want to define numeric constants with an @code{enum}, but have | |
2965 | @samp{#ifdef} be true for each constant. | |
2966 | ||
2967 | If a macro @code{x} expands to use a macro @code{y}, and the expansion of | |
2968 | @code{y} refers to the macro @code{x}, that is an @dfn{indirect | |
2969 | self-reference} of @code{x}. @code{x} is not expanded in this case | |
2970 | either. Thus, if we have | |
2971 | ||
2972 | @smallexample | |
2973 | #define x (4 + y) | |
2974 | #define y (2 * x) | |
2975 | @end smallexample | |
2976 | ||
2977 | @noindent | |
2978 | then @code{x} and @code{y} expand as follows: | |
2979 | ||
2980 | @smallexample | |
2981 | @group | |
2982 | x @expansion{} (4 + y) | |
2983 | @expansion{} (4 + (2 * x)) | |
2984 | ||
2985 | y @expansion{} (2 * x) | |
2986 | @expansion{} (2 * (4 + y)) | |
2987 | @end group | |
2988 | @end smallexample | |
2989 | ||
2990 | @noindent | |
2991 | Each macro is expanded when it appears in the definition of the other | |
2992 | macro, but not when it indirectly appears in its own definition. | |
2993 | ||
2994 | @node Argument Prescan | |
2995 | @subsection Argument Prescan | |
2996 | @cindex expansion of arguments | |
2997 | @cindex macro argument expansion | |
2998 | @cindex prescan of macro arguments | |
2999 | ||
3000 | Macro arguments are completely macro-expanded before they are | |
3001 | substituted into a macro body, unless they are stringized or pasted | |
3002 | with other tokens. After substitution, the entire macro body, including | |
3003 | the substituted arguments, is scanned again for macros to be expanded. | |
3004 | The result is that the arguments are scanned @emph{twice} to expand | |
3005 | macro calls in them. | |
3006 | ||
3007 | Most of the time, this has no effect. If the argument contained any | |
3008 | macro calls, they are expanded during the first scan. The result | |
3009 | therefore contains no macro calls, so the second scan does not change | |
3010 | it. If the argument were substituted as given, with no prescan, the | |
3011 | single remaining scan would find the same macro calls and produce the | |
3012 | same results. | |
3013 | ||
3014 | You might expect the double scan to change the results when a | |
3015 | self-referential macro is used in an argument of another macro | |
3016 | (@pxref{Self-Referential Macros}): the self-referential macro would be | |
3017 | expanded once in the first scan, and a second time in the second scan. | |
3018 | However, this is not what happens. The self-references that do not | |
3019 | expand in the first scan are marked so that they will not expand in the | |
3020 | second scan either. | |
3021 | ||
3022 | You might wonder, ``Why mention the prescan, if it makes no difference? | |
3023 | And why not skip it and make the preprocessor faster?'' The answer is | |
3024 | that the prescan does make a difference in three special cases: | |
3025 | ||
3026 | @itemize @bullet | |
3027 | @item | |
3028 | Nested calls to a macro. | |
3029 | ||
3030 | We say that @dfn{nested} calls to a macro occur when a macro's argument | |
3031 | contains a call to that very macro. For example, if @code{f} is a macro | |
3032 | that expects one argument, @code{f (f (1))} is a nested pair of calls to | |
3033 | @code{f}. The desired expansion is made by expanding @code{f (1)} and | |
3034 | substituting that into the definition of @code{f}. The prescan causes | |
3035 | the expected result to happen. Without the prescan, @code{f (1)} itself | |
3036 | would be substituted as an argument, and the inner use of @code{f} would | |
3037 | appear during the main scan as an indirect self-reference and would not | |
3038 | be expanded. | |
3039 | ||
3040 | @item | |
3041 | Macros that call other macros that stringize or concatenate. | |
3042 | ||
3043 | If an argument is stringized or concatenated, the prescan does not | |
3044 | occur. If you @emph{want} to expand a macro, then stringize or | |
3045 | concatenate its expansion, you can do that by causing one macro to call | |
3046 | another macro that does the stringizing or concatenation. For | |
3047 | instance, if you have | |
3048 | ||
3049 | @smallexample | |
3050 | #define AFTERX(x) X_ ## x | |
3051 | #define XAFTERX(x) AFTERX(x) | |
3052 | #define TABLESIZE 1024 | |
3053 | #define BUFSIZE TABLESIZE | |
3054 | @end smallexample | |
3055 | ||
3056 | then @code{AFTERX(BUFSIZE)} expands to @code{X_BUFSIZE}, and | |
3057 | @code{XAFTERX(BUFSIZE)} expands to @code{X_1024}. (Not to | |
3058 | @code{X_TABLESIZE}. Prescan always does a complete expansion.) | |
3059 | ||
3060 | @item | |
3061 | Macros used in arguments, whose expansions contain unshielded commas. | |
3062 | ||
3063 | This can cause a macro expanded on the second scan to be called with the | |
3064 | wrong number of arguments. Here is an example: | |
3065 | ||
3066 | @smallexample | |
3067 | #define foo a,b | |
3068 | #define bar(x) lose(x) | |
3069 | #define lose(x) (1 + (x)) | |
3070 | @end smallexample | |
3071 | ||
3072 | We would like @code{bar(foo)} to turn into @code{(1 + (foo))}, which | |
3073 | would then turn into @code{(1 + (a,b))}. Instead, @code{bar(foo)} | |
3074 | expands into @code{lose(a,b)}, and you get an error because @code{lose} | |
3075 | requires a single argument. In this case, the problem is easily solved | |
3076 | by the same parentheses that ought to be used to prevent misnesting of | |
3077 | arithmetic operations: | |
3078 | ||
3079 | @smallexample | |
3080 | #define foo (a,b) | |
3081 | @exdent or | |
3082 | #define bar(x) lose((x)) | |
3083 | @end smallexample | |
3084 | ||
3085 | The extra pair of parentheses prevents the comma in @code{foo}'s | |
3086 | definition from being interpreted as an argument separator. | |
3087 | ||
3088 | @end itemize | |
3089 | ||
3090 | @node Newlines in Arguments | |
3091 | @subsection Newlines in Arguments | |
3092 | @cindex newlines in macro arguments | |
3093 | ||
3094 | The invocation of a function-like macro can extend over many logical | |
3095 | lines. However, in the present implementation, the entire expansion | |
3096 | comes out on one line. Thus line numbers emitted by the compiler or | |
3097 | debugger refer to the line the invocation started on, which might be | |
3098 | different to the line containing the argument causing the problem. | |
3099 | ||
3100 | Here is an example illustrating this: | |
3101 | ||
3102 | @smallexample | |
3103 | #define ignore_second_arg(a,b,c) a; c | |
3104 | ||
3105 | ignore_second_arg (foo (), | |
3106 | ignored (), | |
3107 | syntax error); | |
3108 | @end smallexample | |
3109 | ||
3110 | @noindent | |
3111 | The syntax error triggered by the tokens @code{syntax error} results in | |
3112 | an error message citing line three---the line of ignore_second_arg--- | |
3113 | even though the problematic code comes from line five. | |
3114 | ||
3115 | We consider this a bug, and intend to fix it in the near future. | |
3116 | ||
3117 | @node Conditionals | |
3118 | @chapter Conditionals | |
3119 | @cindex conditionals | |
3120 | ||
3121 | A @dfn{conditional} is a directive that instructs the preprocessor to | |
3122 | select whether or not to include a chunk of code in the final token | |
3123 | stream passed to the compiler. Preprocessor conditionals can test | |
3124 | arithmetic expressions, or whether a name is defined as a macro, or both | |
3125 | simultaneously using the special @code{defined} operator. | |
3126 | ||
3127 | A conditional in the C preprocessor resembles in some ways an @code{if} | |
3128 | statement in C, but it is important to understand the difference between | |
3129 | them. The condition in an @code{if} statement is tested during the | |
3130 | execution of your program. Its purpose is to allow your program to | |
3131 | behave differently from run to run, depending on the data it is | |
3132 | operating on. The condition in a preprocessing conditional directive is | |
3133 | tested when your program is compiled. Its purpose is to allow different | |
3134 | code to be included in the program depending on the situation at the | |
3135 | time of compilation. | |
3136 | ||
3137 | However, the distinction is becoming less clear. Modern compilers often | |
3138 | do test @code{if} statements when a program is compiled, if their | |
3139 | conditions are known not to vary at run time, and eliminate code which | |
3140 | can never be executed. If you can count on your compiler to do this, | |
3141 | you may find that your program is more readable if you use @code{if} | |
3142 | statements with constant conditions (perhaps determined by macros). Of | |
3143 | course, you can only use this to exclude code, not type definitions or | |
3144 | other preprocessing directives, and you can only do it if the code | |
3145 | remains syntactically valid when it is not to be used. | |
3146 | ||
3147 | @menu | |
3148 | * Conditional Uses:: | |
3149 | * Conditional Syntax:: | |
3150 | * Deleted Code:: | |
3151 | @end menu | |
3152 | ||
3153 | @node Conditional Uses | |
3154 | @section Conditional Uses | |
3155 | ||
3156 | There are three general reasons to use a conditional. | |
3157 | ||
3158 | @itemize @bullet | |
3159 | @item | |
3160 | A program may need to use different code depending on the machine or | |
3161 | operating system it is to run on. In some cases the code for one | |
3162 | operating system may be erroneous on another operating system; for | |
3163 | example, it might refer to data types or constants that do not exist on | |
3164 | the other system. When this happens, it is not enough to avoid | |
3165 | executing the invalid code. Its mere presence will cause the compiler | |
3166 | to reject the program. With a preprocessing conditional, the offending | |
3167 | code can be effectively excised from the program when it is not valid. | |
3168 | ||
3169 | @item | |
3170 | You may want to be able to compile the same source file into two | |
3171 | different programs. One version might make frequent time-consuming | |
3172 | consistency checks on its intermediate data, or print the values of | |
3173 | those data for debugging, and the other not. | |
3174 | ||
3175 | @item | |
3176 | A conditional whose condition is always false is one way to exclude code | |
3177 | from the program but keep it as a sort of comment for future reference. | |
3178 | @end itemize | |
3179 | ||
3180 | Simple programs that do not need system-specific logic or complex | |
3181 | debugging hooks generally will not need to use preprocessing | |
3182 | conditionals. | |
3183 | ||
3184 | @node Conditional Syntax | |
3185 | @section Conditional Syntax | |
3186 | ||
3187 | @findex #if | |
3188 | A conditional in the C preprocessor begins with a @dfn{conditional | |
3189 | directive}: @samp{#if}, @samp{#ifdef} or @samp{#ifndef}. | |
3190 | ||
3191 | @menu | |
3192 | * Ifdef:: | |
3193 | * If:: | |
3194 | * Defined:: | |
3195 | * Else:: | |
3196 | * Elif:: | |
3197 | * @code{__has_attribute}:: | |
3198 | * @code{__has_cpp_attribute}:: | |
3199 | * @code{__has_c_attribute}:: | |
3200 | * @code{__has_builtin}:: | |
3201 | * @code{__has_include}:: | |
3202 | @end menu | |
3203 | ||
3204 | @node Ifdef | |
3205 | @subsection Ifdef | |
3206 | @findex #ifdef | |
3207 | @findex #endif | |
3208 | ||
3209 | The simplest sort of conditional is | |
3210 | ||
3211 | @smallexample | |
3212 | @group | |
3213 | #ifdef @var{MACRO} | |
3214 | ||
3215 | @var{controlled text} | |
3216 | ||
3217 | #endif /* @var{MACRO} */ | |
3218 | @end group | |
3219 | @end smallexample | |
3220 | ||
3221 | @cindex conditional group | |
3222 | This block is called a @dfn{conditional group}. @var{controlled text} | |
3223 | will be included in the output of the preprocessor if and only if | |
3224 | @var{MACRO} is defined. We say that the conditional @dfn{succeeds} if | |
3225 | @var{MACRO} is defined, @dfn{fails} if it is not. | |
3226 | ||
3227 | The @var{controlled text} inside of a conditional can include | |
3228 | preprocessing directives. They are executed only if the conditional | |
3229 | succeeds. You can nest conditional groups inside other conditional | |
3230 | groups, but they must be completely nested. In other words, | |
3231 | @samp{#endif} always matches the nearest @samp{#ifdef} (or | |
3232 | @samp{#ifndef}, or @samp{#if}). Also, you cannot start a conditional | |
3233 | group in one file and end it in another. | |
3234 | ||
3235 | Even if a conditional fails, the @var{controlled text} inside it is | |
3236 | still run through initial transformations and tokenization. Therefore, | |
3237 | it must all be lexically valid C@. Normally the only way this matters is | |
3238 | that all comments and string literals inside a failing conditional group | |
3239 | must still be properly ended. | |
3240 | ||
3241 | The comment following the @samp{#endif} is not required, but it is a | |
3242 | good practice if there is a lot of @var{controlled text}, because it | |
3243 | helps people match the @samp{#endif} to the corresponding @samp{#ifdef}. | |
3244 | Older programs sometimes put @var{MACRO} directly after the | |
3245 | @samp{#endif} without enclosing it in a comment. This is invalid code | |
3246 | according to the C standard. CPP accepts it with a warning. It | |
3247 | never affects which @samp{#ifndef} the @samp{#endif} matches. | |
3248 | ||
3249 | @findex #ifndef | |
3250 | Sometimes you wish to use some code if a macro is @emph{not} defined. | |
3251 | You can do this by writing @samp{#ifndef} instead of @samp{#ifdef}. | |
3252 | One common use of @samp{#ifndef} is to include code only the first | |
3253 | time a header file is included. @xref{Once-Only Headers}. | |
3254 | ||
3255 | Macro definitions can vary between compilations for several reasons. | |
3256 | Here are some samples. | |
3257 | ||
3258 | @itemize @bullet | |
3259 | @item | |
3260 | Some macros are predefined on each kind of machine | |
3261 | (@pxref{System-specific Predefined Macros}). This allows you to provide | |
3262 | code specially tuned for a particular machine. | |
3263 | ||
3264 | @item | |
3265 | System header files define more macros, associated with the features | |
3266 | they implement. You can test these macros with conditionals to avoid | |
3267 | using a system feature on a machine where it is not implemented. | |
3268 | ||
3269 | @item | |
3270 | Macros can be defined or undefined with the @option{-D} and @option{-U} | |
3271 | command-line options when you compile the program. You can arrange to | |
3272 | compile the same source file into two different programs by choosing a | |
3273 | macro name to specify which program you want, writing conditionals to | |
3274 | test whether or how this macro is defined, and then controlling the | |
3275 | state of the macro with command-line options, perhaps set in the | |
3276 | Makefile. @xref{Invocation}. | |
3277 | ||
3278 | @item | |
3279 | Your program might have a special header file (often called | |
3280 | @file{config.h}) that is adjusted when the program is compiled. It can | |
3281 | define or not define macros depending on the features of the system and | |
3282 | the desired capabilities of the program. The adjustment can be | |
3283 | automated by a tool such as @command{autoconf}, or done by hand. | |
3284 | @end itemize | |
3285 | ||
3286 | @node If | |
3287 | @subsection If | |
3288 | ||
3289 | The @samp{#if} directive allows you to test the value of an arithmetic | |
3290 | expression, rather than the mere existence of one macro. Its syntax is | |
3291 | ||
3292 | @smallexample | |
3293 | @group | |
3294 | #if @var{expression} | |
3295 | ||
3296 | @var{controlled text} | |
3297 | ||
3298 | #endif /* @var{expression} */ | |
3299 | @end group | |
3300 | @end smallexample | |
3301 | ||
3302 | @var{expression} is a C expression of integer type, subject to stringent | |
3303 | restrictions. It may contain | |
3304 | ||
3305 | @itemize @bullet | |
3306 | @item | |
3307 | Integer constants. | |
3308 | ||
3309 | @item | |
3310 | Character constants, which are interpreted as they would be in normal | |
3311 | code. | |
3312 | ||
3313 | @item | |
3314 | Arithmetic operators for addition, subtraction, multiplication, | |
3315 | division, bitwise operations, shifts, comparisons, and logical | |
3316 | operations (@code{&&} and @code{||}). The latter two obey the usual | |
3317 | short-circuiting rules of standard C@. | |
3318 | ||
3319 | @item | |
3320 | Macros. All macros in the expression are expanded before actual | |
3321 | computation of the expression's value begins. | |
3322 | ||
3323 | @item | |
3324 | Uses of the @code{defined} operator, which lets you check whether macros | |
3325 | are defined in the middle of an @samp{#if}. | |
3326 | ||
3327 | @item | |
3328 | Identifiers that are not macros, which are all considered to be the | |
3329 | number zero. This allows you to write @code{@w{#if MACRO}} instead of | |
3330 | @code{@w{#ifdef MACRO}}, if you know that MACRO, when defined, will | |
3331 | always have a nonzero value. Function-like macros used without their | |
3332 | function call parentheses are also treated as zero. | |
3333 | ||
3334 | In some contexts this shortcut is undesirable. The @option{-Wundef} | |
3335 | option causes GCC to warn whenever it encounters an identifier which is | |
3336 | not a macro in an @samp{#if}. | |
3337 | @end itemize | |
3338 | ||
3339 | The preprocessor does not know anything about types in the language. | |
3340 | Therefore, @code{sizeof} operators are not recognized in @samp{#if}, and | |
3341 | neither are @code{enum} constants. They will be taken as identifiers | |
3342 | which are not macros, and replaced by zero. In the case of | |
3343 | @code{sizeof}, this is likely to cause the expression to be invalid. | |
3344 | ||
3345 | The preprocessor calculates the value of @var{expression}. It carries | |
3346 | out all calculations in the widest integer type known to the compiler; | |
3347 | on most machines supported by GCC this is 64 bits. This is not the same | |
3348 | rule as the compiler uses to calculate the value of a constant | |
3349 | expression, and may give different results in some cases. If the value | |
3350 | comes out to be nonzero, the @samp{#if} succeeds and the @var{controlled | |
3351 | text} is included; otherwise it is skipped. | |
3352 | ||
3353 | @node Defined | |
3354 | @subsection Defined | |
3355 | ||
3356 | @cindex @code{defined} | |
3357 | The special operator @code{defined} is used in @samp{#if} and | |
3358 | @samp{#elif} expressions to test whether a certain name is defined as a | |
3359 | macro. @code{defined @var{name}} and @code{defined (@var{name})} are | |
3360 | both expressions whose value is 1 if @var{name} is defined as a macro at | |
3361 | the current point in the program, and 0 otherwise. Thus, @code{@w{#if | |
3362 | defined MACRO}} is precisely equivalent to @code{@w{#ifdef MACRO}}. | |
3363 | ||
3364 | @code{defined} is useful when you wish to test more than one macro for | |
3365 | existence at once. For example, | |
3366 | ||
3367 | @smallexample | |
3368 | #if defined (__vax__) || defined (__ns16000__) | |
3369 | @end smallexample | |
3370 | ||
3371 | @noindent | |
3372 | would succeed if either of the names @code{__vax__} or | |
3373 | @code{__ns16000__} is defined as a macro. | |
3374 | ||
3375 | Conditionals written like this: | |
3376 | ||
3377 | @smallexample | |
3378 | #if defined BUFSIZE && BUFSIZE >= 1024 | |
3379 | @end smallexample | |
3380 | ||
3381 | @noindent | |
3382 | can generally be simplified to just @code{@w{#if BUFSIZE >= 1024}}, | |
3383 | since if @code{BUFSIZE} is not defined, it will be interpreted as having | |
3384 | the value zero. | |
3385 | ||
3386 | If the @code{defined} operator appears as a result of a macro expansion, | |
3387 | the C standard says the behavior is undefined. GNU cpp treats it as a | |
3388 | genuine @code{defined} operator and evaluates it normally. It will warn | |
3389 | wherever your code uses this feature if you use the command-line option | |
3390 | @option{-Wpedantic}, since other compilers may handle it differently. The | |
3391 | warning is also enabled by @option{-Wextra}, and can also be enabled | |
3392 | individually with @option{-Wexpansion-to-defined}. | |
3393 | ||
3394 | @node Else | |
3395 | @subsection Else | |
3396 | ||
3397 | @findex #else | |
3398 | The @samp{#else} directive can be added to a conditional to provide | |
3399 | alternative text to be used if the condition fails. This is what it | |
3400 | looks like: | |
3401 | ||
3402 | @smallexample | |
3403 | @group | |
3404 | #if @var{expression} | |
3405 | @var{text-if-true} | |
3406 | #else /* Not @var{expression} */ | |
3407 | @var{text-if-false} | |
3408 | #endif /* Not @var{expression} */ | |
3409 | @end group | |
3410 | @end smallexample | |
3411 | ||
3412 | @noindent | |
3413 | If @var{expression} is nonzero, the @var{text-if-true} is included and | |
3414 | the @var{text-if-false} is skipped. If @var{expression} is zero, the | |
3415 | opposite happens. | |
3416 | ||
3417 | You can use @samp{#else} with @samp{#ifdef} and @samp{#ifndef}, too. | |
3418 | ||
3419 | @node Elif | |
3420 | @subsection Elif | |
3421 | ||
3422 | @findex #elif | |
3423 | One common case of nested conditionals is used to check for more than two | |
3424 | possible alternatives. For example, you might have | |
3425 | ||
3426 | @smallexample | |
3427 | #if X == 1 | |
3428 | @dots{} | |
3429 | #else /* X != 1 */ | |
3430 | #if X == 2 | |
3431 | @dots{} | |
3432 | #else /* X != 2 */ | |
3433 | @dots{} | |
3434 | #endif /* X != 2 */ | |
3435 | #endif /* X != 1 */ | |
3436 | @end smallexample | |
3437 | ||
3438 | Another conditional directive, @samp{#elif}, allows this to be | |
3439 | abbreviated as follows: | |
3440 | ||
3441 | @smallexample | |
3442 | #if X == 1 | |
3443 | @dots{} | |
3444 | #elif X == 2 | |
3445 | @dots{} | |
3446 | #else /* X != 2 and X != 1*/ | |
3447 | @dots{} | |
3448 | #endif /* X != 2 and X != 1*/ | |
3449 | @end smallexample | |
3450 | ||
3451 | @samp{#elif} stands for ``else if''. Like @samp{#else}, it goes in the | |
3452 | middle of a conditional group and subdivides it; it does not require a | |
3453 | matching @samp{#endif} of its own. Like @samp{#if}, the @samp{#elif} | |
3454 | directive includes an expression to be tested. The text following the | |
3455 | @samp{#elif} is processed only if the original @samp{#if}-condition | |
3456 | failed and the @samp{#elif} condition succeeds. | |
3457 | ||
3458 | More than one @samp{#elif} can go in the same conditional group. Then | |
3459 | the text after each @samp{#elif} is processed only if the @samp{#elif} | |
3460 | condition succeeds after the original @samp{#if} and all previous | |
3461 | @samp{#elif} directives within it have failed. | |
3462 | ||
3463 | @samp{#else} is allowed after any number of @samp{#elif} directives, but | |
3464 | @samp{#elif} may not follow @samp{#else}. | |
3465 | ||
3466 | @node @code{__has_attribute} | |
3467 | @subsection @code{__has_attribute} | |
3468 | @cindex @code{__has_attribute} | |
3469 | ||
3470 | The special operator @code{__has_attribute (@var{operand})} may be used | |
3471 | in @samp{#if} and @samp{#elif} expressions to test whether the attribute | |
3472 | referenced by its @var{operand} is recognized by GCC. Using the operator | |
3473 | in other contexts is not valid. In C code, if compiling for strict | |
3474 | conformance to standards before C2x, @var{operand} must be | |
3475 | a valid identifier. Otherwise, @var{operand} may be optionally | |
3476 | introduced by the @code{@var{attribute-scope}::} prefix. | |
3477 | The @var{attribute-scope} prefix identifies the ``namespace'' within | |
3478 | which the attribute is recognized. The scope of GCC attributes is | |
3479 | @samp{gnu} or @samp{__gnu__}. The @code{__has_attribute} operator by | |
3480 | itself, without any @var{operand} or parentheses, acts as a predefined | |
3481 | macro so that support for it can be tested in portable code. Thus, | |
3482 | the recommended use of the operator is as follows: | |
3483 | ||
3484 | @smallexample | |
3485 | #if defined __has_attribute | |
3486 | # if __has_attribute (nonnull) | |
3487 | # define ATTR_NONNULL __attribute__ ((nonnull)) | |
3488 | # endif | |
3489 | #endif | |
3490 | @end smallexample | |
3491 | ||
3492 | The first @samp{#if} test succeeds only when the operator is supported | |
3493 | by the version of GCC (or another compiler) being used. Only when that | |
3494 | test succeeds is it valid to use @code{__has_attribute} as a preprocessor | |
3495 | operator. As a result, combining the two tests into a single expression as | |
3496 | shown below would only be valid with a compiler that supports the operator | |
3497 | but not with others that don't. | |
3498 | ||
3499 | @smallexample | |
3500 | #if defined __has_attribute && __has_attribute (nonnull) /* not portable */ | |
3501 | @dots{} | |
3502 | #endif | |
3503 | @end smallexample | |
3504 | ||
3505 | @node @code{__has_cpp_attribute} | |
3506 | @subsection @code{__has_cpp_attribute} | |
3507 | @cindex @code{__has_cpp_attribute} | |
3508 | ||
3509 | The special operator @code{__has_cpp_attribute (@var{operand})} may be used | |
3510 | in @samp{#if} and @samp{#elif} expressions in C++ code to test whether | |
3511 | the attribute referenced by its @var{operand} is recognized by GCC. | |
3512 | @code{__has_cpp_attribute (@var{operand})} is equivalent to | |
3513 | @code{__has_attribute (@var{operand})} except that when @var{operand} | |
3514 | designates a supported standard attribute it evaluates to an integer | |
3515 | constant of the form @code{YYYYMM} indicating the year and month when | |
3516 | the attribute was first introduced into the C++ standard. For additional | |
3517 | information including the dates of the introduction of current standard | |
3518 | attributes, see @w{@uref{https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations/, | |
3519 | SD-6: SG10 Feature Test Recommendations}}. | |
3520 | ||
3521 | @node @code{__has_c_attribute} | |
3522 | @subsection @code{__has_c_attribute} | |
3523 | @cindex @code{__has_c_attribute} | |
3524 | ||
3525 | The special operator @code{__has_c_attribute (@var{operand})} may be | |
3526 | used in @samp{#if} and @samp{#elif} expressions in C code to test | |
3527 | whether the attribute referenced by its @var{operand} is recognized by | |
3528 | GCC in attributes using the @samp{[[]]} syntax. GNU attributes must | |
3529 | be specified with the scope @samp{gnu} or @samp{__gnu__} with | |
3530 | @code{__has_c_attribute}. When @var{operand} designates a supported | |
3531 | standard attribute it evaluates to an integer constant of the form | |
3532 | @code{YYYYMM} indicating the year and month when the attribute was | |
3533 | first introduced into the C standard, or when the syntax of operands | |
3534 | to the attribute was extended in the C standard. | |
3535 | ||
3536 | @node @code{__has_builtin} | |
3537 | @subsection @code{__has_builtin} | |
3538 | @cindex @code{__has_builtin} | |
3539 | ||
3540 | The special operator @code{__has_builtin (@var{operand})} may be used in | |
3541 | constant integer contexts and in preprocessor @samp{#if} and @samp{#elif} | |
3542 | expressions to test whether the symbol named by its @var{operand} is | |
3543 | recognized as a built-in function by GCC in the current language and | |
3544 | conformance mode. It evaluates to a constant integer with a nonzero | |
3545 | value if the argument refers to such a function, and to zero otherwise. | |
3546 | The operator may also be used in preprocessor @samp{#if} and @samp{#elif} | |
3547 | expressions. The @code{__has_builtin} operator by itself, without any | |
3548 | @var{operand} or parentheses, acts as a predefined macro so that support | |
3549 | for it can be tested in portable code. Thus, the recommended use of | |
3550 | the operator is as follows: | |
3551 | ||
3552 | @smallexample | |
3553 | #if defined __has_builtin | |
3554 | # if __has_builtin (__builtin_object_size) | |
3555 | # define builtin_object_size(ptr) __builtin_object_size (ptr, 2) | |
3556 | # endif | |
3557 | #endif | |
3558 | #ifndef builtin_object_size | |
3559 | # define builtin_object_size(ptr) ((size_t)-1) | |
3560 | #endif | |
3561 | @end smallexample | |
3562 | ||
3563 | @node @code{__has_include} | |
3564 | @subsection @code{__has_include} | |
3565 | @cindex @code{__has_include} | |
3566 | ||
3567 | The special operator @code{__has_include (@var{operand})} may be used in | |
3568 | @samp{#if} and @samp{#elif} expressions to test whether the header referenced | |
3569 | by its @var{operand} can be included using the @samp{#include} directive. Using | |
3570 | the operator in other contexts is not valid. The @var{operand} takes | |
3571 | the same form as the file in the @samp{#include} directive (@pxref{Include | |
3572 | Syntax}) and evaluates to a nonzero value if the header can be included and | |
3573 | to zero otherwise. Note that that the ability to include a header doesn't | |
3574 | imply that the header doesn't contain invalid constructs or @samp{#error} | |
3575 | directives that would cause the preprocessor to fail. | |
3576 | ||
3577 | The @code{__has_include} operator by itself, without any @var{operand} or | |
3578 | parentheses, acts as a predefined macro so that support for it can be tested | |
3579 | in portable code. Thus, the recommended use of the operator is as follows: | |
3580 | ||
3581 | @smallexample | |
3582 | #if defined __has_include | |
3583 | # if __has_include (<stdatomic.h>) | |
3584 | # include <stdatomic.h> | |
3585 | # endif | |
3586 | #endif | |
3587 | @end smallexample | |
3588 | ||
3589 | The first @samp{#if} test succeeds only when the operator is supported | |
3590 | by the version of GCC (or another compiler) being used. Only when that | |
3591 | test succeeds is it valid to use @code{__has_include} as a preprocessor | |
3592 | operator. As a result, combining the two tests into a single expression | |
3593 | as shown below would only be valid with a compiler that supports the operator | |
3594 | but not with others that don't. | |
3595 | ||
3596 | @smallexample | |
3597 | #if defined __has_include && __has_include ("header.h") /* not portable */ | |
3598 | @dots{} | |
3599 | #endif | |
3600 | @end smallexample | |
3601 | ||
3602 | @node Deleted Code | |
3603 | @section Deleted Code | |
3604 | @cindex commenting out code | |
3605 | ||
3606 | If you replace or delete a part of the program but want to keep the old | |
3607 | code around for future reference, you often cannot simply comment it | |
3608 | out. Block comments do not nest, so the first comment inside the old | |
3609 | code will end the commenting-out. The probable result is a flood of | |
3610 | syntax errors. | |
3611 | ||
3612 | One way to avoid this problem is to use an always-false conditional | |
3613 | instead. For instance, put @code{#if 0} before the deleted code and | |
3614 | @code{#endif} after it. This works even if the code being turned | |
3615 | off contains conditionals, but they must be entire conditionals | |
3616 | (balanced @samp{#if} and @samp{#endif}). | |
3617 | ||
3618 | Some people use @code{#ifdef notdef} instead. This is risky, because | |
3619 | @code{notdef} might be accidentally defined as a macro, and then the | |
3620 | conditional would succeed. @code{#if 0} can be counted on to fail. | |
3621 | ||
3622 | Do not use @code{#if 0} for comments which are not C code. Use a real | |
3623 | comment, instead. The interior of @code{#if 0} must consist of complete | |
3624 | tokens; in particular, single-quote characters must balance. Comments | |
3625 | often contain unbalanced single-quote characters (known in English as | |
3626 | apostrophes). These confuse @code{#if 0}. They don't confuse | |
3627 | @samp{/*}. | |
3628 | ||
3629 | @node Diagnostics | |
3630 | @chapter Diagnostics | |
3631 | @cindex diagnostic | |
3632 | @cindex reporting errors | |
3633 | @cindex reporting warnings | |
3634 | ||
3635 | @findex #error | |
3636 | The directive @samp{#error} causes the preprocessor to report a fatal | |
3637 | error. The tokens forming the rest of the line following @samp{#error} | |
3638 | are used as the error message. | |
3639 | ||
3640 | You would use @samp{#error} inside of a conditional that detects a | |
3641 | combination of parameters which you know the program does not properly | |
3642 | support. For example, if you know that the program will not run | |
3643 | properly on a VAX, you might write | |
3644 | ||
3645 | @smallexample | |
3646 | @group | |
3647 | #ifdef __vax__ | |
3648 | #error "Won't work on VAXen. See comments at get_last_object." | |
3649 | #endif | |
3650 | @end group | |
3651 | @end smallexample | |
3652 | ||
3653 | If you have several configuration parameters that must be set up by | |
3654 | the installation in a consistent way, you can use conditionals to detect | |
3655 | an inconsistency and report it with @samp{#error}. For example, | |
3656 | ||
3657 | @smallexample | |
3658 | #if !defined(FOO) && defined(BAR) | |
3659 | #error "BAR requires FOO." | |
3660 | #endif | |
3661 | @end smallexample | |
3662 | ||
3663 | @findex #warning | |
3664 | The directive @samp{#warning} is like @samp{#error}, but causes the | |
3665 | preprocessor to issue a warning and continue preprocessing. The tokens | |
3666 | following @samp{#warning} are used as the warning message. | |
3667 | ||
3668 | You might use @samp{#warning} in obsolete header files, with a message | |
3669 | directing the user to the header file which should be used instead. | |
3670 | ||
3671 | Neither @samp{#error} nor @samp{#warning} macro-expands its argument. | |
3672 | Internal whitespace sequences are each replaced with a single space. | |
3673 | The line must consist of complete tokens. It is wisest to make the | |
3674 | argument of these directives be a single string constant; this avoids | |
3675 | problems with apostrophes and the like. | |
3676 | ||
3677 | @node Line Control | |
3678 | @chapter Line Control | |
3679 | @cindex line control | |
3680 | ||
3681 | The C preprocessor informs the C compiler of the location in your source | |
3682 | code where each token came from. Presently, this is just the file name | |
3683 | and line number. All the tokens resulting from macro expansion are | |
3684 | reported as having appeared on the line of the source file where the | |
3685 | outermost macro was used. We intend to be more accurate in the future. | |
3686 | ||
3687 | If you write a program which generates source code, such as the | |
3688 | @command{bison} parser generator, you may want to adjust the preprocessor's | |
3689 | notion of the current file name and line number by hand. Parts of the | |
3690 | output from @command{bison} are generated from scratch, other parts come | |
3691 | from a standard parser file. The rest are copied verbatim from | |
3692 | @command{bison}'s input. You would like compiler error messages and | |
3693 | symbolic debuggers to be able to refer to @code{bison}'s input file. | |
3694 | ||
3695 | @findex #line | |
3696 | @command{bison} or any such program can arrange this by writing | |
3697 | @samp{#line} directives into the output file. @samp{#line} is a | |
3698 | directive that specifies the original line number and source file name | |
3699 | for subsequent input in the current preprocessor input file. | |
3700 | @samp{#line} has three variants: | |
3701 | ||
3702 | @table @code | |
3703 | @item #line @var{linenum} | |
3704 | @var{linenum} is a non-negative decimal integer constant. It specifies | |
3705 | the line number which should be reported for the following line of | |
3706 | input. Subsequent lines are counted from @var{linenum}. | |
3707 | ||
3708 | @item #line @var{linenum} @var{filename} | |
3709 | @var{linenum} is the same as for the first form, and has the same | |
3710 | effect. In addition, @var{filename} is a string constant. The | |
3711 | following line and all subsequent lines are reported to come from the | |
3712 | file it specifies, until something else happens to change that. | |
3713 | @var{filename} is interpreted according to the normal rules for a string | |
3714 | constant: backslash escapes are interpreted. This is different from | |
3715 | @samp{#include}. | |
3716 | ||
3717 | @item #line @var{anything else} | |
3718 | @var{anything else} is checked for macro calls, which are expanded. | |
3719 | The result should match one of the above two forms. | |
3720 | @end table | |
3721 | ||
3722 | @samp{#line} directives alter the results of the @code{__FILE__} and | |
3723 | @code{__LINE__} predefined macros from that point on. @xref{Standard | |
3724 | Predefined Macros}. They do not have any effect on @samp{#include}'s | |
3725 | idea of the directory containing the current file. | |
3726 | ||
3727 | @node Pragmas | |
3728 | @chapter Pragmas | |
3729 | ||
3730 | @cindex pragma directive | |
3731 | ||
3732 | The @samp{#pragma} directive is the method specified by the C standard | |
3733 | for providing additional information to the compiler, beyond what is | |
3734 | conveyed in the language itself. The forms of this directive | |
3735 | (commonly known as @dfn{pragmas}) specified by C standard are prefixed with | |
3736 | @code{STDC}. A C compiler is free to attach any meaning it likes to other | |
3737 | pragmas. Most GNU-defined, supported pragmas have been given a | |
3738 | @code{GCC} prefix. | |
3739 | ||
3740 | @cindex @code{_Pragma} | |
3741 | C99 introduced the @code{@w{_Pragma}} operator. This feature addresses a | |
3742 | major problem with @samp{#pragma}: being a directive, it cannot be | |
3743 | produced as the result of macro expansion. @code{@w{_Pragma}} is an | |
3744 | operator, much like @code{sizeof} or @code{defined}, and can be embedded | |
3745 | in a macro. | |
3746 | ||
3747 | Its syntax is @code{@w{_Pragma (@var{string-literal})}}, where | |
3748 | @var{string-literal} can be either a normal or wide-character string | |
3749 | literal. It is destringized, by replacing all @samp{\\} with a single | |
3750 | @samp{\} and all @samp{\"} with a @samp{"}. The result is then | |
3751 | processed as if it had appeared as the right hand side of a | |
3752 | @samp{#pragma} directive. For example, | |
3753 | ||
3754 | @smallexample | |
3755 | _Pragma ("GCC dependency \"parse.y\"") | |
3756 | @end smallexample | |
3757 | ||
3758 | @noindent | |
3759 | has the same effect as @code{#pragma GCC dependency "parse.y"}. The | |
3760 | same effect could be achieved using macros, for example | |
3761 | ||
3762 | @smallexample | |
3763 | #define DO_PRAGMA(x) _Pragma (#x) | |
3764 | DO_PRAGMA (GCC dependency "parse.y") | |
3765 | @end smallexample | |
3766 | ||
3767 | The standard is unclear on where a @code{_Pragma} operator can appear. | |
3768 | The preprocessor does not accept it within a preprocessing conditional | |
3769 | directive like @samp{#if}. To be safe, you are probably best keeping it | |
3770 | out of directives other than @samp{#define}, and putting it on a line of | |
3771 | its own. | |
3772 | ||
3773 | This manual documents the pragmas which are meaningful to the | |
3774 | preprocessor itself. Other pragmas are meaningful to the C or C++ | |
3775 | compilers. They are documented in the GCC manual. | |
3776 | ||
3777 | GCC plugins may provide their own pragmas. | |
3778 | ||
3779 | @ftable @code | |
3780 | @item #pragma GCC dependency | |
3781 | @code{#pragma GCC dependency} allows you to check the relative dates of | |
3782 | the current file and another file. If the other file is more recent than | |
3783 | the current file, a warning is issued. This is useful if the current | |
3784 | file is derived from the other file, and should be regenerated. The | |
3785 | other file is searched for using the normal include search path. | |
3786 | Optional trailing text can be used to give more information in the | |
3787 | warning message. | |
3788 | ||
3789 | @smallexample | |
3790 | #pragma GCC dependency "parse.y" | |
3791 | #pragma GCC dependency "/usr/include/time.h" rerun fixincludes | |
3792 | @end smallexample | |
3793 | ||
3794 | @item #pragma GCC poison | |
3795 | Sometimes, there is an identifier that you want to remove completely | |
3796 | from your program, and make sure that it never creeps back in. To | |
3797 | enforce this, you can @dfn{poison} the identifier with this pragma. | |
3798 | @code{#pragma GCC poison} is followed by a list of identifiers to | |
3799 | poison. If any of those identifiers appears anywhere in the source | |
3800 | after the directive, it is a hard error. For example, | |
3801 | ||
3802 | @smallexample | |
3803 | #pragma GCC poison printf sprintf fprintf | |
3804 | sprintf(some_string, "hello"); | |
3805 | @end smallexample | |
3806 | ||
3807 | @noindent | |
3808 | will produce an error. | |
3809 | ||
3810 | If a poisoned identifier appears as part of the expansion of a macro | |
3811 | which was defined before the identifier was poisoned, it will @emph{not} | |
3812 | cause an error. This lets you poison an identifier without worrying | |
3813 | about system headers defining macros that use it. | |
3814 | ||
3815 | For example, | |
3816 | ||
3817 | @smallexample | |
3818 | #define strrchr rindex | |
3819 | #pragma GCC poison rindex | |
3820 | strrchr(some_string, 'h'); | |
3821 | @end smallexample | |
3822 | ||
3823 | @noindent | |
3824 | will not produce an error. | |
3825 | ||
3826 | @item #pragma GCC system_header | |
3827 | This pragma takes no arguments. It causes the rest of the code in the | |
3828 | current file to be treated as if it came from a system header. | |
3829 | @xref{System Headers}. | |
3830 | ||
3831 | @item #pragma GCC warning | |
3832 | @itemx #pragma GCC error | |
3833 | @code{#pragma GCC warning "message"} causes the preprocessor to issue | |
3834 | a warning diagnostic with the text @samp{message}. The message | |
3835 | contained in the pragma must be a single string literal. Similarly, | |
3836 | @code{#pragma GCC error "message"} issues an error message. Unlike | |
3837 | the @samp{#warning} and @samp{#error} directives, these pragmas can be | |
3838 | embedded in preprocessor macros using @samp{_Pragma}. | |
3839 | ||
3840 | @item #pragma once | |
3841 | If @code{#pragma once} is seen when scanning a header file, that | |
3842 | file will never be read again, no matter what. It is a less-portable | |
3843 | alternative to using @samp{#ifndef} to guard the contents of header files | |
3844 | against multiple inclusions. | |
3845 | ||
f5f2686b JW |
3846 | @item #pragma region @{tokens@}... |
3847 | @itemx #pragma endregion @{tokens@}... | |
ce51e843 ML |
3848 | These pragmas are accepted, but have no effect. |
3849 | ||
d77de738 ML |
3850 | @end ftable |
3851 | ||
3852 | @node Other Directives | |
3853 | @chapter Other Directives | |
3854 | ||
3855 | @findex #ident | |
3856 | @findex #sccs | |
3857 | The @samp{#ident} directive takes one argument, a string constant. On | |
3858 | some systems, that string constant is copied into a special segment of | |
3859 | the object file. On other systems, the directive is ignored. The | |
3860 | @samp{#sccs} directive is a synonym for @samp{#ident}. | |
3861 | ||
3862 | These directives are not part of the C standard, but they are not | |
3863 | official GNU extensions either. What historical information we have | |
3864 | been able to find, suggests they originated with System V@. | |
3865 | ||
3866 | @cindex null directive | |
3867 | The @dfn{null directive} consists of a @samp{#} followed by a newline, | |
3868 | with only whitespace (including comments) in between. A null directive | |
3869 | is understood as a preprocessing directive but has no effect on the | |
3870 | preprocessor output. The primary significance of the existence of the | |
3871 | null directive is that an input line consisting of just a @samp{#} will | |
3872 | produce no output, rather than a line of output containing just a | |
3873 | @samp{#}. Supposedly some old C programs contain such lines. | |
3874 | ||
3875 | @node Preprocessor Output | |
3876 | @chapter Preprocessor Output | |
3877 | ||
3878 | When the C preprocessor is used with the C, C++, or Objective-C | |
3879 | compilers, it is integrated into the compiler and communicates a stream | |
3880 | of binary tokens directly to the compiler's parser. However, it can | |
3881 | also be used in the more conventional standalone mode, where it produces | |
3882 | textual output. | |
3883 | @c FIXME: Document the library interface. | |
3884 | ||
3885 | @cindex output format | |
3886 | The output from the C preprocessor looks much like the input, except | |
3887 | that all preprocessing directive lines have been replaced with blank | |
3888 | lines and all comments with spaces. Long runs of blank lines are | |
3889 | discarded. | |
3890 | ||
3891 | The ISO standard specifies that it is implementation defined whether a | |
3892 | preprocessor preserves whitespace between tokens, or replaces it with | |
3893 | e.g.@: a single space. In GNU CPP, whitespace between tokens is collapsed | |
3894 | to become a single space, with the exception that the first token on a | |
3895 | non-directive line is preceded with sufficient spaces that it appears in | |
3896 | the same column in the preprocessed output that it appeared in the | |
3897 | original source file. This is so the output is easy to read. | |
3898 | CPP does not insert any | |
3899 | whitespace where there was none in the original source, except where | |
3900 | necessary to prevent an accidental token paste. | |
3901 | ||
3902 | @cindex linemarkers | |
3903 | Source file name and line number information is conveyed by lines | |
3904 | of the form | |
3905 | ||
3906 | @smallexample | |
3907 | # @var{linenum} @var{filename} @var{flags} | |
3908 | @end smallexample | |
3909 | ||
3910 | @noindent | |
3911 | These are called @dfn{linemarkers}. They are inserted as needed into | |
3912 | the output (but never within a string or character constant). They mean | |
3913 | that the following line originated in file @var{filename} at line | |
3914 | @var{linenum}. @var{filename} will never contain any non-printing | |
3915 | characters; they are replaced with octal escape sequences. | |
3916 | ||
3917 | After the file name comes zero or more flags, which are @samp{1}, | |
3918 | @samp{2}, @samp{3}, or @samp{4}. If there are multiple flags, spaces | |
3919 | separate them. Here is what the flags mean: | |
3920 | ||
3921 | @table @samp | |
3922 | @item 1 | |
3923 | This indicates the start of a new file. | |
3924 | @item 2 | |
3925 | This indicates returning to a file (after having included another file). | |
3926 | @item 3 | |
3927 | This indicates that the following text comes from a system header file, | |
3928 | so certain warnings should be suppressed. | |
3929 | @item 4 | |
3930 | This indicates that the following text should be treated as being | |
3931 | wrapped in an implicit @code{extern "C"} block. | |
3932 | @c maybe cross reference SYSTEM_IMPLICIT_EXTERN_C | |
3933 | @end table | |
3934 | ||
3935 | As an extension, the preprocessor accepts linemarkers in non-assembler | |
3936 | input files. They are treated like the corresponding @samp{#line} | |
3937 | directive, (@pxref{Line Control}), except that trailing flags are | |
3938 | permitted, and are interpreted with the meanings described above. If | |
3939 | multiple flags are given, they must be in ascending order. | |
3940 | ||
3941 | Some directives may be duplicated in the output of the preprocessor. | |
3942 | These are @samp{#ident} (always), @samp{#pragma} (only if the | |
3943 | preprocessor does not handle the pragma itself), and @samp{#define} and | |
3944 | @samp{#undef} (with certain debugging options). If this happens, the | |
3945 | @samp{#} of the directive will always be in the first column, and there | |
3946 | will be no space between the @samp{#} and the directive name. If macro | |
3947 | expansion happens to generate tokens which might be mistaken for a | |
3948 | duplicated directive, a space will be inserted between the @samp{#} and | |
3949 | the directive name. | |
3950 | ||
3951 | @node Traditional Mode | |
3952 | @chapter Traditional Mode | |
3953 | ||
3954 | Traditional (pre-standard) C preprocessing is rather different from | |
3955 | the preprocessing specified by the standard. When the preprocessor | |
3956 | is invoked with the | |
3957 | @option{-traditional-cpp} option, it attempts to emulate a traditional | |
3958 | preprocessor. | |
3959 | ||
3960 | This mode is not useful for compiling C code with GCC, | |
3961 | but is intended for use with non-C preprocessing applications. Thus | |
3962 | traditional mode semantics are supported only when invoking | |
3963 | the preprocessor explicitly, and not in the compiler front ends. | |
3964 | ||
3965 | The implementation does not correspond precisely to the behavior of | |
3966 | early pre-standard versions of GCC, nor to any true traditional preprocessor. | |
3967 | After all, inconsistencies among traditional implementations were a | |
3968 | major motivation for C standardization. However, we intend that it | |
3969 | should be compatible with true traditional preprocessors in all ways | |
3970 | that actually matter. | |
3971 | ||
3972 | @menu | |
3973 | * Traditional lexical analysis:: | |
3974 | * Traditional macros:: | |
3975 | * Traditional miscellany:: | |
3976 | * Traditional warnings:: | |
3977 | @end menu | |
3978 | ||
3979 | @node Traditional lexical analysis | |
3980 | @section Traditional lexical analysis | |
3981 | ||
3982 | The traditional preprocessor does not decompose its input into tokens | |
3983 | the same way a standards-conforming preprocessor does. The input is | |
3984 | simply treated as a stream of text with minimal internal form. | |
3985 | ||
3986 | This implementation does not treat trigraphs (@pxref{trigraphs}) | |
3987 | specially since they were an invention of the standards committee. It | |
3988 | handles arbitrarily-positioned escaped newlines properly and splices | |
3989 | the lines as you would expect; many traditional preprocessors did not | |
3990 | do this. | |
3991 | ||
3992 | The form of horizontal whitespace in the input file is preserved in | |
3993 | the output. In particular, hard tabs remain hard tabs. This can be | |
3994 | useful if, for example, you are preprocessing a Makefile. | |
3995 | ||
3996 | Traditional CPP only recognizes C-style block comments, and treats the | |
3997 | @samp{/*} sequence as introducing a comment only if it lies outside | |
3998 | quoted text. Quoted text is introduced by the usual single and double | |
3999 | quotes, and also by an initial @samp{<} in a @code{#include} | |
4000 | directive. | |
4001 | ||
4002 | Traditionally, comments are completely removed and are not replaced | |
4003 | with a space. Since a traditional compiler does its own tokenization | |
4004 | of the output of the preprocessor, this means that comments can | |
4005 | effectively be used as token paste operators. However, comments | |
4006 | behave like separators for text handled by the preprocessor itself, | |
4007 | since it doesn't re-lex its input. For example, in | |
4008 | ||
4009 | @smallexample | |
4010 | #if foo/**/bar | |
4011 | @end smallexample | |
4012 | ||
4013 | @noindent | |
4014 | @samp{foo} and @samp{bar} are distinct identifiers and expanded | |
4015 | separately if they happen to be macros. In other words, this | |
4016 | directive is equivalent to | |
4017 | ||
4018 | @smallexample | |
4019 | #if foo bar | |
4020 | @end smallexample | |
4021 | ||
4022 | @noindent | |
4023 | rather than | |
4024 | ||
4025 | @smallexample | |
4026 | #if foobar | |
4027 | @end smallexample | |
4028 | ||
4029 | Generally speaking, in traditional mode an opening quote need not have | |
4030 | a matching closing quote. In particular, a macro may be defined with | |
4031 | replacement text that contains an unmatched quote. Of course, if you | |
4032 | attempt to compile preprocessed output containing an unmatched quote | |
4033 | you will get a syntax error. | |
4034 | ||
4035 | However, all preprocessing directives other than @code{#define} | |
4036 | require matching quotes. For example: | |
4037 | ||
4038 | @smallexample | |
4039 | #define m This macro's fine and has an unmatched quote | |
4040 | "/* This is not a comment. */ | |
4041 | /* @r{This is a comment. The following #include directive | |
4042 | is ill-formed.} */ | |
4043 | #include <stdio.h | |
4044 | @end smallexample | |
4045 | ||
4046 | Just as for the ISO preprocessor, what would be a closing quote can be | |
4047 | escaped with a backslash to prevent the quoted text from closing. | |
4048 | ||
4049 | @node Traditional macros | |
4050 | @section Traditional macros | |
4051 | ||
4052 | The major difference between traditional and ISO macros is that the | |
4053 | former expand to text rather than to a token sequence. CPP removes | |
4054 | all leading and trailing horizontal whitespace from a macro's | |
4055 | replacement text before storing it, but preserves the form of internal | |
4056 | whitespace. | |
4057 | ||
4058 | One consequence is that it is legitimate for the replacement text to | |
4059 | contain an unmatched quote (@pxref{Traditional lexical analysis}). An | |
4060 | unclosed string or character constant continues into the text | |
4061 | following the macro call. Similarly, the text at the end of a macro's | |
4062 | expansion can run together with the text after the macro invocation to | |
4063 | produce a single token. | |
4064 | ||
4065 | Normally comments are removed from the replacement text after the | |
4066 | macro is expanded, but if the @option{-CC} option is passed on the | |
4067 | command-line comments are preserved. (In fact, the current | |
4068 | implementation removes comments even before saving the macro | |
4069 | replacement text, but it careful to do it in such a way that the | |
4070 | observed effect is identical even in the function-like macro case.) | |
4071 | ||
4072 | The ISO stringizing operator @samp{#} and token paste operator | |
4073 | @samp{##} have no special meaning. As explained later, an effect | |
4074 | similar to these operators can be obtained in a different way. Macro | |
4075 | names that are embedded in quotes, either from the main file or after | |
4076 | macro replacement, do not expand. | |
4077 | ||
4078 | CPP replaces an unquoted object-like macro name with its replacement | |
4079 | text, and then rescans it for further macros to replace. Unlike | |
4080 | standard macro expansion, traditional macro expansion has no provision | |
4081 | to prevent recursion. If an object-like macro appears unquoted in its | |
4082 | replacement text, it will be replaced again during the rescan pass, | |
4083 | and so on @emph{ad infinitum}. GCC detects when it is expanding | |
4084 | recursive macros, emits an error message, and continues after the | |
4085 | offending macro invocation. | |
4086 | ||
4087 | @smallexample | |
4088 | #define PLUS + | |
4089 | #define INC(x) PLUS+x | |
4090 | INC(foo); | |
4091 | @expansion{} ++foo; | |
4092 | @end smallexample | |
4093 | ||
4094 | Function-like macros are similar in form but quite different in | |
4095 | behavior to their ISO counterparts. Their arguments are contained | |
4096 | within parentheses, are comma-separated, and can cross physical lines. | |
4097 | Commas within nested parentheses are not treated as argument | |
4098 | separators. Similarly, a quote in an argument cannot be left | |
4099 | unclosed; a following comma or parenthesis that comes before the | |
4100 | closing quote is treated like any other character. There is no | |
4101 | facility for handling variadic macros. | |
4102 | ||
4103 | This implementation removes all comments from macro arguments, unless | |
4104 | the @option{-C} option is given. The form of all other horizontal | |
4105 | whitespace in arguments is preserved, including leading and trailing | |
4106 | whitespace. In particular | |
4107 | ||
4108 | @smallexample | |
4109 | f( ) | |
4110 | @end smallexample | |
4111 | ||
4112 | @noindent | |
4113 | is treated as an invocation of the macro @samp{f} with a single | |
4114 | argument consisting of a single space. If you want to invoke a | |
4115 | function-like macro that takes no arguments, you must not leave any | |
4116 | whitespace between the parentheses. | |
4117 | ||
4118 | If a macro argument crosses a new line, the new line is replaced with | |
4119 | a space when forming the argument. If the previous line contained an | |
4120 | unterminated quote, the following line inherits the quoted state. | |
4121 | ||
4122 | Traditional preprocessors replace parameters in the replacement text | |
4123 | with their arguments regardless of whether the parameters are within | |
4124 | quotes or not. This provides a way to stringize arguments. For | |
4125 | example | |
4126 | ||
4127 | @smallexample | |
4128 | #define str(x) "x" | |
4129 | str(/* @r{A comment} */some text ) | |
4130 | @expansion{} "some text " | |
4131 | @end smallexample | |
4132 | ||
4133 | @noindent | |
4134 | Note that the comment is removed, but that the trailing space is | |
4135 | preserved. Here is an example of using a comment to effect token | |
4136 | pasting. | |
4137 | ||
4138 | @smallexample | |
4139 | #define suffix(x) foo_/**/x | |
4140 | suffix(bar) | |
4141 | @expansion{} foo_bar | |
4142 | @end smallexample | |
4143 | ||
4144 | @node Traditional miscellany | |
4145 | @section Traditional miscellany | |
4146 | ||
4147 | Here are some things to be aware of when using the traditional | |
4148 | preprocessor. | |
4149 | ||
4150 | @itemize @bullet | |
4151 | @item | |
4152 | Preprocessing directives are recognized only when their leading | |
4153 | @samp{#} appears in the first column. There can be no whitespace | |
4154 | between the beginning of the line and the @samp{#}, but whitespace can | |
4155 | follow the @samp{#}. | |
4156 | ||
4157 | @item | |
4158 | A true traditional C preprocessor does not recognize @samp{#error} or | |
4159 | @samp{#pragma}, and may not recognize @samp{#elif}. CPP supports all | |
4160 | the directives in traditional mode that it supports in ISO mode, | |
4161 | including extensions, with the exception that the effects of | |
4162 | @samp{#pragma GCC poison} are undefined. | |
4163 | ||
4164 | @item | |
4165 | __STDC__ is not defined. | |
4166 | ||
4167 | @item | |
4168 | If you use digraphs the behavior is undefined. | |
4169 | ||
4170 | @item | |
4171 | If a line that looks like a directive appears within macro arguments, | |
4172 | the behavior is undefined. | |
4173 | ||
4174 | @end itemize | |
4175 | ||
4176 | @node Traditional warnings | |
4177 | @section Traditional warnings | |
4178 | You can request warnings about features that did not exist, or worked | |
4179 | differently, in traditional C with the @option{-Wtraditional} option. | |
4180 | GCC does not warn about features of ISO C which you must use when you | |
4181 | are using a conforming compiler, such as the @samp{#} and @samp{##} | |
4182 | operators. | |
4183 | ||
4184 | Presently @option{-Wtraditional} warns about: | |
4185 | ||
4186 | @itemize @bullet | |
4187 | @item | |
4188 | Macro parameters that appear within string literals in the macro body. | |
4189 | In traditional C macro replacement takes place within string literals, | |
4190 | but does not in ISO C@. | |
4191 | ||
4192 | @item | |
4193 | In traditional C, some preprocessor directives did not exist. | |
4194 | Traditional preprocessors would only consider a line to be a directive | |
4195 | if the @samp{#} appeared in column 1 on the line. Therefore | |
4196 | @option{-Wtraditional} warns about directives that traditional C | |
4197 | understands but would ignore because the @samp{#} does not appear as the | |
4198 | first character on the line. It also suggests you hide directives like | |
4199 | @samp{#pragma} not understood by traditional C by indenting them. Some | |
4200 | traditional implementations would not recognize @samp{#elif}, so it | |
4201 | suggests avoiding it altogether. | |
4202 | ||
4203 | @item | |
4204 | A function-like macro that appears without an argument list. In some | |
4205 | traditional preprocessors this was an error. In ISO C it merely means | |
4206 | that the macro is not expanded. | |
4207 | ||
4208 | @item | |
4209 | The unary plus operator. This did not exist in traditional C@. | |
4210 | ||
4211 | @item | |
4212 | The @samp{U} and @samp{LL} integer constant suffixes, which were not | |
4213 | available in traditional C@. (Traditional C does support the @samp{L} | |
4214 | suffix for simple long integer constants.) You are not warned about | |
4215 | uses of these suffixes in macros defined in system headers. For | |
4216 | instance, @code{UINT_MAX} may well be defined as @code{4294967295U}, but | |
4217 | you will not be warned if you use @code{UINT_MAX}. | |
4218 | ||
4219 | You can usually avoid the warning, and the related warning about | |
4220 | constants which are so large that they are unsigned, by writing the | |
4221 | integer constant in question in hexadecimal, with no U suffix. Take | |
4222 | care, though, because this gives the wrong result in exotic cases. | |
4223 | @end itemize | |
4224 | ||
4225 | @node Implementation Details | |
4226 | @chapter Implementation Details | |
4227 | ||
4228 | Here we document details of how the preprocessor's implementation | |
4229 | affects its user-visible behavior. You should try to avoid undue | |
4230 | reliance on behavior described here, as it is possible that it will | |
4231 | change subtly in future implementations. | |
4232 | ||
4233 | Also documented here are obsolete features still supported by CPP@. | |
4234 | ||
4235 | @menu | |
4236 | * Implementation-defined behavior:: | |
4237 | * Implementation limits:: | |
4238 | * Obsolete Features:: | |
4239 | @end menu | |
4240 | ||
4241 | @node Implementation-defined behavior | |
4242 | @section Implementation-defined behavior | |
4243 | @cindex implementation-defined behavior | |
4244 | ||
4245 | This is how CPP behaves in all the cases which the C standard | |
4246 | describes as @dfn{implementation-defined}. This term means that the | |
4247 | implementation is free to do what it likes, but must document its choice | |
4248 | and stick to it. | |
4249 | @c FIXME: Check the C++ standard for more implementation-defined stuff. | |
4250 | ||
4251 | @itemize @bullet | |
4252 | @need 1000 | |
4253 | @item The mapping of physical source file multi-byte characters to the | |
4254 | execution character set. | |
4255 | ||
4256 | The input character set can be specified using the | |
4257 | @option{-finput-charset} option, while the execution character set may | |
4258 | be controlled using the @option{-fexec-charset} and | |
4259 | @option{-fwide-exec-charset} options. | |
4260 | ||
4261 | @item Identifier characters. | |
4262 | @anchor{Identifier characters} | |
4263 | ||
4264 | The C and C++ standards allow identifiers to be composed of @samp{_} | |
4265 | and the alphanumeric characters. C++ also allows universal character | |
4266 | names. C99 and later C standards permit both universal character | |
4267 | names and implementation-defined characters. In both C and C++ modes, | |
4268 | GCC accepts in identifiers exactly those extended characters that | |
4269 | correspond to universal character names permitted by the chosen | |
4270 | standard. | |
4271 | ||
4272 | GCC allows the @samp{$} character in identifiers as an extension for | |
4273 | most targets. This is true regardless of the @option{std=} switch, | |
4274 | since this extension cannot conflict with standards-conforming | |
4275 | programs. When preprocessing assembler, however, dollars are not | |
4276 | identifier characters by default. | |
4277 | ||
4278 | Currently the targets that by default do not permit @samp{$} are AVR, | |
4279 | IP2K, MMIX, MIPS Irix 3, ARM aout, and PowerPC targets for the AIX | |
4280 | operating system. | |
4281 | ||
4282 | You can override the default with @option{-fdollars-in-identifiers} or | |
4283 | @option{-fno-dollars-in-identifiers}. @xref{fdollars-in-identifiers}. | |
4284 | ||
4285 | @item Non-empty sequences of whitespace characters. | |
4286 | ||
4287 | In textual output, each whitespace sequence is collapsed to a single | |
4288 | space. For aesthetic reasons, the first token on each non-directive | |
4289 | line of output is preceded with sufficient spaces that it appears in the | |
4290 | same column as it did in the original source file. | |
4291 | ||
4292 | @item The numeric value of character constants in preprocessor expressions. | |
4293 | ||
4294 | The preprocessor and compiler interpret character constants in the | |
4295 | same way; i.e.@: escape sequences such as @samp{\a} are given the | |
4296 | values they would have on the target machine. | |
4297 | ||
4298 | The compiler evaluates a multi-character character constant a character | |
4299 | at a time, shifting the previous value left by the number of bits per | |
4300 | target character, and then or-ing in the bit-pattern of the new | |
4301 | character truncated to the width of a target character. The final | |
4302 | bit-pattern is given type @code{int}, and is therefore signed, | |
4303 | regardless of whether single characters are signed or not. | |
4304 | If there are more | |
4305 | characters in the constant than would fit in the target @code{int} the | |
4306 | compiler issues a warning, and the excess leading characters are | |
4307 | ignored. | |
4308 | ||
4309 | For example, @code{'ab'} for a target with an 8-bit @code{char} would be | |
4310 | interpreted as @w{@samp{(int) ((unsigned char) 'a' * 256 + (unsigned char) | |
4311 | 'b')}}, and @code{'\234a'} as @w{@samp{(int) ((unsigned char) '\234' * | |
4312 | 256 + (unsigned char) 'a')}}. | |
4313 | ||
4314 | @item Source file inclusion. | |
4315 | ||
4316 | For a discussion on how the preprocessor locates header files, | |
4317 | @ref{Include Operation}. | |
4318 | ||
4319 | @item Interpretation of the filename resulting from a macro-expanded | |
4320 | @samp{#include} directive. | |
4321 | ||
4322 | @xref{Computed Includes}. | |
4323 | ||
4324 | @item Treatment of a @samp{#pragma} directive that after macro-expansion | |
4325 | results in a standard pragma. | |
4326 | ||
4327 | No macro expansion occurs on any @samp{#pragma} directive line, so the | |
4328 | question does not arise. | |
4329 | ||
4330 | Note that GCC does not yet implement any of the standard | |
4331 | pragmas. | |
4332 | ||
4333 | @end itemize | |
4334 | ||
4335 | @node Implementation limits | |
4336 | @section Implementation limits | |
4337 | @cindex implementation limits | |
4338 | ||
4339 | CPP has a small number of internal limits. This section lists the | |
4340 | limits which the C standard requires to be no lower than some minimum, | |
4341 | and all the others known. It is intended that there should be as few limits | |
4342 | as possible. If you encounter an undocumented or inconvenient limit, | |
4343 | please report that as a bug. @xref{Bugs, , Reporting Bugs, gcc, Using | |
4344 | the GNU Compiler Collection (GCC)}. | |
4345 | ||
4346 | Where we say something is limited @dfn{only by available memory}, that | |
4347 | means that internal data structures impose no intrinsic limit, and space | |
4348 | is allocated with @code{malloc} or equivalent. The actual limit will | |
4349 | therefore depend on many things, such as the size of other things | |
4350 | allocated by the compiler at the same time, the amount of memory | |
4351 | consumed by other processes on the same computer, etc. | |
4352 | ||
4353 | @itemize @bullet | |
4354 | ||
4355 | @item Nesting levels of @samp{#include} files. | |
4356 | ||
4357 | We impose an arbitrary limit of 200 levels, to avoid runaway recursion. | |
4358 | The standard requires at least 15 levels. | |
4359 | ||
4360 | @item Nesting levels of conditional inclusion. | |
4361 | ||
4362 | The C standard mandates this be at least 63. CPP is limited only by | |
4363 | available memory. | |
4364 | ||
4365 | @item Levels of parenthesized expressions within a full expression. | |
4366 | ||
4367 | The C standard requires this to be at least 63. In preprocessor | |
4368 | conditional expressions, it is limited only by available memory. | |
4369 | ||
4370 | @item Significant initial characters in an identifier or macro name. | |
4371 | ||
4372 | The preprocessor treats all characters as significant. The C standard | |
4373 | requires only that the first 63 be significant. | |
4374 | ||
4375 | @item Number of macros simultaneously defined in a single translation unit. | |
4376 | ||
4377 | The standard requires at least 4095 be possible. CPP is limited only | |
4378 | by available memory. | |
4379 | ||
4380 | @item Number of parameters in a macro definition and arguments in a macro call. | |
4381 | ||
4382 | We allow @code{USHRT_MAX}, which is no smaller than 65,535. The minimum | |
4383 | required by the standard is 127. | |
4384 | ||
4385 | @item Number of characters on a logical source line. | |
4386 | ||
4387 | The C standard requires a minimum of 4096 be permitted. CPP places | |
4388 | no limits on this, but you may get incorrect column numbers reported in | |
4389 | diagnostics for lines longer than 65,535 characters. | |
4390 | ||
4391 | @item Maximum size of a source file. | |
4392 | ||
4393 | The standard does not specify any lower limit on the maximum size of a | |
4394 | source file. GNU cpp maps files into memory, so it is limited by the | |
4395 | available address space. This is generally at least two gigabytes. | |
4396 | Depending on the operating system, the size of physical memory may or | |
4397 | may not be a limitation. | |
4398 | ||
4399 | @end itemize | |
4400 | ||
4401 | @node Obsolete Features | |
4402 | @section Obsolete Features | |
4403 | ||
4404 | CPP has some features which are present mainly for compatibility with | |
4405 | older programs. We discourage their use in new code. In some cases, | |
4406 | we plan to remove the feature in a future version of GCC@. | |
4407 | ||
4408 | @subsection Assertions | |
4409 | @cindex assertions | |
4410 | ||
4411 | @dfn{Assertions} are a deprecated alternative to macros in writing | |
4412 | conditionals to test what sort of computer or system the compiled | |
4413 | program will run on. Assertions are usually predefined, but you can | |
4414 | define them with preprocessing directives or command-line options. | |
4415 | ||
4416 | Assertions were intended to provide a more systematic way to describe | |
4417 | the compiler's target system and we added them for compatibility with | |
4418 | existing compilers. In practice they are just as unpredictable as the | |
4419 | system-specific predefined macros. In addition, they are not part of | |
4420 | any standard, and only a few compilers support them. | |
4421 | Therefore, the use of assertions is @strong{less} portable than the use | |
4422 | of system-specific predefined macros. We recommend you do not use them at | |
4423 | all. | |
4424 | ||
4425 | @cindex predicates | |
4426 | An assertion looks like this: | |
4427 | ||
4428 | @smallexample | |
4429 | #@var{predicate} (@var{answer}) | |
4430 | @end smallexample | |
4431 | ||
4432 | @noindent | |
4433 | @var{predicate} must be a single identifier. @var{answer} can be any | |
4434 | sequence of tokens; all characters are significant except for leading | |
4435 | and trailing whitespace, and differences in internal whitespace | |
4436 | sequences are ignored. (This is similar to the rules governing macro | |
4437 | redefinition.) Thus, @code{(x + y)} is different from @code{(x+y)} but | |
4438 | equivalent to @code{@w{( x + y )}}. Parentheses do not nest inside an | |
4439 | answer. | |
4440 | ||
4441 | @cindex testing predicates | |
4442 | To test an assertion, you write it in an @samp{#if}. For example, this | |
4443 | conditional succeeds if either @code{vax} or @code{ns16000} has been | |
4444 | asserted as an answer for @code{machine}. | |
4445 | ||
4446 | @smallexample | |
4447 | #if #machine (vax) || #machine (ns16000) | |
4448 | @end smallexample | |
4449 | ||
4450 | @noindent | |
4451 | You can test whether @emph{any} answer is asserted for a predicate by | |
4452 | omitting the answer in the conditional: | |
4453 | ||
4454 | @smallexample | |
4455 | #if #machine | |
4456 | @end smallexample | |
4457 | ||
4458 | @findex #assert | |
4459 | Assertions are made with the @samp{#assert} directive. Its sole | |
4460 | argument is the assertion to make, without the leading @samp{#} that | |
4461 | identifies assertions in conditionals. | |
4462 | ||
4463 | @smallexample | |
4464 | #assert @var{predicate} (@var{answer}) | |
4465 | @end smallexample | |
4466 | ||
4467 | @noindent | |
4468 | You may make several assertions with the same predicate and different | |
4469 | answers. Subsequent assertions do not override previous ones for the | |
4470 | same predicate. All the answers for any given predicate are | |
4471 | simultaneously true. | |
4472 | ||
4473 | @cindex assertions, canceling | |
4474 | @findex #unassert | |
4475 | Assertions can be canceled with the @samp{#unassert} directive. It | |
4476 | has the same syntax as @samp{#assert}. In that form it cancels only the | |
4477 | answer which was specified on the @samp{#unassert} line; other answers | |
4478 | for that predicate remain true. You can cancel an entire predicate by | |
4479 | leaving out the answer: | |
4480 | ||
4481 | @smallexample | |
4482 | #unassert @var{predicate} | |
4483 | @end smallexample | |
4484 | ||
4485 | @noindent | |
4486 | In either form, if no such assertion has been made, @samp{#unassert} has | |
4487 | no effect. | |
4488 | ||
4489 | You can also make or cancel assertions using command-line options. | |
4490 | @xref{Invocation}. | |
4491 | ||
4492 | @node Invocation | |
4493 | @chapter Invocation | |
4494 | @cindex invocation | |
4495 | @cindex command line | |
4496 | ||
4497 | Most often when you use the C preprocessor you do not have to invoke it | |
4498 | explicitly: the C compiler does so automatically. However, the | |
4499 | preprocessor is sometimes useful on its own. You can invoke the | |
4500 | preprocessor either with the @command{cpp} command, or via @command{gcc -E}. | |
4501 | In GCC, the preprocessor is actually integrated with the compiler | |
4502 | rather than a separate program, and both of these commands invoke | |
4503 | GCC and tell it to stop after the preprocessing phase. | |
4504 | ||
4505 | The @command{cpp} options listed here are also accepted by | |
4506 | @command{gcc} and have the same meaning. Likewise the @command{cpp} | |
4507 | command accepts all the usual @command{gcc} driver options, although those | |
4508 | pertaining to compilation phases after preprocessing are ignored. | |
4509 | ||
4510 | Only options specific to preprocessing behavior are documented here. | |
4511 | Refer to the GCC manual for full documentation of other driver options. | |
4512 | ||
4513 | @ignore | |
4514 | @c man begin SYNOPSIS | |
4515 | cpp [@option{-D}@var{macro}[=@var{defn}]@dots{}] [@option{-U}@var{macro}] | |
4516 | [@option{-I}@var{dir}@dots{}] [@option{-iquote}@var{dir}@dots{}] | |
4517 | [@option{-M}|@option{-MM}] [@option{-MG}] [@option{-MF} @var{filename}] | |
4518 | [@option{-MP}] [@option{-MQ} @var{target}@dots{}] | |
4519 | [@option{-MT} @var{target}@dots{}] | |
4520 | @var{infile} [[@option{-o}] @var{outfile}] | |
4521 | ||
4522 | Only the most useful options are given above; see below for a more | |
4523 | complete list of preprocessor-specific options. | |
4524 | In addition, @command{cpp} accepts most @command{gcc} driver options, which | |
4525 | are not listed here. Refer to the GCC documentation for details. | |
4526 | @c man end | |
4527 | @c man begin SEEALSO | |
4528 | gpl(7), gfdl(7), fsf-funding(7), | |
4529 | gcc(1), and the Info entries for @file{cpp} and @file{gcc}. | |
4530 | @c man end | |
4531 | @end ignore | |
4532 | ||
4533 | @c man begin OPTIONS | |
4534 | The @command{cpp} command expects two file names as arguments, @var{infile} and | |
4535 | @var{outfile}. The preprocessor reads @var{infile} together with any | |
4536 | other files it specifies with @samp{#include}. All the output generated | |
4537 | by the combined input files is written in @var{outfile}. | |
4538 | ||
4539 | Either @var{infile} or @var{outfile} may be @option{-}, which as | |
4540 | @var{infile} means to read from standard input and as @var{outfile} | |
4541 | means to write to standard output. If either file is omitted, it | |
4542 | means the same as if @option{-} had been specified for that file. | |
4543 | You can also use the @option{-o @var{outfile}} option to specify the | |
4544 | output file. | |
4545 | ||
4546 | Unless otherwise noted, or the option ends in @samp{=}, all options | |
4547 | which take an argument may have that argument appear either immediately | |
4548 | after the option, or with a space between option and argument: | |
4549 | @option{-Ifoo} and @option{-I foo} have the same effect. | |
4550 | ||
4551 | @cindex grouping options | |
4552 | @cindex options, grouping | |
4553 | Many options have multi-letter names; therefore multiple single-letter | |
4554 | options may @emph{not} be grouped: @option{-dM} is very different from | |
4555 | @w{@samp{-d -M}}. | |
4556 | ||
4557 | @cindex options | |
4558 | ||
4559 | @table @gcctabopt | |
4560 | @include cppopts.texi | |
4561 | @include cppdiropts.texi | |
4562 | @include cppwarnopts.texi | |
4563 | @end table | |
4564 | @c man end | |
4565 | ||
4566 | @node Environment Variables | |
4567 | @chapter Environment Variables | |
4568 | @cindex environment variables | |
4569 | @c man begin ENVIRONMENT | |
4570 | ||
4571 | This section describes the environment variables that affect how CPP | |
4572 | operates. You can use them to specify directories or prefixes to use | |
4573 | when searching for include files, or to control dependency output. | |
4574 | ||
4575 | Note that you can also specify places to search using options such as | |
4576 | @option{-I}, and control dependency output with options like | |
4577 | @option{-M} (@pxref{Invocation}). These take precedence over | |
4578 | environment variables, which in turn take precedence over the | |
4579 | configuration of GCC@. | |
4580 | ||
4581 | @include cppenv.texi | |
4582 | @c man end | |
4583 | ||
4584 | @page | |
4585 | @include fdl.texi | |
4586 | ||
4587 | @page | |
4588 | @node Index of Directives | |
4589 | @unnumbered Index of Directives | |
4590 | @printindex fn | |
4591 | ||
4592 | @node Option Index | |
4593 | @unnumbered Option Index | |
4594 | @noindent | |
4595 | CPP's command-line options and environment variables are indexed here | |
4596 | without any initial @samp{-} or @samp{--}. | |
4597 | @printindex op | |
4598 | ||
4599 | @page | |
4600 | @node Concept Index | |
4601 | @unnumbered Concept Index | |
4602 | @printindex cp | |
4603 | ||
4604 | @bye |