OSDN Git Service

* include/ext/stl_rope.h (_Rope_RopeRep<>::_M_c_string_lock): Tweak.
[pf3gnuchains/gcc-fork.git] / gcc / c-pch.c
1 /* Precompiled header implementation for the C languages.
2    Copyright (C) 2000, 2002 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
35 struct c_pch_validity
36 {
37   unsigned char debug_info_type;
38 };
39
40 struct c_pch_header 
41 {
42   unsigned long asm_size;
43 };
44
45 #define IDENT_LENGTH 8
46
47 static FILE *pch_outfile;
48
49 extern char *asm_file_name;
50 static long asm_file_startpos;
51
52 static const char * get_ident PARAMS((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()
61 {
62   static char result[IDENT_LENGTH];
63   static const char template[IDENT_LENGTH] = "gpch.011";
64   
65   memcpy (result, template, IDENT_LENGTH);
66   if (c_language == clk_c)
67     result[4] = flag_objc ? 'o' : 'C';
68   else if (c_language == clk_cplusplus)
69     result[4] = flag_objc ? 'O' : '+';
70   else
71     abort ();
72   return result;
73 }
74
75 /* Prepare to write a PCH file.  This is called at the start of 
76    compilation.  */
77
78 void
79 pch_init ()
80 {
81   FILE *f;
82   struct c_pch_validity v;
83   
84   if (! pch_file)
85     return;
86   
87   f = fopen (pch_file, "w+b");
88   if (f == NULL)
89     fatal_io_error ("can't open %s", pch_file);
90   pch_outfile = f;
91   
92   v.debug_info_type = write_symbols;
93   if (fwrite (get_ident(), IDENT_LENGTH, 1, f) != 1
94       || fwrite (&v, sizeof (v), 1, f) != 1)
95     fatal_io_error ("can't write to %s", pch_file);
96
97   /* We need to be able to re-read the output.  */
98   /* The driver always provides a valid -o option.  */
99   if (asm_file_name == NULL
100       || strcmp (asm_file_name, "-") == 0)
101     fatal_error ("`%s' is not a valid output file", asm_file_name);
102   
103   asm_file_startpos = ftell (asm_out_file);
104   
105   /* Let the debugging format deal with the PCHness.  */
106   (*debug_hooks->handle_pch) (0);
107   
108   cpp_save_state (parse_in, f);
109 }
110
111 /* Write the PCH file.  This is called at the end of a compilation which
112    will produce a PCH file.  */
113
114 void
115 c_common_write_pch ()
116 {
117   char *buf;
118   long asm_file_end;
119   long written;
120   struct c_pch_header h;
121
122   (*debug_hooks->handle_pch) (1);
123
124   cpp_write_pch_deps (parse_in, pch_outfile);
125
126   asm_file_end = ftell (asm_out_file);
127   h.asm_size = asm_file_end - asm_file_startpos;
128   
129   if (fwrite (&h, sizeof (h), 1, pch_outfile) != 1)
130     fatal_io_error ("can't write %s", pch_file);
131   
132   buf = xmalloc (16384);
133   fflush (asm_out_file);
134
135   if (fseek (asm_out_file, asm_file_startpos, SEEK_SET) != 0)
136     fatal_io_error ("can't seek in %s", asm_file_name);
137
138   for (written = asm_file_startpos; written < asm_file_end; )
139     {
140       long size = asm_file_end - written;
141       if (size > 16384)
142         size = 16384;
143       if (fread (buf, size, 1, asm_out_file) != 1)
144         fatal_io_error ("can't read %s", asm_file_name);
145       if (fwrite (buf, size, 1, pch_outfile) != 1)
146         fatal_io_error ("can't write %s", pch_file);
147       written += size;
148     }
149   free (buf);
150
151   gt_pch_save (pch_outfile);
152   cpp_write_pch_state (parse_in, pch_outfile);
153
154   fclose (pch_outfile);
155 }
156
157 /* Check the PCH file called NAME, open on FD, to see if it can be used
158    in this compilation.  */
159
160 int
161 c_common_valid_pch (pfile, name, fd)
162      cpp_reader *pfile;
163      const char *name;
164      int fd;
165 {
166   int sizeread;
167   int result;
168   char ident[IDENT_LENGTH];
169   const char *pch_ident;
170   struct c_pch_validity v;
171
172   if (! allow_pch)
173     return 2;
174
175   /* Perform a quick test of whether this is a valid
176      precompiled header for the current language.  */
177
178   sizeread = read (fd, ident, IDENT_LENGTH);
179   if (sizeread == -1)
180     {
181       fatal_io_error ("can't read %s", name);
182       return 2;
183     }
184   else if (sizeread != IDENT_LENGTH)
185     return 2;
186   
187   pch_ident = get_ident();
188   if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
189     {
190       if (cpp_get_options (pfile)->warn_invalid_pch)
191         {
192           if (memcmp (ident, pch_ident, 5) == 0)
193             /* It's a PCH, for the right language, but has the wrong version.
194              */
195             cpp_error (pfile, DL_WARNING, 
196                        "%s: not compatible with this GCC version", name);
197           else if (memcmp (ident, pch_ident, 4) == 0)
198             /* It's a PCH for the wrong language.  */
199             cpp_error (pfile, DL_WARNING, "%s: not for %s", name,
200                        lang_hooks.name);
201           else 
202             /* Not any kind of PCH.  */
203             cpp_error (pfile, DL_WARNING, "%s: not a PCH file", name);
204         }
205       return 2;
206     }
207
208   if (read (fd, &v, sizeof (v)) != sizeof (v))
209     {
210       fatal_io_error ("can't read %s", name);
211       return 2;
212     }
213
214   /* The allowable debug info combinations are that either the PCH file
215      was built with the same as is being used now, or the PCH file was
216      built for some kind of debug info but now none is in use.  */
217   if (v.debug_info_type != write_symbols
218       && write_symbols != NO_DEBUG)
219     {
220       if (cpp_get_options (pfile)->warn_invalid_pch)
221         cpp_error (pfile, DL_WARNING, 
222                    "%s: created with -g%s, but used with -g%s", name,
223                    debug_type_names[v.debug_info_type],
224                    debug_type_names[write_symbols]);
225       return 2;
226     }
227
228   /* Check the preprocessor macros are the same as when the PCH was
229      generated.  */
230   
231   result = cpp_valid_state (pfile, name, fd);
232   if (result == -1)
233     return 2;
234   else
235     return result == 0;
236 }
237
238 /* Load in the PCH file NAME, open on FD.  It was originally searched for
239    by ORIG_NAME.  */
240
241 void
242 c_common_read_pch (pfile, name, fd, orig_name)
243      cpp_reader *pfile;
244      const char *name;
245      int fd;
246      const char *orig_name ATTRIBUTE_UNUSED;
247 {
248   FILE *f;
249   struct c_pch_header h;
250   char *buf;
251   unsigned long written;
252   struct save_macro_data *smd;
253   
254   f = fdopen (fd, "rb");
255   if (f == NULL)
256     {
257       cpp_errno (pfile, DL_ERROR, "calling fdopen");
258       return;
259     }
260
261   allow_pch = 0;
262
263   if (fread (&h, sizeof (h), 1, f) != 1)
264     {
265       cpp_errno (pfile, DL_ERROR, "reading");
266       return;
267     }
268
269   buf = xmalloc (16384);
270   for (written = 0; written < h.asm_size; )
271     {
272       long size = h.asm_size - written;
273       if (size > 16384)
274         size = 16384;
275       if (fread (buf, size, 1, f) != 1
276           || fwrite (buf, size, 1, asm_out_file) != 1)
277         cpp_errno (pfile, DL_ERROR, "reading");
278       written += size;
279     }
280   free (buf);
281
282   cpp_prepare_state (pfile, &smd);
283
284   gt_pch_restore (f);
285
286   if (cpp_read_state (pfile, name, f, smd) != 0)
287     return;
288
289   fclose (f);
290 }