Menu

Cppcheck architecture and writting my own rules in C++

hardork
2021-10-11
2021-12-06
  • hardork

    hardork - 2021-10-11

    Hello,
    I am new on the cppcheck topic, and I am trying to understand how to write my own rules.
    I have seen the addon part to write with python, but I would like to try the C++ part.
    There is a link on the wiki on a documentation, but the link is broken : http://sourceforge.net/projects/cppcheck/files/Articles/writing-rules-3.pdf/download

    Is there any documentation to help me start, some clue about the architecture, the tokens etc. ?

    Thanks in advance and thank you for this tool!

     
  • Daniel Marjamäki

    do you have some specific idea?

     
  • hardork

    hardork - 2021-10-12

    Well currently the first check I want to try to implement is pretty straightforward.
    I want to report an error when a call to the strtok function is done.

     
  • Daniel Marjamäki

    yes that rule sounds pretty easy to implement. I guess you can look at CheckFunctions::memsetZeroBytes().

     
  • hardork

    hardork - 2021-10-13

    Thank you for the entry point.
    Is there an easiest way to know that the current token is a function call?
    In the case of memsetZeroBytes I see that it test the name of the function and the number or arguments with numberOfArguments and if I look at the code of numberOfArguments I see that it test that the next token is a "("

    I take a quick look at astutils but didn't manage to find another solution and don't see anything specifics in the ast dump file.
    Just to be sure I am doing the more direct approach.

     
  • Daniel Marjamäki

    in this case I would check if strtok is a library function:

    if (Token::simpleMatch(tok, "strtok (") && mSettings->library.getFunction(tok))
    
     
  • hardork

    hardork - 2021-10-21

    It works like a charm, thank you.
    Now I am trying to implement something harder.

    The rules is : when you are calling a function named QueryInterface(arg1, arg2)
    You should call arg2.Release() before the end the scope of arg2.

    I try to do, based on code in checkfunctions.cpp :

    for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
                if (Token::Match(tok, "QueryInterface") &&(numberOfArguments(tok) == 2))
                {
                    const std::vector<const Token*>& arguments = getArguments(tok);
                    if (WRONG_DATA(arguments.size() != 2U, tok))
                        continue;
                    const Token* lastParamTok = arguments[1];
    

    at my great surprise lastParamTok return '('
    The code I am parsing is :
    pBS->QueryInterface(mkBSIntf::ClassId(), (void**)&PtrToBSIntf);

    Is there a method for me to get PtrToBSIntf instead of '(' ?
    Am I suppose to move on the ast starting from the '(' to find the variable name?

     
  • Daniel Marjamäki

    Am I suppose to move on the ast starting from the '(' to find the variable name?

    Yes. I recommend that you use astOperand1 and astOperand2. As you might have figured out a cast is in cppcheck AST a unary operator with 1 operand.

     

    Last edit: Daniel Marjamäki 2021-10-21
  • hardork

    hardork - 2021-12-06

    Hello,
    I have made some great progress on my customs errors.

    I have a new question:
    My check search method1 call, but there is multiple definition of method1 coming from various class.
    So I need to find method1 from myClass

    How I am supposed to find the inheritance of the token named method1 in the source that I am currently checking to test that it come from myClass?
    I take a look at the function class but didn't find anything usefull

    Thank you for you endless help Daniel!

     
    • Daniel Marjamäki

      not sure. I think something like this:

      Function::functionScope() => Scope::functionOf() => class scope

      I assume that the class declaration is seen.

       

      Last edit: Daniel Marjamäki 2021-12-06

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.