]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR c/24293 (Undefined behaviour not diagnosed with -fsyntax-only)
authorPrathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
Wed, 20 Jan 2016 16:25:23 +0000 (16:25 +0000)
committerPrathamesh Kulkarni <prathamesh3492@gcc.gnu.org>
Wed, 20 Jan 2016 16:25:23 +0000 (16:25 +0000)
2016-01-15  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

        PR c/24293
        * c-tree.h (incomplete_record_decls): Declare.
        * c-parser.c (incomplete_record_decls): Define.
        (c_parser_translation_unit): Iterate through incomplete_record_decls and
        report error if any decl has zero size.
        * c-decl.c (finish_decl): Append static decl with incomplete struct/union
        or enum type to incomplete_record_decls.

testsuite/
        * gcc.dg/pr24293.c: New test.
        * gcc.dg/Wcxx-compat-8.c: Adjust to accept error due to
        incomplete struct type.
        * gcc.dg/declspec-1.c: Likewise.
        * gcc.dg/pr63549.c: Likewise.

From-SVN: r232622

gcc/c/ChangeLog
gcc/c/c-decl.c
gcc/c/c-parser.c
gcc/c/c-tree.h
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/Wcxx-compat-8.c
gcc/testsuite/gcc.dg/declspec-1.c
gcc/testsuite/gcc.dg/pr24293.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr63549.c

index d11a822b217d7761d4a5b9500451fff46bff87ae..697dd8dc806e9212f404f00a8a38bfc2be004773 100644 (file)
@@ -1,3 +1,13 @@
+2016-01-15  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>
+
+       PR c/24293
+       * c-tree.h (incomplete_record_decls): Declare.
+       * c-parser.c (incomplete_record_decls): Define.
+       (c_parser_translation_unit): Iterate through incomplete_record_decls and
+       report error if any decl has zero size.
+       * c-decl.c (finish_decl): Append static decl with incomplete struct/union
+       or enum type to incomplete_record_decls.
+
 2016-01-14  Tom de Vries  <tom@codesourcery.com>
 
        PR tree-optimization/68773
index 5830e22a7fc525fd8372c2d465f886799b7204ca..1ec60420db9c758c9f32cdf874ece697c7dc2c8b 100644 (file)
@@ -4791,6 +4791,12 @@ finish_decl (tree decl, location_t init_loc, tree init,
           TREE_TYPE (decl) = error_mark_node;
         }
 
+      if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
+         || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
+         && DECL_SIZE (decl) == NULL_TREE
+         && TREE_STATIC (decl))
+       incomplete_record_decls.safe_push (decl);
+
       if (is_global_var (decl) && DECL_SIZE (decl) != 0)
        {
          if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
index 919680aab47ca17d5411a7f19e17837875245302..efac47b62bbc1828c6b487225a8de53a9770ad37 100644 (file)
@@ -59,6 +59,15 @@ along with GCC; see the file COPYING3.  If not see
 #include "gimple-expr.h"
 #include "context.h"
 
+/* We need to walk over decls with incomplete struct/union/enum types
+   after parsing the whole translation unit.
+   In finish_decl(), if the decl is static, has incomplete
+   struct/union/enum type, it is appeneded to incomplete_record_decls.
+   In c_parser_translation_unit(), we iterate over incomplete_record_decls
+   and report error if any of the decls are still incomplete.  */ 
+
+vec<tree> incomplete_record_decls = vNULL;
+
 void
 set_c_expr_source_range (c_expr *expr,
                         location_t start, location_t finish)
@@ -1421,6 +1430,16 @@ c_parser_translation_unit (c_parser *parser)
        }
       while (c_parser_next_token_is_not (parser, CPP_EOF));
     }
+
+  for (unsigned i = 0; i < incomplete_record_decls.length (); ++i)
+    {
+      tree decl = incomplete_record_decls[i];
+      if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
+       {
+         error ("storage size of %q+D isn%'t known", decl);
+         TREE_TYPE (decl) = error_mark_node;
+       }
+    }
 }
 
 /* Parse an external declaration (C90 6.7, C99 6.9).
index 81a3d581ad8eb71a17a692d514d473457c33960b..cf79ba71b156632b81e6f4671b523e4ec7936e81 100644 (file)
@@ -731,4 +731,6 @@ set_c_expr_source_range (c_expr *expr,
 /* In c-fold.c */
 extern tree decl_constant_value_for_optimization (tree);
 
+extern vec<tree> incomplete_record_decls;
+
 #endif /* ! GCC_C_TREE_H */
index fb9826ecbdc3fb3d0d6ef647b505acea3218fe67..a2c9ba919dcdae76a048430577ab69bf8f1a7961 100644 (file)
@@ -1,3 +1,12 @@
+2016-01-20  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>
+
+       PR c/24293
+       * gcc.dg/pr24293.c: New test.
+       * gcc.dg/Wcxx-compat-8.c: Adjust to accept error due to
+       incomplete struct type.
+       * gcc.dg/declspec-1.c: Likewise.
+       * gcc.dg/pr63549.c: Likewise.
+
 2016-01-20  Jakub Jelinek  <jakub@redhat.com>
 
        PR tree-optimization/66612
index f7e8c557914625d909e5aa52b4419488be2989d8..4e9ddc1b3974488bb71c3b49764ebdd3715e9dbe 100644 (file)
@@ -33,6 +33,7 @@ enum e3
 
 __typeof__ (struct s5 { int i; }) v5; /* { dg-warning "invalid in C\[+\]\[+\]" } */
 __typeof__ (struct t5) w5; /* { dg-bogus "invalid in C\[+\]\[+\]" } */
+  /* { dg-error "storage size of 'w5' isn't known" "" { target *-*-* } 35 } */
 
 int
 f1 (struct s1 *p)
@@ -64,4 +65,4 @@ f5 ()
   return &((struct t8) { });  /* { dg-warning "invalid in C\[+\]\[+\]" } */
 }
 
-/* { dg-error "invalid use of undefined type" "" { target *-*-* } 64 } */
+/* { dg-error "invalid use of undefined type" "" { target *-*-* } 65 } */
index c19f1074ebb09d424abc21c9b496cbf3025441ab..b0246011dd0cc834ebecb82ae015992f9a7af50c 100644 (file)
@@ -9,13 +9,15 @@ typedef int t;
 /* These should all be diagnosed, but only once, not for every
    identifier declared.  */
 struct s0 int x0, /* { dg-error "two or more data types" } */
-x1;
+/* { dg-error "storage size of 'x0' isn't known" "" { target *-*-* } 11 } */
+x1; /* { dg-error "storage size of 'x1' isn't known" } */ 
 
 char union u0 x2, /* { dg-error "two or more data types" } */
 x3;
 
 enum e0 struct s1 x4, /* { dg-error "two or more data types" } */
-x5;
+ /* { dg-error "storage size of 'x4' isn't known" "" { target *-*-* } 18 } */
+x5; /* { dg-error "storage size of 'x5' isn't known" } */
 
 short short x6, /* { dg-error "duplicate" } */
 x7;
diff --git a/gcc/testsuite/gcc.dg/pr24293.c b/gcc/testsuite/gcc.dg/pr24293.c
new file mode 100644 (file)
index 0000000..5bf7ad1
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-fsyntax-only" } */
+
+static struct foo x;  /* { dg-error "storage size of 'x' isn't known" } */
+static union bar y;  /* { dg-error "storage size of 'y' isn't known" } */
+
+typedef struct P p;
+static p p_obj;  /* { dg-error "storage size of 'p_obj' isn't known" } */
+
+static enum e e_var; /* { dg-error "storage size of 'e_var' isn't known" } */
+
+extern struct undefined_object object;
index c9b1718f0cc3d6d4ab0011139c3373f5b2f039f2..bd0b70672679be6d7cce6f6fee3654e01336f6ac 100644 (file)
@@ -2,6 +2,6 @@
 /* { dg-do compile } */
 /* { dg-options "" } */
 
-enum E e;
+enum E e;  /* { dg-error "storage size of 'e' isn't known" } */
 int a[10];
 int i = a[e]; /* { dg-error "has an incomplete type" } */