Line-Numbering.html 6.8 KB

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