Compiler warnings in Python’s SRE

After a long time, and many people complaining about it, I finally took some time to fix some annoying compiler warnings in Python’s regular expression engine. Since it’s a rather uncommon case, I’ll explain it here with a quick example.

Have a look at the following code:

#include <stdio.h>

int main(int argc, char *argv[])
{
   unsigned char c = 1;
   if (c < 256)
      printf("Hello world!n");
   return 0;
}

If you try to compile that code, you'll probably get a warning like this:

test.c: In function `main':
test.c:7: warning: comparison is always true due to
limited range of data type

Yes, the compiler is right. Our char type will never reach the given limit. On the other hand, suppose that this code is preprocessed, and that the c variable has sometimes a multibyte character type, like wchar_t, for example. In this case our test is legitimate, and the dozens of warnings being caused by a common macro are really annoying.

There are many different ways to remove these warnings. Unfortunately, the most obvious one, which is casting the variable to a larger type, doesn't work as expected.

My adopted solution was to reimplement the same test in another way, surpassing the compiler warnings for this specific case. Instead of writing (c < 255), I've written (!(c & ~255)). This is 100% safe for any unsigned type, which is my case. Ok, perhaps it's a little bit sick, but a large comment should leave everyone aware about the rationale, and away from the warnings.

This entry was posted in C/C++, Patch, Python. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *