-->

Can I include cppcheck suppression within a functi

2020-07-18 04:59发布

问题:

I have added an inline comment to suppress a cppcheck unusedFunction warning for a function, but I would like to include this within the function header so that Doxygen can document all of the unused functions (I am implementing an API, so I have many functions that will not be used in my source). I would prefer not to suppress all unusedFunction errors, but rather on a per-function basis.

I would like to do something like this:

/**
 * API function description
 * 
 * @param p1 function pointer to the ...
 * @return 0 if successful, -1 otherwise.
 * // cppcheck-suppress unusedFunction
 */
int CreateTask(Task_FuncPtr p1)
{    
    return doSomething();
}

But when I do this, cppcheck does not "see" the inline suppression. If I move it outside the header, but just before the function declaration then the suppression works. The cppcheck documentation seems to imply that the suppression needs to be directly before the line generating then error.

Has any had success with this?

回答1:

Taking a look at cppcheck sources (file preprocessor.cpp function RemoveComments()), it seems that you cannot do it.

The code to identify comments is:

if (str.compare(i, 2, "//") == 0) { /* ... */ }

and

else if (str.compare(i, 2, "/*") == 0) { /* ... */ }

When a comment is found, the code that manages warning suppression is:

if (_settings && _settings->_inlineSuppressions) {
    std::istringstream iss(comment);
    std::string word;
    iss >> word;
    if (word == "cppcheck-suppress") {
        iss >> word;
        if (iss)
            suppressionIDs.push_back(word);
    }
}

So cppcheck will skip spaces and check the first token immediately after // or /*.

Unfortunately Doxygen's special comment blocks start with /**, ///, /*! or //! and the third character prevents the "correct match".

Changing:

if (word == "cppcheck-suppress") { /* ... */ }

into:

if (contains(word, "cppcheck-suppress")) { /* ... */ }
// or if (ends_with(word, "cppcheck-suppress"))

should allow what you want:

/**
 * API function description
 *
 * @param p1 function pointer to the ...
 * @return 0 if successful, -1 otherwise.
 */
/** cppcheck-suppress unusedFunction */

or

/// API function description
///
/// @param p1 function pointer to the ...
/// @return 0 if successful, -1 otherwise.
///
/// cppcheck-suppress unusedFunction

You can probably open a ticket on http://trac.cppcheck.net/