Menu

Checking does not proceed after macro definition

Martijn
2018-12-21
2019-01-03
  • Martijn

    Martijn - 2018-12-21

    Hi all,

    Using a very simple test application I noticed that Cppcheck seems to fail to check code after a macro has been used. More specifically, this seems to be an issue when there is no semicolon after the line refering to the macro.
    Assume I have some code that uses the USE_A_MACRO macro, defined in an external header called macrodef.h. This header is not part of the files being checked (assume it's in an external library). First of all, I would expect cppcheck to complain about a missing macro definition. I have seen this before, but it does not happen in this case. Worse, when checking the code below, cppcheck will completely skip checking the testfunc() function.
    Strangely enough, when I add a semicolon anywhere before the definition of testfunc() cppcheck seems to ignore the missing macro and checks testfunc() as well (without reporting anything about a missing definition).

    #include <iostream>
    #include "macrodef.h"
    
    USE_A_MACRO(test)
    
    void testfunc()
    {
        int k = 0;
        k = 1;
    }
    
    int main(int argc, char** argv)
    {
        int i = 0;
        i = 1;
        std::cout << "test this!\n";
        return 0;
    }
    

    I would think this is incorrect behavior, resulting in unpredictable code checking results. Would it be possible to look into this?

     
  • versat

    versat - 2019-01-03

    Related trac tickets i found:
    https://trac.cppcheck.net/ticket/6975
    https://trac.cppcheck.net/ticket/7902

    With the latest git HEAD i can reproduce the results:

    $ ./cppcheck --enable=all --inconclusive --debug --template=gcc macro.cpp
    Checking macro.cpp ...
    
    ##file macro.cpp
    4: USE_A_MACRO ( test )
    5:
    6: void testfunc ( )
    7: {
    8: int k@1 ; k@1 = 0 ;
    9: k@1 = 1 ;
    10: }
    11:
    12: int main ( int argc@2 , char * * argv@3 )
    13: {
    14: int i@4 ; i@4 = 0 ;
    15: i@4 = 1 ;
    16: std :: cout << "test this!\n" ;
    17: return 0 ;
    18: }
    
    ##Value flow
    Line 8
      0 always 0
    Line 9
      1 always 1
    Line 14
      0 always 0
    Line 15
      1 always 1
    Line 16
      "test this!\n" always "test this!\n"
    Line 17
      0 always 0
    macro.cpp:15:7: warning: Variable 'i' is assigned a value that is never used. [unreadVariable]
        i = 1;
          ^
    nofile:0:0: warning: Cppcheck cannot find all the include files (use --check-config for details) [missingInclude]
    

    In the ticket https://trac.cppcheck.net/ticket/6975 which is somewhat similar Daniel wrote that the macro definition should be shown to Cppcheck.
    IMHO since there is now a message to report unknown macros i would expect to get at least such a message so the user knows that something is missing.

     

    Last edit: versat 2019-01-03

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.