source: public/doc/gnu-c/Argument-Prescan.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.3 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Previous: <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> &nbsp; [<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
69substituted into a macro body, unless they are stringified or pasted
70with other tokens. After substitution, the entire macro body, including
71the substituted arguments, is scanned again for macros to be expanded.
72The result is that the arguments are scanned <em>twice</em> to expand
73macro calls in them.
74</p>
75<p>Most of the time, this has no effect. If the argument contained any
76macro calls, they were expanded during the first scan. The result
77therefore contains no macro calls, so the second scan does not change
78it. If the argument were substituted as given, with no prescan, the
79single remaining scan would find the same macro calls and produce the
80same results.
81</p>
82<p>You might expect the double scan to change the results when a
83self-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
85expanded once in the first scan, and a second time in the second scan.
86However, this is not what happens. The self-references that do not
87expand in the first scan are marked so that they will not expand in the
88second scan either.
89</p>
90<p>You might wonder, &ldquo;Why mention the prescan, if it makes no difference?
91And why not skip it and make preprocessing go faster?&rdquo; The answer is
92that 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&rsquo;s argument
98contains a call to that very macro. For example, if <code>f</code> is a macro
99that 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
101substituting that into the definition of <code>f</code>. The prescan causes
102the expected result to happen. Without the prescan, <code>f (1)</code> itself
103would be substituted as an argument, and the inner use of <code>f</code> would
104appear during the main scan as an indirect self-reference and would not
105be 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
110occur. If you <em>want</em> to expand a macro, then stringify or
111concatenate its expansion, you can do that by causing one macro to call
112another macro that does the stringification or concatenation. For
113instance, 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
129wrong 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
138would then turn into <code>(1 + (a,b))</code>. Instead, <code>bar(foo)</code>
139expands into <code>lose(a,b)</code>, which gives an error because <code>lose</code>
140requires a single argument. In this case, the problem is easily solved
141by the same parentheses that ought to be used to prevent misnesting of
142arithmetic 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>&rsquo;s
151definition from being interpreted as an argument separator.
152</p></li></ul>
153
154
155<hr>
156<div class="header">
157<p>
158Previous: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.