#include "config.h"
#include "system.h"
#include "cpplib.h"
#include "tree.h"
#include "flags.h"
#include "c-common.h"
#include "output.h"
#include "toplev.h"
#include "debug.h"
#include "c-pragma.h"
#include "ggc.h"
#include "langhooks.h"
#include "flags.h"
#include "cpphash.h"
#include "genindex.h"
struct c_pch_validity
{
unsigned char debug_info_type;
};
struct c_pch_header
{
unsigned long asm_size;
};
#define IDENT_LENGTH 8
static FILE *pch_outfile;
extern char *asm_file_name;
static off_t asm_file_startpos;
static const char * get_ident PARAMS((void));
static const char *
get_ident()
{
static char result[IDENT_LENGTH];
static const char template[IDENT_LENGTH] = "gpch.011";
memcpy (result, template, IDENT_LENGTH);
if (c_language == clk_c)
result[4] = flag_objc ? 'o' : 'C';
else if (c_language == clk_cplusplus)
result[4] = flag_objc ? 'O' : '+';
else
abort ();
return result;
}
void
pch_init ()
{
FILE *f;
struct c_pch_validity v;
if (! pch_file)
return;
f = fopen (pch_file, "w+b");
if (f == NULL)
fatal_io_error ("can't open %s", pch_file);
pch_outfile = f;
v.debug_info_type = write_symbols;
if (fwrite (get_ident(), IDENT_LENGTH, 1, f) != 1
|| fwrite (&v, sizeof (v), 1, f) != 1)
fatal_io_error ("can't write to %s", pch_file);
if (asm_file_name == NULL
|| strcmp (asm_file_name, "-") == 0)
fatal_error ("`%s' is not a valid output file", asm_file_name);
asm_file_startpos = ftell (asm_out_file);
(*debug_hooks->handle_pch) (0);
cpp_save_state (parse_in, f);
}
void
c_common_write_pch ()
{
char *buf;
off_t asm_file_end;
off_t written;
struct c_pch_header h;
(*debug_hooks->handle_pch) (1);
cpp_write_pch_deps (parse_in, pch_outfile);
asm_file_end = ftello (asm_out_file);
h.asm_size = asm_file_end - asm_file_startpos;
if (fwrite (&h, sizeof (h), 1, pch_outfile) != 1)
fatal_io_error ("can't write %s", pch_file);
buf = xmalloc (16384);
fflush (asm_out_file);
if (fseeko (asm_out_file, asm_file_startpos, SEEK_SET) != 0)
fatal_io_error ("can't seek in %s", asm_file_name);
for (written = asm_file_startpos; written < asm_file_end; )
{
off_t size = asm_file_end - written;
if (size > 16384)
size = 16384;
if (fread (buf, size, 1, asm_out_file) != 1)
fatal_io_error ("can't read %s", asm_file_name);
if (fwrite (buf, size, 1, pch_outfile) != 1)
fatal_io_error ("can't write %s", pch_file);
written += size;
}
free (buf);
gt_pch_save (pch_outfile);
cpp_write_pch_state (parse_in, pch_outfile);
fclose (pch_outfile);
}
int
c_common_valid_pch (pfile, name, fd)
cpp_reader *pfile;
const char *name;
int fd;
{
int sizeread;
int result;
char ident[IDENT_LENGTH];
const char *pch_ident;
struct c_pch_validity v;
if (! allow_pch)
return 2;
sizeread = read (fd, ident, IDENT_LENGTH);
if (sizeread == -1)
{
fatal_io_error ("can't read %s", name);
return 2;
}
else if (sizeread != IDENT_LENGTH)
return 2;
pch_ident = get_ident();
if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
{
if (cpp_get_options (pfile)->warn_invalid_pch
|| cpp_get_options (pfile)->warn_invalid_sr)
{
if (memcmp (ident, pch_ident, 5) == 0)
cpp_error (pfile, DL_WARNING,
"%s: not compatible with this GCC version", name);
else if (memcmp (ident, pch_ident, 4) == 0)
cpp_error (pfile, DL_WARNING, "%s: not for %s", name,
lang_hooks.name);
else
cpp_error (pfile, DL_WARNING, "%s: not a PCH file", name);
}
return 2;
}
if (read (fd, &v, sizeof (v)) != sizeof (v))
{
fatal_io_error ("can't read %s", name);
return 2;
}
if (v.debug_info_type != write_symbols
&& write_symbols != NO_DEBUG)
{
if (cpp_get_options (pfile)->warn_invalid_pch)
cpp_error (pfile, DL_WARNING,
"%s: created with -g%s, but used with -g%s", name,
debug_type_names[v.debug_info_type],
debug_type_names[write_symbols]);
return 2;
}
result = cpp_valid_state (pfile, name, fd);
if (result == -1)
return 2;
else
return result == 0;
}
void
c_common_read_pch (pfile, name, fd, orig_name)
cpp_reader *pfile;
const char *name;
int fd;
const char *orig_name ATTRIBUTE_UNUSED;
{
FILE *f;
struct c_pch_header h;
char *buf;
unsigned long written;
struct save_macro_data *smd;
if (flag_gen_index_original)
flag_gen_index = 0;
if (flag_pch_preprocess
&& flag_preprocess_only)
{
fprintf (pfile->print.outf, "#include_pch \"%s\"\n", name);
pfile->print.line++;
pfile->print.printed = 0;
}
f = fdopen (fd, "rb");
if (f == NULL)
{
cpp_errno (pfile, DL_ERROR, "calling fdopen");
return;
}
allow_pch = 0;
if (fread (&h, sizeof (h), 1, f) != 1)
{
cpp_errno (pfile, DL_ERROR, "reading");
return;
}
buf = xmalloc (16384);
for (written = 0; written < h.asm_size; )
{
off_t size = h.asm_size - written;
if (size > 16384)
size = 16384;
if (fread (buf, size, 1, f) != 1)
cpp_errno (pfile, DL_ERROR, "reading");
else if (!flag_preprocess_only
&& fwrite (buf, size, 1, asm_out_file) != 1)
cpp_errno (pfile, DL_ERROR, "writing");
written += size;
}
free (buf);
cpp_prepare_state (pfile, &smd);
gt_pch_restore (f);
if (cpp_read_state (pfile, name, f, smd) != 0)
return;
fclose (f);
if (flag_gen_index_original)
flag_gen_index = 1;
}