Argument-Prescan.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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-2017 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 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>The C Preprocessor: Argument Prescan</title>
  20. <meta name="description" content="The C Preprocessor: Argument Prescan">
  21. <meta name="keywords" content="The C Preprocessor: Argument Prescan">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Index-of-Directives.html#Index-of-Directives" 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#Macro-Pitfalls" rel="up" title="Macro Pitfalls">
  30. <link href="Newlines-in-Arguments.html#Newlines-in-Arguments" rel="next" title="Newlines in Arguments">
  31. <link href="Self_002dReferential-Macros.html#Self_002dReferential-Macros" rel="prev" title="Self-Referential Macros">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Argument-Prescan"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Newlines-in-Arguments.html#Newlines-in-Arguments" accesskey="n" rel="next">Newlines in Arguments</a>, Previous: <a href="Self_002dReferential-Macros.html#Self_002dReferential-Macros" accesskey="p" rel="prev">Self-Referential Macros</a>, Up: <a href="Macro-Pitfalls.html#Macro-Pitfalls" 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#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Argument-Prescan-1"></a>
  69. <h4 class="subsection">3.10.6 Argument Prescan</h4>
  70. <a name="index-expansion-of-arguments"></a>
  71. <a name="index-macro-argument-expansion"></a>
  72. <a name="index-prescan-of-macro-arguments"></a>
  73. <p>Macro arguments are completely macro-expanded before they are
  74. substituted into a macro body, unless they are stringized or pasted
  75. with other tokens. After substitution, the entire macro body, including
  76. the substituted arguments, is scanned again for macros to be expanded.
  77. The result is that the arguments are scanned <em>twice</em> to expand
  78. macro calls in them.
  79. </p>
  80. <p>Most of the time, this has no effect. If the argument contained any
  81. macro calls, they are expanded during the first scan. The result
  82. therefore contains no macro calls, so the second scan does not change
  83. it. If the argument were substituted as given, with no prescan, the
  84. single remaining scan would find the same macro calls and produce the
  85. same results.
  86. </p>
  87. <p>You might expect the double scan to change the results when a
  88. self-referential macro is used in an argument of another macro
  89. (see <a href="Self_002dReferential-Macros.html#Self_002dReferential-Macros">Self-Referential Macros</a>): the self-referential macro would be
  90. expanded once in the first scan, and a second time in the second scan.
  91. However, this is not what happens. The self-references that do not
  92. expand in the first scan are marked so that they will not expand in the
  93. second scan either.
  94. </p>
  95. <p>You might wonder, &ldquo;Why mention the prescan, if it makes no difference?
  96. And why not skip it and make the preprocessor faster?&rdquo; The answer is
  97. that the prescan does make a difference in three special cases:
  98. </p>
  99. <ul>
  100. <li> Nested calls to a macro.
  101. <p>We say that <em>nested</em> calls to a macro occur when a macro&rsquo;s argument
  102. contains a call to that very macro. For example, if <code>f</code> is a macro
  103. that expects one argument, <code>f (f (1))</code> is a nested pair of calls to
  104. <code>f</code>. The desired expansion is made by expanding <code>f (1)</code> and
  105. substituting that into the definition of <code>f</code>. The prescan causes
  106. the expected result to happen. Without the prescan, <code>f (1)</code> itself
  107. would be substituted as an argument, and the inner use of <code>f</code> would
  108. appear during the main scan as an indirect self-reference and would not
  109. be expanded.
  110. </p>
  111. </li><li> Macros that call other macros that stringize or concatenate.
  112. <p>If an argument is stringized or concatenated, the prescan does not
  113. occur. If you <em>want</em> to expand a macro, then stringize or
  114. concatenate its expansion, you can do that by causing one macro to call
  115. another macro that does the stringizing or concatenation. For
  116. instance, if you have
  117. </p>
  118. <div class="smallexample">
  119. <pre class="smallexample">#define AFTERX(x) X_ ## x
  120. #define XAFTERX(x) AFTERX(x)
  121. #define TABLESIZE 1024
  122. #define BUFSIZE TABLESIZE
  123. </pre></div>
  124. <p>then <code>AFTERX(BUFSIZE)</code> expands to <code>X_BUFSIZE</code>, and
  125. <code>XAFTERX(BUFSIZE)</code> expands to <code>X_1024</code>. (Not to
  126. <code>X_TABLESIZE</code>. Prescan always does a complete expansion.)
  127. </p>
  128. </li><li> Macros used in arguments, whose expansions contain unshielded commas.
  129. <p>This can cause a macro expanded on the second scan to be called with the
  130. wrong number of arguments. Here is an example:
  131. </p>
  132. <div class="smallexample">
  133. <pre class="smallexample">#define foo a,b
  134. #define bar(x) lose(x)
  135. #define lose(x) (1 + (x))
  136. </pre></div>
  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>, and you get 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="smallexample">
  145. <pre class="smallexample">#define foo (a,b)
  146. </pre><pre class="smallexample">or
  147. </pre><pre class="smallexample">#define bar(x) lose((x))
  148. </pre></div>
  149. <p>The extra pair of parentheses prevents the comma in <code>foo</code>&rsquo;s
  150. definition from being interpreted as an argument separator.
  151. </p>
  152. </li></ul>
  153. <hr>
  154. <div class="header">
  155. <p>
  156. Next: <a href="Newlines-in-Arguments.html#Newlines-in-Arguments" accesskey="n" rel="next">Newlines in Arguments</a>, Previous: <a href="Self_002dReferential-Macros.html#Self_002dReferential-Macros" accesskey="p" rel="prev">Self-Referential Macros</a>, Up: <a href="Macro-Pitfalls.html#Macro-Pitfalls" 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#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
  157. </div>
  158. </body>
  159. </html>