libcpp: dependency emission tidying
[gcc.git] / libcpp / directives.c
index 7a8db572c3f1d27f7e6396452dcd3b294ae2f8cd..4295a67f1e5de812e7c6281e221571fdf9075d69 100644 (file)
@@ -1,5 +1,5 @@
 /* CPP Library. (Directive handling.)
-   Copyright (C) 1986-2018 Free Software Foundation, Inc.
+   Copyright (C) 1986-2020 Free Software Foundation, Inc.
    Contributed by Per Bothner, 1994-95.
    Based on CCCP program by Paul Rubin, June 1986
    Adapted to ANSI C, Richard Stallman, Jan 1987
@@ -204,8 +204,6 @@ static const directive linemarker_dir =
   do_linemarker, UC"#", 1, KANDR, IN_I
 };
 
-#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
-
 /* Skip any remaining tokens in a directive.  */
 static void
 skip_rest_of_line (cpp_reader *pfile)
@@ -408,13 +406,13 @@ directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
     }
 }
 
-/* Check if we have a known directive.  INDENTED is nonzero if the
+/* Check if we have a known directive.  INDENTED is true if the
    '#' of the directive was indented.  This function is in this file
    to save unnecessarily exporting dtable etc. to lex.c.  Returns
    nonzero if the line of tokens has been handled, zero if we should
    continue processing the line.  */
 int
-_cpp_handle_directive (cpp_reader *pfile, int indented)
+_cpp_handle_directive (cpp_reader *pfile, bool indented)
 {
   const directive *dir = 0;
   const cpp_token *dname;
@@ -597,14 +595,11 @@ lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
     {
       cpp_hashnode *node = token->val.node.node;
 
-      if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
-       cpp_error (pfile, CPP_DL_ERROR,
-                  "\"defined\" cannot be used as a macro name");
-      else if (is_def_or_undef
-           && (node == pfile->spec_nodes.n__has_include__
-            || node == pfile->spec_nodes.n__has_include_next__))
+      if (is_def_or_undef
+         && node == pfile->spec_nodes.n_defined)
        cpp_error (pfile, CPP_DL_ERROR,
-                  "\"__has_include__\" cannot be used as a macro name");
+                  "\"%s\" cannot be used as a macro name",
+                  NODE_NAME (node));
       else if (! (node->flags & NODE_POISONED))
        return node;
     }
@@ -820,6 +815,10 @@ do_include_common (cpp_reader *pfile, enum include_type type)
      callback can dump comments which follow #include.  */
   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
 
+  /* Tell the lexer this is an include directive -- we want it to
+     increment the line number even if this is the last line of a file.  */
+  pfile->state.in_directive = 2;
+
   fname = parse_include (pfile, &angle_brackets, &buf, &location);
   if (!fname)
     goto done;
@@ -833,8 +832,13 @@ do_include_common (cpp_reader *pfile, enum include_type type)
     }
 
   /* Prevent #include recursion.  */
-  if (pfile->line_table->depth >= CPP_STACK_MAX)
-    cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
+  if (pfile->line_table->depth >= CPP_OPTION (pfile, max_include_depth))
+    cpp_error (pfile, 
+              CPP_DL_ERROR, 
+              "#include nested depth %u exceeds maximum of %u"
+              " (use -fmax-include-depth=DEPTH to increase the maximum)",
+              pfile->line_table->depth,
+              CPP_OPTION (pfile, max_include_depth));
   else
     {
       /* Get out of macro context, if we are.  */
@@ -936,11 +940,11 @@ strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
 
 /* Interpret #line command.
    Note that the filename string (if any) is a true string constant
-   (escapes are interpreted), unlike in #line.  */
+   (escapes are interpreted).  */
 static void
 do_line (cpp_reader *pfile)
 {
-  struct line_maps *line_table = pfile->line_table;
+  class line_maps *line_table = pfile->line_table;
   const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
 
   /* skip_rest_of_line() may cause line table to be realloc()ed so note down
@@ -1003,7 +1007,7 @@ do_line (cpp_reader *pfile)
 static void
 do_linemarker (cpp_reader *pfile)
 {
-  struct line_maps *line_table = pfile->line_table;
+  class line_maps *line_table = pfile->line_table;
   const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
   const cpp_token *token;
   const char *new_file = ORDINARY_MAP_FILE_NAME (map);
@@ -1081,9 +1085,17 @@ do_linemarker (cpp_reader *pfile)
       map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
       const line_map_ordinary *from
        = linemap_included_from_linemap (line_table, map);
-      if (MAIN_FILE_P (map)
-         || (from
-             && filename_cmp (ORDINARY_MAP_FILE_NAME (from), new_file) != 0))
+
+      if (!from)
+       /* Not nested.  */;
+      else if (!new_file[0])
+       /* Leaving to "" means fill in the popped-to name.  */
+       new_file = ORDINARY_MAP_FILE_NAME (from);
+      else if (filename_cmp (ORDINARY_MAP_FILE_NAME (from), new_file) != 0)
+       /* It's the wrong name, Grommit!  */
+       from = NULL;
+
+      if (!from)
        {
          cpp_warning (pfile, CPP_W_NONE,
                       "file \"%s\" linemarker ignored due to "
@@ -1091,6 +1103,7 @@ do_linemarker (cpp_reader *pfile)
          return;
        }
     }
+
   /* Compensate for the increment in linemap_add that occurs in
      _cpp_do_file_change.  We're currently at the start of the line
      *following* the #line directive.  A separate location_t for this
@@ -1102,27 +1115,44 @@ do_linemarker (cpp_reader *pfile)
   line_table->seen_line_directive = true;
 }
 
-/* Arrange the file_change callback.  pfile->line has changed to
-   FILE_LINE of TO_FILE, for reason REASON.  SYSP is 1 for a system
-   header, 2 for a system header that needs to be extern "C" protected,
-   and zero otherwise.  */
+/* Arrange the file_change callback.  Changing to TO_FILE:TO_LINE for
+   REASON.  SYSP is 1 for a system header, 2 for a system header that
+   needs to be extern "C" protected, and zero otherwise.  */
 void
 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
-                    const char *to_file, linenum_type file_line,
+                    const char *to_file, linenum_type to_line,
                     unsigned int sysp)
 {
   linemap_assert (reason != LC_ENTER_MACRO);
-  const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
-                                           to_file, file_line);
+
   const line_map_ordinary *ord_map = NULL;
-  if (map != NULL)
-    {
-      ord_map = linemap_check_ordinary (map);
-      linemap_line_start (pfile->line_table,
-                         ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map),
-                         127);
+  if (!to_line && reason == LC_RENAME_VERBATIM)
+    {
+      /* A linemarker moving to line zero.  If we're on the second
+         line of the current map, and it also starts at zero, just
+         rewind -- we're probably reading the builtins of a
+         preprocessed source.  */
+      line_map_ordinary *last = LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table);
+      if (!ORDINARY_MAP_STARTING_LINE_NUMBER (last)
+         && 0 == filename_cmp (to_file, ORDINARY_MAP_FILE_NAME (last))
+         && SOURCE_LINE (last, pfile->line_table->highest_line) == 2)
+       {
+         ord_map = last;
+         pfile->line_table->highest_location
+           = pfile->line_table->highest_line = MAP_START_LOCATION (last);
+       }
     }
 
+  if (!ord_map)
+    if (const line_map *map = linemap_add (pfile->line_table, reason, sysp,
+                                          to_file, to_line))
+      {
+       ord_map = linemap_check_ordinary (map);
+       linemap_line_start (pfile->line_table,
+                           ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map),
+                           127);
+      }
+
   if (pfile->cb.file_change)
     pfile->cb.file_change (pfile, ord_map);
 }
@@ -1566,6 +1596,8 @@ do_pragma_push_macro (cpp_reader *pfile)
   node = _cpp_lex_identifier (pfile, c->name);
   if (node->type == NT_VOID)
     c->is_undef = 1;
+  else if (node->type == NT_BUILTIN_MACRO)
+    c->is_builtin = 1;
   else
     {
       defn = cpp_macro_definition (pfile, node);
@@ -1948,13 +1980,9 @@ do_ifdef (cpp_reader *pfile)
 
       if (node)
        {
-         /* Do not treat conditional macros as being defined.  This is due to
-            the powerpc and spu ports using conditional macros for 'vector',
-            'bool', and 'pixel' to act as conditional keywords.  This messes
-            up tests like #ifndef bool.  */
-         skip = !cpp_macro_p (node) || (node->flags & NODE_CONDITIONAL);
+         skip = !_cpp_defined_macro_p (node);
          _cpp_mark_macro_used (node);
-         _cpp_maybe_notify_macro_use (pfile, node);
+         _cpp_maybe_notify_macro_use (pfile, node, pfile->directive_line);
          if (pfile->cb.used)
            pfile->cb.used (pfile, pfile->directive_line, node);
          check_eol (pfile, false);
@@ -1977,14 +2005,9 @@ do_ifndef (cpp_reader *pfile)
 
       if (node)
        {
-         /* Do not treat conditional macros as being defined.  This is due to
-            the powerpc and spu ports using conditional macros for 'vector',
-            'bool', and 'pixel' to act as conditional keywords.  This messes
-            up tests like #ifndef bool.  */
-         skip = (cpp_macro_p (node)
-                 && !(node->flags & NODE_CONDITIONAL));
+         skip = _cpp_defined_macro_p (node);
          _cpp_mark_macro_used (node);
-         _cpp_maybe_notify_macro_use (pfile, node);
+         _cpp_maybe_notify_macro_use (pfile, node, pfile->directive_line);
          if (pfile->cb.used)
            pfile->cb.used (pfile, pfile->directive_line, node);
          check_eol (pfile, false);
@@ -2454,6 +2477,11 @@ cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c)
 
   if (c->is_undef)
     return;
+  if (c->is_builtin)
+    {
+      _cpp_restore_special_builtin (pfile, c);
+      return;
+    }
 
   {
     size_t namelen;
@@ -2541,10 +2569,10 @@ cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
 }
 
 /* The dependencies structure.  (Creates one if it hasn't already been.)  */
-struct deps *
+class mkdeps *
 cpp_get_deps (cpp_reader *pfile)
 {
-  if (!pfile->deps)
+  if (!pfile->deps && CPP_OPTION (pfile, deps.style) != DEPS_NONE)
     pfile->deps = deps_init ();
   return pfile->deps;
 }
@@ -2615,12 +2643,9 @@ _cpp_pop_buffer (cpp_reader *pfile)
 void
 _cpp_init_directives (cpp_reader *pfile)
 {
-  unsigned int i;
-  cpp_hashnode *node;
-
-  for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
+  for (int i = 0; i < N_DIRECTIVES; i++)
     {
-      node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
+      cpp_hashnode *node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
       node->is_directive = 1;
       node->directive_index = i;
     }