Token-Spacing.html 9.8 KB

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