Documenting conditional exclusive code in Doxygen

谁说我不能喝 提交于 2021-01-28 12:36:05

问题


Consider

// EXTERNAL_MACRO is an external macro defined to some value by build system

#if EXTERNAL_MACRO == 1
#   define EXCLUSIVE_MACRO_ONE
#elif EXTERNAL_MACRO == 2
#   define EXCLUSIVE_MACRO_TWO
#else
#   define EXCLUSIVE_MACRO_OTHER
#endif

At built time, only one of the EXCLUSIVE_MACRO_... macros is defined.

How can one document all three EXCLUSIVE_MACRO_... macros in Doxygen?

PREDEFINED configuration setting is not helpful for this because it allows to define EXTERNAL_MACRO to only a single value (and therefore document only a single EXCLUSIVE_MACRO_...).

This SO answer offers a work-around solution that does the job but it requires an additional logic. I am looking for a simpler answer, one that uses Doxygen configuration (if possible) rather than modifying the original code (though some modification is of course fine).


回答1:


See related question and answer: Document a config macro with doxygen

Basically:

In the doxygen configuration file, add:

PREDEFINED = IN_DOXYGEN

Somewhere in the source code, add:

/*
  Exporting build configuration macros to doxygen,
  so they get documented.
*/
#ifdef IN_DOXYGEN
#   define EXCLUSIVE_MACRO_ONE
#   define EXCLUSIVE_MACRO_TWO
#   define EXCLUSIVE_MACRO_THREE
#endif /* IN_DOXYGEN */

Then the documentation for these macros is picked up properly.

If you don't want to change the source, you can add EXCLUSIVE_MACRO_ONE and friends in PREDEFINED, instead of setting EXTERNAL_MACRO.




回答2:


Inspired by answer.

PROBLEM: We have an existing header that, based on an external macro (provided by build system), defines one of several exclusive macros during compilation. Since only one of the compile-time macros is defined (for each individual build), it is difficult to add Doxygen documentation to all of the exclusive options.

// DEFINED_EXTERNALLY is a macro provided by the build system (e.g. via -D). It is defined as 1, 2, or some other natural number.

// Based on DEFINED_EXTERNALLY we want to define one of several exclusive DEFINED_HERE_... macros (e.g. DEFINED_HERE_1, DEFINED_HERE_2, and DEFINED_HERE_OTHER).

// First, we check that none of the DEFINED_HERE_... macros is defined yet.
#if defined(DEFINED_HERE_1) || defined(DEFINED_HERE_2) || defined(DEFINED_HERE_OTHER)
#   error "Conflict: macro DEFINED_HERE_... is already defined!"
#endif

// Then, we define one, and only one, of the DEFINED_HERE_... macros.
#if DEFINED_EXTERNALLY == 1
#   define DEFINED_HERE_1
#elif DEFINED_EXTERNALLY == 2
#   define DEFINED_HERE_2
#else
#   define DEFINED_HERE_OTHER
#endif

SOLUTION: We temporarily define all variants of the DEFINED_HERE_... macros, together with their documentation. This will cause Doxygen to generate documentation for all of them. We then undefine all of the variants and resume normal macro definition logic. As a result, we will have documentation for all variants yet only one of them will be defined during compilation as was the case before adding the documentation.

Original code

// DEFINED_EXTERNALLY is a macro provided by the build system (e.g. via -D). It is defined as 1, 2, or some other natural number.

// Based on DEFINED_EXTERNALLY we want to define one of several DEFINED_HERE_... macros (e.g. DEFINED_HERE_1, DEFINED_HERE_2, and DEFINED_HERE_OTHER).

// First, we check that none of the DEFINED_HERE_... macros is defined yet.
#if defined(DEFINED_HERE_1) || defined(DEFINED_HERE_2) || defined(DEFINED_HERE_OTHER)
#   error "Conflict: macro DEFINED_HERE_... is already defined!"
#endif

New inserted code to generate documentation

/// \brief Value 1.
/// \details It does the following...
#define DEFINED_HERE_1
#undef DEFINED_HERE_1
/// \brief Value 2.
/// \details It does the following...
#define DEFINED_HERE_2
#undef DEFINED_HERE_2
/// \brief Other value.
/// \details It does the following...
#define DEFINED_HERE_OTHER
#undef DEFINED_HERE_OTHER

Original code

// Then, we define one, and only one, of the DEFINED_HERE_... macros.
#if DEFINED_EXTERNALLY == 1
#   define DEFINED_HERE_1
#elif DEFINED_EXTERNALLY == 2
#   define DEFINED_HERE_2
#else
#   define DEFINED_HERE_OTHER
#endif

NB no change to the default Doxygen settings is required

ENABLE_PREPROCESSING   = YES
MACRO_EXPANSION        = NO
EXPAND_ONLY_PREDEF     = NO
PREDEFINED             =
EXPAND_AS_DEFINED      =


来源:https://stackoverflow.com/questions/54060666/documenting-conditional-exclusive-code-in-doxygen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!