1

I would like the c compiler to check the usage of simple types. My preferred usage would be checking typedefs, but that is not done even with -Wall and -Wextra:

#include <math.h>
#include <stdio.h>

typedef float deg;
typedef float rad;

float sinus( rad angle ) {
    return sin( angle );
}

int main() {
    deg a = 180;
    printf( "The sinus of %g is %g\n", a, sinus(a));
    return 0;
}

This compiles without a hitch.

So I'm considering using one-member structures:

#include <math.h>
#include <stdio.h>

typedef struct { float value; } deg;
typedef struct { float value; } rad;

float sinus( rad angle ) {
    return sin( angle.value );
}

int main() {
    deg a = {.value=180};
    printf( "The sinus of %g is %g\n", a, sinus(a));
    return 0;
}

This gives an outright error (as it should). However, I'm wondering about the performance. If I correct the above examples, the produced binaries have the same size, with optimization or without. However, the produced assembly differs. Can the compiler optimize out the struct, even in more complicated contexts? If not, is there another way to achieve type-safety for primitive types?

I'm using GCC (9.1.0), but I'd consider llvm if that helps. This is supposed to work for common desktop architectures, i.e. x86, x86_64, ARM.

Edit: I noticed you could also use union instead of struct in the above example. This again produces slightly different assembly code and again I'm not clear on the real world performance consequences.

3
  • This depends a lot on the ABI of the target architecture of your compiler. x86-64 is not the same as ARM-64 bits. This also depends upon your compilation options. With recent GCC 10, did you try to compile and link with gcc -Wall -Wextra -O3 -flto ? Commented Dec 28, 2020 at 7:19
  • I'm on GCC 9.1.0 where -Wextra does not help. The change log for GCC 10 does not mention warnings for typedefs. Commented Dec 28, 2020 at 7:26
  • If you email me, please give some budget. My opinion is that at least 50k€ is required, and perhaps 3x more. But I am on holidays right now and could be very wrong Commented Dec 28, 2020 at 7:40

1 Answer 1

2

I would like the c compiler to check the usage of simple types. My preferred usage would be checking typedefs, but that is not done

A possible approach with a recent GCC might be to add your extra __attribute__ on your typedefs (or #pragma-s) and to code your own GCC plugin doing the checks (perhaps using both type inference and abstract interpretation techniques)

Notice that the features you want are beyond the semantics of standard C, as defined in e.g. n2176.

If permitted (and funded), I might extend (in spring 2021) the Bismon static analyzer (described in this DRAFT report) towards your goals. See also CHARIOT and DECODER projects.

An alternative approach is to design your own programming language, and to write your compiler generating C code. Then ACM SIGPLAN conferences are relevant.

Or adapt Frama-C or the Clang static analyzer for your needs.

In all cases, budget several months of specialized software engineers labor. Be aware of Rice's theorem.

The checks you are dreaming of could be partly automated, but requires costly development.

Your question is related to dimensional analysis: a distance in meters divided by a duration in seconds gives a speed. And also to units (angles can be in radian and in degrees)

IIRC, some spacecraft has been lost because of such mistakes.

5
  • I realize this check is not required by standard C (though I don't understand why there shouldn't be at least a strict typedef for this functionality), but that's what compiler options are for, no? Commented Dec 28, 2020 at 7:30
  • Consider contributing to the C standardization process to push such features into future standards Commented Dec 28, 2020 at 7:31
  • How feasible is that for Joe C. Random? Commented Dec 28, 2020 at 7:32
  • It depends upon your education and your software engineering skills. Commented Dec 28, 2020 at 7:36
  • 2
    Or get someone already prominent in the standardization to promote this idea :D Commented Dec 28, 2020 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.