doxygen 1.11.0 使用详解(十二)——预处理

目录

Source files that are used as input to doxygen can be parsed by doxygen’s built-in C-preprocessor.

By default doxygen does only partial preprocessing. That is, it evaluates conditional compilation statements (like #if ) and evaluates macro definitions, but it does not perform macro expansion.

So if you have the following code fragment

#define VERSION 200
#define CONST_STRING const char *

#if VERSION >= 200
  static CONST_STRING version = "2.xx";
#else
  static CONST_STRING version = "1.xx";
#endif

Then by default doxygen will feed the following to its parser:

#define VERSION
#define CONST_STRING
  
  static CONST_STRING version = "2.xx";

You can disable all preprocessing by setting ENABLE_PREPROCESSING to NO in the configuration file. In the case above doxygen will then read both statements, i.e.:

static CONST_STRING version = "2.xx";
static CONST_STRING version = "1.xx";

In case you want to expand the CONST_STRING macro, you should set the MACRO_EXPANSION tag in the configuration file to YES . Then the result after preprocessing becomes:

#define VERSION
#define CONST_STRING

  static const char * version = "2.xx";

Note that doxygen will now expand all macro definitions (recursively if needed). This is often too much. Therefore, doxygen also allows you to expand only those defines that you explicitly specify. For this you have to set the EXPAND_ONLY_PREDEF tag to YES and specify the macro definitions after the PREDEFINED or EXPAND_AS_DEFINED tag.

A typically example where some help from the preprocessor is needed is when dealing with the language extension from Microsoft: __declspec . The same goes for GNU’s attribute extension. Here is an example function.

extern "C" void __declspec(dllexport) ErrorMsg( String aMessage,...);

When nothing is done, doxygen will be confused and see __declspec as some sort of function. To help doxygen one typically uses the following preprocessor settings:

ENABLE_PREPROCESSING = YES
MACRO_EXPANSION      = YES
EXPAND_ONLY_PREDEF   = YES
PREDEFINED           = __declspec(x)=

This will make sure the __declspec(dllexport) is removed before doxygen parses the source code.

Similar settings can be used for removing attribute expressions from the input:

ENABLE_PREPROCESSING = YES
MACRO_EXPANSION      = YES
EXPAND_ONLY_PREDEF   = YES
PREDEFINED           = __attribute__(x)=

For a more complex example, suppose you have the following obfuscated code fragment of an abstract base class called IUnknown:

/*! A reference to an IID */
#ifdef __cplusplus
#define REFIID const IID &
#else
#define REFIID const IID *
#endif

/*! The IUnknown interface */
DECLARE_INTERFACE(IUnknown)
{
  STDMETHOD(HRESULT,QueryInterface) (THIS_ REFIID iid, void **ppv) PURE;
  STDMETHOD(ULONG,AddRef) (THIS) PURE;
  STDMETHOD(ULONG,Release) (THIS) PURE;
};

without macro expansion doxygen will get confused, but we may not want to expand the REFIID macro, because it is documented and the user that reads the documentation should use it when implementing the interface.

By setting the following in the configuration file:

ENABLE_PREPROCESSING = YES
MACRO_EXPANSION      = YES
EXPAND_ONLY_PREDEF   = YES
PREDEFINED           = "DECLARE_INTERFACE(name)=class name" \
                       "STDMETHOD(result,name)=virtual result name" \
                       "PURE= = 0" \
                       THIS_= \
                       THIS= \
                     __cplusplus

we can make sure that the proper result is fed to doxygen’s parser:

/*! A reference to an IID */
#define REFIID

/*! The IUnknown interface */
class IUnknown
{
  virtual HRESULT QueryInterface ( REFIID iid, void **ppv) = 0;
  virtual ULONG AddRef () = 0;
  virtual ULONG Release () = 0;
}

Note that the PREDEFINED tag accepts function like macro definitions (like DECLARE_INTERFACE ), normal macro substitutions (like PURE and THIS ) and plain defines (like __cplusplus ).

Note also that preprocessor definitions that are normally defined automatically by the preprocessor (like __cplusplus ), have to be defined by hand with doxygen’s parser (this is done because these defines are often platform/compiler specific).

In some cases you may want to substitute a macro name or function by something else without exposing the result to further macro substitution. You can do this but using the := operator instead of =

As an example suppose we have the following piece of code:

#define QList QListT
class QListT
{
};

Then the only way to get doxygen interpret this as a class definition for class QList is to define:

PREDEFINED = QListT:=QList

Here is an example that helps doxygen to wade through the boilerplate code in Microsoft’s ATL & MFC libraries:

PREDEFINED  = "DECLARE_INTERFACE(name)=class name" \
              "STDMETHOD(result,name)=virtual result name" \
              "PURE= = 0" \
              THIS_= \
              THIS= \
              DECLARE_REGISTRY_RESOURCEID=// \
              DECLARE_PROTECT_FINAL_CONSTRUCT=// \
              "DECLARE_AGGREGATABLE(Class)= " \
              "DECLARE_REGISTRY_RESOURCEID(Id)= " \
              DECLARE_MESSAGE_MAP= \
              BEGIN_MESSAGE_MAP=/* \
              END_MESSAGE_MAP=*/// \

As you can see doxygen’s preprocessor is quite powerful, but if you want even more flexibility you can always write an input filter and specify it after the INPUT_FILTER tag or the FILTER_PATTERNS tag (or the FILTER_SOURCE_PATTERNS tag).

If you are unsure what the effect of the filter will be you can run doxygen as follows:

doxygen -d filteroutput

If you are unsure what the effect of doxygen’s preprocessing will be you can run doxygen as follows:

doxygen -d Preprocessor

or when the line numbers are not wanted:

doxygen -d Preprocessor -d NoLineno

This will instruct doxygen to dump the input sources to standard output after preprocessing has been done (Hint:set QUIET = YES and WARNINGS = NO in the configuration file to disable any other output).

Note preprocessing is not done for all languages. Preprocessing is enabled for files that use the “C” scanner (with the exception of ‘java’, ‘d’ and ‘php’), Fortran files (only in case the extension contains at least one upper case character) and vhdl files.

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值