You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Line-Numbering.html 7.3KB

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