OSDN Git Service

2006-10-21 H.J. Lu <hongjiu.lu@intel.com>
[pf3gnuchains/gcc-fork.git] / gcc / c-common.c
index fe99dfb..62d5261 100644 (file)
@@ -261,20 +261,10 @@ int flag_ms_extensions;
 
 int flag_no_asm;
 
-/* Nonzero means give string constants the type `const char *', as mandated
-   by the standard.  */
-
-int flag_const_strings;
-
 /* Nonzero means to treat bitfields as signed unless they say `unsigned'.  */
 
 int flag_signed_bitfields = 1;
 
-/* Nonzero means warn about deprecated conversion from string constant to
-   `char *'.  */
-
-int warn_write_strings;
-
 /* Warn about #pragma directives that are not recognized.  */
 
 int warn_unknown_pragmas; /* Tri state variable.  */
@@ -434,10 +424,16 @@ int flag_weak = 1;
 int flag_working_directory = -1;
 
 /* Nonzero to use __cxa_atexit, rather than atexit, to register
-   destructors for local statics and global objects.  */
+   destructors for local statics and global objects.  '2' means it has been
+   set nonzero as a default, not by a command-line flag.  */
 
 int flag_use_cxa_atexit = DEFAULT_USE_CXA_ATEXIT;
 
+/* Nonzero to use __cxa_get_exception_ptr in C++ exception-handling
+   code.  '2' means it has not been set explicitly on the command line.  */
+
+int flag_use_cxa_get_exception_ptr = 2;
+
 /* Nonzero means make the default pedwarns warnings instead of errors.
    The value of this flag is ignored if -pedantic is specified.  */
 
@@ -578,7 +574,7 @@ const struct attribute_spec c_common_attribute_table[] =
   { "always_inline",          0, 0, true,  false, false,
                              handle_always_inline_attribute },
   { "flatten",                0, 0, true,  false, false,
-                              handle_flatten_attribute },
+                             handle_flatten_attribute },
   { "used",                   0, 0, true,  false, false,
                              handle_used_attribute },
   { "unused",                 0, 0, false, false, false,
@@ -777,10 +773,10 @@ c_expand_decl (tree decl)
     {
       /* Let the back-end know about this variable.  */
       if (!anon_aggr_type_p (TREE_TYPE (decl)))
-        emit_local_var (decl);
+       emit_local_var (decl);
       else
-        expand_anon_union_decl (decl, NULL_TREE,
-                                DECL_ANON_UNION_ELEMS (decl));
+       expand_anon_union_decl (decl, NULL_TREE,
+                               DECL_ANON_UNION_ELEMS (decl));
     }
   else
     return 0;
@@ -844,7 +840,6 @@ fix_string_type (tree value)
 {
   const int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
   const int wide_flag = TREE_TYPE (value) == wchar_array_type_node;
-  const int nchars_max = flag_isoc99 ? 4095 : 509;
   int length = TREE_STRING_LENGTH (value);
   int nchars;
   tree e_type, i_type, a_type;
@@ -852,15 +847,32 @@ fix_string_type (tree value)
   /* Compute the number of elements, for the array type.  */
   nchars = wide_flag ? length / wchar_bytes : length;
 
-  if (pedantic && nchars - 1 > nchars_max && !c_dialect_cxx ())
-    pedwarn ("string length %qd is greater than the length %qd ISO C%d compilers are required to support",
-            nchars - 1, nchars_max, flag_isoc99 ? 99 : 89);
-
-  e_type = wide_flag ? wchar_type_node : char_type_node;
-  /* Create the array type for the string constant.  flag_const_strings
-     says make the string constant an array of const char so that
-     copying it to a non-const pointer will get a warning.  For C++,
-     this is the standard behavior.
+  /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits).  The analogous
+     limit in C++98 Annex B is very large (65536) and is not normative,
+     so we do not diagnose it (warn_overlength_strings is forced off
+     in c_common_post_options).  */
+  if (warn_overlength_strings)
+    {
+      const int nchars_max = flag_isoc99 ? 4095 : 509;
+      const int relevant_std = flag_isoc99 ? 99 : 90;
+      if (nchars - 1 > nchars_max)
+       /* Translators: The %d after 'ISO C' will be 90 or 99.  Do not
+          separate the %d from the 'C'.  'ISO' should not be
+          translated, but it may be moved after 'C%d' in languages
+          where modifiers follow nouns.  */
+       pedwarn ("string length %qd is greater than the length %qd "
+                "ISO C%d compilers are required to support",
+                nchars - 1, nchars_max, relevant_std);
+    }
+
+  /* Create the array type for the string constant.  The ISO C++
+     standard says that a string literal has type `const char[N]' or
+     `const wchar_t[N]'.  We use the same logic when invoked as a C
+     front-end with -Wwrite-strings.
+     ??? We should change the type of an expression depending on the
+     state of a warning flag.  We should just be warning -- see how
+     this is handled in the C++ front-end for the deprecated implicit
+     conversion from string literals to `char*' or `wchar_t*'.
 
      The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
      array type being the unqualified version of that type.
@@ -868,9 +880,10 @@ fix_string_type (tree value)
      construct the matching unqualified array type first.  The C front
      end does not require this, but it does no harm, so we do it
      unconditionally.  */
+  e_type = wide_flag ? wchar_type_node : char_type_node;
   i_type = build_index_type (build_int_cst (NULL_TREE, nchars - 1));
   a_type = build_array_type (e_type, i_type);
-  if (flag_const_strings)
+  if (c_dialect_cxx() || warn_write_strings)
     a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
 
   TREE_TYPE (value) = a_type;
@@ -893,7 +906,9 @@ constant_expression_warning (tree value)
   if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
        || TREE_CODE (value) == VECTOR_CST
        || TREE_CODE (value) == COMPLEX_CST)
-      && TREE_CONSTANT_OVERFLOW (value) && pedantic)
+      && TREE_CONSTANT_OVERFLOW (value)
+      && warn_overflow
+      && pedantic)
     pedwarn ("overflow in constant expression");
 }
 
@@ -914,7 +929,7 @@ overflow_warning (tree value)
     {
       TREE_OVERFLOW (value) = 0;
       if (skip_evaluation == 0)
-       warning (0, "integer overflow in expression");
+       warning (OPT_Woverflow, "integer overflow in expression");
     }
   else if ((TREE_CODE (value) == REAL_CST
            || (TREE_CODE (value) == COMPLEX_CST
@@ -923,13 +938,13 @@ overflow_warning (tree value)
     {
       TREE_OVERFLOW (value) = 0;
       if (skip_evaluation == 0)
-       warning (0, "floating point overflow in expression");
+       warning (OPT_Woverflow, "floating point overflow in expression");
     }
   else if (TREE_CODE (value) == VECTOR_CST && TREE_OVERFLOW (value))
     {
       TREE_OVERFLOW (value) = 0;
       if (skip_evaluation == 0)
-       warning (0, "vector overflow in expression");
+       warning (OPT_Woverflow, "vector overflow in expression");
     }
 }
 
@@ -938,7 +953,7 @@ overflow_warning (tree value)
    Invoke this function on every expression that might be implicitly
    converted to an unsigned type.  */
 
-void
+static void
 unsigned_conversion_warning (tree result, tree operand)
 {
   tree type = TREE_TYPE (result);
@@ -951,7 +966,8 @@ unsigned_conversion_warning (tree result, tree operand)
     {
       if (!int_fits_type_p (operand, c_common_signed_type (type)))
        /* This detects cases like converting -129 or 256 to unsigned char.  */
-       warning (0, "large integer implicitly truncated to unsigned type");
+       warning (OPT_Woverflow,
+                "large integer implicitly truncated to unsigned type");
       else
        warning (OPT_Wconversion,
                 "negative integer implicitly converted to unsigned type");
@@ -960,11 +976,11 @@ unsigned_conversion_warning (tree result, tree operand)
 
 /* Print a warning about casts that might indicate violation
    of strict aliasing rules if -Wstrict-aliasing is used and
-   strict aliasing mode is in effect. otype is the original
-   TREE_TYPE of expr, and type the type we're casting to. */
+   strict aliasing mode is in effect. OTYPE is the original
+   TREE_TYPE of EXPR, and TYPE the type we're casting to. */
 
 void
-strict_aliasing_warning(tree otype, tree type, tree expr)
+strict_aliasing_warning (tree otype, tree type, tree expr)
 {
   if (flag_strict_aliasing && warn_strict_aliasing
       && POINTER_TYPE_P (type) && POINTER_TYPE_P (otype)
@@ -1044,8 +1060,8 @@ vector_types_convertible_p (tree t1, tree t2)
 {
   return targetm.vector_opaque_p (t1)
         || targetm.vector_opaque_p (t2)
-         || (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
-            && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE || 
+        || (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
+            && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
                 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
             && INTEGRAL_TYPE_P (TREE_TYPE (t1))
                == INTEGRAL_TYPE_P (TREE_TYPE (t2)));
@@ -1067,7 +1083,8 @@ convert_and_check (tree type, tree expr)
 
          /* Do not diagnose overflow in a constant expression merely
             because a conversion overflowed.  */
-         TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
+         TREE_CONSTANT_OVERFLOW (t) = CONSTANT_CLASS_P (expr)
+                                       && TREE_CONSTANT_OVERFLOW (expr);
 
          /* No warning for converting 0x80000000 to int.  */
          if (!(TYPE_UNSIGNED (type) < TYPE_UNSIGNED (TREE_TYPE (expr))
@@ -1080,7 +1097,8 @@ convert_and_check (tree type, tree expr)
                 || !constant_fits_type_p (expr,
                                           c_common_unsigned_type (type)))
                && skip_evaluation == 0)
-             warning (0, "overflow in implicit constant conversion");
+             warning (OPT_Woverflow,
+                       "overflow in implicit constant conversion");
        }
       else
        unsigned_conversion_warning (t, expr);
@@ -1428,7 +1446,7 @@ verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
 
     default:
       /* For other expressions, simply recurse on their operands.
-         Manual tail recursion for unary expressions.
+        Manual tail recursion for unary expressions.
         Other non-expressions need not be processed.  */
       if (cl == tcc_unary)
        {
@@ -1858,6 +1876,31 @@ c_common_signed_or_unsigned_type (int unsignedp, tree type)
     return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
 }
 
+/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP.  */
+
+tree
+c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
+{
+  /* Extended integer types of the same width as a standard type have
+     lesser rank, so those of the same width as int promote to int or
+     unsigned int and are valid for printf formats expecting int or
+     unsigned int.  To avoid such special cases, avoid creating
+     extended integer types for bit-fields if a standard integer type
+     is available.  */
+  if (width == TYPE_PRECISION (integer_type_node))
+    return unsignedp ? unsigned_type_node : integer_type_node;
+  if (width == TYPE_PRECISION (signed_char_type_node))
+    return unsignedp ? unsigned_char_type_node : signed_char_type_node;
+  if (width == TYPE_PRECISION (short_integer_type_node))
+    return unsignedp ? short_unsigned_type_node : short_integer_type_node;
+  if (width == TYPE_PRECISION (long_integer_type_node))
+    return unsignedp ? long_unsigned_type_node : long_integer_type_node;
+  if (width == TYPE_PRECISION (long_long_integer_type_node))
+    return (unsignedp ? long_long_unsigned_type_node
+           : long_long_integer_type_node);
+  return build_nonstandard_integer_type (width, unsignedp);
+}
+
 /* The C version of the register_builtin_type langhook.  */
 
 void
@@ -2458,11 +2501,13 @@ c_common_truthvalue_conversion (tree expr)
       {
        tree inner = TREE_OPERAND (expr, 0);
        if (DECL_P (inner)
-           && (TREE_CODE (inner) == PARM_DECL || !DECL_WEAK (inner)))
+           && (TREE_CODE (inner) == PARM_DECL
+               || TREE_CODE (inner) == LABEL_DECL
+               || !DECL_WEAK (inner)))
          {
            /* Common Ada/Pascal programmer's mistake.  We always warn
               about this since it is so bad.  */
-           warning (OPT_Walways_true, "the address of %qD, will always evaluate as %<true%>",
+           warning (OPT_Walways_true, "the address of %qD will always evaluate as %<true%>",
                     inner);
            return truthvalue_true_node;
          }
@@ -2524,37 +2569,6 @@ c_common_truthvalue_conversion (tree expr)
        return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
       break;
 
-    case MINUS_EXPR:
-      /* Perhaps reduce (x - y) != 0 to (x != y).  The expressions
-        aren't guaranteed to the be same for modes that can represent
-        infinity, since if x and y are both +infinity, or both
-        -infinity, then x - y is not a number.
-
-        Note that this transformation is safe when x or y is NaN.
-        (x - y) is then NaN, and both (x - y) != 0 and x != y will
-        be false.  */
-      if (HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0)))))
-       break;
-      /* Fall through....  */
-    case BIT_XOR_EXPR:
-      /* This and MINUS_EXPR can be changed into a comparison of the
-        two objects.  */
-      if (TREE_TYPE (TREE_OPERAND (expr, 0))
-         == TREE_TYPE (TREE_OPERAND (expr, 1)))
-       return fold_build2 (NE_EXPR, truthvalue_type_node,
-                           TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
-      return fold_build2 (NE_EXPR, truthvalue_type_node,
-                         TREE_OPERAND (expr, 0),
-                         fold_convert (TREE_TYPE (TREE_OPERAND (expr, 0)),
-                                       TREE_OPERAND (expr, 1)));
-
-    case BIT_AND_EXPR:
-      if (integer_onep (TREE_OPERAND (expr, 1))
-         && TREE_TYPE (expr) != truthvalue_type_node)
-       /* Using convert here would cause infinite recursion.  */
-       return build1 (NOP_EXPR, truthvalue_type_node, expr);
-      break;
-
     case MODIFY_EXPR:
       if (!TREE_NO_WARNING (expr))
        warning (OPT_Wparentheses,
@@ -2787,9 +2801,9 @@ c_common_get_alias_set (tree t)
         But, the standard is wrong.  In particular, this code is
         legal C++:
 
-            int *ip;
-            int **ipp = &ip;
-            const int* const* cipp = ipp;
+           int *ip;
+           int **ipp = &ip;
+           const int* const* cipp = ipp;
 
         And, it doesn't make sense for that to be legal unless you
         can dereference IPP and CIPP.  So, we ignore cv-qualifiers on
@@ -3346,10 +3360,10 @@ c_common_nodes_and_builtins (void)
                    NONANSI_P, ATTRS, IMPLICIT, COND)                   \
   if (NAME && COND)                                                    \
     def_builtin_1 (ENUM, NAME, CLASS,                                   \
-                   builtin_types[(int) TYPE],                           \
-                   builtin_types[(int) LIBTYPE],                        \
-                   BOTH_P, FALLBACK_P, NONANSI_P,                       \
-                   built_in_attributes[(int) ATTRS], IMPLICIT);
+                  builtin_types[(int) TYPE],                           \
+                  builtin_types[(int) LIBTYPE],                        \
+                  BOTH_P, FALLBACK_P, NONANSI_P,                       \
+                  built_in_attributes[(int) ATTRS], IMPLICIT);
 #include "builtins.def"
 #undef DEF_BUILTIN
 
@@ -3536,6 +3550,9 @@ self_promoting_args_p (tree parms)
     {
       tree type = TREE_VALUE (t);
 
+      if (type == error_mark_node)
+       continue;
+
       if (TREE_CHAIN (t) == 0 && type != void_type_node)
        return 0;
 
@@ -3648,8 +3665,8 @@ c_add_case_label (splay_tree cases, tree cond, tree orig_type,
   if (low_value && high_value)
     {
       /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
-         really a case range, even though it was written that way.
-         Remove the HIGH_VALUE to simplify later processing.  */
+        really a case range, even though it was written that way.
+        Remove the HIGH_VALUE to simplify later processing.  */
       if (tree_int_cst_equal (low_value, high_value))
        high_value = NULL_TREE;
       else if (!tree_int_cst_lt (low_value, high_value))
@@ -3780,6 +3797,9 @@ match_case_to_enum_1 (tree key, tree type, tree label)
             CASE_LABEL (label), buf, type);
 }
 
+/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
+   Used to verify that case values match up with enumerator values.  */
+
 static int
 match_case_to_enum (splay_tree_node node, void *data)
 {
@@ -3790,26 +3810,22 @@ match_case_to_enum (splay_tree_node node, void *data)
   if (!CASE_LOW (label))
     return 0;
 
-  /* If TREE_ADDRESSABLE is not set, that means CASE_LOW did not appear
+  /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
      when we did our enum->case scan.  Reset our scratch bit after.  */
-  if (!TREE_ADDRESSABLE (label))
+  if (!CASE_LOW_SEEN (label))
     match_case_to_enum_1 (CASE_LOW (label), type, label);
   else
-    TREE_ADDRESSABLE (label) = 0;
+    CASE_LOW_SEEN (label) = 0;
 
-  /* If CASE_HIGH is non-null, we have a range.  Here we must search.
-     Note that the old code in stmt.c did not check for the values in
-     the range either, just the endpoints.  */
+  /* If CASE_HIGH is non-null, we have a range.  If CASE_HIGH_SEEN is
+     not set, that means that CASE_HIGH did not appear when we did our
+     enum->case scan.  Reset our scratch bit after.  */
   if (CASE_HIGH (label))
     {
-      tree chain, key = CASE_HIGH (label);
-
-      for (chain = TYPE_VALUES (type);
-          chain && !tree_int_cst_equal (key, TREE_VALUE (chain));
-          chain = TREE_CHAIN (chain))
-       continue;
-      if (!chain)
-       match_case_to_enum_1 (key, type, label);
+      if (!CASE_HIGH_SEEN (label))
+       match_case_to_enum_1 (CASE_HIGH (label), type, label);
+      else
+       CASE_HIGH_SEEN (label) = 0;
     }
 
   return 0;
@@ -3827,6 +3843,8 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location,
                      tree type, tree cond)
 {
   splay_tree_node default_node;
+  splay_tree_node node;
+  tree chain;
 
   if (!warn_switch && !warn_switch_enum && !warn_switch_default)
     return;
@@ -3836,79 +3854,79 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location,
     warning (OPT_Wswitch_default, "%Hswitch missing default case",
             &switch_location);
 
+  /* From here on, we only care about about enumerated types.  */
+  if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
+    return;
+
   /* If the switch expression was an enumerated type, check that
      exactly all enumeration literals are covered by the cases.
      The check is made when -Wswitch was specified and there is no
      default case, or when -Wswitch-enum was specified.  */
-  if (((warn_switch && !default_node) || warn_switch_enum)
-      && type && TREE_CODE (type) == ENUMERAL_TYPE
-      && TREE_CODE (cond) != INTEGER_CST)
-    {
-      tree chain;
 
-      /* The time complexity here is O(N*lg(N)) worst case, but for the
-        common case of monotonically increasing enumerators, it is
-        O(N), since the nature of the splay tree will keep the next
-        element adjacent to the root at all times.  */
+  if (!warn_switch_enum
+      && !(warn_switch && !default_node))
+    return;
+
+  /* Clearing COND if it is not an integer constant simplifies
+     the tests inside the loop below.  */
+  if (TREE_CODE (cond) != INTEGER_CST)
+    cond = NULL_TREE;
 
-      for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
+  /* The time complexity here is O(N*lg(N)) worst case, but for the
+      common case of monotonically increasing enumerators, it is
+      O(N), since the nature of the splay tree will keep the next
+      element adjacent to the root at all times.  */
+
+  for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
+    {
+      tree value = TREE_VALUE (chain);
+      node = splay_tree_lookup (cases, (splay_tree_key) value);
+      if (node)
        {
-         splay_tree_node node
-           = splay_tree_lookup (cases, (splay_tree_key) TREE_VALUE (chain));
-         if (!node)
-           {
-             tree low_value = TREE_VALUE (chain);
-             splay_tree_node low_bound;
-             splay_tree_node high_bound;
-             /* Even though there wasn't an exact match, there might be a
-                case range which includes the enumator's value.  */
-             low_bound = splay_tree_predecessor (cases,
-                                                 (splay_tree_key) low_value);
-             high_bound = splay_tree_successor (cases,
-                                                (splay_tree_key) low_value);
-             
-             /* It is smaller than the LOW_VALUE, so there is no need to check
-                unless the LOW_BOUND is in fact itself a case range.  */
-             if (low_bound
-                 && CASE_HIGH ((tree) low_bound->value)
-                 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
-                                           low_value) >= 0)
-               node = low_bound;
-             /* The low end of that range is bigger than the current value. */
-             else if (high_bound
-                      && (tree_int_cst_compare ((tree) high_bound->key,
-                                                low_value)
-                          <= 0))
-               node = high_bound;
-           }
-         if (node)
-           {
-             /* Mark the CASE_LOW part of the case entry as seen, so
-                that we save time later.  Choose TREE_ADDRESSABLE
-                randomly as a bit that won't have been set to-date.  */
-             tree label = (tree) node->value;
-             TREE_ADDRESSABLE (label) = 1;
-           }
-         else
+         /* Mark the CASE_LOW part of the case entry as seen.  */
+         tree label = (tree) node->value;
+         CASE_LOW_SEEN (label) = 1;
+         continue;
+       }
+
+      /* Even though there wasn't an exact match, there might be a
+        case range which includes the enumator's value.  */
+      node = splay_tree_predecessor (cases, (splay_tree_key) value);
+      if (node && CASE_HIGH ((tree) node->value))
+       {
+         tree label = (tree) node->value;
+         int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
+         if (cmp >= 0)
            {
-             /* Warn if there are enumerators that don't correspond to
-                case expressions.  */
-             warning (0, "%Henumeration value %qE not handled in switch",
-                      &switch_location, TREE_PURPOSE (chain));
+             /* If we match the upper bound exactly, mark the CASE_HIGH
+                part of the case entry as seen.  */
+             if (cmp == 0)
+               CASE_HIGH_SEEN (label) = 1;
+             continue;
            }
        }
 
-      /* Warn if there are case expressions that don't correspond to
-        enumerators.  This can occur since C and C++ don't enforce
-        type-checking of assignments to enumeration variables.
+      /* We've now determined that this enumerated literal isn't
+        handled by the case labels of the switch statement.  */
 
-        The time complexity here is O(N**2) worst case, since we've
-        not sorted the enumeration values.  However, in the absence
-        of case ranges this is O(N), since all single cases that
-        corresponded to enumerations have been marked above.  */
+      /* If the switch expression is a constant, we only really care
+        about whether that constant is handled by the switch.  */
+      if (cond && tree_int_cst_compare (cond, value))
+       continue;
 
-      splay_tree_foreach (cases, match_case_to_enum, type);
+      warning (0, "%Henumeration value %qE not handled in switch",
+              &switch_location, TREE_PURPOSE (chain));
     }
+
+  /* Warn if there are case expressions that don't correspond to
+     enumerators.  This can occur since C and C++ don't enforce
+     type-checking of assignments to enumeration variables.
+
+     The time complexity here is now always O(N) worst case, since
+     we should have marked both the lower bound and upper bound of
+     every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
+     above.  This scan also resets those fields.  */
+  splay_tree_foreach (cases, match_case_to_enum, type);
 }
 
 /* Finish an expression taking the address of LABEL (an
@@ -4065,20 +4083,6 @@ handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
       if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
        *node = build_variant_type_copy (*node);
       TYPE_PACKED (*node) = 1;
-      if (TYPE_MAIN_VARIANT (*node) == *node)
-       {
-         /* If it is the main variant, then pack the other variants
-            too. This happens in,
-
-            struct Foo {
-              struct Foo const *ptr; // creates a variant w/o packed flag
-            } __ attribute__((packed)); // packs it now.
-          */
-         tree probe;
-
-         for (probe = *node; probe; probe = TYPE_NEXT_VARIANT (probe))
-           TYPE_PACKED (probe) = 1;
-       }
     }
   else if (TREE_CODE (*node) == FIELD_DECL)
     {
@@ -4213,8 +4217,8 @@ handle_always_inline_attribute (tree *node, tree name,
 
 static tree
 handle_flatten_attribute (tree *node, tree name,
-                          tree args ATTRIBUTE_UNUSED,
-                          int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
+                         tree args ATTRIBUTE_UNUSED,
+                         int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
 {
   if (TREE_CODE (*node) == FUNCTION_DECL)
     /* Do nothing else, just set the attribute.  We'll get at
@@ -4298,26 +4302,15 @@ handle_externally_visible_attribute (tree *pnode, tree name,
 {
   tree node = *pnode;
 
-  if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL)
-      || !TREE_PUBLIC (node))
+  if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
     {
-      warning (OPT_Wattributes,
-              "%qE attribute have effect only on public objects", name);
-      *no_add_attrs = true;
-    }
-  else if (TREE_CODE (node) == FUNCTION_DECL)
-    {
-      struct cgraph_node *n = cgraph_node (node);
-      n->local.externally_visible = true;
-      if (n->local.finalized)
-       cgraph_mark_needed_node (n);
-    }
-  else if (TREE_CODE (node) == VAR_DECL)
-    {
-      struct cgraph_varpool_node *n = cgraph_varpool_node (node);
-      n->externally_visible = true;
-      if (n->finalized)
-       cgraph_varpool_mark_needed_node (n);
+      if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
+          && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
+       {
+         warning (OPT_Wattributes,
+                  "%qE attribute have effect only on public objects", name);
+         *no_add_attrs = true;
+       }
     }
   else
     {
@@ -4556,14 +4549,14 @@ handle_mode_attribute (tree *node, tree name, tree args,
              return NULL_TREE;
            }
 
-          if (TREE_CODE (type) == POINTER_TYPE)
+         if (TREE_CODE (type) == POINTER_TYPE)
            fn = build_pointer_type_for_mode;
          else
            fn = build_reference_type_for_mode;
          typefm = fn (TREE_TYPE (type), mode, false);
        }
       else
-        typefm = lang_hooks.types.type_for_mode (mode, TYPE_UNSIGNED (type));
+       typefm = lang_hooks.types.type_for_mode (mode, TYPE_UNSIGNED (type));
 
       if (typefm == NULL_TREE)
        {
@@ -4745,12 +4738,17 @@ handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
    struct attribute_spec.handler.  */
 
 static tree
-handle_weak_attribute (tree *node, tree ARG_UNUSED (name),
+handle_weak_attribute (tree *node, tree name,
                       tree ARG_UNUSED (args),
                       int ARG_UNUSED (flags),
                       bool * ARG_UNUSED (no_add_attrs))
 {
-  declare_weak (*node);
+  if (TREE_CODE (*node) == FUNCTION_DECL
+      || TREE_CODE (*node) == VAR_DECL)
+    declare_weak (*node);
+  else
+    warning (OPT_Wattributes, "%qE attribute ignored", name);
+       
 
   return NULL_TREE;
 }
@@ -4825,6 +4823,16 @@ handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
 {
   tree attr = NULL_TREE;
 
+  /* We must ignore the attribute when it is associated with
+     local-scoped decls, since attribute alias is ignored and many
+     such symbols do not even have a DECL_WEAK field.  */
+  if (decl_function_context (*node) || current_function_decl)
+    {
+      warning (OPT_Wattributes, "%qE attribute ignored", name);
+      *no_add_attrs = true;
+      return NULL_TREE;
+    }
+
   /* The idea here is that `weakref("name")' mutates into `weakref,
      alias("name")', and weakref without arguments, in turn,
      implicitly adds weak. */
@@ -4859,21 +4867,28 @@ handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
 static tree
 handle_visibility_attribute (tree *node, tree name, tree args,
                             int ARG_UNUSED (flags),
-                            bool *no_add_attrs)
+                            bool *ARG_UNUSED (no_add_attrs))
 {
   tree decl = *node;
   tree id = TREE_VALUE (args);
-
-  *no_add_attrs = true;
+  enum symbol_visibility vis;
 
   if (TYPE_P (*node))
     {
-      if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
-       {
-         warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
-                 name);
-         return NULL_TREE;
-       }
+      if (TREE_CODE (*node) == ENUMERAL_TYPE)
+       /* OK */;
+      else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
+       {
+         warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
+                  name);
+         return NULL_TREE;
+       }
+      else if (TYPE_FIELDS (*node))
+       {
+         error ("%qE attribute ignored because %qT is already defined",
+                name, *node);
+         return NULL_TREE;
+       }
     }
   else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
     {
@@ -4892,7 +4907,7 @@ handle_visibility_attribute (tree *node, tree name, tree args,
     {
       decl = TYPE_NAME (decl);
       if (!decl)
-        return NULL_TREE;
+       return NULL_TREE;
       if (TREE_CODE (decl) == IDENTIFIER_NODE)
        {
           warning (OPT_Wattributes, "%qE attribute ignored on types",
@@ -4902,23 +4917,33 @@ handle_visibility_attribute (tree *node, tree name, tree args,
     }
 
   if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
-    DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
+    vis = VISIBILITY_DEFAULT;
   else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
-    DECL_VISIBILITY (decl) = VISIBILITY_INTERNAL;
+    vis = VISIBILITY_INTERNAL;
   else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
-    DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
+    vis = VISIBILITY_HIDDEN;
   else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
-    DECL_VISIBILITY (decl) = VISIBILITY_PROTECTED;
+    vis = VISIBILITY_PROTECTED;
   else
-    error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
+    {
+      error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
+      vis = VISIBILITY_DEFAULT;
+    }
+
+  if (DECL_VISIBILITY_SPECIFIED (decl)
+      && vis != DECL_VISIBILITY (decl)
+      && lookup_attribute ("visibility", (TYPE_P (*node)
+                                         ? TYPE_ATTRIBUTES (*node)
+                                         : DECL_ATTRIBUTES (decl))))
+    error ("%qD redeclared with different visibility", decl);
+
+  DECL_VISIBILITY (decl) = vis;
   DECL_VISIBILITY_SPECIFIED (decl) = 1;
 
-  /* For decls only, go ahead and attach the attribute to the node as well.
-     This is needed so we can determine whether we have VISIBILITY_DEFAULT
-     because the visibility was not specified, or because it was explicitly
-     overridden from the class visibility.  */
-  if (DECL_P (*node))
-    *no_add_attrs = false;
+  /* Go ahead and attach the attribute to the node as well.  This is needed
+     so we can determine whether we have VISIBILITY_DEFAULT because the
+     visibility was not specified, or because it was explicitly overridden
+     from the containing scope.  */
 
   return NULL_TREE;
 }
@@ -4955,6 +4980,13 @@ c_determine_visibility (tree decl)
       return true;
     }
 
+  /* Set default visibility to whatever the user supplied with
+     visibility_specified depending on #pragma GCC visibility.  */
+  if (!DECL_VISIBILITY_SPECIFIED (decl))
+    {
+      DECL_VISIBILITY (decl) = default_visibility;
+      DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
+    }
   return false;
 }
 
@@ -5376,15 +5408,15 @@ check_function_sentinel (tree attrs, tree params, tree typelist)
        typelist = TREE_CHAIN (typelist);
        params = TREE_CHAIN (params);
       }
-      
+
       if (typelist || !params)
        warning (OPT_Wformat,
                 "not enough variable arguments to fit a sentinel");
       else
-        {
+       {
          tree sentinel, end;
          unsigned pos = 0;
-         
+
          if (TREE_VALUE (attr))
            {
              tree p = TREE_VALUE (TREE_VALUE (attr));
@@ -5421,7 +5453,7 @@ check_function_sentinel (tree attrs, tree params, tree typelist)
                 as wide as a pointer, and we don't want to force
                 users to cast the NULL they have written there.
                 We warn with -Wstrict-null-sentinel, though.  */
-              && (warn_strict_null_sentinel
+             && (warn_strict_null_sentinel
                  || null_node != TREE_VALUE (sentinel)))
            warning (OPT_Wformat, "missing sentinel in function call");
        }
@@ -5585,24 +5617,24 @@ handle_sentinel_attribute (tree *node, tree name, tree args,
        params = TREE_CHAIN (params);
 
       if (VOID_TYPE_P (TREE_VALUE (params)))
-        {
+       {
          warning (OPT_Wattributes,
                   "%qE attribute only applies to variadic functions", name);
          *no_add_attrs = true;
        }
     }
-  
+
   if (args)
     {
       tree position = TREE_VALUE (args);
 
       if (TREE_CODE (position) != INTEGER_CST)
-        {
+       {
          warning (0, "requested position is not an integer constant");
          *no_add_attrs = true;
        }
       else
-        {
+       {
          if (tree_int_cst_lt (position, integer_zero_node))
            {
              warning (0, "requested position is less than zero");
@@ -5610,7 +5642,7 @@ handle_sentinel_attribute (tree *node, tree name, tree args,
            }
        }
     }
-  
+
   return NULL_TREE;
 }
 \f
@@ -5815,9 +5847,9 @@ c_parse_error (const char *gmsgid, enum cpp_ttype token, tree value)
       unsigned int val = TREE_INT_CST_LOW (value);
       const char *const ell = (token == CPP_CHAR) ? "" : "L";
       if (val <= UCHAR_MAX && ISGRAPH (val))
-        message = catenate_messages (gmsgid, " before %s'%c'");
+       message = catenate_messages (gmsgid, " before %s'%c'");
       else
-        message = catenate_messages (gmsgid, " before %s'\\x%x'");
+       message = catenate_messages (gmsgid, " before %s'\\x%x'");
 
       error (message, ell, val);
       free (message);
@@ -5853,7 +5885,7 @@ c_parse_error (const char *gmsgid, enum cpp_ttype token, tree value)
       error (message);
       free (message);
     }
-#undef catenate_messages  
+#undef catenate_messages
 }
 
 /* Walk a gimplified function and warn for functions whose return value is
@@ -5951,26 +5983,44 @@ c_common_to_target_charset (HOST_WIDE_INT c)
 }
 
 /* Build the result of __builtin_offsetof.  EXPR is a nested sequence of
-   component references, with an INDIRECT_REF at the bottom; much like
-   the traditional rendering of offsetof as a macro.  Returns the folded
-   and properly cast result.  */
+   component references, with STOP_REF, or alternatively an INDIRECT_REF of
+   NULL, at the bottom; much like the traditional rendering of offsetof as a
+   macro.  Returns the folded and properly cast result.  */
 
 static tree
-fold_offsetof_1 (tree expr)
+fold_offsetof_1 (tree expr, tree stop_ref)
 {
   enum tree_code code = PLUS_EXPR;
   tree base, off, t;
 
+  if (expr == stop_ref && TREE_CODE (expr) != ERROR_MARK)
+    return size_zero_node;
+
   switch (TREE_CODE (expr))
     {
     case ERROR_MARK:
       return expr;
 
-    case INDIRECT_REF:
+    case VAR_DECL:
+      error ("cannot apply %<offsetof%> to static data member %qD", expr);
+      return error_mark_node;
+
+    case CALL_EXPR:
+      error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
+      return error_mark_node;
+
+    case INTEGER_CST:
+      gcc_assert (integer_zerop (expr));
       return size_zero_node;
 
+    case NOP_EXPR:
+    case INDIRECT_REF:
+      base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
+      gcc_assert (base == error_mark_node || base == size_zero_node);
+      return base;
+
     case COMPONENT_REF:
-      base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
+      base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
       if (base == error_mark_node)
        return base;
 
@@ -5987,7 +6037,7 @@ fold_offsetof_1 (tree expr)
       break;
 
     case ARRAY_REF:
-      base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
+      base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
       if (base == error_mark_node)
        return base;
 
@@ -6009,10 +6059,10 @@ fold_offsetof_1 (tree expr)
 }
 
 tree
-fold_offsetof (tree expr)
+fold_offsetof (tree expr, tree stop_ref)
 {
   /* Convert back from the internal sizetype to size_t.  */
-  return convert (size_type_node, fold_offsetof_1 (expr));
+  return convert (size_type_node, fold_offsetof_1 (expr, stop_ref));
 }
 
 /* Print an error message for an invalid lvalue.  USE says
@@ -6185,7 +6235,7 @@ sync_resolve_size (tree function, tree params)
   return 0;
 }
 
-/* A helper function for resolve_overloaded_builtin.  Adds casts to 
+/* A helper function for resolve_overloaded_builtin.  Adds casts to
    PARAMS to make arguments match up with those of FUNCTION.  Drops
    the variadic arguments at the end.  Returns false if some error
    was encountered; true on success.  */
@@ -6238,7 +6288,7 @@ sync_resolve_params (tree orig_function, tree function, tree params)
   return true;
 }
 
-/* A helper function for resolve_overloaded_builtin.  Adds a cast to 
+/* A helper function for resolve_overloaded_builtin.  Adds a cast to
    RESULT to make it match the type of the first pointer argument in
    PARAMS.  */
 
@@ -6269,13 +6319,13 @@ resolve_overloaded_builtin (tree function, tree params)
       break;
     case BUILT_IN_MD:
       if (targetm.resolve_overloaded_builtin)
-        return targetm.resolve_overloaded_builtin (function, params);
+       return targetm.resolve_overloaded_builtin (function, params);
       else
-        return NULL_TREE;
+       return NULL_TREE;
     default:
       return NULL_TREE;
     }
-    
+
   /* Handle BUILT_IN_NORMAL here.  */
   switch (orig_code)
     {