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;
}

Reply via email to