Menu

Support for strong check with typedef ?

2024-06-17
2024-06-18
  • Robert Morin

    Robert Morin - 2024-06-17

    Hi,

    is there an option with cppcheck to catch wrong typedef in parameters ?

    See the example here, ConvertMeter() and ConvertFeet() with the wrong parameter type.

    Thanks.

    typedef float Meter;
    typedef float Feet;
    
    Feet ConvertMeter( Meter a )
       {
       return a * 3.2807;
       }
    
    Meter ConvertFeet( Feet a )
      {
      return a / 3.2807;
      }
    
    
    void TestThis(void)
      {
      Feet a = 1.0;
      Meter b = 1.0;
    
      ConvertMeter( a );
      ConvertFeet( b );
      }
    
     
  • CHR

    CHR - 2024-06-17

    Such a check would be very noisy when types and typedefs are used interchangeably.
    If the code was C++, you could do this to get comilation errors:
    https://godbolt.org/z/PqrozxKG9

     
    • Robert Morin

      Robert Morin - 2024-06-17

      Hi,

      I saw this solution in StackOverflow but with 25+ years old code base with 500+ KLOC (C/C++), it's not my favorite solution.

      This is why I am looking for something only for the function parameters. This would reduce the scope to something maybe more manageable.

       
  • Daniel Marjamäki

    I have sympathy for this request. Yes it could probably be noisy for normal regular C/C++ code.

    I would like to see a "strong type checking" addon in cppcheck.

    I wonder if https://github.com/unl-nimbus-lab/phriky-units would catch this problem. However unfortunately that does not seem to be maintained.

     

Log in to post a comment.