Menu

writing cppcheck configuration files for class member functions

COLIN VOSS
2020-01-29
2020-01-31
  • COLIN VOSS

    COLIN VOSS - 2020-01-29

    Hi all,

    I'm just starting to use cppcheck and am writing a configuration file to enable checking of various library functions. The ones for standard functions seem to work fine, but the ones for class member functions dont seem to work. Is there anything special I have to do for classes (do I need t?

    Here's an example of a part that doesn't work (calls to TestFunct that do not use the return value or use a bool for the arg. are NOT detected).

      <!-- int CAAA::TestFunct( int Inval ); -->
      <function name="CAAA::TestFunct">
        <use-retval/>
        <returnValue type="int"/>
        <noreturn>false</noreturn>
        <arg nr="1" direction="in">
          <not-bool/>
        </arg>
      </function>
    

    Whereas this works fine (calls to TestFunct2 that do not use the return value or use a bool for the arg. are ARE detected)

      <!-- int TestFunct2( int Inval ); -->
      <function name="TestFunct2">
        <use-retval/>
        <returnValue type="int"/>
        <noreturn>false</noreturn>
        <arg nr="1" direction="in">
          <not-bool/>
        </arg>
      </function>
    

    Thanks in advance,

    Colin

     

    Last edit: COLIN VOSS 2020-01-29
  • Daniel Marjamäki

    Your configuration looks fine.

    One possible thing could be that a configuration is not used when the implementation is seen. Normally standard headers are not included and if there is a function like open that has implementation.. Cppcheck will assume that it is not the standard function but your own function.

     
  • COLIN VOSS

    COLIN VOSS - 2020-01-30

    Hi there & thanks for the reply.

    To give more information - both of those function checks are in the same cfg file (libCx) and loaded via --library=libCx so the cfg is being used, and the source code has calls to the two functions to be checked on consecutive lines..
    ....yet the standard function is being checked, the member function isn't.

    The original function being checked was...

    <!-- ITC_Status_t CItcLib::ReadSSS_Data( SSS_ID_t SSS_ID, int *DataSize, void *pData ); -->

    ...so nothing like any std function (I created CAAA::TestFunct so I could easily move it around to try and get a class member function check working. I've also tried it in a unique namespace too).

    So there is nothing special about where the instance of the class lives (..if it is available via extern etc...)?

     

    Last edit: COLIN VOSS 2020-01-30
  • Daniel Marjamäki

    Not sure what the problem is. I have tried your configuration and it seems to work;

    foo.cpp

    void f(CAAA *c) {
        c->TestFunct(c != 0);
    }
    

    foo.cfg

    <?xml version="1.0"?>
    <def format="2">
      <function name="CAAA::TestFunct">
        <use-retval/>
        <returnValue type="int"/>
        <noreturn>false</noreturn>
        <arg nr="1" direction="in">
          <not-bool/>
        </arg>
      </function>
    </def>
    

    Output:

    $ ./cppcheck --library=foo foo.cpp
    Checking foo.cpp ...
    foo.cpp:3:20: error: Invalid TestFunct() argument nr 1. A non-boolean value is required. [invalidFunctionArgBool]
        c->TestFunct(c != 0);
                       ^
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.