LCOV - code coverage report
Current view: top level - gcc/fortran - cpp.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 55.6 % 563 313
Test Date: 2025-04-19 15:48:17 Functions: 56.8 % 37 21
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Copyright (C) 2008-2025 Free Software Foundation, Inc.
       2                 :             : 
       3                 :             : This file is part of GCC.
       4                 :             : 
       5                 :             : GCC is free software; you can redistribute it and/or modify it under
       6                 :             : the terms of the GNU General Public License as published by the Free
       7                 :             : Software Foundation; either version 3, or (at your option) any later
       8                 :             : version.
       9                 :             : 
      10                 :             : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11                 :             : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12                 :             : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13                 :             : for more details.
      14                 :             : 
      15                 :             : You should have received a copy of the GNU General Public License
      16                 :             : along with GCC; see the file COPYING3.  If not see
      17                 :             : <http://www.gnu.org/licenses/>.  */
      18                 :             : 
      19                 :             : #include "config.h"
      20                 :             : #include "system.h"
      21                 :             : #include "coretypes.h"
      22                 :             : 
      23                 :             : #define GCC_C_COMMON_C
      24                 :             : #include "options.h"  /* For cpp_reason_option_codes. */
      25                 :             : #undef GCC_C_COMMON_C
      26                 :             : 
      27                 :             : #include "target.h"
      28                 :             : #include "gfortran.h"
      29                 :             : #include "diagnostic.h"
      30                 :             : 
      31                 :             : #include "toplev.h"
      32                 :             : 
      33                 :             : #include "../../libcpp/internal.h"
      34                 :             : #include "cpp.h"
      35                 :             : #include "incpath.h"
      36                 :             : #include "cppbuiltin.h"
      37                 :             : #include "mkdeps.h"
      38                 :             : 
      39                 :             : #ifndef TARGET_SYSTEM_ROOT
      40                 :             : # define TARGET_SYSTEM_ROOT NULL
      41                 :             : #endif
      42                 :             : 
      43                 :             : #ifndef TARGET_CPU_CPP_BUILTINS
      44                 :             : # define TARGET_CPU_CPP_BUILTINS()
      45                 :             : #endif
      46                 :             : 
      47                 :             : #ifndef TARGET_OS_CPP_BUILTINS
      48                 :             : # define TARGET_OS_CPP_BUILTINS()
      49                 :             : #endif
      50                 :             : 
      51                 :             : #ifndef TARGET_OBJFMT_CPP_BUILTINS
      52                 :             : # define TARGET_OBJFMT_CPP_BUILTINS()
      53                 :             : #endif
      54                 :             : 
      55                 :             : 
      56                 :             : /* Holds switches parsed by gfc_cpp_handle_option (), but whose
      57                 :             :    handling is deferred to gfc_cpp_init ().  */
      58                 :             : typedef struct
      59                 :             : {
      60                 :             :     enum opt_code code;
      61                 :             :     const char *arg;
      62                 :             : }
      63                 :             : gfc_cpp_deferred_opt_t;
      64                 :             : 
      65                 :             : 
      66                 :             : /* Defined and undefined macros being queued for output with -dU at
      67                 :             :    the next newline.  */
      68                 :             : typedef struct gfc_cpp_macro_queue
      69                 :             : {
      70                 :             :   struct gfc_cpp_macro_queue *next;     /* Next macro in the list.  */
      71                 :             :   char *macro;                          /* The name of the macro if not
      72                 :             :                                            defined, the full definition if
      73                 :             :                                            defined.  */
      74                 :             : } gfc_cpp_macro_queue;
      75                 :             : static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue;
      76                 :             : 
      77                 :             : struct gfc_cpp_option_data
      78                 :             : {
      79                 :             :   /* Argument of -cpp, implied by SPEC;
      80                 :             :      if NULL, preprocessing disabled.  */
      81                 :             :   const char *temporary_filename;
      82                 :             : 
      83                 :             :   const char *output_filename;          /* -o <arg>  */
      84                 :             :   int preprocess_only;                  /* -E  */
      85                 :             :   int discard_comments;                 /* -C  */
      86                 :             :   int discard_comments_in_macro_exp;    /* -CC  */
      87                 :             :   int print_include_names;              /* -H  */
      88                 :             :   int no_line_commands;                 /* -P  */
      89                 :             :   char dump_macros;                     /* -d[DMNU]  */
      90                 :             :   int dump_includes;                    /* -dI  */
      91                 :             :   int working_directory;                /* -fworking-directory  */
      92                 :             :   int no_predefined;                    /* -undef */
      93                 :             :   int standard_include_paths;           /* -nostdinc */
      94                 :             :   int verbose;                          /* -v */
      95                 :             :   int deps;                             /* -M */
      96                 :             :   int deps_skip_system;                 /* -MM */
      97                 :             :   const char *deps_filename;            /* -M[M]D */
      98                 :             :   const char *deps_filename_user;       /* -MF <arg> */
      99                 :             :   const char *deps_target_filename;     /* -MT / -MQ <arg> */
     100                 :             :   bool quote_deps_target_filename;      /* -MQ */
     101                 :             :   int deps_missing_are_generated;       /* -MG */
     102                 :             :   int deps_phony;                       /* -MP */
     103                 :             :   int warn_date_time;                   /* -Wdate-time */
     104                 :             : 
     105                 :             :   const char *multilib;                 /* -imultilib <dir>  */
     106                 :             :   const char *prefix;                   /* -iprefix <dir>  */
     107                 :             :   const char *sysroot;                  /* -isysroot <dir>  */
     108                 :             : 
     109                 :             :   /* Options whose handling needs to be deferred until the
     110                 :             :      appropriate cpp-objects are created:
     111                 :             :       -A predicate=answer
     112                 :             :       -D <macro>[=<val>]
     113                 :             :       -U <macro>  */
     114                 :             :   gfc_cpp_deferred_opt_t *deferred_opt;
     115                 :             :   int deferred_opt_count;
     116                 :             : }
     117                 :             : gfc_cpp_option;
     118                 :             : 
     119                 :             : /* Structures used with libcpp:  */
     120                 :             : static cpp_options *cpp_option = NULL;
     121                 :             : static cpp_reader *cpp_in = NULL;
     122                 :             : 
     123                 :             : /* Encapsulates state used to convert a stream of cpp-tokens into
     124                 :             :    a text file.  */
     125                 :             : static struct
     126                 :             : {
     127                 :             :   FILE *outf;                   /* Stream to write to.  */
     128                 :             :   const cpp_token *prev;        /* Previous token.  */
     129                 :             :   const cpp_token *source;      /* Source token for spacing.  */
     130                 :             :   int src_line;                 /* Line number currently being written.  */
     131                 :             :   unsigned char printed;        /* Nonzero if something output at line.  */
     132                 :             :   bool first_time;              /* cb_file_change hasn't been called yet.  */
     133                 :             : } print;
     134                 :             : 
     135                 :             : /* General output routines.  */
     136                 :             : static void scan_translation_unit (cpp_reader *);
     137                 :             : static void scan_translation_unit_trad (cpp_reader *);
     138                 :             : 
     139                 :             : /* Callback routines for the parser. Most of these are active only
     140                 :             :    in specific modes.  */
     141                 :             : static void cb_file_change (cpp_reader *, const line_map_ordinary *);
     142                 :             : static void cb_line_change (cpp_reader *, const cpp_token *, int);
     143                 :             : static void cb_define (cpp_reader *, location_t, cpp_hashnode *);
     144                 :             : static void cb_undef (cpp_reader *, location_t, cpp_hashnode *);
     145                 :             : static void cb_def_pragma (cpp_reader *, location_t);
     146                 :             : static void cb_include (cpp_reader *, location_t, const unsigned char *,
     147                 :             :                         const char *, int, const cpp_token **);
     148                 :             : static void cb_ident (cpp_reader *, location_t, const cpp_string *);
     149                 :             : static void cb_used_define (cpp_reader *, location_t, cpp_hashnode *);
     150                 :             : static void cb_used_undef (cpp_reader *, location_t, cpp_hashnode *);
     151                 :             : static bool cb_cpp_diagnostic (cpp_reader *, enum cpp_diagnostic_level,
     152                 :             :                                enum cpp_warning_reason, rich_location *,
     153                 :             :                                const char *, va_list *)
     154                 :             :      ATTRIBUTE_GCC_DIAG(5,0);
     155                 :             : void pp_dir_change (cpp_reader *, const char *);
     156                 :             : 
     157                 :             : static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
     158                 :             : static void dump_queued_macros (cpp_reader *);
     159                 :             : 
     160                 :             : 
     161                 :             : static void
     162                 :        1142 : cpp_define_builtins (cpp_reader *pfile)
     163                 :             : {
     164                 :             :   /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted'
     165                 :             :      in C, defines __STDC_HOSTED__?!  */
     166                 :        1142 :   cpp_init_builtins (pfile, 0);
     167                 :             : 
     168                 :             :   /* Initialize GFORTRAN specific builtins.
     169                 :             :      These are documented.  */
     170                 :        1142 :   define_language_independent_builtin_macros (pfile);
     171                 :        1142 :   cpp_define (pfile, "__GFORTRAN__=1");
     172                 :        1142 :   cpp_define (pfile, "_LANGUAGE_FORTRAN=1");
     173                 :             : 
     174                 :        1142 :   if (flag_openacc)
     175                 :         121 :     cpp_define (pfile, "_OPENACC=201711");
     176                 :             : 
     177                 :        1142 :   if (flag_openmp)
     178                 :          60 :     cpp_define (pfile, "_OPENMP=201511");
     179                 :             : 
     180                 :             :   /* The defines below are necessary for the TARGET_* macros.
     181                 :             : 
     182                 :             :      FIXME:  Note that builtin_define_std() actually is a function
     183                 :             :      in c-cppbuiltin.cc which uses flags undefined for Fortran.
     184                 :             :      Let's skip this for now. If needed, one needs to look into it
     185                 :             :      once more.  */
     186                 :             : 
     187                 :             : # define builtin_define(TXT) cpp_define (pfile, TXT)
     188                 :             : # define builtin_define_std(TXT)
     189                 :             : # define builtin_assert(TXT) cpp_assert (pfile, TXT)
     190                 :             : 
     191                 :             :   /* FIXME: Pandora's Box
     192                 :             :     Using the macros below results in multiple breakages:
     193                 :             :      - mingw will fail to compile this file as dependent macros
     194                 :             :        assume to be used in c-cppbuiltin.cc only. Further, they use
     195                 :             :        flags only valid/defined in C (same as noted above).
     196                 :             :        [config/i386/mingw32.h, config/i386/cygming.h]
     197                 :             :      - other platforms (not as popular) break similarly
     198                 :             :        [grep for 'builtin_define_with_int_value' in gcc/config/]
     199                 :             : 
     200                 :             :   TARGET_CPU_CPP_BUILTINS ();
     201                 :             :   TARGET_OS_CPP_BUILTINS ();
     202                 :             :   TARGET_OBJFMT_CPP_BUILTINS (); */
     203                 :             : 
     204                 :             : #undef builtin_define
     205                 :             : #undef builtin_define_std
     206                 :             : #undef builtin_assert
     207                 :        1142 : }
     208                 :             : 
     209                 :             : bool
     210                 :      214242 : gfc_cpp_enabled (void)
     211                 :             : {
     212                 :      214242 :   return gfc_cpp_option.temporary_filename != NULL;
     213                 :             : }
     214                 :             : 
     215                 :             : bool
     216                 :       64327 : gfc_cpp_preprocess_only (void)
     217                 :             : {
     218                 :       64327 :   return gfc_cpp_option.preprocess_only;
     219                 :             : }
     220                 :             : 
     221                 :             : bool
     222                 :       84268 : gfc_cpp_makedep (void)
     223                 :             : {
     224                 :       84268 :   return gfc_cpp_option.deps;
     225                 :             : }
     226                 :             : 
     227                 :             : void
     228                 :           0 : gfc_cpp_add_dep (const char *name, bool system)
     229                 :             : {
     230                 :           0 :   if (!gfc_cpp_option.deps_skip_system || !system)
     231                 :           0 :     if (mkdeps *deps = cpp_get_deps (cpp_in))
     232                 :           0 :       deps_add_dep (deps, name);
     233                 :           0 : }
     234                 :             : 
     235                 :             : void
     236                 :           0 : gfc_cpp_add_target (const char *name)
     237                 :             : {
     238                 :           0 :   if (mkdeps *deps = cpp_get_deps (cpp_in))
     239                 :           0 :     deps_add_target (deps, name, 0);
     240                 :           0 : }
     241                 :             : 
     242                 :             : 
     243                 :             : const char *
     244                 :        1129 : gfc_cpp_temporary_file (void)
     245                 :             : {
     246                 :        1129 :   return gfc_cpp_option.temporary_filename;
     247                 :             : }
     248                 :             : 
     249                 :             : static void
     250                 :        1142 : gfc_cpp_register_include_paths (bool verbose_missing_dir_warn)
     251                 :             : {
     252                 :        1142 :   int cxx_stdinc = 0;
     253                 :        2284 :   cpp_get_options (cpp_in)->warn_missing_include_dirs
     254                 :        2284 :     = (global_options.x_cpp_warn_missing_include_dirs
     255                 :        1142 :        && verbose_missing_dir_warn);
     256                 :        1142 :   register_include_chains (cpp_in, gfc_cpp_option.sysroot,
     257                 :             :                            gfc_cpp_option.prefix, gfc_cpp_option.multilib,
     258                 :             :                            gfc_cpp_option.standard_include_paths, cxx_stdinc,
     259                 :             :                            gfc_cpp_option.verbose);
     260                 :        1142 : }
     261                 :             : 
     262                 :             : void
     263                 :       30445 : gfc_cpp_init_options (unsigned int decoded_options_count,
     264                 :             :                       struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED)
     265                 :             : {
     266                 :             :   /* Do not create any objects from libcpp here. If no
     267                 :             :      preprocessing is requested, this would be wasted
     268                 :             :      time and effort.
     269                 :             : 
     270                 :             :      See gfc_cpp_post_options() instead.  */
     271                 :             : 
     272                 :       30445 :   gfc_cpp_option.temporary_filename = NULL;
     273                 :       30445 :   gfc_cpp_option.output_filename = NULL;
     274                 :       30445 :   gfc_cpp_option.preprocess_only = 0;
     275                 :       30445 :   gfc_cpp_option.discard_comments = 1;
     276                 :       30445 :   gfc_cpp_option.discard_comments_in_macro_exp = 1;
     277                 :       30445 :   gfc_cpp_option.print_include_names = 0;
     278                 :       30445 :   gfc_cpp_option.no_line_commands = 0;
     279                 :       30445 :   gfc_cpp_option.dump_macros = '\0';
     280                 :       30445 :   gfc_cpp_option.dump_includes = 0;
     281                 :       30445 :   gfc_cpp_option.working_directory = -1;
     282                 :       30445 :   gfc_cpp_option.no_predefined = 0;
     283                 :       30445 :   gfc_cpp_option.standard_include_paths = 1;
     284                 :       30445 :   gfc_cpp_option.verbose = 0;
     285                 :       30445 :   gfc_cpp_option.warn_date_time = 0;
     286                 :       30445 :   gfc_cpp_option.deps = 0;
     287                 :       30445 :   gfc_cpp_option.deps_skip_system = 0;
     288                 :       30445 :   gfc_cpp_option.deps_phony = 0;
     289                 :       30445 :   gfc_cpp_option.deps_missing_are_generated = 0;
     290                 :       30445 :   gfc_cpp_option.deps_filename = NULL;
     291                 :       30445 :   gfc_cpp_option.deps_filename_user = NULL;
     292                 :       30445 :   gfc_cpp_option.deps_target_filename = NULL;
     293                 :       30445 :   gfc_cpp_option.quote_deps_target_filename = false;
     294                 :             : 
     295                 :       30445 :   gfc_cpp_option.multilib = NULL;
     296                 :       30445 :   gfc_cpp_option.prefix = NULL;
     297                 :       30445 :   gfc_cpp_option.sysroot = TARGET_SYSTEM_ROOT;
     298                 :             : 
     299                 :       30445 :   gfc_cpp_option.deferred_opt = XNEWVEC (gfc_cpp_deferred_opt_t,
     300                 :             :                                          decoded_options_count);
     301                 :       30445 :   gfc_cpp_option.deferred_opt_count = 0;
     302                 :       30445 : }
     303                 :             : 
     304                 :             : bool
     305                 :      204633 : gfc_cpp_handle_option (size_t scode, const char *arg, int value ATTRIBUTE_UNUSED)
     306                 :             : {
     307                 :      204633 :   bool result = true;
     308                 :      204633 :   enum opt_code code = (enum opt_code) scode;
     309                 :             : 
     310                 :      204633 :   switch (code)
     311                 :             :   {
     312                 :             :     default:
     313                 :             :       result = false;
     314                 :             :       break;
     315                 :             : 
     316                 :        1142 :     case OPT_cpp_:
     317                 :        1142 :       gfc_cpp_option.temporary_filename = arg;
     318                 :        1142 :       break;
     319                 :             : 
     320                 :           0 :     case OPT_nocpp:
     321                 :           0 :       gfc_cpp_option.temporary_filename = 0L;
     322                 :           0 :       break;
     323                 :             : 
     324                 :             :     case OPT_d:
     325                 :          14 :       for ( ; *arg; ++arg)
     326                 :           7 :         switch (*arg)
     327                 :             :         {
     328                 :           0 :           case 'D':
     329                 :           0 :           case 'M':
     330                 :           0 :           case 'N':
     331                 :           0 :           case 'U':
     332                 :           0 :             gfc_cpp_option.dump_macros = *arg;
     333                 :           0 :             break;
     334                 :             : 
     335                 :           0 :           case 'I':
     336                 :           0 :             gfc_cpp_option.dump_includes = 1;
     337                 :           0 :             break;
     338                 :             :         }
     339                 :             :       break;
     340                 :             : 
     341                 :           0 :     case OPT_fworking_directory:
     342                 :           0 :       gfc_cpp_option.working_directory = value;
     343                 :           0 :       break;
     344                 :             : 
     345                 :           3 :     case OPT_idirafter:
     346                 :           3 :       gfc_cpp_add_include_path_after (xstrdup(arg), true);
     347                 :           3 :       break;
     348                 :             : 
     349                 :         347 :     case OPT_imultilib:
     350                 :         347 :       gfc_cpp_option.multilib = arg;
     351                 :         347 :       break;
     352                 :             : 
     353                 :        1142 :     case OPT_iprefix:
     354                 :        1142 :       gfc_cpp_option.prefix = arg;
     355                 :        1142 :       break;
     356                 :             : 
     357                 :           0 :     case OPT_isysroot:
     358                 :           0 :       gfc_cpp_option.sysroot = arg;
     359                 :           0 :       break;
     360                 :             : 
     361                 :        4875 :     case OPT_iquote:
     362                 :        4875 :     case OPT_isystem:
     363                 :        4875 :       gfc_cpp_add_include_path (xstrdup(arg), true);
     364                 :        4875 :       break;
     365                 :             : 
     366                 :           0 :     case OPT_nostdinc:
     367                 :           0 :       gfc_cpp_option.standard_include_paths = value;
     368                 :           0 :       break;
     369                 :             : 
     370                 :       30445 :     case OPT_o:
     371                 :       30445 :       if (!gfc_cpp_option.output_filename)
     372                 :       30445 :         gfc_cpp_option.output_filename = arg;
     373                 :             :       else
     374                 :           0 :         gfc_fatal_error ("output filename specified twice");
     375                 :       30445 :       break;
     376                 :             : 
     377                 :           0 :     case OPT_undef:
     378                 :           0 :       gfc_cpp_option.no_predefined = value;
     379                 :           0 :       break;
     380                 :             : 
     381                 :           0 :     case OPT_v:
     382                 :           0 :       gfc_cpp_option.verbose = value;
     383                 :           0 :       break;
     384                 :             : 
     385                 :           1 :     case OPT_Wdate_time:
     386                 :           1 :       gfc_cpp_option.warn_date_time = value;
     387                 :           1 :       break;
     388                 :             : 
     389                 :        1139 :     case OPT_A:
     390                 :        1139 :     case OPT_D:
     391                 :        1139 :     case OPT_U:
     392                 :        1139 :       gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
     393                 :        1139 :       gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
     394                 :        1139 :       gfc_cpp_option.deferred_opt_count++;
     395                 :        1139 :       break;
     396                 :             : 
     397                 :           0 :     case OPT_C:
     398                 :           0 :       gfc_cpp_option.discard_comments = 0;
     399                 :           0 :       break;
     400                 :             : 
     401                 :           0 :     case OPT_CC:
     402                 :           0 :       gfc_cpp_option.discard_comments = 0;
     403                 :           0 :       gfc_cpp_option.discard_comments_in_macro_exp = 0;
     404                 :           0 :       break;
     405                 :             : 
     406                 :          13 :     case OPT_E:
     407                 :          13 :       gfc_cpp_option.preprocess_only = 1;
     408                 :          13 :       break;
     409                 :             : 
     410                 :           0 :     case OPT_H:
     411                 :           0 :       gfc_cpp_option.print_include_names = 1;
     412                 :           0 :       break;
     413                 :             : 
     414                 :           0 :     case OPT_MM:
     415                 :           0 :       gfc_cpp_option.deps_skip_system = 1;
     416                 :             :       /* fall through */
     417                 :             : 
     418                 :           1 :     case OPT_M:
     419                 :           1 :       gfc_cpp_option.deps = 1;
     420                 :           1 :       break;
     421                 :             : 
     422                 :           0 :     case OPT_MMD:
     423                 :           0 :       gfc_cpp_option.deps_skip_system = 1;
     424                 :             :       /* fall through */
     425                 :             : 
     426                 :           0 :     case OPT_MD:
     427                 :           0 :       gfc_cpp_option.deps = 1;
     428                 :           0 :       gfc_cpp_option.deps_filename = arg;
     429                 :           0 :       break;
     430                 :             : 
     431                 :           1 :     case OPT_MF:
     432                 :             :       /* If specified multiple times, last one wins.  */
     433                 :           1 :       gfc_cpp_option.deps_filename_user = arg;
     434                 :           1 :       break;
     435                 :             : 
     436                 :           0 :     case OPT_MG:
     437                 :           0 :       gfc_cpp_option.deps_missing_are_generated = 1;
     438                 :           0 :       break;
     439                 :             : 
     440                 :           0 :     case OPT_MP:
     441                 :           0 :       gfc_cpp_option.deps_phony = 1;
     442                 :           0 :       break;
     443                 :             : 
     444                 :           1 :     case OPT_MQ:
     445                 :           1 :     case OPT_MT:
     446                 :           1 :       gfc_cpp_option.quote_deps_target_filename = (code == OPT_MQ);
     447                 :           1 :       gfc_cpp_option.deps_target_filename = arg;
     448                 :           1 :       break;
     449                 :             : 
     450                 :           0 :     case OPT_P:
     451                 :           0 :       gfc_cpp_option.no_line_commands = 1;
     452                 :           0 :       break;
     453                 :             :   }
     454                 :             : 
     455                 :      204633 :   return result;
     456                 :             : }
     457                 :             : 
     458                 :             : /* This function needs to be called before gfc_cpp_register_include_paths
     459                 :             :    as the latter may diagnose missing include directories.  */
     460                 :             : static void
     461                 :        1142 : gfc_cpp_init_cb (void)
     462                 :             : {
     463                 :        1142 :   struct cpp_callbacks *cb;
     464                 :             : 
     465                 :        1142 :   cb = cpp_get_callbacks (cpp_in);
     466                 :        1142 :   cb->file_change = cb_file_change;
     467                 :        1142 :   cb->line_change = cb_line_change;
     468                 :        1142 :   cb->ident = cb_ident;
     469                 :        1142 :   cb->def_pragma = cb_def_pragma;
     470                 :        1142 :   cb->diagnostic = cb_cpp_diagnostic;
     471                 :             : 
     472                 :        1142 :   if (gfc_cpp_option.dump_includes)
     473                 :           0 :     cb->include = cb_include;
     474                 :             : 
     475                 :        1142 :   if ((gfc_cpp_option.dump_macros == 'D')
     476                 :        1142 :       || (gfc_cpp_option.dump_macros == 'N'))
     477                 :             :     {
     478                 :           0 :       cb->define = cb_define;
     479                 :           0 :       cb->undef  = cb_undef;
     480                 :             :     }
     481                 :             : 
     482                 :        1142 :   if (gfc_cpp_option.dump_macros == 'U')
     483                 :             :     {
     484                 :           0 :       cb->before_define = dump_queued_macros;
     485                 :           0 :       cb->used_define = cb_used_define;
     486                 :           0 :       cb->used_undef = cb_used_undef;
     487                 :             :     }
     488                 :        1142 : }
     489                 :             : 
     490                 :             : void
     491                 :       30444 : gfc_cpp_post_options (bool verbose_missing_dir_warn)
     492                 :             : {
     493                 :             :   /* Any preprocessing-related option without '-cpp' is considered
     494                 :             :      an error.  */
     495                 :       30444 :   if (!gfc_cpp_enabled ()
     496                 :       30444 :       && (gfc_cpp_preprocess_only ()
     497                 :       29302 :           || gfc_cpp_makedep ()
     498                 :       29302 :           || !gfc_cpp_option.discard_comments
     499                 :       29302 :           || !gfc_cpp_option.discard_comments_in_macro_exp
     500                 :       29302 :           || gfc_cpp_option.print_include_names
     501                 :             :           || gfc_cpp_option.no_line_commands
     502                 :       29302 :           || gfc_cpp_option.dump_macros
     503                 :       29302 :           || gfc_cpp_option.dump_includes))
     504                 :           0 :     gfc_fatal_error ("To enable preprocessing, use %<-cpp%>");
     505                 :             : 
     506                 :       30444 :   if (!gfc_cpp_enabled ())
     507                 :             :     return;
     508                 :             : 
     509                 :        1142 :   cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
     510                 :        1142 :   gcc_assert (cpp_in);
     511                 :             : 
     512                 :             :   /* The cpp_options-structure defines far more flags than those set here.
     513                 :             :      If any other is implemented, see c-opt.c (sanitize_cpp_opts) for
     514                 :             :      inter-option dependencies that may need to be enforced.  */
     515                 :        1142 :   cpp_option = cpp_get_options (cpp_in);
     516                 :        1142 :   gcc_assert (cpp_option);
     517                 :             : 
     518                 :             :   /* TODO: allow non-traditional modes, e.g. by -cpp-std=...?  */
     519                 :        1142 :   cpp_option->traditional = 1;
     520                 :        1142 :   cpp_option->cplusplus_comments = 0;
     521                 :             : 
     522                 :        1142 :   cpp_option->cpp_pedantic = pedantic;
     523                 :             : 
     524                 :        1142 :   cpp_option->dollars_in_ident = flag_dollar_ok;
     525                 :        1142 :   cpp_option->discard_comments = gfc_cpp_option.discard_comments;
     526                 :        1142 :   cpp_option->discard_comments_in_macro_exp = gfc_cpp_option.discard_comments_in_macro_exp;
     527                 :        1142 :   cpp_option->print_include_names = gfc_cpp_option.print_include_names;
     528                 :        1142 :   cpp_option->preprocessed = gfc_option.flag_preprocessed;
     529                 :        1142 :   cpp_option->warn_date_time = gfc_cpp_option.warn_date_time;
     530                 :             : 
     531                 :        1142 :   if (gfc_cpp_makedep ())
     532                 :             :     {
     533                 :           1 :       cpp_option->deps.style = DEPS_USER;
     534                 :           1 :       cpp_option->deps.phony_targets = gfc_cpp_option.deps_phony;
     535                 :           1 :       cpp_option->deps.missing_files = gfc_cpp_option.deps_missing_are_generated;
     536                 :             : 
     537                 :             :       /* -MF <arg> overrides -M[M]D.  */
     538                 :           1 :       if (gfc_cpp_option.deps_filename_user)
     539                 :           1 :         gfc_cpp_option.deps_filename = gfc_cpp_option.deps_filename_user;
     540                 :             :   }
     541                 :             : 
     542                 :        1142 :   if (gfc_cpp_option.working_directory == -1)
     543                 :        1142 :     gfc_cpp_option.working_directory = (debug_info_level != DINFO_LEVEL_NONE);
     544                 :             : 
     545                 :        1142 :   cpp_post_options (cpp_in);
     546                 :             : 
     547                 :             : 
     548                 :             :   /* Let diagnostics infrastructure know how to convert input files the same
     549                 :             :      way libcpp will do it, namely, with no charset conversion but with
     550                 :             :      skipping of a UTF-8 BOM if present.  */
     551                 :        1142 :   diagnostic_initialize_input_context (global_dc, nullptr, true);
     552                 :        1142 :   gfc_cpp_init_cb ();
     553                 :             : 
     554                 :        1142 :   gfc_cpp_register_include_paths (verbose_missing_dir_warn);
     555                 :             : }
     556                 :             : 
     557                 :             : 
     558                 :             : void
     559                 :        1142 : gfc_cpp_init_0 (void)
     560                 :             : {
     561                 :             :   /* Initialize the print structure.  Setting print.src_line to -1 here is
     562                 :             :      a trick to guarantee that the first token of the file will cause
     563                 :             :      a linemarker to be output by maybe_print_line.  */
     564                 :        1142 :   print.src_line = -1;
     565                 :        1142 :   print.printed = 0;
     566                 :        1142 :   print.prev = 0;
     567                 :        1142 :   print.first_time = 1;
     568                 :             : 
     569                 :        1142 :   if (gfc_cpp_preprocess_only ())
     570                 :             :     {
     571                 :          13 :       if (gfc_cpp_option.output_filename)
     572                 :             :         {
     573                 :             :           /* This needs cheating: with "-E -o <file>", the user wants the
     574                 :             :              preprocessed output in <file>. However, if nothing is done
     575                 :             :              about it <file> is also used for assembler output. Hence, it
     576                 :             :              is necessary to redirect assembler output (actually nothing
     577                 :             :              as -E implies -fsyntax-only) to another file, otherwise the
     578                 :             :              output from preprocessing is lost.  */
     579                 :          13 :           asm_file_name = gfc_cpp_option.temporary_filename;
     580                 :             : 
     581                 :          13 :           print.outf = fopen (gfc_cpp_option.output_filename, "w");
     582                 :          13 :           if (print.outf == NULL)
     583                 :           0 :             gfc_fatal_error ("opening output file %qs: %s",
     584                 :             :                              gfc_cpp_option.output_filename,
     585                 :           0 :                              xstrerror (errno));
     586                 :             :         }
     587                 :             :       else
     588                 :           0 :         print.outf = stdout;
     589                 :             :     }
     590                 :             :   else
     591                 :             :     {
     592                 :        1129 :       print.outf = fopen (gfc_cpp_option.temporary_filename, "w");
     593                 :        1129 :       if (print.outf == NULL)
     594                 :           0 :         gfc_fatal_error ("opening output file %qs: %s",
     595                 :           0 :                          gfc_cpp_option.temporary_filename, xstrerror (errno));
     596                 :             :     }
     597                 :             : 
     598                 :        1142 :   gcc_assert(cpp_in);
     599                 :             : 
     600                 :        1142 :   if (gfc_cpp_option.deps_target_filename)
     601                 :           1 :     if (mkdeps *deps = cpp_get_deps (cpp_in))
     602                 :           1 :       deps_add_target (deps, gfc_cpp_option.deps_target_filename,
     603                 :           1 :                        gfc_cpp_option.quote_deps_target_filename);
     604                 :             : 
     605                 :        1142 :   if (!cpp_read_main_file (cpp_in, gfc_source_file))
     606                 :           0 :     errorcount++;
     607                 :        1142 : }
     608                 :             : 
     609                 :             : void
     610                 :        1142 : gfc_cpp_init (void)
     611                 :             : {
     612                 :        1142 :   int i;
     613                 :             : 
     614                 :        1142 :   if (gfc_option.flag_preprocessed)
     615                 :             :     return;
     616                 :             : 
     617                 :        1142 :   cpp_change_file (cpp_in, LC_RENAME, special_fname_builtin ());
     618                 :        1142 :   if (!gfc_cpp_option.no_predefined)
     619                 :             :     {
     620                 :             :       /* Make sure all of the builtins about to be declared have
     621                 :             :         BUILTINS_LOCATION has their location_t.  */
     622                 :        1142 :       cpp_force_token_locations (cpp_in, BUILTINS_LOCATION);
     623                 :             : 
     624                 :        1142 :       cpp_define_builtins (cpp_in);
     625                 :             : 
     626                 :        1142 :       cpp_stop_forcing_token_locations (cpp_in);
     627                 :             :     }
     628                 :             : 
     629                 :             :   /* Handle deferred options from command-line.  */
     630                 :        1142 :   cpp_change_file (cpp_in, LC_RENAME, _("<command-line>"));
     631                 :             : 
     632                 :        2281 :   for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++)
     633                 :             :     {
     634                 :        1139 :       gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i];
     635                 :             : 
     636                 :        1139 :       if (opt->code == OPT_D)
     637                 :        1139 :         cpp_define (cpp_in, opt->arg);
     638                 :           0 :       else if (opt->code == OPT_U)
     639                 :           0 :         cpp_undef (cpp_in, opt->arg);
     640                 :           0 :       else if (opt->code == OPT_A)
     641                 :             :         {
     642                 :           0 :           if (opt->arg[0] == '-')
     643                 :           0 :             cpp_unassert (cpp_in, opt->arg + 1);
     644                 :             :           else
     645                 :           0 :             cpp_assert (cpp_in, opt->arg);
     646                 :             :         }
     647                 :             :     }
     648                 :             : 
     649                 :             :   /* Pre-defined macros for non-required INTEGER kind types.  */
     650                 :        6505 :   for (gfc_integer_info *itype = gfc_integer_kinds; itype->kind != 0; itype++)
     651                 :             :     {
     652                 :        5363 :       if (itype->kind == 1)
     653                 :        1142 :         cpp_define (cpp_in, "__GFC_INT_1__=1");
     654                 :        5363 :       if (itype->kind == 2)
     655                 :        1142 :         cpp_define (cpp_in, "__GFC_INT_2__=1");
     656                 :        5363 :       if (itype->kind == 8)
     657                 :        1142 :         cpp_define (cpp_in, "__GFC_INT_8__=1");
     658                 :        5363 :       if (itype->kind == 16)
     659                 :         795 :         cpp_define (cpp_in, "__GFC_INT_16__=1");
     660                 :             :     }
     661                 :             : 
     662                 :             :   /* Pre-defined macros for non-required REAL kind types.  */
     663                 :        5710 :   for (gfc_real_info *rtype = gfc_real_kinds; rtype->kind != 0; rtype++)
     664                 :             :     {
     665                 :        4568 :       if (rtype->kind == 10)
     666                 :        1142 :         cpp_define (cpp_in, "__GFC_REAL_10__=1");
     667                 :        4568 :       if (rtype->kind == 16)
     668                 :        1142 :         cpp_define (cpp_in, "__GFC_REAL_16__=1");
     669                 :             :     }
     670                 :             : 
     671                 :        1142 :   if (gfc_cpp_option.working_directory
     672                 :         770 :       && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands)
     673                 :           0 :     pp_dir_change (cpp_in, get_src_pwd ());
     674                 :             : }
     675                 :             : 
     676                 :             : bool
     677                 :        1142 : gfc_cpp_preprocess (const char *source_file)
     678                 :             : {
     679                 :        1142 :   if (!gfc_cpp_enabled ())
     680                 :             :     return false;
     681                 :             : 
     682                 :        1142 :   cpp_change_file (cpp_in, LC_RENAME, source_file);
     683                 :             : 
     684                 :        1142 :   if (cpp_option->traditional)
     685                 :        1142 :     scan_translation_unit_trad (cpp_in);
     686                 :             :   else
     687                 :           0 :     scan_translation_unit (cpp_in);
     688                 :             : 
     689                 :             :   /* -dM command line option.  */
     690                 :        1142 :   if (gfc_cpp_preprocess_only () &&
     691                 :          13 :       gfc_cpp_option.dump_macros == 'M')
     692                 :             :     {
     693                 :           0 :       putc ('\n', print.outf);
     694                 :           0 :       cpp_forall_identifiers (cpp_in, dump_macro, NULL);
     695                 :             :     }
     696                 :             : 
     697                 :        1142 :   putc ('\n', print.outf);
     698                 :             : 
     699                 :        1142 :   if (!gfc_cpp_preprocess_only ()
     700                 :        1142 :       || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename))
     701                 :        1142 :     fclose (print.outf);
     702                 :             : 
     703                 :             :   return true;
     704                 :             : }
     705                 :             : 
     706                 :             : void
     707                 :       30427 : gfc_cpp_done (void)
     708                 :             : {
     709                 :       30427 :   if (!gfc_cpp_enabled ())
     710                 :             :     return;
     711                 :             : 
     712                 :        1142 :   gcc_assert (cpp_in);
     713                 :             : 
     714                 :        1142 :   if (gfc_cpp_makedep ())
     715                 :             :     {
     716                 :           1 :       if (gfc_cpp_option.deps_filename)
     717                 :             :         {
     718                 :           1 :           FILE *f = fopen (gfc_cpp_option.deps_filename, "w");
     719                 :           1 :           if (f)
     720                 :             :             {
     721                 :           1 :               cpp_finish (cpp_in, f);
     722                 :           1 :               fclose (f);
     723                 :             :             }
     724                 :             :           else
     725                 :           0 :             gfc_fatal_error ("opening output file %qs: %s",
     726                 :             :                              gfc_cpp_option.deps_filename,
     727                 :           0 :                              xstrerror (errno));
     728                 :             :         }
     729                 :             :       else
     730                 :           0 :         cpp_finish (cpp_in, stdout);
     731                 :             :     }
     732                 :             : 
     733                 :        1142 :   cpp_undef_all (cpp_in);
     734                 :        1142 :   cpp_clear_file_cache (cpp_in);
     735                 :             : }
     736                 :             : 
     737                 :             : /* PATH must be malloc-ed and NULL-terminated.  */
     738                 :             : void
     739                 :       56878 : gfc_cpp_add_include_path (char *path, bool user_supplied)
     740                 :             : {
     741                 :             :   /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
     742                 :             :      include path. Fortran does not define any system include paths.  */
     743                 :       56878 :   int cxx_aware = 0;
     744                 :             : 
     745                 :       56878 :   add_path (path, INC_BRACKET, cxx_aware, user_supplied);
     746                 :       56878 : }
     747                 :             : 
     748                 :             : void
     749                 :           3 : gfc_cpp_add_include_path_after (char *path, bool user_supplied)
     750                 :             : {
     751                 :           3 :   int cxx_aware = 0;
     752                 :           3 :   add_path (path, INC_AFTER, cxx_aware, user_supplied);
     753                 :           3 : }
     754                 :             : 
     755                 :             : 
     756                 :             : static void scan_translation_unit_trad (cpp_reader *);
     757                 :             : static void account_for_newlines (const unsigned char *, size_t);
     758                 :             : 
     759                 :             : static void print_line (location_t, const char *);
     760                 :             : static void maybe_print_line (location_t);
     761                 :             : 
     762                 :             : 
     763                 :             : /* Writes out the preprocessed file, handling spacing and paste
     764                 :             :    avoidance issues.  */
     765                 :             : static void
     766                 :           0 : scan_translation_unit (cpp_reader *pfile)
     767                 :             : {
     768                 :           0 :   bool avoid_paste = false;
     769                 :             : 
     770                 :           0 :   print.source = NULL;
     771                 :           0 :   for (;;)
     772                 :             :     {
     773                 :           0 :       const cpp_token *token = cpp_get_token (pfile);
     774                 :             : 
     775                 :           0 :       if (token->type == CPP_PADDING)
     776                 :             :         {
     777                 :           0 :           avoid_paste = true;
     778                 :           0 :           if (print.source == NULL
     779                 :           0 :               || (!(print.source->flags & PREV_WHITE)
     780                 :           0 :                   && token->val.source == NULL))
     781                 :           0 :             print.source = token->val.source;
     782                 :           0 :           continue;
     783                 :             :         }
     784                 :             : 
     785                 :           0 :       if (token->type == CPP_EOF)
     786                 :             :         break;
     787                 :             : 
     788                 :             :       /* Subtle logic to output a space if and only if necessary.  */
     789                 :           0 :       if (avoid_paste)
     790                 :             :         {
     791                 :           0 :           if (print.source == NULL)
     792                 :           0 :             print.source = token;
     793                 :           0 :           if (print.source->flags & PREV_WHITE
     794                 :           0 :               || (print.prev
     795                 :           0 :                   && cpp_avoid_paste (pfile, print.prev, token))
     796                 :           0 :               || (print.prev == NULL && token->type == CPP_HASH))
     797                 :           0 :             putc (' ', print.outf);
     798                 :             :         }
     799                 :           0 :       else if (token->flags & PREV_WHITE)
     800                 :           0 :         putc (' ', print.outf);
     801                 :             : 
     802                 :           0 :       avoid_paste = false;
     803                 :           0 :       print.source = NULL;
     804                 :           0 :       print.prev = token;
     805                 :           0 :       cpp_output_token (token, print.outf);
     806                 :             : 
     807                 :           0 :       if (token->type == CPP_COMMENT)
     808                 :           0 :         account_for_newlines (token->val.str.text, token->val.str.len);
     809                 :             :     }
     810                 :           0 : }
     811                 :             : 
     812                 :             : /* Adjust print.src_line for newlines embedded in output.  */
     813                 :             : static void
     814                 :           0 : account_for_newlines (const unsigned char *str, size_t len)
     815                 :             : {
     816                 :           0 :   while (len--)
     817                 :           0 :     if (*str++ == '\n')
     818                 :           0 :       print.src_line++;
     819                 :           0 : }
     820                 :             : 
     821                 :             : /* Writes out a traditionally preprocessed file.  */
     822                 :             : static void
     823                 :        1142 : scan_translation_unit_trad (cpp_reader *pfile)
     824                 :             : {
     825                 :      689350 :   while (_cpp_read_logical_line_trad (pfile))
     826                 :             :     {
     827                 :      687066 :       size_t len = pfile->out.cur - pfile->out.base;
     828                 :      687066 :       maybe_print_line (pfile->out.first_line);
     829                 :      687066 :       fwrite (pfile->out.base, 1, len, print.outf);
     830                 :      687066 :       print.printed = 1;
     831                 :      687066 :       if (!CPP_OPTION (pfile, discard_comments))
     832                 :           0 :         account_for_newlines (pfile->out.base, len);
     833                 :             :     }
     834                 :        1142 : }
     835                 :             : 
     836                 :             : /* If the token read on logical line LINE needs to be output on a
     837                 :             :    different line to the current one, output the required newlines or
     838                 :             :    a line marker.  */
     839                 :             : static void
     840                 :      689180 : maybe_print_line (location_t src_loc)
     841                 :             : {
     842                 :      689180 :   const line_map_ordinary *map
     843                 :      689180 :     = linemap_check_ordinary (linemap_lookup (line_table, src_loc));
     844                 :      689180 :   int src_line = SOURCE_LINE (map, src_loc);
     845                 :             : 
     846                 :             :   /* End the previous line of text.  */
     847                 :      689180 :   if (print.printed)
     848                 :             :     {
     849                 :      685225 :       putc ('\n', print.outf);
     850                 :      685225 :       print.src_line++;
     851                 :      685225 :       print.printed = 0;
     852                 :             :     }
     853                 :             : 
     854                 :      689180 :   if (src_line >= print.src_line && src_line < print.src_line + 8)
     855                 :             :     {
     856                 :      949221 :       while (src_line > print.src_line)
     857                 :             :         {
     858                 :      260128 :           putc ('\n', print.outf);
     859                 :      260128 :           print.src_line++;
     860                 :             :         }
     861                 :             :     }
     862                 :             :   else
     863                 :          87 :     print_line (src_loc, "");
     864                 :      689180 : }
     865                 :             : 
     866                 :             : /* Output a line marker for logical line LINE.  Special flags are "1"
     867                 :             :    or "2" indicating entering or leaving a file.  */
     868                 :             : static void
     869                 :        8883 : print_line (location_t src_loc, const char *special_flags)
     870                 :             : {
     871                 :             :   /* End any previous line of text.  */
     872                 :        8883 :   if (print.printed)
     873                 :         750 :     putc ('\n', print.outf);
     874                 :        8883 :   print.printed = 0;
     875                 :             : 
     876                 :        8883 :   if (!gfc_cpp_option.no_line_commands)
     877                 :             :     {
     878                 :        8883 :       expanded_location loc;
     879                 :        8883 :       size_t to_file_len;
     880                 :        8883 :       unsigned char *to_file_quoted;
     881                 :        8883 :       unsigned char *p;
     882                 :        8883 :       int sysp;
     883                 :             : 
     884                 :        8883 :       loc = expand_location (src_loc);
     885                 :        8883 :       to_file_len = strlen (loc.file);
     886                 :        8883 :       to_file_quoted = (unsigned char *) alloca (to_file_len * 4 + 1);
     887                 :             : 
     888                 :        8883 :       print.src_line = loc.line;
     889                 :             : 
     890                 :             :       /* cpp_quote_string does not nul-terminate, so we have to do it
     891                 :             :          ourselves.  */
     892                 :        8883 :       p = cpp_quote_string (to_file_quoted,
     893                 :             :                             (const unsigned char *) loc.file, to_file_len);
     894                 :        8883 :       *p = '\0';
     895                 :        8883 :       fprintf (print.outf, "# %u \"%s\"%s",
     896                 :        8883 :                print.src_line == 0 ? 1 : print.src_line,
     897                 :             :                to_file_quoted, special_flags);
     898                 :             : 
     899                 :        8883 :       sysp = in_system_header_at (src_loc);
     900                 :        8883 :       if (sysp == 2)
     901                 :           0 :         fputs (" 3 4", print.outf);
     902                 :        8883 :       else if (sysp == 1)
     903                 :           0 :         fputs (" 3", print.outf);
     904                 :             : 
     905                 :        8883 :       putc ('\n', print.outf);
     906                 :             :     }
     907                 :        8883 : }
     908                 :             : 
     909                 :             : static void
     910                 :        9938 : cb_file_change (cpp_reader * ARG_UNUSED (pfile), const line_map_ordinary *map)
     911                 :             : {
     912                 :        9938 :   const char *flags = "";
     913                 :             : 
     914                 :        9938 :   if (gfc_cpp_option.no_line_commands)
     915                 :             :     return;
     916                 :             : 
     917                 :        9938 :   if (!map)
     918                 :             :     return;
     919                 :             : 
     920                 :        8796 :       if (print.first_time)
     921                 :             :         {
     922                 :             :           /* Avoid printing foo.i when the main file is foo.c.  */
     923                 :        1142 :           if (!cpp_get_options (cpp_in)->preprocessed)
     924                 :        1142 :             print_line (map->start_location, flags);
     925                 :        1142 :           print.first_time = 0;
     926                 :             :         }
     927                 :             :       else
     928                 :             :         {
     929                 :             :           /* Bring current file to correct line when entering a new file.  */
     930                 :        7654 :           if (map->reason == LC_ENTER)
     931                 :        2114 :             maybe_print_line (linemap_included_from (map));
     932                 :        7654 :           if (map->reason == LC_ENTER)
     933                 :             :             flags = " 1";
     934                 :        5540 :           else if (map->reason == LC_LEAVE)
     935                 :        2114 :             flags = " 2";
     936                 :        7654 :           print_line (map->start_location, flags);
     937                 :             :         }
     938                 :             : 
     939                 :             : }
     940                 :             : 
     941                 :             : /* Called when a line of output is started.  TOKEN is the first token
     942                 :             :    of the line, and at end of file will be CPP_EOF.  */
     943                 :             : static void
     944                 :           0 : cb_line_change (cpp_reader *pfile, const cpp_token *token,
     945                 :             :                 int parsing_args)
     946                 :             : {
     947                 :           0 :   location_t src_loc = token->src_loc;
     948                 :             : 
     949                 :           0 :   if (token->type == CPP_EOF || parsing_args)
     950                 :             :     return;
     951                 :             : 
     952                 :           0 :   maybe_print_line (src_loc);
     953                 :           0 :   print.prev = 0;
     954                 :           0 :   print.source = 0;
     955                 :             : 
     956                 :             :   /* Supply enough spaces to put this token in its original column,
     957                 :             :      one space per column greater than 2, since scan_translation_unit
     958                 :             :      will provide a space if PREV_WHITE.  Don't bother trying to
     959                 :             :      reconstruct tabs; we can't get it right in general, and nothing
     960                 :             :      ought to care.  Some things do care; the fault lies with them.  */
     961                 :           0 :   if (!CPP_OPTION (pfile, traditional))
     962                 :             :     {
     963                 :           0 :       const line_map_ordinary *map
     964                 :           0 :         = linemap_check_ordinary (linemap_lookup (line_table, src_loc));
     965                 :           0 :       int spaces = SOURCE_COLUMN (map, src_loc) - 2;
     966                 :           0 :       print.printed = 1;
     967                 :             : 
     968                 :           0 :       while (-- spaces >= 0)
     969                 :           0 :         putc (' ', print.outf);
     970                 :             :     }
     971                 :             : }
     972                 :             : 
     973                 :             : static void
     974                 :           0 : cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
     975                 :             :           const cpp_string *str)
     976                 :             : {
     977                 :           0 :   maybe_print_line (line);
     978                 :           0 :   fprintf (print.outf, "#ident %s\n", str->text);
     979                 :           0 :   print.src_line++;
     980                 :           0 : }
     981                 :             : 
     982                 :             : static void
     983                 :           0 : cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
     984                 :             :            cpp_hashnode *node ATTRIBUTE_UNUSED)
     985                 :             : {
     986                 :           0 :   maybe_print_line (line);
     987                 :           0 :   fputs ("#define ", print.outf);
     988                 :             : 
     989                 :             :   /* 'D' is whole definition; 'N' is name only.  */
     990                 :           0 :   if (gfc_cpp_option.dump_macros == 'D')
     991                 :           0 :     fputs ((const char *) cpp_macro_definition (pfile, node),
     992                 :             :            print.outf);
     993                 :             :   else
     994                 :           0 :     fputs ((const char *) NODE_NAME (node), print.outf);
     995                 :             : 
     996                 :           0 :   putc ('\n', print.outf);
     997                 :           0 :   if (LOCATION_LINE (line) != 0)
     998                 :           0 :     print.src_line++;
     999                 :           0 : }
    1000                 :             : 
    1001                 :             : static void
    1002                 :           0 : cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
    1003                 :             :           cpp_hashnode *node)
    1004                 :             : {
    1005                 :           0 :   maybe_print_line (line);
    1006                 :           0 :   fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
    1007                 :           0 :   print.src_line++;
    1008                 :           0 : }
    1009                 :             : 
    1010                 :             : static void
    1011                 :           0 : cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
    1012                 :             :             const unsigned char *dir, const char *header, int angle_brackets,
    1013                 :             :             const cpp_token **comments)
    1014                 :             : {
    1015                 :           0 :   maybe_print_line (line);
    1016                 :           0 :   if (angle_brackets)
    1017                 :           0 :     fprintf (print.outf, "#%s <%s>", dir, header);
    1018                 :             :   else
    1019                 :           0 :     fprintf (print.outf, "#%s \"%s\"", dir, header);
    1020                 :             : 
    1021                 :           0 :   if (comments != NULL)
    1022                 :             :     {
    1023                 :           0 :       while (*comments != NULL)
    1024                 :             :         {
    1025                 :           0 :           if ((*comments)->flags & PREV_WHITE)
    1026                 :           0 :             putc (' ', print.outf);
    1027                 :           0 :           cpp_output_token (*comments, print.outf);
    1028                 :           0 :           ++comments;
    1029                 :             :         }
    1030                 :             :     }
    1031                 :             : 
    1032                 :           0 :   putc ('\n', print.outf);
    1033                 :           0 :   print.src_line++;
    1034                 :           0 : }
    1035                 :             : 
    1036                 :             : /* Dump out the hash table.  */
    1037                 :             : static int
    1038                 :           0 : dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
    1039                 :             : {
    1040                 :           0 :   if (cpp_user_macro_p (node))
    1041                 :             :     {
    1042                 :           0 :       fputs ("#define ", print.outf);
    1043                 :           0 :       fputs ((const char *) cpp_macro_definition (pfile, node),
    1044                 :             :              print.outf);
    1045                 :           0 :       putc ('\n', print.outf);
    1046                 :           0 :       print.src_line++;
    1047                 :             :     }
    1048                 :             : 
    1049                 :           0 :   return 1;
    1050                 :             : }
    1051                 :             : 
    1052                 :             : static void
    1053                 :           0 : cb_used_define (cpp_reader *pfile, location_t line ATTRIBUTE_UNUSED,
    1054                 :             :                 cpp_hashnode *node)
    1055                 :             : {
    1056                 :           0 :   gfc_cpp_macro_queue *q;
    1057                 :           0 :   q = XNEW (gfc_cpp_macro_queue);
    1058                 :           0 :   q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
    1059                 :           0 :   q->next = cpp_define_queue;
    1060                 :           0 :   cpp_define_queue = q;
    1061                 :           0 : }
    1062                 :             : 
    1063                 :             : /* Return the gcc option code associated with the reason for a cpp
    1064                 :             :    message, or 0 if none.  */
    1065                 :             : 
    1066                 :             : static diagnostic_option_id
    1067                 :          15 : cb_cpp_diagnostic_cpp_option (enum cpp_warning_reason reason)
    1068                 :             : {
    1069                 :          15 :   const struct cpp_reason_option_codes_t *entry;
    1070                 :             : 
    1071                 :         318 :   for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
    1072                 :         316 :     if (entry->reason == reason)
    1073                 :          13 :       return entry->option_code;
    1074                 :           0 :   return 0;
    1075                 :             : }
    1076                 :             : 
    1077                 :             : 
    1078                 :             : /* Callback from cpp_error for PFILE to print diagnostics from the
    1079                 :             :    preprocessor.  The diagnostic is of type LEVEL, with REASON set
    1080                 :             :    to the reason code if LEVEL is represents a warning, at location
    1081                 :             :    RICHLOC; MSG is the translated message and AP the arguments.
    1082                 :             :    Returns true if a diagnostic was emitted, false otherwise.  */
    1083                 :             : 
    1084                 :             : static bool
    1085                 :          15 : cb_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
    1086                 :             :                    enum cpp_diagnostic_level level,
    1087                 :             :                    enum cpp_warning_reason reason,
    1088                 :             :                    rich_location *richloc,
    1089                 :             :                    const char *msg, va_list *ap)
    1090                 :             : {
    1091                 :          15 :   diagnostic_info diagnostic;
    1092                 :          15 :   diagnostic_t dlevel;
    1093                 :          15 :   bool save_warn_system_headers = global_dc->m_warn_system_headers;
    1094                 :          15 :   bool ret;
    1095                 :             : 
    1096                 :          15 :   switch (level)
    1097                 :             :     {
    1098                 :           6 :     case CPP_DL_WARNING_SYSHDR:
    1099                 :           6 :       global_dc->m_warn_system_headers = 1;
    1100                 :             :       /* Fall through.  */
    1101                 :             :     case CPP_DL_WARNING:
    1102                 :             :       dlevel = DK_WARNING;
    1103                 :             :       break;
    1104                 :             :     case CPP_DL_PEDWARN:
    1105                 :             :       dlevel = DK_PEDWARN;
    1106                 :             :       break;
    1107                 :           2 :     case CPP_DL_ERROR:
    1108                 :           2 :       dlevel = DK_ERROR;
    1109                 :           2 :       break;
    1110                 :           0 :     case CPP_DL_ICE:
    1111                 :           0 :       dlevel = DK_ICE;
    1112                 :           0 :       break;
    1113                 :           0 :     case CPP_DL_NOTE:
    1114                 :           0 :       dlevel = DK_NOTE;
    1115                 :           0 :       break;
    1116                 :           0 :     case CPP_DL_FATAL:
    1117                 :           0 :       dlevel = DK_FATAL;
    1118                 :           0 :       break;
    1119                 :           0 :     default:
    1120                 :           0 :       gcc_unreachable ();
    1121                 :             :     }
    1122                 :          15 :   diagnostic_set_info_translated (&diagnostic, msg, ap,
    1123                 :             :                                   richloc, dlevel);
    1124                 :          30 :   diagnostic_set_option_id (&diagnostic,
    1125                 :             :                             cb_cpp_diagnostic_cpp_option (reason));
    1126                 :          15 :   ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
    1127                 :          15 :   if (level == CPP_DL_WARNING_SYSHDR)
    1128                 :           6 :     global_dc->m_warn_system_headers = save_warn_system_headers;
    1129                 :          15 :   return ret;
    1130                 :          15 : }
    1131                 :             : 
    1132                 :             : /* Callback called when -fworking-director and -E to emit working
    1133                 :             :    directory in cpp output file.  */
    1134                 :             : 
    1135                 :             : void
    1136                 :           0 : pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
    1137                 :             : {
    1138                 :           0 :   size_t to_file_len = strlen (dir);
    1139                 :           0 :   unsigned char *to_file_quoted =
    1140                 :           0 :      (unsigned char *) alloca (to_file_len * 4 + 1);
    1141                 :           0 :   unsigned char *p;
    1142                 :             : 
    1143                 :             :   /* cpp_quote_string does not nul-terminate, so we have to do it ourselves.  */
    1144                 :           0 :   p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
    1145                 :           0 :   *p = '\0';
    1146                 :           0 :   fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
    1147                 :           0 : }
    1148                 :             : 
    1149                 :             : /* Copy a #pragma directive to the preprocessed output.  */
    1150                 :             : static void
    1151                 :           0 : cb_def_pragma (cpp_reader *pfile, location_t line)
    1152                 :             : {
    1153                 :           0 :   maybe_print_line (line);
    1154                 :           0 :   fputs ("#pragma ", print.outf);
    1155                 :           0 :   cpp_output_line (pfile, print.outf);
    1156                 :           0 :   print.src_line++;
    1157                 :           0 : }
    1158                 :             : 
    1159                 :             : static void
    1160                 :           0 : cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
    1161                 :             :                location_t line ATTRIBUTE_UNUSED,
    1162                 :             :                cpp_hashnode *node)
    1163                 :             : {
    1164                 :           0 :   gfc_cpp_macro_queue *q;
    1165                 :           0 :   q = XNEW (gfc_cpp_macro_queue);
    1166                 :           0 :   q->macro = xstrdup ((const char *) NODE_NAME (node));
    1167                 :           0 :   q->next = cpp_undefine_queue;
    1168                 :           0 :   cpp_undefine_queue = q;
    1169                 :           0 : }
    1170                 :             : 
    1171                 :             : static void
    1172                 :           0 : dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
    1173                 :             : {
    1174                 :           0 :   gfc_cpp_macro_queue *q;
    1175                 :             : 
    1176                 :             :   /* End the previous line of text.  */
    1177                 :           0 :   if (print.printed)
    1178                 :             :     {
    1179                 :           0 :       putc ('\n', print.outf);
    1180                 :           0 :       print.src_line++;
    1181                 :           0 :       print.printed = 0;
    1182                 :             :     }
    1183                 :             : 
    1184                 :           0 :   for (q = cpp_define_queue; q;)
    1185                 :             :     {
    1186                 :           0 :       gfc_cpp_macro_queue *oq;
    1187                 :           0 :       fputs ("#define ", print.outf);
    1188                 :           0 :       fputs (q->macro, print.outf);
    1189                 :           0 :       putc ('\n', print.outf);
    1190                 :           0 :       print.src_line++;
    1191                 :           0 :       oq = q;
    1192                 :           0 :       q = q->next;
    1193                 :           0 :       free (oq->macro);
    1194                 :           0 :       free (oq);
    1195                 :             :     }
    1196                 :           0 :   cpp_define_queue = NULL;
    1197                 :           0 :   for (q = cpp_undefine_queue; q;)
    1198                 :             :     {
    1199                 :           0 :       gfc_cpp_macro_queue *oq;
    1200                 :           0 :       fprintf (print.outf, "#undef %s\n", q->macro);
    1201                 :           0 :       print.src_line++;
    1202                 :           0 :       oq = q;
    1203                 :           0 :       q = q->next;
    1204                 :           0 :       free (oq->macro);
    1205                 :           0 :       free (oq);
    1206                 :             :     }
    1207                 :           0 :   cpp_undefine_queue = NULL;
    1208                 :           0 : }
        

Generated by: LCOV version 2.1-beta

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,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.