]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR c/67784 (Incorrect parsing when using declarations in for loops and typedefs)
authorMarek Polacek <polacek@redhat.com>
Thu, 12 Nov 2015 21:07:04 +0000 (21:07 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Thu, 12 Nov 2015 21:07:04 +0000 (21:07 +0000)
PR c/67784
* c-parser.c (c_parser_for_statement): Reclassify the token in
a correct scope.

* gcc.dg/pr67784-1.c: New test.
* gcc.dg/pr67784-2.c: New test.

From-SVN: r230273

gcc/c/ChangeLog
gcc/c/c-parser.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr67784-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr67784-2.c [new file with mode: 0644]

index 87f6a2d30e3b9a80eb9049f65d2dee74c3b4224d..0191b455398197e579f1734dd4da634f5b1e3c4e 100644 (file)
@@ -1,3 +1,9 @@
+2015-11-12  Marek Polacek  <polacek@redhat.com>
+
+       PR c/67784
+       * c-parser.c (c_parser_for_statement): Reclassify the token in
+       a correct scope.
+
 2015-11-11  Marek Polacek  <polacek@redhat.com>
 
        PR c/68107
index 2484b920fad35a3a30f30954d282836ea88a5f78..89498254b7858725c001137aa7c25dd38a62499c 100644 (file)
@@ -5749,6 +5749,21 @@ c_parser_for_statement (c_parser *parser, bool ivdep)
     c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
 
+  /* We might need to reclassify any previously-lexed identifier, e.g.
+     when we've left a for loop with an if-statement without else in the
+     body - we might have used a wrong scope for the token.  See PR67784.  */
+  if (c_parser_next_token_is (parser, CPP_NAME))
+    {
+      c_token *token = c_parser_peek_token (parser);
+      tree decl = lookup_name (token->value);
+      if (decl == NULL_TREE)
+       ;
+      else if (TREE_CODE (decl) == TYPE_DECL)
+       token->id_kind = C_ID_TYPENAME;
+      else if (VAR_P (decl))
+       token->id_kind = C_ID_ID;
+    }
+
   token_indent_info next_tinfo
     = get_token_indent_info (c_parser_peek_token (parser));
   warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
index 680b9def71f319b5f73c695fb4550052bfe75604..ac1aa6c649842def9b07f08359222bb09b9dac15 100644 (file)
@@ -1,3 +1,9 @@
+2015-11-12  Marek Polacek  <polacek@redhat.com>
+
+       PR c/67784
+       * gcc.dg/pr67784-1.c: New test.
+       * gcc.dg/pr67784-2.c: New test.
+
 2015-11-12  Martin Liska  <mliska@suse.cz>
 
        * gcc.dg/ipa/pr68035.c: New test.
diff --git a/gcc/testsuite/gcc.dg/pr67784-1.c b/gcc/testsuite/gcc.dg/pr67784-1.c
new file mode 100644 (file)
index 0000000..d5e85fc
--- /dev/null
@@ -0,0 +1,54 @@
+/* PR c/67784 */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+typedef int T;
+
+void
+fn1 (void)
+{
+  for (int T;;)
+    if (1)
+      ;
+  T *x;
+}
+
+void
+fn2 (void)
+{
+  for (int T;;)
+    if (1)
+      T = 1;
+  T *x;
+}
+
+void
+fn3 (void)
+{
+  for (int T;;)
+    if (1)
+      {
+      }
+  T *x;
+}
+
+void
+fn4 (void)
+{
+  for (int T;;)
+    if (1)
+L:
+      ;
+  T *x;
+}
+
+void
+fn5 (void)
+{
+  for (int T;;)
+    if (1)
+      ;
+    else
+      ;
+  T *x;
+}
diff --git a/gcc/testsuite/gcc.dg/pr67784-2.c b/gcc/testsuite/gcc.dg/pr67784-2.c
new file mode 100644 (file)
index 0000000..de3b1c8
--- /dev/null
@@ -0,0 +1,54 @@
+/* PR c/67784 */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+int T;
+
+void
+fn1 (void)
+{
+  for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+    if (1)
+      ;
+  T *x; /* { dg-error "undeclared" } */
+}
+
+void
+fn2 (void)
+{
+  for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+    if (1)
+      T = 1; /* { dg-error "expected expression" } */
+  T *x; /* { dg-error "undeclared" } */
+}
+
+void
+fn3 (void)
+{
+  for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+    if (1)
+      {
+      }
+  T *x; /* { dg-error "undeclared" } */
+}
+
+void
+fn4 (void)
+{
+  for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+    if (1)
+L:
+      ;
+  T *x; /* { dg-error "undeclared" } */
+}
+
+void
+fn5 (void)
+{
+  for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+    if (1)
+      ;
+    else
+      ;
+  T *x; /* { dg-error "undeclared" } */
+}