Argument-Prescan.html 7.4 KB

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