1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Argument Prescan (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Argument Prescan (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Argument Prescan (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Macro-Pitfalls.html" rel="up" title="Macro Pitfalls">
|
---|
33 | <link href="Conditionals.html" rel="next" title="Conditionals">
|
---|
34 | <link href="Self_002dReferential-Macros.html" rel="prev" title="Self-Referential Macros">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Argument-Prescan"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Previous: <a href="Self_002dReferential-Macros.html" accesskey="p" rel="prev">Self-Referential Macros</a>, Up: <a href="Macro-Pitfalls.html" accesskey="u" rel="up">Macro Pitfalls</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Argument-Prescan-1"></span><h4 class="subsubsection">26.5.10.7 Argument Prescan</h4>
|
---|
64 | <span id="index-expansion-of-arguments"></span>
|
---|
65 | <span id="index-macro-argument-expansion"></span>
|
---|
66 | <span id="index-prescan-of-macro-arguments"></span>
|
---|
67 |
|
---|
68 | <p>Macro arguments are completely macro-expanded before they are
|
---|
69 | substituted into a macro body, unless they are stringified or pasted
|
---|
70 | with other tokens. After substitution, the entire macro body, including
|
---|
71 | the substituted arguments, is scanned again for macros to be expanded.
|
---|
72 | The result is that the arguments are scanned <em>twice</em> to expand
|
---|
73 | macro calls in them.
|
---|
74 | </p>
|
---|
75 | <p>Most of the time, this has no effect. If the argument contained any
|
---|
76 | macro calls, they were expanded during the first scan. The result
|
---|
77 | therefore contains no macro calls, so the second scan does not change
|
---|
78 | it. If the argument were substituted as given, with no prescan, the
|
---|
79 | single remaining scan would find the same macro calls and produce the
|
---|
80 | same results.
|
---|
81 | </p>
|
---|
82 | <p>You might expect the double scan to change the results when a
|
---|
83 | self-referential macro is used in an argument of another macro
|
---|
84 | (see <a href="Self_002dReferential-Macros.html">Self-Referential Macros</a>): the self-referential macro would be
|
---|
85 | expanded once in the first scan, and a second time in the second scan.
|
---|
86 | However, this is not what happens. The self-references that do not
|
---|
87 | expand in the first scan are marked so that they will not expand in the
|
---|
88 | second scan either.
|
---|
89 | </p>
|
---|
90 | <p>You might wonder, “Why mention the prescan, if it makes no difference?
|
---|
91 | And why not skip it and make preprocessing go faster?” The answer is
|
---|
92 | that the prescan does make a difference in three special cases:
|
---|
93 | </p>
|
---|
94 | <ul>
|
---|
95 | <li> Nested calls to a macro.
|
---|
96 |
|
---|
97 | <p>We say that <em>nested</em> calls to a macro occur when a macro’s argument
|
---|
98 | contains a call to that very macro. For example, if <code>f</code> is a macro
|
---|
99 | that expects one argument, <code>f (f (1))</code> is a nested pair of calls to
|
---|
100 | <code>f</code>. The desired expansion is made by expanding <code>f (1)</code> and
|
---|
101 | substituting that into the definition of <code>f</code>. The prescan causes
|
---|
102 | the expected result to happen. Without the prescan, <code>f (1)</code> itself
|
---|
103 | would be substituted as an argument, and the inner use of <code>f</code> would
|
---|
104 | appear during the main scan as an indirect self-reference and would not
|
---|
105 | be expanded.
|
---|
106 | </p>
|
---|
107 | </li><li> Macros that call other macros that stringify or concatenate.
|
---|
108 |
|
---|
109 | <p>If an argument is stringified or concatenated, the prescan does not
|
---|
110 | occur. If you <em>want</em> to expand a macro, then stringify or
|
---|
111 | concatenate its expansion, you can do that by causing one macro to call
|
---|
112 | another macro that does the stringification or concatenation. For
|
---|
113 | instance, if you have
|
---|
114 | </p>
|
---|
115 | <div class="example">
|
---|
116 | <pre class="example">#define AFTERX(x) X_ ## x
|
---|
117 | #define XAFTERX(x) AFTERX(x)
|
---|
118 | #define TABLESIZE 1024
|
---|
119 | #define BUFSIZE TABLESIZE
|
---|
120 | </pre></div>
|
---|
121 |
|
---|
122 | <p>then <code>AFTERX(BUFSIZE)</code> expands to <code>X_BUFSIZE</code>, and
|
---|
123 | <code>XAFTERX(BUFSIZE)</code> expands to <code>X_1024</code>. (Not to
|
---|
124 | <code>X_TABLESIZE</code>. Prescan always does a complete expansion.)
|
---|
125 | </p>
|
---|
126 | </li><li> Macros used in arguments, whose expansions contain unshielded commas.
|
---|
127 |
|
---|
128 | <p>This can cause a macro expanded on the second scan to be called with the
|
---|
129 | wrong number of arguments. Here is an example:
|
---|
130 | </p>
|
---|
131 | <div class="example">
|
---|
132 | <pre class="example">#define foo a,b
|
---|
133 | #define bar(x) lose(x)
|
---|
134 | #define lose(x) (1 + (x))
|
---|
135 | </pre></div>
|
---|
136 |
|
---|
137 | <p>We would like <code>bar(foo)</code> to turn into <code>(1 + (foo))</code>, which
|
---|
138 | would then turn into <code>(1 + (a,b))</code>. Instead, <code>bar(foo)</code>
|
---|
139 | expands into <code>lose(a,b)</code>, which gives an error because <code>lose</code>
|
---|
140 | requires a single argument. In this case, the problem is easily solved
|
---|
141 | by the same parentheses that ought to be used to prevent misnesting of
|
---|
142 | arithmetic operations:
|
---|
143 | </p>
|
---|
144 | <div class="example">
|
---|
145 | <pre class="example">#define foo (a,b)
|
---|
146 | </pre><pre class="example">or
|
---|
147 | </pre><pre class="example">#define bar(x) lose((x))
|
---|
148 | </pre></div>
|
---|
149 |
|
---|
150 | <p>The extra pair of parentheses prevents the comma in <code>foo</code>’s
|
---|
151 | definition from being interpreted as an argument separator.
|
---|
152 | </p></li></ul>
|
---|
153 |
|
---|
154 |
|
---|
155 | <hr>
|
---|
156 | <div class="header">
|
---|
157 | <p>
|
---|
158 | Previous: <a href="Self_002dReferential-Macros.html" accesskey="p" rel="prev">Self-Referential Macros</a>, Up: <a href="Macro-Pitfalls.html" accesskey="u" rel="up">Macro Pitfalls</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
159 | </div>
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 | </body>
|
---|
164 | </html>
|
---|