public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "iains at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug objc/103639] [11/12 Regression] switch case with break in fast enumeration loop generates wrong code
Date: Fri, 10 Dec 2021 09:52:41 +0000	[thread overview]
Message-ID: <bug-103639-4-hBds6wvGUd@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-103639-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103639

--- Comment #8 from Iain Sandoe <iains at gcc dot gnu.org> ---
Here's a version using the testsuite object etc. it does not need Foundation
(it needs CoreFoundation on Darwin for NSString, but only libobjc on Linux)

the issue repeats equally with gnu-runtime on Linux.

gcc pr103639.m -I path/to/gcc-sources/gcc/testsuite/objc-obj-c++-shared -o t
-lobjc

=====

#import "TestsuiteObject.m"
#ifndef __NEXT_RUNTIME__
#include <objc/NXConstStr.h>
#else
#include "nsconstantstring-class.h"
#endif

extern int printf (const char *, ...);
#include <stdlib.h>

/* A mini-array implementation that can be used to test fast
    enumeration.  You create the array with some objects; you can
    mutate the array, and you can fast-enumerate it.
 */
@interface MyArray : TestsuiteObject
{
  unsigned int length;
  id *objects;
  unsigned long mutated;
}
- (id) initWithLength: (unsigned int)l  objects: (id *)o;
- (void) mutate;
- (unsigned long)countByEnumeratingWithState: (struct
__objcFastEnumerationState *)state
                                     objects:(id *)stackbuf 
                                       count:(unsigned long)len;
@end

@implementation MyArray : TestsuiteObject
- (id) initWithLength: (unsigned int)l
              objects: (id *)o
{
  length = l;
  objects = o;
  mutated = 0;
  return self;
}
- (void) mutate
{
  mutated = 1;
}
- (unsigned long)countByEnumeratingWithState: (struct
__objcFastEnumerationState*)state 
                                     objects: (id*)stackbuf
                                       count: (unsigned long)len
{
  unsigned long i, batch_size;

  /* We keep how many objects we served in the state->state counter.  So the
next batch
     will contain up to length - state->state objects.  */
  batch_size = length - state->state;

  /* Make obvious adjustments.  */
  if (batch_size < 0)
    batch_size = 0;

  if (batch_size > len)
    batch_size = len;

  /* Copy the objects.  */
  for (i = 0; i < batch_size; i++)
    stackbuf[i] = objects[i];

  state->state += batch_size;
  state->itemsPtr = stackbuf;
  state->mutationsPtr = &mutated;

  return batch_size;
}
@end

int
main()
{
  id *objects = malloc (sizeof (id) * 2);
  objects[0] = @"a";
  objects[1] = @"b";

  MyArray *array = [[MyArray alloc] initWithLength: 2 objects: objects];

// NSArray *array = [NSArray arrayWithObjects: @"a", @"b", nil];
  int someVar = 0;
  for (id object in array) {
    switch (someVar) {
      case 0:
        printf ("case 0: %s\n", [object cString]);
        break;
    }
    printf ("foo\n");
  }

  return 0;
}

  parent reply	other threads:[~2021-12-10  9:52 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-09 20:59 [Bug objc/103639] New: [REGRESSION] GCC 11.2 (or even earlier) breaks switch case with break in fast enumeration loop js-gcc at webkeks dot org
2021-12-09 21:05 ` [Bug objc/103639] " js-gcc at webkeks dot org
2021-12-09 21:11 ` js-gcc at webkeks dot org
2021-12-09 21:42 ` pinskia at gcc dot gnu.org
2021-12-09 21:44 ` js-gcc at webkeks dot org
2021-12-10  1:40 ` egallager at gcc dot gnu.org
2021-12-10  8:17 ` iains at gcc dot gnu.org
2021-12-10  8:41 ` [Bug objc/103639] [11/12 Regression] switch case with break in fast enumeration loop generates wrong code iains at gcc dot gnu.org
2021-12-10  9:52 ` iains at gcc dot gnu.org [this message]
2021-12-10 20:43 ` pinskia at gcc dot gnu.org
2021-12-29 15:19 ` jakub at gcc dot gnu.org
2021-12-29 15:31 ` jakub at gcc dot gnu.org
2021-12-29 15:36 ` iains at gcc dot gnu.org
2021-12-29 16:22 ` jakub at gcc dot gnu.org
2021-12-29 19:59 ` iains at gcc dot gnu.org
2021-12-29 20:29 ` iains at gcc dot gnu.org
2021-12-29 20:30 ` jakub at gcc dot gnu.org
2021-12-29 20:35 ` iains at gcc dot gnu.org
2022-01-01  5:32 ` cvs-commit at gcc dot gnu.org
2022-01-04  9:11 ` [Bug objc/103639] [11 " rguenth at gcc dot gnu.org
2022-01-24  9:21 ` cvs-commit at gcc dot gnu.org
2022-01-24  9:29 ` jakub at gcc dot gnu.org
2022-01-24  9:31 ` jakub at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-103639-4-hBds6wvGUd@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).