Menu

Problems configuring std::string::compare

versat
2018-01-11
2018-04-13
  • versat

    versat - 2018-01-11

    From this ticket https://trac.cppcheck.net/ticket/8084 i see that it is currently not possible to configure overloaded functions.
    I tried it nonetheless. But with a configuration like this only the last function configuration seems to be used:

      <!--int compare (const string& str) const;-->
      <!--int compare (const char* s) const;-->
      <function name="std::string::compare">
        <use-retval/>
        <returnValue type="int"/>
        <noreturn>false</noreturn>
        <leak-ignore/>
        <arg nr="1">
          <not-uninit/>
          <not-null/>
          <not-bool/>
        </arg>
      </function>
      <!--int compare (size_t pos, size_t len, const string& str) const;-->
      <!--int compare (size_t pos, size_t len, const char* s) const;-->
      <function name="std::string::compare">
        <use-retval/>
        <returnValue type="int"/>
        <noreturn>false</noreturn>
        <leak-ignore/>
        <arg nr="1">
          <not-uninit/>
          <not-bool/>
        </arg>
        <arg nr="2">
          <not-uninit/>
          <not-bool/>
        </arg>
        <arg nr="3">
          <not-uninit/>
          <not-null/>
          <not-bool/>
        </arg>
      </function>
      <!--int compare (size_t pos, size_t len, const char* s, size_t n) const;-->
      <!--Since C++14: int compare (size_t pos, size_t len, const string& str,
                 size_t subpos, size_t sublen = npos) const;-->
      <function name="std::string::compare">
        <use-retval/>
        <returnValue type="int"/>
        <noreturn>false</noreturn>
        <leak-ignore/>
        <arg nr="1">
          <not-uninit/>
          <not-bool/>
        </arg>
        <arg nr="2">
          <not-uninit/>
          <not-bool/>
        </arg>
        <arg nr="3">
          <not-uninit/>
          <not-null/>
          <not-bool/>
        </arg>
        <arg nr="4">
          <not-uninit/>
        </arg>
      </function>
      <!--int compare (size_t pos, size_t len, const string& str,
                 size_t subpos, size_t sublen) const;-->
      <function name="std::string::compare">
        <use-retval/>
        <returnValue type="int"/>
        <noreturn>false</noreturn>
        <leak-ignore/>
        <arg nr="1">
          <not-uninit/>
          <not-bool/>
        </arg>
        <arg nr="2">
          <not-uninit/>
          <not-bool/>
        </arg>
        <arg nr="3">
          <not-uninit/>
          <not-bool/>
        </arg>
        <arg nr="4">
          <not-uninit/>
          <not-bool/>
        </arg>
        <arg nr="5">
          <not-uninit/>
          <not-bool/>
        </arg>
      </function>
    

    I expected this to not work so i thought i try a minimal approach:

      <!--int compare (const string& str) const;-->
      <!--int compare (const char* s) const;-->
      <!--int compare (size_t pos, size_t len, const string& str) const;-->
      <!--int compare (size_t pos, size_t len, const char* s) const;-->
      <!--int compare (size_t pos, size_t len, const char* s, size_t n) const;-->
      <!--Since C++14: int compare (size_t pos, size_t len, const string& str,
                 size_t subpos, size_t sublen = npos) const;-->
      <!--int compare (size_t pos, size_t len, const string& str,
                 size_t subpos, size_t sublen) const;-->
      <function name="std::string::compare">
        <use-retval/>
        <returnValue type="int"/>
        <noreturn>false</noreturn>
        <leak-ignore/>
        <arg nr="any">
          <not-uninit/>
          <not-null/>
          <not-bool/>
        </arg>
      </function>
    

    cppcheck does not complain that the function is not configured, but seems to ignore the configuration somehow.
    I thought "any" means any number/count of parameters, but it seems to work in a different way.
    There is no error message that the return value is not used for code like this:

        std::string s1;
        std::string s2 = "test";
        s1.compare(s2);
    

    When i add an entry like

      <arg nr="1"/>
    

    the error is found.
    But for code like

        s1.compare(0, 2, s2);
    

    i have to add

      <arg nr="2"/>
      <arg nr="3"/>
    

    but then the error above is no longer found (which makes sense).

    I'm not sure if i am missing something or if this is simply not possible at the moment.

     
  • Daniel Marjamäki

    I'm not sure if i am missing something or if this is simply not possible at the moment.

    I think this is a limit right now.

    we should add a way to configure overloaded methods properly.

     
    • Markus Elfring

      Markus Elfring - 2018-04-13

      I am also curious on how overloaded functions will be properly handled by this software.
      Would you like to reuse any approaches from other information sources?

       

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.