Files.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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>Files (The GNU C Preprocessor Internals)</title>
  7. <meta name="description" content="Files (The GNU C Preprocessor Internals)">
  8. <meta name="keywords" content="Files (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="Concept-Index.html" rel="next" title="Concept Index">
  17. <link href="Guard-Macros.html" rel="prev" title="Guard Macros">
  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="Files"></span><div class="header">
  39. <p>
  40. Next: <a href="Concept-Index.html" accesskey="n" rel="next">Concept Index</a>, Previous: <a href="Guard-Macros.html" accesskey="p" rel="prev">Guard Macros</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="File-Handling"></span><h2 class="unnumbered">File Handling</h2>
  44. <span id="index-files"></span>
  45. <p>Fairly obviously, the file handling code of cpplib resides in the file
  46. <samp>files.cc</samp>. It takes care of the details of file searching,
  47. opening, reading and caching, for both the main source file and all the
  48. headers it recursively includes.
  49. </p>
  50. <p>The basic strategy is to minimize the number of system calls. On many
  51. systems, the basic <code>open ()</code> and <code>fstat ()</code> system calls can
  52. be quite expensive. For every <code>#include</code>-d file, we need to try
  53. all the directories in the search path until we find a match. Some
  54. projects, such as glibc, pass twenty or thirty include paths on the
  55. command line, so this can rapidly become time consuming.
  56. </p>
  57. <p>For a header file we have not encountered before we have little choice
  58. but to do this. However, it is often the case that the same headers are
  59. repeatedly included, and in these cases we try to avoid repeating the
  60. filesystem queries whilst searching for the correct file.
  61. </p>
  62. <p>For each file we try to open, we store the constructed path in a splay
  63. tree. This path first undergoes simplification by the function
  64. <code>_cpp_simplify_pathname</code>. For example,
  65. <samp>/usr/include/bits/../foo.h</samp> is simplified to
  66. <samp>/usr/include/foo.h</samp> before we enter it in the splay tree and try
  67. to <code>open ()</code> the file. CPP will then find subsequent uses of
  68. <samp>foo.h</samp>, even as <samp>/usr/include/foo.h</samp>, in the splay tree and
  69. save system calls.
  70. </p>
  71. <p>Further, it is likely the file contents have also been cached, saving a
  72. <code>read ()</code> system call. We don&rsquo;t bother caching the contents of
  73. header files that are re-inclusion protected, and whose re-inclusion
  74. macro is defined when we leave the header file for the first time. If
  75. the host supports it, we try to map suitably large files into memory,
  76. rather than reading them in directly.
  77. </p>
  78. <p>The include paths are internally stored on a null-terminated
  79. singly-linked list, starting with the <code>&quot;header.h&quot;</code> directory search
  80. chain, which then links into the <code>&lt;header.h&gt;</code> directory chain.
  81. </p>
  82. <p>Files included with the <code>&lt;foo.h&gt;</code> syntax start the lookup directly
  83. in the second half of this chain. However, files included with the
  84. <code>&quot;foo.h&quot;</code> syntax start at the beginning of the chain, but with one
  85. extra directory prepended. This is the directory of the current file;
  86. the one containing the <code>#include</code> directive. Prepending this
  87. directory on a per-file basis is handled by the function
  88. <code>search_from</code>.
  89. </p>
  90. <p>Note that a header included with a directory component, such as
  91. <code>#include &quot;mydir/foo.h&quot;</code> and opened as
  92. <samp>/usr/local/include/mydir/foo.h</samp>, will have the complete path minus
  93. the basename &lsquo;<samp>foo.h</samp>&rsquo; as the current directory.
  94. </p>
  95. <p>Enough information is stored in the splay tree that CPP can immediately
  96. tell whether it can skip the header file because of the multiple include
  97. optimization, whether the file didn&rsquo;t exist or couldn&rsquo;t be opened for
  98. some reason, or whether the header was flagged not to be re-used, as it
  99. is with the obsolete <code>#import</code> directive.
  100. </p>
  101. <p>For the benefit of MS-DOS filesystems with an 8.3 filename limitation,
  102. CPP offers the ability to treat various include file names as aliases
  103. for the real header files with shorter names. The map from one to the
  104. other is found in a special file called &lsquo;<samp>header.gcc</samp>&rsquo;, stored in the
  105. command line (or system) include directories to which the mapping
  106. applies. This may be higher up the directory tree than the full path to
  107. the file minus the base name.
  108. </p>
  109. <hr>
  110. <div class="header">
  111. <p>
  112. Next: <a href="Concept-Index.html" accesskey="n" rel="next">Concept Index</a>, Previous: <a href="Guard-Macros.html" accesskey="p" rel="prev">Guard Macros</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>
  113. </div>
  114. </body>
  115. </html>