Line-Numbering.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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: Line Numbering</title>
  6. <meta name="description" content="The GNU C Preprocessor Internals: Line Numbering">
  7. <meta name="keywords" content="The GNU C Preprocessor Internals: Line Numbering">
  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="Guard-Macros.html#Guard-Macros" rel="next" title="Guard Macros">
  17. <link href="Token-Spacing.html#Token-Spacing" rel="prev" title="Token Spacing">
  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="Line-Numbering"></a>
  49. <div class="header">
  50. <p>
  51. Next: <a href="Guard-Macros.html#Guard-Macros" accesskey="n" rel="next">Guard Macros</a>, Previous: <a href="Token-Spacing.html#Token-Spacing" accesskey="p" rel="prev">Token Spacing</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="Line-numbering"></a>
  55. <h2 class="unnumbered">Line numbering</h2>
  56. <a name="index-line-numbers"></a>
  57. <a name="Just-which-line-number-anyway_003f"></a>
  58. <h3 class="section">Just which line number anyway?</h3>
  59. <p>There are three reasonable requirements a cpplib client might have for
  60. the line number of a token passed to it:
  61. </p>
  62. <ul>
  63. <li> The source line it was lexed on.
  64. </li><li> The line it is output on. This can be different to the line it was
  65. lexed on if, for example, there are intervening escaped newlines or
  66. C-style comments. For example:
  67. <div class="smallexample">
  68. <pre class="smallexample">foo /* <span class="roman">A long
  69. comment</span> */ bar \
  70. baz
  71. &rArr;
  72. foo bar baz
  73. </pre></div>
  74. </li><li> If the token results from a macro expansion, the line of the macro name,
  75. or possibly the line of the closing parenthesis in the case of
  76. function-like macro expansion.
  77. </li></ul>
  78. <p>The <code>cpp_token</code> structure contains <code>line</code> and <code>col</code>
  79. members. The lexer fills these in with the line and column of the first
  80. character of the token. Consequently, but maybe unexpectedly, a token
  81. from the replacement list of a macro expansion carries the location of
  82. the token within the <code>#define</code> directive, because cpplib expands a
  83. macro by returning pointers to the tokens in its replacement list. The
  84. current implementation of cpplib assigns tokens created from built-in
  85. macros and the &lsquo;<samp>#</samp>&rsquo; and &lsquo;<samp>##</samp>&rsquo; operators the location of the most
  86. recently lexed token. This is a because they are allocated from the
  87. lexer&rsquo;s token runs, and because of the way the diagnostic routines infer
  88. the appropriate location to report.
  89. </p>
  90. <p>The diagnostic routines in cpplib display the location of the most
  91. recently <em>lexed</em> token, unless they are passed a specific line and
  92. column to report. For diagnostics regarding tokens that arise from
  93. macro expansions, it might also be helpful for the user to see the
  94. original location in the macro definition that the token came from.
  95. Since that is exactly the information each token carries, such an
  96. enhancement could be made relatively easily in future.
  97. </p>
  98. <p>The stand-alone preprocessor faces a similar problem when determining
  99. the correct line to output the token on: the position attached to a
  100. token is fairly useless if the token came from a macro expansion. All
  101. tokens on a logical line should be output on its first physical line, so
  102. the token&rsquo;s reported location is also wrong if it is part of a physical
  103. line other than the first.
  104. </p>
  105. <p>To solve these issues, cpplib provides a callback that is generated
  106. whenever it lexes a preprocessing token that starts a new logical line
  107. other than a directive. It passes this token (which may be a
  108. <code>CPP_EOF</code> token indicating the end of the translation unit) to the
  109. callback routine, which can then use the line and column of this token
  110. to produce correct output.
  111. </p>
  112. <a name="Representation-of-line-numbers"></a>
  113. <h3 class="section">Representation of line numbers</h3>
  114. <p>As mentioned above, cpplib stores with each token the line number that
  115. it was lexed on. In fact, this number is not the number of the line in
  116. the source file, but instead bears more resemblance to the number of the
  117. line in the translation unit.
  118. </p>
  119. <p>The preprocessor maintains a monotonic increasing line count, which is
  120. incremented at every new line character (and also at the end of any
  121. buffer that does not end in a new line). Since a line number of zero is
  122. useful to indicate certain special states and conditions, this variable
  123. starts counting from one.
  124. </p>
  125. <p>This variable therefore uniquely enumerates each line in the translation
  126. unit. With some simple infrastructure, it is straight forward to map
  127. from this to the original source file and line number pair, saving space
  128. whenever line number information needs to be saved. The code the
  129. implements this mapping lies in the files <samp>line-map.c</samp> and
  130. <samp>line-map.h</samp>.
  131. </p>
  132. <p>Command-line macros and assertions are implemented by pushing a buffer
  133. containing the right hand side of an equivalent <code>#define</code> or
  134. <code>#assert</code> directive. Some built-in macros are handled similarly.
  135. Since these are all processed before the first line of the main input
  136. file, it will typically have an assigned line closer to twenty than to
  137. one.
  138. </p>
  139. <hr>
  140. <div class="header">
  141. <p>
  142. Next: <a href="Guard-Macros.html#Guard-Macros" accesskey="n" rel="next">Guard Macros</a>, Previous: <a href="Token-Spacing.html#Token-Spacing" accesskey="p" rel="prev">Token Spacing</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>
  143. </div>
  144. </body>
  145. </html>