Warn if another typedef'd name of a type is used in an argument list

后端 未结 6 1498
天涯浪人
天涯浪人 2021-01-02 09:46

Consider a large project, where many types are typedef\'d, e.g.

typedef int age;
typedef int height;

and some functions gettin

相关标签:
6条回答
  • 2021-01-02 10:17

    Error will be generated only when types are different. can you wrap these types inside struct and use macros to automate definition and assignment.

    If you are willing to use enum instead of integers then there is an option for warnings on use of mixed enums in static code analysis tool named coverity.

    https://wiki.ubuntu.com/CoverityCheckerDictionary look for MIXED_ENUMS.

    0 讨论(0)
  • 2021-01-02 10:18

    There is no built-in support for this in GCC.

    There is a feature request to add this, based on the Sparse nocast attribute. However, this hasn't been implemented. If you can use Sparse, though, you could do this by marking each typedef with __attribute__((nocast)).

    In C++ you can do this by making wrapper classes rather than typedefs, and then simply not defining implicit conversions for them.

    0 讨论(0)
  • 2021-01-02 10:21

    As others already stated, there is no support for this in C. If you absolutely want strong type checking to happen, you could do like this:

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

    Beware that this might have some performance impact, though.

    0 讨论(0)
  • 2021-01-02 10:25

    No, there cannot be. A compiler will warn you if you're doing (or about to do) something illegal. It is not supposed to know (or determine the correctness of) the values which you will be passing as function parameter. As long as the types are same, it does not have any reason to complain.

    However, in case of type mismatch, it will alert you.

    0 讨论(0)
  • 2021-01-02 10:34

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

    For your code it throws STRONG.TYPE.ASSIGN.ARG because argument types do not match.

    It also complains about assigning int values (the consts) to age and height typed variables and about using the variables as int in printf.

    I heard it is quite expensive, though.

    0 讨论(0)
  • 2021-01-02 10:36

    As has been made clear by the other responses, you're not going to get this for free from gcc. You are definitely into the world of static analysis tools to solve this.

    There have been several suggestions for this, some of which require extra annotation, some of which don't but may be more than you're looking for. I therefore thought I'd throw one more into the mix...

    A long stand by for me has been the various command line lint tools. In your case, I think PC-lint/flexelint fits very well even though it is a commercial tool. See here for its strong type checking.

    0 讨论(0)
提交回复
热议问题