Token-Spacing.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>Token Spacing (The GNU C Preprocessor Internals)</title>
  7. <meta name="description" content="Token Spacing (The GNU C Preprocessor Internals)">
  8. <meta name="keywords" content="Token Spacing (The GNU C Preprocessor Internals)">
  9. <meta name="resource-type" content="document">
  10. <meta name="distribution" content="global">
  11. <meta name="Generator" content="makeinfo">
  12. <link href="index.html" rel="start" title="Top">
  13. <link href="Concept-Index.html" rel="index" title="Concept Index">
  14. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  15. <link href="index.html" rel="up" title="Top">
  16. <link href="Line-Numbering.html" rel="next" title="Line Numbering">
  17. <link href="Macro-Expansion.html" rel="prev" title="Macro Expansion">
  18. <style type="text/css">
  19. <!--
  20. a.summary-letter {text-decoration: none}
  21. blockquote.indentedblock {margin-right: 0em}
  22. div.display {margin-left: 3.2em}
  23. div.example {margin-left: 3.2em}
  24. div.lisp {margin-left: 3.2em}
  25. kbd {font-style: oblique}
  26. pre.display {font-family: inherit}
  27. pre.format {font-family: inherit}
  28. pre.menu-comment {font-family: serif}
  29. pre.menu-preformatted {font-family: serif}
  30. span.nolinebreak {white-space: nowrap}
  31. span.roman {font-family: initial; font-weight: normal}
  32. span.sansserif {font-family: sans-serif; font-weight: normal}
  33. ul.no-bullet {list-style: none}
  34. -->
  35. </style>
  36. </head>
  37. <body lang="en">
  38. <span id="Token-Spacing"></span><div class="header">
  39. <p>
  40. Next: <a href="Line-Numbering.html" accesskey="n" rel="next">Line Numbering</a>, Previous: <a href="Macro-Expansion.html" accesskey="p" rel="prev">Macro Expansion</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
  41. </div>
  42. <hr>
  43. <span id="Token-Spacing-1"></span><h2 class="unnumbered">Token Spacing</h2>
  44. <span id="index-paste-avoidance"></span>
  45. <span id="index-spacing"></span>
  46. <span id="index-token-spacing"></span>
  47. <p>First, consider an issue that only concerns the stand-alone
  48. preprocessor: there needs to be a guarantee that re-reading its preprocessed
  49. output results in an identical token stream. Without taking special
  50. measures, this might not be the case because of macro substitution.
  51. For example:
  52. </p>
  53. <div class="example">
  54. <pre class="example">#define PLUS +
  55. #define EMPTY
  56. #define f(x) =x=
  57. +PLUS -EMPTY- PLUS+ f(=)
  58. &rarr; + + - - + + = = =
  59. <em>not</em>
  60. &rarr; ++ -- ++ ===
  61. </pre></div>
  62. <p>One solution would be to simply insert a space between all adjacent
  63. tokens. However, we would like to keep space insertion to a minimum,
  64. both for aesthetic reasons and because it causes problems for people who
  65. still try to abuse the preprocessor for things like Fortran source and
  66. Makefiles.
  67. </p>
  68. <p>For now, just notice that when tokens are added (or removed, as shown by
  69. the <code>EMPTY</code> example) from the original lexed token stream, we need
  70. to check for accidental token pasting. We call this <em>paste
  71. avoidance</em>. Token addition and removal can only occur because of macro
  72. expansion, but accidental pasting can occur in many places: both before
  73. and after each macro replacement, each argument replacement, and
  74. additionally each token created by the &lsquo;<samp>#</samp>&rsquo; and &lsquo;<samp>##</samp>&rsquo; operators.
  75. </p>
  76. <p>Look at how the preprocessor gets whitespace output correct
  77. normally. The <code>cpp_token</code> structure contains a flags byte, and one
  78. of those flags is <code>PREV_WHITE</code>. This is flagged by the lexer, and
  79. indicates that the token was preceded by whitespace of some form other
  80. than a new line. The stand-alone preprocessor can use this flag to
  81. decide whether to insert a space between tokens in the output.
  82. </p>
  83. <p>Now consider the result of the following macro expansion:
  84. </p>
  85. <div class="example">
  86. <pre class="example">#define add(x, y, z) x + y +z;
  87. sum = add (1,2, 3);
  88. &rarr; sum = 1 + 2 +3;
  89. </pre></div>
  90. <p>The interesting thing here is that the tokens &lsquo;<samp>1</samp>&rsquo; and &lsquo;<samp>2</samp>&rsquo; are
  91. output with a preceding space, and &lsquo;<samp>3</samp>&rsquo; is output without a
  92. preceding space, but when lexed none of these tokens had that property.
  93. Careful consideration reveals that &lsquo;<samp>1</samp>&rsquo; gets its preceding
  94. whitespace from the space preceding &lsquo;<samp>add</samp>&rsquo; in the macro invocation,
  95. <em>not</em> replacement list. &lsquo;<samp>2</samp>&rsquo; gets its whitespace from the
  96. space preceding the parameter &lsquo;<samp>y</samp>&rsquo; in the macro replacement list,
  97. and &lsquo;<samp>3</samp>&rsquo; has no preceding space because parameter &lsquo;<samp>z</samp>&rsquo; has none
  98. in the replacement list.
  99. </p>
  100. <p>Once lexed, tokens are effectively fixed and cannot be altered, since
  101. pointers to them might be held in many places, in particular by
  102. in-progress macro expansions. So instead of modifying the two tokens
  103. above, the preprocessor inserts a special token, which I call a
  104. <em>padding token</em>, into the token stream to indicate that spacing of
  105. the subsequent token is special. The preprocessor inserts padding
  106. tokens in front of every macro expansion and expanded macro argument.
  107. These point to a <em>source token</em> from which the subsequent real token
  108. should inherit its spacing. In the above example, the source tokens are
  109. &lsquo;<samp>add</samp>&rsquo; in the macro invocation, and &lsquo;<samp>y</samp>&rsquo; and &lsquo;<samp>z</samp>&rsquo; in the
  110. macro replacement list, respectively.
  111. </p>
  112. <p>It is quite easy to get multiple padding tokens in a row, for example if
  113. a macro&rsquo;s first replacement token expands straight into another macro.
  114. </p>
  115. <div class="example">
  116. <pre class="example">#define foo bar
  117. #define bar baz
  118. [foo]
  119. &rarr; [baz]
  120. </pre></div>
  121. <p>Here, two padding tokens are generated with sources the &lsquo;<samp>foo</samp>&rsquo; token
  122. between the brackets, and the &lsquo;<samp>bar</samp>&rsquo; token from foo&rsquo;s replacement
  123. list, respectively. Clearly the first padding token is the one to
  124. use, so the output code should contain a rule that the first
  125. padding token in a sequence is the one that matters.
  126. </p>
  127. <p>But what if a macro expansion is left? Adjusting the above
  128. example slightly:
  129. </p>
  130. <div class="example">
  131. <pre class="example">#define foo bar
  132. #define bar EMPTY baz
  133. #define EMPTY
  134. [foo] EMPTY;
  135. &rarr; [ baz] ;
  136. </pre></div>
  137. <p>As shown, now there should be a space before &lsquo;<samp>baz</samp>&rsquo; and the
  138. semicolon in the output.
  139. </p>
  140. <p>The rules we decided above fail for &lsquo;<samp>baz</samp>&rsquo;: we generate three
  141. padding tokens, one per macro invocation, before the token &lsquo;<samp>baz</samp>&rsquo;.
  142. We would then have it take its spacing from the first of these, which
  143. carries source token &lsquo;<samp>foo</samp>&rsquo; with no leading space.
  144. </p>
  145. <p>It is vital that cpplib get spacing correct in these examples since any
  146. of these macro expansions could be stringized, where spacing matters.
  147. </p>
  148. <p>So, this demonstrates that not just entering macro and argument
  149. expansions, but leaving them requires special handling too. I made
  150. cpplib insert a padding token with a <code>NULL</code> source token when
  151. leaving macro expansions, as well as after each replaced argument in a
  152. macro&rsquo;s replacement list. It also inserts appropriate padding tokens on
  153. either side of tokens created by the &lsquo;<samp>#</samp>&rsquo; and &lsquo;<samp>##</samp>&rsquo; operators.
  154. I expanded the rule so that, if we see a padding token with a
  155. <code>NULL</code> source token, <em>and</em> that source token has no leading
  156. space, then we behave as if we have seen no padding tokens at all. A
  157. quick check shows this rule will then get the above example correct as
  158. well.
  159. </p>
  160. <p>Now a relationship with paste avoidance is apparent: we have to be
  161. careful about paste avoidance in exactly the same locations we have
  162. padding tokens in order to get white space correct. This makes
  163. implementation of paste avoidance easy: wherever the stand-alone
  164. preprocessor is fixing up spacing because of padding tokens, and it
  165. turns out that no space is needed, it has to take the extra step to
  166. check that a space is not needed after all to avoid an accidental paste.
  167. The function <code>cpp_avoid_paste</code> advises whether a space is required
  168. between two consecutive tokens. To avoid excessive spacing, it tries
  169. hard to only require a space if one is likely to be necessary, but for
  170. reasons of efficiency it is slightly conservative and might recommend a
  171. space where one is not strictly needed.
  172. </p>
  173. <hr>
  174. <div class="header">
  175. <p>
  176. Next: <a href="Line-Numbering.html" accesskey="n" rel="next">Line Numbering</a>, Previous: <a href="Macro-Expansion.html" accesskey="p" rel="prev">Macro Expansion</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
  177. </div>
  178. </body>
  179. </html>