Menu

How to add library configuration for methods?

versat
2017-11-28
2017-12-06
  • versat

    versat - 2017-11-28

    Hi,
    i was trying to add library configuration for my methods, but cppcheck always says that there is no configuration.
    I really don't know what is wrong, maybe just a stupid error.
    In the manual it says:
    "For functions in namespaces or classes, just provide
    their fully qualified name. For example: <function name="memcpy,std::memcpy">"
    My understanding is to add a configuration for <function name="foo::bar"> if i have a method called bar in a class foo.
    Here is a reduced example:

    >"C:\Program Files\Cppcheck\cppcheck" --version
    Cppcheck 1.81
    
    >"C:\Program Files\Cppcheck\cppcheck" --enable=all --check-library --platform=win32W --library=windows --library=no_func_config --debug "no_func_config.cpp"
    Checking no_func_config.cpp ...
    [no_func_config.cpp:15]: (debug) valueflow.cpp:3099:valueFlowFunctionReturn bailout: function return; nontrivial function body
    [no_func_config.cpp:18]: (information) --check-library: There is no matching configuration for function foo::bar()
    [no_func_config.cpp:19]: (information) --check-library: There is no matching configuration for function Sleep()
    
    ##file no_func_config.cpp
    4: class foo
    5: {
    6: public:
    7: void bar ( int a@1 ) ;
    8: } ;
    9:
    10: void foo :: bar ( int a@2 )
    11: {
    12: printf ( "%d" , a@2 ) ;
    13: }
    14:
    15: int main ( )
    16: {
    17: foo test@3 ;
    18: test@3 . bar ( 42 ) ;
    19: Sleep ( 2000 ) ;
    20: return 1 ;
    21: }
    
    ##Value flow
    Line 12
      "%d" always "%d"
      a possible 42
    Line 18
      42 always 42
    Line 19
      2000 always 2000
    Line 20
      1 always 1
    (information) Cppcheck cannot find all the include files (use --check-config for details)
    
    >type no_func_config.cpp
    #include <windows.h>
    #include <cstdio>
    
    class foo
    {
    public:
        void bar(int a);
    };
    
    void foo::bar(int a)
    {
        printf("%d", a);
    }
    
    int main()
    {
        foo test;
        test.bar(42);
        Sleep(2000);
        return 1;
    }
    
    >type no_func_config.cfg
    <?xml version="1.0"?>
    <def format="2">
      <function name="foo::bar">
        <noreturn>false</noreturn>
        <leak-ignore/>
        <arg nr="1">
          <not-uninit/>
        </arg>
      </function>
    </def>
    
     

    Last edit: versat 2017-11-28
  • Daniel Marjamäki

    I see nothing wrong with your configuration.

    I believe we have tweaked this since 1.81 was released. The --check-library was tweaked so it will match the actual library behaviour better. I don't see those warning messages with latest git head.

    as far as I remember we currently skip library configuration if the function implementation is seen. To avoid that the wrong configuration is used. For instance if you define your own local exit() function then you don't want that the exit configuration in std.cfg is used for that. maybe we could add some element/attribute so cppcheck will know that it is expected that the implementation is seen. Feel free to create a ticket about that.

     
  • versat

    versat - 2017-12-06

    You are right, with the latest git head i do not get the --check-library message for the configured method any longer.
    When changing the configuration a bit so it only allows specific values and the method is called with an invalid one, there is no warning message (because the method definition is seen and the library configuration ignored).
    This is indeed not what i expected:

    >"E:\Files\GitHub\cppcheck_versat\cppcheck\bin\debug\cppcheck" --version
    Cppcheck 1.82 dev
    
    >"E:\Files\GitHub\cppcheck_versat\cppcheck\bin\debug\cppcheck" --enable=all --check-library --platform=win32W --library=windows --library=no_func_config --debug "no_func_config.cpp"
    Checking no_func_config.cpp ...
    [no_func_config.cpp:15]: (debug) valueflow.cpp:3128:valueFlowFunctionReturn bailout: function return; nontrivial function body
    
    ##file no_func_config.cpp
    4: class foo
    5: {
    6: public:
    7: void bar ( int a@1 ) ;
    8: } ;
    9:
    10: void foo :: bar ( int a@2 )
    11: {
    12: printf ( "%d" , a@2 ) ;
    13: }
    14:
    15: int main ( )
    16: {
    17: foo test@3 ;
    18: test@3 . bar ( 42 ) ;
    19: Sleep ( 2000 ) ;
    20: return 1 ;
    21: }
    
    ##Value flow
    Line 12
      "%d" always "%d"
      a possible 42
    Line 18
      42 always 42
    Line 19
      2000 always 2000
    Line 20
      1 always 1
    (information) Cppcheck cannot find all the include files (use --check-config for details)
    
    >type no_func_config.cpp
    #include <windows.h>
    #include <cstdio>
    
    class foo
    {
    public:
        void bar(int a);
    };
    
    void foo::bar(int a)
    {
        printf("%d", a);
    }
    
    int main()
    {
        foo test;
        test.bar(42);
        Sleep(2000);
        return 1;
    }
    
    >type no_func_config.cfg
    <?xml version="1.0"?>
    <def format="2">
      <function name="foo::bar">
        <noreturn>false</noreturn>
        <leak-ignore/>
        <arg nr="1">
          <not-uninit/>
          <valid>0,1</valid>
        </arg>
      </function>
    </def>
    

    At least some (debug) message that the library configuration is ignored because the implementation is seen would be great.
    Or maybe an inconclusive warning that the parameter value could be invalid according to the library config, with a hint that the implementation is seen and the configuration may be not suitible.
    I will see if i can create a useful ticket.

     
  • versat

    versat - 2017-12-06

    I created the following ticket:
    https://trac.cppcheck.net/ticket/8290

     

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.