[comp.lang.c++] cfront 1.2 bug

dld@F.GP.CS.CMU.EDU (David Detlefs) (04/13/89)

I think the following is a bug in cfront 1.2.  The C++ program

----------------------------------------------------------------------
// This may look like C code, but it is really -*- C++ -*-

typedef unsigned int u_int;

struct foo {
  u_int bar : 2;
};

#include <stream.h>

void main() {
  foo f;
  f.bar = 2;
  cout << "f.bar is " << f.bar << ".\n";
}
----------------------------------------------------------------------

Translates to a ..c file that starts with

----------------------------------------------------------------------
/* <<cfront 1.2.1 2/16/87>> */
/* < bitfield-bug.c */
char *_new(); char _delete(); char *_vec_new(); char _vec_delete();


typedef unsigned int u_int ;

struct foo {	/* sizeof foo == 4 */

int _foo_bar :2;
};

// ...
----------------------------------------------------------------------

Note the "int" type of the "bar" field, instead of "unsigned int" or
"u_int".  When I finish compiling and linking this program on a vax,
it produces the result

f.bar is -2

which is obviously confusing.  Using "unsigned" in the original
declaration of foo instead of "u_int" is a workaround.  Just thought
I'd post this so that whoever is working on 2.0 can make sure this bug
does not survive in that version.
--
Dave Detlefs			Any correlation between my employer's opinion
Carnegie-Mellon CS		and my own is statistical rather than causal,
dld@cs.cmu.edu			except in those cases where I have helped to
				form my employer's opinion.  (Null disclaimer.)
--