]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/cppinternals/files.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / cppinternals / files.rst
1 .. _files:
2
3 File Handling
4 =============
5
6 .. index:: files
7
8 Fairly obviously, the file handling code of cpplib resides in the file
9 :samp:`files.c`. It takes care of the details of file searching,
10 opening, reading and caching, for both the main source file and all the
11 headers it recursively includes.
12
13 The basic strategy is to minimize the number of system calls. On many
14 systems, the basic ``open ()`` and ``fstat ()`` system calls can
15 be quite expensive. For every ``#include`` -d file, we need to try
16 all the directories in the search path until we find a match. Some
17 projects, such as glibc, pass twenty or thirty include paths on the
18 command line, so this can rapidly become time consuming.
19
20 For a header file we have not encountered before we have little choice
21 but to do this. However, it is often the case that the same headers are
22 repeatedly included, and in these cases we try to avoid repeating the
23 filesystem queries whilst searching for the correct file.
24
25 For each file we try to open, we store the constructed path in a splay
26 tree. This path first undergoes simplification by the function
27 ``_cpp_simplify_pathname``. For example,
28 :samp:`/usr/include/bits/../foo.h` is simplified to
29 :samp:`/usr/include/foo.h` before we enter it in the splay tree and try
30 to ``open ()`` the file. CPP will then find subsequent uses of
31 :samp:`foo.h`, even as :samp:`/usr/include/foo.h`, in the splay tree and
32 save system calls.
33
34 Further, it is likely the file contents have also been cached, saving a
35 ``read ()`` system call. We don't bother caching the contents of
36 header files that are re-inclusion protected, and whose re-inclusion
37 macro is defined when we leave the header file for the first time. If
38 the host supports it, we try to map suitably large files into memory,
39 rather than reading them in directly.
40
41 The include paths are internally stored on a null-terminated
42 singly-linked list, starting with the ``"header.h"`` directory search
43 chain, which then links into the ``<header.h>`` directory chain.
44
45 Files included with the ``<foo.h>`` syntax start the lookup directly
46 in the second half of this chain. However, files included with the
47 ``"foo.h"`` syntax start at the beginning of the chain, but with one
48 extra directory prepended. This is the directory of the current file;
49 the one containing the ``#include`` directive. Prepending this
50 directory on a per-file basis is handled by the function
51 ``search_from``.
52
53 Note that a header included with a directory component, such as
54 ``#include "mydir/foo.h"`` and opened as
55 :samp:`/usr/local/include/mydir/foo.h`, will have the complete path minus
56 the basename :samp:`foo.h` as the current directory.
57
58 Enough information is stored in the splay tree that CPP can immediately
59 tell whether it can skip the header file because of the multiple include
60 optimization, whether the file didn't exist or couldn't be opened for
61 some reason, or whether the header was flagged not to be re-used, as it
62 is with the obsolete ``#import`` directive.
63
64 For the benefit of MS-DOS filesystems with an 8.3 filename limitation,
65 CPP offers the ability to treat various include file names as aliases
66 for the real header files with shorter names. The map from one to the
67 other is found in a special file called :samp:`header.gcc`, stored in the
68 command line (or system) include directories to which the mapping
69 applies. This may be higher up the directory tree than the full path to
70 the file minus the base name.