Search code examples
doxygenunused-variables

Doxygen : handling unused function parameters with "UNUSED" macro


Short version

To prevent the compiler from raising a warning about unused variables I define the macro UNUSED as:

UNUSED(x)=x __attribute__((__unused__))

This macro is then employed in some functions' prototypes, for instance :

void ext(int foo, int UNUSED( bar ) )

However, doxygen is unhappy about that and returns some warnings:

/tmp/sandbox/main.cpp:13: warning: argument 'bar' of command @param is not found in the argument list of Dummy::ext(int foo, intUNUSEDbar)
/tmp/sandbox/main.cpp:13: warning: The following parameters of Dummy::ext(int foo, intUNUSEDbar) are not documented:
  parameter 'UNUSED'

How should I tell doxygen to ignore the UNUSED macro ?

Long version

I have a code that looks like the following :

#include <iostream>

class Dummy
//! Dummy class
{
public :

    //!Dummy function
    /**
     * \param foo First  variable
     * \param bar Second variable
    */
    void ext(int foo, int UNUSED( bar ) )
    {
        std::cout << "foo = " << foo << std::endl;
    }
};


//!Main function
int main(void)
{
    Dummy MyDummy;
    MyDummy.ext(1, 2);
    return 0;
}

I compile it by invoking :

g++ -D 'UNUSED(x)=x __attribute__((__unused__))' main.cpp

To generate the default doxygen configuration file named Doxyfile I enter :

doxygen -g

Eventually, to generate the documentation I enter :

doxygen Doxyfile

The latter command outputs the following warning :

/tmp/sandbox/main.cpp:13: warning: argument 'bar' of command @param is not found in the argument list of Dummy::ext(int foo, intUNUSEDbar)
/tmp/sandbox/main.cpp:13: warning: The following parameters of Dummy::ext(int foo, intUNUSEDbar) are not documented:
  parameter 'UNUSED'

Solution

  • Following instructions from the doxygen documentation, modify the Doxyfile so that it has the following parameters :

    #Doxygen will run its own preprocessor before parsing the file
    ENABLE_PREPROCESSING   = YES
    
    #The Doxygen preprocessor will not only define the macros (default
    #behaviour) but also expand them
    MACRO_EXPANSION        = YES
    
    #The Doxygen preprocessor will only expand the macros that are listed in
    #the PREDEFINED setting. Any other macro will merely be defined, and not
    #expanded.
    EXPAND_ONLY_PREDEF     = YES
    
    #The Doxygen preprocessor will replace any occurrence of the UNUSED
    #macro by its argument
    PREDEFINED             = UNUSED(x)=x
    

    After those changes, invoking doxygen Doxyfile no longer raises warnings.