Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress multiple warnings from one line?

Tags:

cppcheck

I have a code fragment that drives cppcheck nuts, because it doesn't see variables used in the log call as used. So I get unused variables and a scope reduction warning:

    double start = GetTimeOfDayDoubleSec(), afterDb = 0;
    if (LoadFromDatabase(serials)) {
        afterDb = GetTimeOfDayDoubleSec();
        Cache.ResetDebugFlags(serials);
    }
    double end = GetTimeOfDayDoubleSec();
    ZLOG_INFO("DB time  %f, total %f", afterDb ? afterDb - start : 0, end - start);

Cppcheck says:

The scope of the variable 'afterDb' can be reduced.
Variable 'afterDb' is assigned a value that is never used.

I can't work out the syntax to suppress both of those and the manual is unhelpful. Separate line, spaces, commas, colons and semicolons all fail. Separate lines gives me "suppression not matched", the rest are just invalid:

    //cppcheck-suppress variableScope
    //cppcheck-suppress unreadVariable
    //cppcheck-suppress variableScope unreadVariable
    //cppcheck-suppress variableScope,unreadVariable
    //cppcheck-suppress variableScope;unreadVariable
    double afterDb = 0;

 Failed to add suppression. Invalid id "variableScope:unreadVariable"

Does cppcheck let me do this inline, or do I have to do it using XML on the command line?


cppcheck-gui toolbar

Sorry, it turns out that there's a confounding issue: hitting "refresh" in cppcheck-gui doesn't refresh everything, I have to reload the cppcheck file to get changes in suppressions to update. Viz, the "open file" icon on the toolbar rather than the right hand "refresh" one.

It turns out that space separated works:

//cppcheck-suppress variableScope unreadVariable
like image 482
Code Abominator Avatar asked Feb 10 '23 22:02

Code Abominator


2 Answers

To use multiple suppressions on the same line, use a comma-delimited list.

From the manual:

arr[10] = arr[10] / 0; // cppcheck-suppress[arrayIndexOutOfBounds,zerodiv]

You can also stack the suppressions:

// cppcheck-suppress arrayIndexOutOfBounds
// cppcheck-suppress zerodiv
arr[10] = arr[10] / 0;
like image 73
pixelgrease Avatar answered Feb 13 '23 11:02

pixelgrease


First of all... as I read your code it seems to me that Cppcheck writes FP. We should not warn about that code, should we? I'd like that the FP is fixed. I copy and pasted your code, and fail to reproduce a FP for it though - so there is probably something in your real code that confuses Cppcheck.

The code for inline suppressions is in the cppcheck preprocessor (search for "cppcheck-suppress" in the lib/preprocessor.cpp file). As far as I see you can't use multiple inline suppressions currently.

If you have some time and want to help us, feel free to either:

  • create a ticket in our trac http://trac.cppcheck.net and attach a patch that fixes this.
  • or, create a pull request in the github repo http://github.com/danmar/cppcheck that fixes this.

I'd say space separated inline suppressions would be fine.

Does cppcheck let me do this inline, or do I have to do it using XML on the command line?

maybe that is what you meant.. but you can use --suppress or --suppressions-list.

like image 37
Daniel Marjamäki Avatar answered Feb 13 '23 10:02

Daniel Marjamäki