]> git.saurik.com Git - wxWidgets.git/blob - utils/ifacecheck/src/xmlparser.h
fix heap corruption bug: use delete[] to delete a buffer allocated with new[], not...
[wxWidgets.git] / utils / ifacecheck / src / xmlparser.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: xmlparser.h
3 // Purpose: Parser of the API/interface XML files
4 // Author: Francesco Montorsi
5 // Created: 2008/03/17
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 #ifndef _XMLPARSER_H_
13 #define _XMLPARSER_H_
14
15 #include <wx/txtstrm.h>
16 #include <wx/dynarray.h>
17 #include <wx/xml/xml.h>
18 #include <wx/platinfo.h>
19
20 // helper macros
21 #define LogMessage(fmt, ...) { wxPrintf(fmt "\n", __VA_ARGS__); fflush(stdout); }
22 #define LogWarning(fmt, ...) { wxPrintf(fmt "\n", __VA_ARGS__); fflush(stdout); }
23 #define LogError(fmt, ...) { wxPrintf("ERROR: " fmt "\n", __VA_ARGS__); fflush(stdout); }
24 #define wxPrint(str) { wxPrintf(str); fflush(stdout); }
25
26
27 // ----------------------------------------------------------------------------
28 // Represents a type with or without const/static/ qualifier
29 // and with or without & and * operators
30 // ----------------------------------------------------------------------------
31 class wxType
32 {
33 public:
34 wxType() {}
35 wxType(const wxString& type)
36 { SetTypeFromString(type); }
37
38 void SetTypeFromString(const wxString& t);
39 wxString GetAsString() const
40 { return m_strType; }
41
42 bool IsConst() const
43 { return m_strType.Contains("const"); }
44 bool IsStatic() const
45 { return m_strType.Contains("static"); }
46 bool IsPointer() const
47 { return m_strType.Contains("*"); }
48 bool IsReference() const
49 { return m_strType.Contains("&"); }
50
51 bool operator==(const wxType& m) const;
52 bool operator!=(const wxType& m) const
53 { return !(*this == m); }
54
55 bool IsOk() const;
56
57 protected:
58 wxString m_strType,
59 m_strTypeClean; // m_strType "cleaned" of its attributes
60 // (only for internal use)
61 };
62
63 extern wxType wxEmptyType;
64 WX_DECLARE_OBJARRAY(wxType, wxTypeArray);
65
66
67 // ----------------------------------------------------------------------------
68 // Represents a type used as argument for some wxMethod
69 // ----------------------------------------------------------------------------
70 class wxArgumentType : public wxType
71 {
72 public:
73 wxArgumentType() {}
74 wxArgumentType(const wxString& type, const wxString& defVal,
75 const wxString& argName = wxEmptyString)
76 { SetTypeFromString(type); SetDefaultValue(defVal); m_strArgName = argName; }
77
78 void SetArgumentName(const wxString& name)
79 { m_strArgName=name.Strip(wxString::both); }
80 wxString GetArgumentName() const
81 { return m_strArgName; }
82
83 void SetDefaultValue(const wxString& defval, const wxString& defvalForCmp = wxEmptyString);
84 wxString GetDefaultValue() const
85 { return m_strDefaultValue; }
86
87 bool HasDefaultValue() const
88 { return !m_strDefaultValue.IsEmpty(); }
89
90 bool operator==(const wxArgumentType& m) const;
91 bool operator!=(const wxArgumentType& m) const
92 { return !(*this == m); }
93
94 protected:
95 wxString m_strDefaultValue;
96
97 // this string may differ from m_strDefaultValue if there were
98 // preprocessor substitutions; can be wxEmptyString.
99 wxString m_strDefaultValueForCmp;
100
101 wxString m_strArgName; // this one only makes sense when this wxType is
102 // used as argument type (and not as return type)
103 // and can be empty.
104 };
105
106 extern wxArgumentType wxEmptyArgumentType;
107 WX_DECLARE_OBJARRAY(wxArgumentType, wxArgumentTypeArray);
108
109
110 // ----------------------------------------------------------------------------
111 // Represents a single prototype of a class' member.
112 // ----------------------------------------------------------------------------
113 class wxMethod
114 {
115 public:
116 wxMethod()
117 { m_bConst=m_bVirtual=m_bPureVirtual=m_bStatic=m_bDeprecated=false;
118 m_nLine=-1; m_nAvailability=wxPORT_UNKNOWN; }
119
120 wxMethod(const wxType& rettype, const wxString& name,
121 const wxArgumentTypeArray& arguments,
122 bool isConst, bool isStatic, bool isVirtual)
123 : m_retType(rettype), m_strName(name.Strip(wxString::both)),
124 m_bConst(isConst), m_bStatic(isStatic), m_bVirtual(isVirtual)
125 { SetArgumentTypes(arguments); m_nLine=-1; }
126
127
128 public: // getters
129
130 wxString GetAsString(bool bWithArgumentNames = true) const;
131
132 // parser of the prototype:
133 // all these functions return strings with spaces stripped
134 wxType GetReturnType() const
135 { return m_retType; }
136 wxString GetName() const
137 { return m_strName; }
138 wxArgumentTypeArray GetArgumentTypes() const
139 { return m_args; }
140 int GetLocation() const
141 { return m_nLine; }
142 int GetAvailability() const
143 { return m_nAvailability; }
144
145 bool IsConst() const
146 { return m_bConst; }
147 bool IsStatic() const
148 { return m_bStatic; }
149 bool IsVirtual() const
150 { return m_bVirtual; }
151 bool IsPureVirtual() const
152 { return m_bPureVirtual; }
153
154 bool IsOk() const;
155 bool IsCtor() const
156 { return m_retType==wxEmptyType && !m_strName.StartsWith("~"); }
157 bool IsDtor() const
158 { return m_retType==wxEmptyType && m_strName.StartsWith("~"); }
159
160 bool IsDeprecated() const
161 { return m_bDeprecated; }
162
163
164 public: // setters
165
166 void SetReturnType(const wxType& t)
167 { m_retType=t; }
168 void SetName(const wxString& name)
169 { m_strName=name; }
170 void SetArgumentTypes(const wxArgumentTypeArray& arr)
171 { m_args=arr; }
172 void SetConst(bool c = true)
173 { m_bConst=c; }
174 void SetStatic(bool c = true)
175 { m_bStatic=c; }
176 void SetVirtual(bool c = true)
177 { m_bVirtual=c; }
178 void SetPureVirtual(bool c = true)
179 {
180 m_bPureVirtual=c;
181 if (c) m_bVirtual=c; // pure virtual => virtual
182 }
183 void SetDeprecated(bool c = true)
184 { m_bDeprecated=c; }
185 void SetLocation(int lineNumber)
186 { m_nLine=lineNumber; }
187 void SetAvailability(int nAvail)
188 { m_nAvailability=nAvail; }
189
190 public: // misc
191
192 bool operator==(const wxMethod&) const;
193 bool operator!=(const wxMethod& m) const
194 { return !(*this == m); }
195
196 void Dump(wxTextOutputStream& stream) const;
197
198 protected:
199 wxType m_retType;
200 wxString m_strName;
201 wxArgumentTypeArray m_args;
202
203 // misc attributes:
204 bool m_bConst;
205 bool m_bStatic;
206 bool m_bVirtual;
207 bool m_bPureVirtual;
208 bool m_bDeprecated;
209
210 // m_nLine can be -1 if no location infos are available
211 int m_nLine;
212
213 // this is a combination of wxPORT_* flags (see wxPortId) or wxPORT_UNKNOWN
214 // if this method should be available for all wxWidgets ports.
215 // NOTE: this is not used for comparing wxMethod objects
216 // (gccXML never gives this kind of info).
217 int m_nAvailability;
218 };
219
220 WX_DECLARE_OBJARRAY(wxMethod, wxMethodArray);
221 WX_DEFINE_ARRAY(const wxMethod*, wxMethodPtrArray);
222
223
224 // ----------------------------------------------------------------------------
225 // Represents a class of the wx API/interface.
226 // ----------------------------------------------------------------------------
227 class wxClass
228 {
229 public:
230 wxClass() {}
231 wxClass(const wxString& name, const wxString& headername)
232 : m_strName(name), m_strHeader(headername) {}
233
234
235 public: // setters
236
237 void SetHeader(const wxString& header)
238 { m_strHeader=header; }
239 void SetName(const wxString& name)
240 { m_strName=name; }
241 void SetAvailability(int nAvail)
242 { m_nAvailability=nAvail; }
243
244
245 public: // getters
246
247 bool IsOk() const
248 { return !m_strName.IsEmpty() && !m_methods.IsEmpty(); }
249
250 bool IsValidCtorForThisClass(const wxMethod& m) const;
251 bool IsValidDtorForThisClass(const wxMethod& m) const;
252
253 wxString GetName() const
254 { return m_strName; }
255 wxString GetHeader() const
256 { return m_strHeader; }
257 wxString GetNameWithoutTemplate() const;
258
259 unsigned int GetMethodCount() const
260 { return m_methods.GetCount(); }
261 wxMethod& GetMethod(unsigned int n) const
262 { return m_methods[n]; }
263 wxMethod& GetLastMethod() const
264 { return m_methods.Last(); }
265
266 int GetAvailability() const
267 { return m_nAvailability; }
268
269 public: // misc
270
271 void AddMethod(const wxMethod& func)
272 { m_methods.Add(func); }
273
274 // returns a single result (the first, which is also the only
275 // one if CheckConsistency() return true)
276 const wxMethod* FindMethod(const wxMethod& m) const;
277
278 // returns an array of pointers to the overloaded methods with the
279 // same given name
280 wxMethodPtrArray FindMethodNamed(const wxString& m) const;
281
282 // dumps all methods to the given output stream
283 void Dump(wxTextOutputStream& stream) const;
284
285 // slow check
286 bool CheckConsistency() const;
287
288 protected:
289 wxString m_strName;
290 wxString m_strHeader;
291 wxMethodArray m_methods;
292
293 // see the wxMethod::m_nAvailability field for more info
294 int m_nAvailability;
295 };
296
297 WX_DECLARE_OBJARRAY(wxClass, wxClassArray);
298 WX_DEFINE_ARRAY(const wxClass*, wxClassPtrArray);
299
300
301
302 // ----------------------------------------------------------------------------
303 // wxXmlInterface
304 // ----------------------------------------------------------------------------
305 class wxXmlInterface
306 {
307 public:
308 wxXmlInterface() {}
309
310 const wxClass* FindClass(const wxString& classname) const
311 {
312 for (unsigned int i=0; i<m_classes.GetCount(); i++)
313 if (m_classes[i].GetName() == classname)
314 return &m_classes[i];
315 return NULL;
316 }
317
318 void Dump(const wxString& filename);
319
320 const wxClassArray& GetClasses() const
321 { return m_classes; }
322
323 unsigned int GetClassesCount() const
324 { return m_classes.GetCount(); }
325
326 unsigned int GetMethodCount() const
327 {
328 unsigned int methods = 0;
329 for (unsigned i=0; i < m_classes.GetCount(); i++)
330 methods += m_classes[i].GetMethodCount();
331 return methods;
332 }
333
334 // pass a full-path header filename:
335 wxClassPtrArray FindClassesDefinedIn(const wxString& headerfile) const;
336
337 void ShowProgress()
338 { /*wxPrint(".");*/ }
339
340 bool CheckParseResults() const;
341
342 protected:
343 wxClassArray m_classes;
344 };
345
346 #if 1
347 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
348 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
349 WX_DECLARE_HASH_MAP( unsigned long, wxString,
350 wxIntegerHash, wxIntegerEqual,
351 wxTypeIdHashMap );
352
353 WX_DECLARE_STRING_HASH_MAP( wxString, wxStringHashMap );
354 #else
355 #include <map>
356 typedef std::basic_string<char> stlString;
357 typedef std::map<unsigned long, stlString> wxTypeIdHashMap;
358 #endif
359
360
361 // ----------------------------------------------------------------------------
362 // Represents the real interface of wxWidgets
363 // Loads it from the XML produced by gccXML: http://www.gccxml.org
364 // ----------------------------------------------------------------------------
365 class wxXmlGccInterface : public wxXmlInterface
366 {
367 public:
368 wxXmlGccInterface()
369 {
370 // FIXME: we should retrieve this from the XML file!
371 // here we suppose the XML was created for the currently-running port
372 m_portId = wxPlatformInfo::Get().GetPortId();
373 }
374
375 bool Parse(const wxString& filename);
376 bool ParseMethod(const wxXmlNode *p,
377 const wxTypeIdHashMap& types,
378 wxMethod& m);
379
380 wxPortId GetInterfacePort() const
381 { return m_portId; }
382
383 wxString GetInterfacePortName() const
384 { return wxPlatformInfo::GetPortIdName(m_portId, false); }
385
386 protected:
387 // the port for which the gcc XML was generated
388 wxPortId m_portId;
389 };
390
391
392 // ----------------------------------------------------------------------------
393 // Represents the interface of the doxygen headers of wxWidgets
394 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
395 // ----------------------------------------------------------------------------
396 class wxXmlDoxygenInterface : public wxXmlInterface
397 {
398 public:
399 wxXmlDoxygenInterface() {}
400
401 // !!SPEEDUP-TODO!!
402 // Using wxXmlDocument::Load as is, the entire XML file is parsed
403 // and an entire tree of wxXmlNodes is built in memory.
404 // We need however only small portions of the Doxygen-generated XML: to speedup the
405 // processing we could detach the expat callbacks when we detect the beginning of a
406 // node we're not interested about, or just don't create a wxXmlNode for it!
407 // This needs a modification of wxXml API.
408
409 bool Parse(const wxString& filename);
410 bool ParseCompoundDefinition(const wxString& filename);
411 bool ParseMethod(const wxXmlNode*, wxMethod&, wxString& header);
412
413 // this class can take advantage of the preprocessor output to give
414 // a minor number of false positive warnings in the final comparison
415 void AddPreprocessorValue(const wxString& name, const wxString& val)
416 { m_preproc[name]=val; }
417
418 protected:
419 wxStringHashMap m_preproc;
420 };
421
422
423
424 #endif // _XMLPARSER_H_
425