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