Warn if another typedef'd type name is used in the argument list

Consider a large project where many typedef 'd types, for example

 typedef int age; typedef int height; 

and some functions that take arguments of these types:

 void printPerson(age a, height h) { printf("Age %d, Height %d\n", a, h); } 

Is there a way to warn at compile time if these arguments are of the wrong type, for example.

 age a = 30; height h = 180; printPerson(h, a); /* No warning, because a and h are both integers */ 

Is there a gcc way (or some kind of static code analysis tool) in such cases?

+7
c gcc
source share
6 answers

GCC has no built-in support.

There is a function request to add this, based on the Sparse nocast attribute. However, this has not been implemented. If you can use Sparse, you can do this by marking each typedef with __attribute__((nocast)) .

In C ++, you can do this by creating wrapper classes, not typedefs, and then simply not defining implicit conversions to them.

+6
source share

Klocwork contains some checks related to what they call "strong typing."

For your code, it returns STRONG.TYPE.ASSIGN.ARG, because the argument types do not match.

He also complains about assigning int (const) values ​​to age and height typed variables and about using variables like int in printf .

I heard that it is quite expensive.

+2
source share

As was clear from the other answers, you will not get this for free from gcc. You are definitely entering the world of static analysis tools to solve this problem.

There were several suggestions for this, some of which require additional annotation, some of which may not be larger than what you are looking for. So I thought that I would throw another mix ...

For a long time for me there have been various command line command line tools. In your case, I think PC-lint / flexelint works very well, although it is a commercial tool. See here for checking its strong type.

+2
source share

No, it can not be. The compiler will warn you if you are doing (or going to do) something illegal. It should not know (or determine the correctness) of the value that you will pass as a parameter to the function. As long as the types are the same, he has no reason to complain.

However, in case of type mismatch, he will warn you.

0
source share

An error will be generated only when the types are different. you can wrap these types inside a structure and use macros to automate the definition and assignment.

If you want to use an enumeration instead of integers, then there is an option for warnings about using mixed enumerations in a static code analysis tool called coverty.

https://wiki.ubuntu.com/CoverityCheckerDictionary find MIXED_ENUMS.

0
source share

As others have said, C. does not have support for this. If you absolutely need type checking, you can do the following:

 typedef struct {int a;} age; typedef struct {int h;} height; void printPerson(age a, height h) { printf("Age %d, height %d\n", aa, hh); } age a = {30}; height h = {180}; printPerson(h, a); // will generate errors 

Remember that this can affect performance.

0
source share

All Articles