LCOV - code coverage report
Current view: top level - gcc/c-family - c-pch.c (source / functions) Hit Total Coverage
Test: gcc.info Lines: 132 172 76.7 %
Date: 2020-03-28 11:57:23 Functions: 7 8 87.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* Precompiled header implementation for the C languages.
       2                 :            :    Copyright (C) 2000-2020 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 3, 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 COPYING3.  If not see
      18                 :            : <http://www.gnu.org/licenses/>.  */
      19                 :            : 
      20                 :            : #include "config.h"
      21                 :            : #include "system.h"
      22                 :            : #include "coretypes.h"
      23                 :            : #include "target.h"
      24                 :            : #include "c-common.h"
      25                 :            : #include "timevar.h"
      26                 :            : #include "flags.h"
      27                 :            : #include "debug.h"
      28                 :            : #include "c-pragma.h"
      29                 :            : #include "langhooks.h"
      30                 :            : #include "hosthooks.h"
      31                 :            : 
      32                 :            : /* This is a list of flag variables that must match exactly, and their
      33                 :            :    names for the error message.  The possible values for *flag_var must
      34                 :            :    fit in a 'signed char'.  */
      35                 :            : 
      36                 :            : static const struct c_pch_matching
      37                 :            : {
      38                 :            :   int *flag_var;
      39                 :            :   const char *flag_name;
      40                 :            : } pch_matching[] = {
      41                 :            :   { &flag_exceptions, "-fexceptions" },
      42                 :            : };
      43                 :            : 
      44                 :            : enum {
      45                 :            :   MATCH_SIZE = ARRAY_SIZE (pch_matching)
      46                 :            : };
      47                 :            : 
      48                 :            : /* Information about flags and suchlike that affect PCH validity.
      49                 :            : 
      50                 :            :    Before this structure is read, both an initial 8-character identification
      51                 :            :    string, and a 16-byte checksum, have been read and validated.  */
      52                 :            : 
      53                 :            : struct c_pch_validity
      54                 :            : {
      55                 :            :   unsigned char debug_info_type;
      56                 :            :   signed char match[MATCH_SIZE];
      57                 :            :   void (*pch_init) (void);
      58                 :            :   size_t target_data_length;
      59                 :            : };
      60                 :            : 
      61                 :            : #define IDENT_LENGTH 8
      62                 :            : 
      63                 :            : /* The file we'll be writing the PCH to.  */
      64                 :            : static FILE *pch_outfile;
      65                 :            : 
      66                 :            : static const char *get_ident (void);
      67                 :            : 
      68                 :            : /* Compute an appropriate 8-byte magic number for the PCH file, so that
      69                 :            :    utilities like file(1) can identify it, and so that GCC can quickly
      70                 :            :    ignore non-PCH files and PCH files that are of a completely different
      71                 :            :    format.  */
      72                 :            : 
      73                 :            : static const char *
      74                 :      12902 : get_ident (void)
      75                 :            : {
      76                 :      12902 :   static char result[IDENT_LENGTH];
      77                 :      12902 :   static const char templ[] = "gpch.014";
      78                 :      12902 :   static const char c_language_chars[] = "Co+O";
      79                 :            : 
      80                 :      12902 :   memcpy (result, templ, IDENT_LENGTH);
      81                 :      12902 :   result[4] = c_language_chars[c_language];
      82                 :            : 
      83                 :      12902 :   return result;
      84                 :            : }
      85                 :            : 
      86                 :            : /* Whether preprocessor state should be saved by pch_init.  */
      87                 :            : 
      88                 :            : static bool pch_ready_to_save_cpp_state = false;
      89                 :            : 
      90                 :            : /* Prepare to write a PCH file, if one is being written.  This is
      91                 :            :    called at the start of compilation.  */
      92                 :            : 
      93                 :            : void
      94                 :     154908 : pch_init (void)
      95                 :            : {
      96                 :     154908 :   FILE *f;
      97                 :     154908 :   struct c_pch_validity v;
      98                 :     154908 :   void *target_validity;
      99                 :     154908 :   static const char partial_pch[] = "gpcWrite";
     100                 :            : 
     101                 :     154908 :   if (!pch_file)
     102                 :     154562 :     return;
     103                 :            : 
     104                 :        346 :   f = fopen (pch_file, "w+b");
     105                 :        346 :   if (f == NULL)
     106                 :          0 :     fatal_error (input_location, "cannot create precompiled header %s: %m",
     107                 :            :                  pch_file);
     108                 :        346 :   pch_outfile = f;
     109                 :            : 
     110                 :        346 :   memset (&v, '\0', sizeof (v));
     111                 :        346 :   v.debug_info_type = write_symbols;
     112                 :        346 :   {
     113                 :        346 :     size_t i;
     114                 :        692 :     for (i = 0; i < MATCH_SIZE; i++)
     115                 :            :       {
     116                 :        346 :         v.match[i] = *pch_matching[i].flag_var;
     117                 :        346 :         gcc_assert (v.match[i] == *pch_matching[i].flag_var);
     118                 :            :       }
     119                 :            :   }
     120                 :        346 :   v.pch_init = &pch_init;
     121                 :        346 :   target_validity = targetm.get_pch_validity (&v.target_data_length);
     122                 :            : 
     123                 :        346 :   if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
     124                 :        346 :       || fwrite (executable_checksum, 16, 1, f) != 1
     125                 :        346 :       || fwrite (&v, sizeof (v), 1, f) != 1
     126                 :        692 :       || fwrite (target_validity, v.target_data_length, 1, f) != 1)
     127                 :          0 :     fatal_error (input_location, "cannot write to %s: %m", pch_file);
     128                 :            : 
     129                 :            :   /* Let the debugging format deal with the PCHness.  */
     130                 :        346 :   (*debug_hooks->handle_pch) (0);
     131                 :            : 
     132                 :        346 :   if (pch_ready_to_save_cpp_state)
     133                 :          0 :     pch_cpp_save_state ();
     134                 :            : 
     135                 :        346 :   XDELETE (target_validity);
     136                 :            : }
     137                 :            : 
     138                 :            : /* Whether preprocessor state has been saved in a PCH file.  */
     139                 :            : 
     140                 :            : static bool pch_cpp_state_saved = false;
     141                 :            : 
     142                 :            : /* Save preprocessor state in a PCH file, after implicitly included
     143                 :            :    headers have been read.  If the PCH file has not yet been opened,
     144                 :            :    record that state should be saved when it is opened.  */
     145                 :            : 
     146                 :            : void
     147                 :     548669 : pch_cpp_save_state (void)
     148                 :            : {
     149                 :     548669 :   if (!pch_cpp_state_saved)
     150                 :            :     {
     151                 :     547604 :       if (pch_outfile)
     152                 :            :         {
     153                 :        346 :           cpp_save_state (parse_in, pch_outfile);
     154                 :        346 :           pch_cpp_state_saved = true;
     155                 :            :         }
     156                 :            :       else
     157                 :     547258 :         pch_ready_to_save_cpp_state = true;
     158                 :            :     }
     159                 :     548669 : }
     160                 :            : 
     161                 :            : /* Write the PCH file.  This is called at the end of a compilation which
     162                 :            :    will produce a PCH file.  */
     163                 :            : 
     164                 :            : void
     165                 :        346 : c_common_write_pch (void)
     166                 :            : {
     167                 :        346 :   timevar_push (TV_PCH_SAVE);
     168                 :            : 
     169                 :        346 :   targetm.prepare_pch_save ();
     170                 :            : 
     171                 :        346 :   (*debug_hooks->handle_pch) (1);
     172                 :            : 
     173                 :        346 :   prepare_target_option_nodes_for_pch ();
     174                 :            : 
     175                 :        346 :   cpp_write_pch_deps (parse_in, pch_outfile);
     176                 :            : 
     177                 :        346 :   gt_pch_save (pch_outfile);
     178                 :            : 
     179                 :        346 :   timevar_push (TV_PCH_CPP_SAVE);
     180                 :        346 :   cpp_write_pch_state (parse_in, pch_outfile);
     181                 :        346 :   timevar_pop (TV_PCH_CPP_SAVE);
     182                 :            : 
     183                 :        346 :   if (fseek (pch_outfile, 0, SEEK_SET) != 0
     184                 :        346 :       || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
     185                 :          0 :     fatal_error (input_location, "cannot write %s: %m", pch_file);
     186                 :            : 
     187                 :        346 :   fclose (pch_outfile);
     188                 :            : 
     189                 :        346 :   timevar_pop (TV_PCH_SAVE);
     190                 :        346 : }
     191                 :            : 
     192                 :            : /* Check the PCH file called NAME, open on FD, to see if it can be
     193                 :            :    used in this compilation.  Return 1 if valid, 0 if the file can't
     194                 :            :    be used now but might be if it's seen later in the compilation, and
     195                 :            :    2 if this file could never be used in the compilation.  */
     196                 :            : 
     197                 :            : int
     198                 :      12556 : c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
     199                 :            : {
     200                 :      12556 :   int sizeread;
     201                 :      12556 :   int result;
     202                 :      12556 :   char ident[IDENT_LENGTH + 16];
     203                 :      12556 :   const char *pch_ident;
     204                 :      12556 :   struct c_pch_validity v;
     205                 :            : 
     206                 :            :   /* Perform a quick test of whether this is a valid
     207                 :            :      precompiled header for the current language.  */
     208                 :            : 
     209                 :      12556 :   sizeread = read (fd, ident, IDENT_LENGTH + 16);
     210                 :      12556 :   if (sizeread == -1)
     211                 :          0 :     fatal_error (input_location, "cannot read %s: %m", name);
     212                 :      12556 :   else if (sizeread != IDENT_LENGTH + 16)
     213                 :            :     {
     214                 :          0 :       if (cpp_get_options (pfile)->warn_invalid_pch)
     215                 :          0 :         cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file",
     216                 :            :                    name);
     217                 :          0 :       return 2;
     218                 :            :     }
     219                 :            : 
     220                 :      12556 :   pch_ident = get_ident();
     221                 :      12556 :   if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
     222                 :            :     {
     223                 :          0 :       if (cpp_get_options (pfile)->warn_invalid_pch)
     224                 :            :         {
     225                 :          0 :           if (memcmp (ident, pch_ident, 5) == 0)
     226                 :            :             /* It's a PCH, for the right language, but has the wrong version.
     227                 :            :              */
     228                 :          0 :             cpp_error (pfile, CPP_DL_WARNING,
     229                 :            :                        "%s: not compatible with this GCC version", name);
     230                 :          0 :           else if (memcmp (ident, pch_ident, 4) == 0)
     231                 :            :             /* It's a PCH for the wrong language.  */
     232                 :          0 :             cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
     233                 :            :                        lang_hooks.name);
     234                 :            :           else
     235                 :            :             /* Not any kind of PCH.  */
     236                 :          0 :             cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
     237                 :            :         }
     238                 :          0 :       return 2;
     239                 :            :     }
     240                 :      12556 :   if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
     241                 :            :     {
     242                 :          0 :       if (cpp_get_options (pfile)->warn_invalid_pch)
     243                 :          0 :         cpp_error (pfile, CPP_DL_WARNING,
     244                 :            :                    "%s: created by a different GCC executable", name);
     245                 :          0 :       return 2;
     246                 :            :     }
     247                 :            : 
     248                 :            :   /* At this point, we know it's a PCH file created by this
     249                 :            :      executable, so it ought to be long enough that we can read a
     250                 :            :      c_pch_validity structure.  */
     251                 :      12556 :   if (read (fd, &v, sizeof (v)) != sizeof (v))
     252                 :          0 :     fatal_error (input_location, "cannot read %s: %m", name);
     253                 :            : 
     254                 :            :   /* The allowable debug info combinations are that either the PCH file
     255                 :            :      was built with the same as is being used now, or the PCH file was
     256                 :            :      built for some kind of debug info but now none is in use.  */
     257                 :      12556 :   if (v.debug_info_type != write_symbols
     258                 :         12 :       && write_symbols != NO_DEBUG)
     259                 :            :     {
     260                 :          6 :       if (cpp_get_options (pfile)->warn_invalid_pch)
     261                 :          6 :         cpp_error (pfile, CPP_DL_WARNING,
     262                 :            :                    "%s: created with -g%s, but used with -g%s", name,
     263                 :          6 :                    debug_type_names[v.debug_info_type],
     264                 :          6 :                    debug_type_names[write_symbols]);
     265                 :          6 :       return 2;
     266                 :            :     }
     267                 :            : 
     268                 :            :   /* Check flags that must match exactly.  */
     269                 :            :   {
     270                 :            :     size_t i;
     271                 :      25089 :     for (i = 0; i < MATCH_SIZE; i++)
     272                 :      12550 :       if (*pch_matching[i].flag_var != v.match[i])
     273                 :            :         {
     274                 :         11 :           if (cpp_get_options (pfile)->warn_invalid_pch)
     275                 :          6 :             cpp_error (pfile, CPP_DL_WARNING,
     276                 :            :                        "%s: settings for %s do not match", name,
     277                 :            :                        pch_matching[i].flag_name);
     278                 :         11 :           return 2;
     279                 :            :         }
     280                 :            :   }
     281                 :            : 
     282                 :            :   /* If the text segment was not loaded at the same address as it was
     283                 :            :      when the PCH file was created, function pointers loaded from the
     284                 :            :      PCH will not be valid.  We could in theory remap all the function
     285                 :            :      pointers, but no support for that exists at present.
     286                 :            :      Since we have the same executable, it should only be necessary to
     287                 :            :      check one function.  */
     288                 :      12539 :   if (v.pch_init != &pch_init)
     289                 :            :     {
     290                 :          0 :       if (cpp_get_options (pfile)->warn_invalid_pch)
     291                 :          0 :         cpp_error (pfile, CPP_DL_WARNING,
     292                 :            :                    "%s: had text segment at different address", name);
     293                 :          0 :       return 2;
     294                 :            :     }
     295                 :            : 
     296                 :            :   /* Check the target-specific validity data.  */
     297                 :      12539 :   {
     298                 :      12539 :     void *this_file_data = xmalloc (v.target_data_length);
     299                 :      12539 :     const char *msg;
     300                 :            : 
     301                 :      12539 :     if ((size_t) read (fd, this_file_data, v.target_data_length)
     302                 :      12539 :         != v.target_data_length)
     303                 :          0 :       fatal_error (input_location, "cannot read %s: %m", name);
     304                 :      12539 :     msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
     305                 :      12539 :     free (this_file_data);
     306                 :      12539 :     if (msg != NULL)
     307                 :            :       {
     308                 :          0 :         if (cpp_get_options (pfile)->warn_invalid_pch)
     309                 :          0 :           cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
     310                 :          0 :         return 2;
     311                 :            :       }
     312                 :            :   }
     313                 :            : 
     314                 :            :   /* Check the preprocessor macros are the same as when the PCH was
     315                 :            :      generated.  */
     316                 :            : 
     317                 :      12539 :   result = cpp_valid_state (pfile, name, fd);
     318                 :      12539 :   if (result == -1)
     319                 :            :     return 2;
     320                 :            :   else
     321                 :      12539 :     return result == 0;
     322                 :            : }
     323                 :            : 
     324                 :            : /* If non-NULL, this function is called after a precompile header file
     325                 :            :    is loaded.  */
     326                 :            : void (*lang_post_pch_load) (void);
     327                 :            : 
     328                 :            : /* Load in the PCH file NAME, open on FD.  It was originally searched for
     329                 :            :    by ORIG_NAME.  */
     330                 :            : 
     331                 :            : void
     332                 :       6399 : c_common_read_pch (cpp_reader *pfile, const char *name,
     333                 :            :                    int fd, const char *orig_name ATTRIBUTE_UNUSED)
     334                 :            : {
     335                 :       6399 :   FILE *f;
     336                 :       6399 :   struct save_macro_data *smd;
     337                 :       6399 :   expanded_location saved_loc;
     338                 :       6399 :   bool saved_trace_includes;
     339                 :            : 
     340                 :       6399 :   timevar_push (TV_PCH_RESTORE);
     341                 :            : 
     342                 :       6399 :   f = fdopen (fd, "rb");
     343                 :       6399 :   if (f == NULL)
     344                 :            :     {
     345                 :          0 :       cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
     346                 :          0 :       close (fd);
     347                 :          0 :       goto end;
     348                 :            :     }
     349                 :            : 
     350                 :       6399 :   cpp_get_callbacks (parse_in)->valid_pch = NULL;
     351                 :            : 
     352                 :            :   /* Save the location and then restore it after reading the PCH.  */
     353                 :       6399 :   saved_loc = expand_location (line_table->highest_line);
     354                 :       6399 :   saved_trace_includes = line_table->trace_includes;
     355                 :            : 
     356                 :       6399 :   timevar_push (TV_PCH_CPP_RESTORE);
     357                 :       6399 :   cpp_prepare_state (pfile, &smd);
     358                 :       6399 :   timevar_pop (TV_PCH_CPP_RESTORE);
     359                 :            : 
     360                 :       6399 :   gt_pch_restore (f);
     361                 :       6399 :   cpp_set_line_map (pfile, line_table);
     362                 :       6399 :   rebuild_location_adhoc_htab (line_table);
     363                 :            : 
     364                 :       6399 :   timevar_push (TV_PCH_CPP_RESTORE);
     365                 :       6399 :   if (cpp_read_state (pfile, name, f, smd) != 0)
     366                 :            :     {
     367                 :          0 :       fclose (f);
     368                 :          0 :       timevar_pop (TV_PCH_CPP_RESTORE);
     369                 :          0 :       goto end;
     370                 :            :     }
     371                 :       6399 :   timevar_pop (TV_PCH_CPP_RESTORE);
     372                 :            : 
     373                 :            : 
     374                 :       6399 :   fclose (f);
     375                 :            : 
     376                 :       6399 :   line_table->trace_includes = saved_trace_includes;
     377                 :       6399 :   linemap_add (line_table, LC_ENTER, 0, saved_loc.file, saved_loc.line);
     378                 :            : 
     379                 :            :   /* Give the front end a chance to take action after a PCH file has
     380                 :            :      been loaded.  */
     381                 :       6399 :   if (lang_post_pch_load)
     382                 :          0 :     (*lang_post_pch_load) ();
     383                 :            : 
     384                 :       6399 : end:
     385                 :       6399 :   timevar_pop (TV_PCH_RESTORE);
     386                 :       6399 : }
     387                 :            : 
     388                 :            : /* Indicate that no more PCH files should be read.  */
     389                 :            : 
     390                 :            : void
     391                 :     236112 : c_common_no_more_pch (void)
     392                 :            : {
     393                 :     236112 :   if (cpp_get_callbacks (parse_in)->valid_pch)
     394                 :            :     {
     395                 :     148498 :       cpp_get_callbacks (parse_in)->valid_pch = NULL;
     396                 :     148498 :       host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
     397                 :            :     }
     398                 :     236112 : }
     399                 :            : 
     400                 :            : /* Handle #pragma GCC pch_preprocess, to load in the PCH file.  */
     401                 :            : 
     402                 :            : void
     403                 :          9 : c_common_pch_pragma (cpp_reader *pfile, const char *name)
     404                 :            : {
     405                 :          9 :   int fd;
     406                 :            : 
     407                 :          9 :   if (!cpp_get_options (pfile)->preprocessed)
     408                 :            :     {
     409                 :          0 :       error ("%<pch_preprocess%> pragma should only be used "
     410                 :            :              "with %<-fpreprocessed%>");
     411                 :          0 :       inform (input_location, "use %<#include%> instead");
     412                 :          0 :       return;
     413                 :            :     }
     414                 :            : 
     415                 :          9 :   fd = open (name, O_RDONLY | O_BINARY, 0666);
     416                 :          9 :   if (fd == -1)
     417                 :          0 :     fatal_error (input_location, "%s: couldn%'t open PCH file: %m", name);
     418                 :            : 
     419                 :          9 :   if (c_common_valid_pch (pfile, name, fd) != 1)
     420                 :            :     {
     421                 :          0 :       if (!cpp_get_options (pfile)->warn_invalid_pch)
     422                 :          0 :         inform (input_location, "use %<-Winvalid-pch%> for more information");
     423                 :          0 :       fatal_error (input_location, "%s: PCH file was invalid", name);
     424                 :            :     }
     425                 :            : 
     426                 :          9 :   c_common_read_pch (pfile, name, fd, name);
     427                 :            : 
     428                 :          9 :   close (fd);
     429                 :            : }
     430                 :            : 

Generated by: LCOV version 1.0

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto --enable-host-shared. GCC test suite is run with the built compiler.