summaryrefslogtreecommitdiffstats
path: root/doc/preprocessing.doc
blob: 9eb5f6cb870da6a0c5e46924f4c17063e2a2797e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/******************************************************************************
 *
 * 
 *
 * Copyright (C) 1997-2001 by Dimitri van Heesch.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
 *
 */
/*! \page preprocessing Preprocessing

Source files that are used as input to doxygen can be parsed by doxygen's
build-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
\verbatim
#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
\endverbatim

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

\verbatim
#define VERSION
#define CONST_STRING

  static CONST_STRING version = "2.xx";
\endverbatim

You can disable all preprocessing by setting 
\ref cfg_enable_preprocessing "ENABLE_PREPROCESSING" to \c 
NO in the configuation file. In the case above doxygen will then reads
both statements!

In case you want to expand the \c CONST_STRING macro, you should set the
\ref cfg_macro_expansion "MACRO_EXPANSION" tag in the config file 
to \c YES. Then the result after preprocessing becomes:

\verbatim
#define VERSION
#define CONST_STRING

  static const char * version = "1.xx";
\endverbatim

Note that doxygen will now expand \e 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 
\ref cfg_expand_only_predef "EXPAND_ONLY_PREDEF" tag to \c YES
and specify the macro definitions after 
the \ref cfg_predefined "PREDEFINED" tag.  

As an example, suppose you have the following obfusciated code fragment
of an abstract base class called \c IUnknown:

\verbatim
/*! 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;
};
\endverbatim

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 config file:

\verbatim
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
\endverbatim

we can make sure that the proper result is fed to doxygen's parser:
\verbatim
/*! 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;
};
\endverbatim

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

Note also that preprocessor definitions that are normally defined 
automatically by the preprocessor (like \c __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 <code>:=</code> operator instead of 
<code>=</code>

As an example suppose we have the following piece of code:
\verbatim
#define QList QListT
class QListT
{
};
\endverbatim

Then the only way to get doxygen interpret this as a class definition
for class QList is to define:
\verbatim
PREDEFINED = QListT:=QList
\endverbatim

Here is example provided by Valter Minute that helps doxygen to
wade through the boilerplate code in Microsoft's ATL library:

\verbatim
PREDEFINED = DECLARE_REGISTRY_RESOURCEID=// \                                   
   DECLARE_PROTECT_FINAL_CONSTRUCT=// \                                         
   BEGIN_COM_MAP=/* \                                    
   END_COM_MAP=*/// \                                            
   BEGIN_PROP_MAP=/* \                                   
   END_PROP_MAP=*/// \                                        
   BEGIN_MSG_MAP=/* \                                                     
   END_MSG_MAP=*/// \                                                          
   DECLARE_VIEW_STATUS=// \
   "STDMETHOD(a)=HRESULT a" \                                                   
   "ATL_NO_VTABLE= "\                                                      
   "__declspec(a)= "\                                                  
   BEGIN_CONNECTION_POINT_MAP=/* \
   END_CONNECTION_POINT_MAP=*/// \
   "DECLARE_AGGREGATABLE(Class)= " \
   "DECLARE_REGISTRY_RESOURCEID(id)= " 
\endverbatim

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 \ref cfg_input_filter "INPUT_FILTER" tag.

If you are unsure what the effect of doxygen's preprocessing will be
you can run doxygen as follows:
\verbatim
  doxygen -d Preprocessor
\endverbatim
This will instruct doxygen to dump the input sources to standard output after
preprocessing has been done (Hint: set <code>QUIET = YES</code> and 
<code>WARNINGS = NO</code> in the configuration file to disable any other 
output).

\htmlonly
Go to the <a href="external.html">next</a> section or return to the
 <a href="index.html">index</a>.
\endhtmlonly

*/