NO

Author Topic: -w2 warnings divulge undesirable compiler behavior  (Read 2830 times)

severach

  • Guest
-w2 warnings divulge undesirable compiler behavior
« on: February 04, 2006, 08:40:52 am »
While -w2 finds a lot of odd cases that need fixing, it finds many more cases that are nothing but the compiler at work. Included is some nonsense code that demonstrates them. Three things that bother me:

1) I am not guilty of any warning in the code shown.

2) POCC is guilty of all shown warnings and is blaming me for them. The warnings divulge the inner workings of the compiler, constantly promoting and demoting. The best case is to emit instructions of the proper width so there's nothing to promote or demote but barring that, at least POCC could avoid blaming me for what it's doing which I can't change. Such warnings are unfixable and crying wolf makes similar warnings that need attention harder to find.

3) In some cases, signed casts are appearing where only unsigned values are present. Signed variables are of little use to me so I write almost entirely with unsigned variables. A signed intermediate acted upon would be catastrophic.

I like the fully functional visual debugger and Pelles & MSVCRT produce smaller executables than MinGW but I've too much testing ahead to ensure that the signs are being handled right.

Code: [Select]
#ifndef NELEM
#define NELEM(xyzzy) (sizeof(xyzzy)/sizeof(xyzzy[0]))
#endif

wchar_t arrayW[1];
wchar_t funcW(wchar_t wc) {
  return wc;
}
wchar_t func(int which) {
  wchar_t rv=which?funcW(L'A'):arrayW[0]; // warning #2215: Conversion from 'int' to 'unsigned short int'; possible loss of data.
  return rv;
}

USHORT usa,usb;
BOOL flagit(short int *flags,USHORT usx) {
  *flags |= 0x8; //warning #2215: Conversion from 'int' to 'short int'; possible loss of data.
  BOOL kscapital=(GetAsyncKeyState(VK_CAPITAL)&0x8000)?TRUE:FALSE; // warning #2215: Conversion from 'int' to 'short int'; possible loss of data.
  usa=usb=usx+1; // warning #2215: Conversion from 'int' to 'unsigned short int'; possible loss of data.
  return kscapital;
}

char forit(void) {
  WORD i;
  for(i=0; i<NELEM(arrayW); i++) // warning #2215: Conversion from 'int' to 'unsigned short int'; possible loss of data.
    funcW(arrayW[i]); // warning #2215: Conversion from 'int' to 'unsigned short int'; possible loss of data.
  return 'a';
}

void xxit(void) {
  char c=forit(); //warning #2215: Conversion from 'int' to 'char'; possible loss of data.
}

unsigned char flagchars(unsigned char *t) {
  unsigned char *u=NULL;
  unsigned char bits6=t-u; // warning #2215: Conversion from 'long int' to 'unsigned char'; possible loss of data.
  *t |= 0x3F; // warning #2215: Conversion from 'int' to 'unsigned char'; possible loss of data.
  *u |= *t; //warning #2215: Conversion from 'int' to 'unsigned char'; possible loss of data.
  return bits6;
}

// Why can't I do bitwise math on anything but 32 bit values?
// Why can't I use anything but a 32 bit value as a counter?
// Why are char and short int not handled by the registers of the proper width?
// Why is the result type of a ?: not the common result type of both items?
// Why is the type of a pointer difference long int?
// Why is the result of an assignment promoted to 32 bits?
// Why are implicit signed casts occuring where only unsigned variables are used?