OSDN Git Service

003-08-02 Andreas Tobler <a.tobler@schweiz.ch>
[pf3gnuchains/gcc-fork.git] / gcc / c-pch.c
1 /* Precompiled header implementation for the C languages.
2    Copyright (C) 2000, 2002, 2003 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cpplib.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "c-common.h"
28 #include "output.h"
29 #include "toplev.h"
30 #include "debug.h"
31 #include "c-pragma.h"
32 #include "ggc.h"
33 #include "langhooks.h"
34 #include "hosthooks.h"
35
36 struct c_pch_validity
37 {
38   unsigned char debug_info_type;
39 };
40
41 struct c_pch_header 
42 {
43   unsigned long asm_size;
44 };
45
46 #define IDENT_LENGTH 8
47
48 static FILE *pch_outfile;
49
50 static long asm_file_startpos;
51
52 static const char *get_ident (void);
53
54 /* Compute an appropriate 8-byte magic number for the PCH file, so that
55    utilities like file(1) can identify it, and so that GCC can quickly
56    ignore non-PCH files and PCH files that are of a completely different
57    format.  */
58
59 static const char *
60 get_ident(void)
61 {
62   static char result[IDENT_LENGTH];
63   static const char template[IDENT_LENGTH] = "gpch.011";
64   static const char c_language_chars[] = "Co+O";
65   
66   memcpy (result, template, IDENT_LENGTH);
67   result[4] = c_language_chars[c_language];
68
69   return result;
70 }
71
72 /* Prepare to write a PCH file.  This is called at the start of 
73    compilation.  */
74
75 void
76 pch_init (void)
77 {
78   FILE *f;
79   struct c_pch_validity v;
80   
81   if (! pch_file)
82     return;
83   
84   f = fopen (pch_file, "w+b");
85   if (f == NULL)
86     fatal_error ("can't open %s: %m", pch_file);
87   pch_outfile = f;
88   
89   v.debug_info_type = write_symbols;
90   if (fwrite (get_ident(), IDENT_LENGTH, 1, f) != 1
91       || fwrite (&v, sizeof (v), 1, f) != 1)
92     fatal_error ("can't write to %s: %m", pch_file);
93
94   /* We need to be able to re-read the output.  */
95   /* The driver always provides a valid -o option.  */
96   if (asm_file_name == NULL
97       || strcmp (asm_file_name, "-") == 0)
98     fatal_error ("`%s' is not a valid output file", asm_file_name);
99   
100   asm_file_startpos = ftell (asm_out_file);
101   
102   /* Let the debugging format deal with the PCHness.  */
103   (*debug_hooks->handle_pch) (0);
104   
105   cpp_save_state (parse_in, f);
106 }
107
108 /* Write the PCH file.  This is called at the end of a compilation which
109    will produce a PCH file.  */
110
111 void
112 c_common_write_pch (void)
113 {
114   char *buf;
115   long asm_file_end;
116   long written;
117   struct c_pch_header h;
118
119   (*debug_hooks->handle_pch) (1);
120
121   cpp_write_pch_deps (parse_in, pch_outfile);
122
123   asm_file_end = ftell (asm_out_file);
124   h.asm_size = asm_file_end - asm_file_startpos;
125   
126   if (fwrite (&h, sizeof (h), 1, pch_outfile) != 1)
127     fatal_error ("can't write %s: %m", pch_file);
128   
129   buf = xmalloc (16384);
130   fflush (asm_out_file);
131
132   if (fseek (asm_out_file, asm_file_startpos, SEEK_SET) != 0)
133     fatal_error ("can't seek in %s: %m", asm_file_name);
134
135   for (written = asm_file_startpos; written < asm_file_end; )
136     {
137       long size = asm_file_end - written;
138       if (size > 16384)
139         size = 16384;
140       if (fread (buf, size, 1, asm_out_file) != 1)
141         fatal_error ("can't read %s: %m", asm_file_name);
142       if (fwrite (buf, size, 1, pch_outfile) != 1)
143         fatal_error ("can't write %s: %m", pch_file);
144       written += size;
145     }
146   free (buf);
147   /* asm_out_file can be written afterwards, so must be flushed first.  */
148   fflush (asm_out_file);
149
150   gt_pch_save (pch_outfile);
151   cpp_write_pch_state (parse_in, pch_outfile);
152
153   fclose (pch_outfile);
154 }
155
156 /* Check the PCH file called NAME, open on FD, to see if it can be used
157    in this compilation.  */
158
159 int
160 c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
161 {
162   int sizeread;
163   int result;
164   char ident[IDENT_LENGTH];
165   const char *pch_ident;
166   struct c_pch_validity v;
167
168   /* Perform a quick test of whether this is a valid
169      precompiled header for the current language.  */
170
171   sizeread = read (fd, ident, IDENT_LENGTH);
172   if (sizeread == -1)
173     fatal_error ("can't read %s: %m", name);
174   else if (sizeread != IDENT_LENGTH)
175     return 2;
176   
177   pch_ident = get_ident();
178   if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
179     {
180       if (cpp_get_options (pfile)->warn_invalid_pch)
181         {
182           if (memcmp (ident, pch_ident, 5) == 0)
183             /* It's a PCH, for the right language, but has the wrong version.
184              */
185             cpp_error (pfile, DL_WARNING, 
186                        "%s: not compatible with this GCC version", name);
187           else if (memcmp (ident, pch_ident, 4) == 0)
188             /* It's a PCH for the wrong language.  */
189             cpp_error (pfile, DL_WARNING, "%s: not for %s", name,
190                        lang_hooks.name);
191           else 
192             /* Not any kind of PCH.  */
193             cpp_error (pfile, DL_WARNING, "%s: not a PCH file", name);
194         }
195       return 2;
196     }
197
198   if (read (fd, &v, sizeof (v)) != sizeof (v))
199     fatal_error ("can't read %s: %m", name);
200
201   /* The allowable debug info combinations are that either the PCH file
202      was built with the same as is being used now, or the PCH file was
203      built for some kind of debug info but now none is in use.  */
204   if (v.debug_info_type != write_symbols
205       && write_symbols != NO_DEBUG)
206     {
207       if (cpp_get_options (pfile)->warn_invalid_pch)
208         cpp_error (pfile, DL_WARNING, 
209                    "%s: created with -g%s, but used with -g%s", name,
210                    debug_type_names[v.debug_info_type],
211                    debug_type_names[write_symbols]);
212       return 2;
213     }
214
215   /* Check the preprocessor macros are the same as when the PCH was
216      generated.  */
217   
218   result = cpp_valid_state (pfile, name, fd);
219   if (result == -1)
220     return 2;
221   else
222     return result == 0;
223 }
224
225 /* Load in the PCH file NAME, open on FD.  It was originally searched for
226    by ORIG_NAME.  */
227
228 void
229 c_common_read_pch (cpp_reader *pfile, const char *name,
230                    int fd, const char *orig_name ATTRIBUTE_UNUSED)
231 {
232   FILE *f;
233   struct c_pch_header h;
234   char *buf;
235   unsigned long written;
236   struct save_macro_data *smd;
237   
238   f = fdopen (fd, "rb");
239   if (f == NULL)
240     {
241       cpp_errno (pfile, DL_ERROR, "calling fdopen");
242       return;
243     }
244
245   cpp_get_callbacks (parse_in)->valid_pch = NULL;
246
247   if (fread (&h, sizeof (h), 1, f) != 1)
248     {
249       cpp_errno (pfile, DL_ERROR, "reading");
250       return;
251     }
252
253   buf = xmalloc (16384);
254   for (written = 0; written < h.asm_size; )
255     {
256       long size = h.asm_size - written;
257       if (size > 16384)
258         size = 16384;
259       if (fread (buf, size, 1, f) != 1
260           || fwrite (buf, size, 1, asm_out_file) != 1)
261         cpp_errno (pfile, DL_ERROR, "reading");
262       written += size;
263     }
264   free (buf);
265
266   cpp_prepare_state (pfile, &smd);
267
268   gt_pch_restore (f);
269
270   if (cpp_read_state (pfile, name, f, smd) != 0)
271     return;
272
273   fclose (f);
274 }
275
276 /* Indicate that no more PCH files should be read.  */
277
278 void
279 c_common_no_more_pch (void)
280 {
281   if (cpp_get_callbacks (parse_in)->valid_pch)
282     {
283       cpp_get_callbacks (parse_in)->valid_pch = NULL;
284       host_hooks.gt_pch_use_address (NULL, 0);
285     }
286 }