1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Parser of the API/interface XML files
4 // Author: Francesco Montorsi
7 // Copyright: (c) 2008 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
15 #include <wx/txtstrm.h>
16 #include <wx/dynarray.h>
17 #include <wx/xml/xml.h>
18 #include <wx/platinfo.h>
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:
30 // ...fix description...
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); }}
39 #define wxPrint(str) { wxPrintf(str); fflush(stdout); }
41 // enable/disable logging
42 extern bool g_bLogEnabled
;
47 LogNull() { g_bLogEnabled
= false; }
48 ~LogNull() { g_bLogEnabled
= true; }
53 // ----------------------------------------------------------------------------
54 // Represents a type with or without const/static/ qualifier
55 // and with or without & and * operators
56 // ----------------------------------------------------------------------------
61 wxType(const wxString
& type
)
62 { SetTypeFromString(type
); }
64 void SetTypeFromString(const wxString
& t
);
65 wxString
GetAsString() const
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.
71 wxString
GetAsCleanString() const
72 { return m_strTypeClean
; }
75 { return m_strType
.Contains("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("&"); }
83 bool operator==(const wxType
& m
) const;
84 bool operator!=(const wxType
& m
) const
85 { return !(*this == m
); }
91 m_strTypeClean
; // m_strType "cleaned" of its attributes
92 // (only for internal use)
95 extern wxType wxEmptyType
;
96 WX_DECLARE_OBJARRAY(wxType
, wxTypeArray
);
99 // ----------------------------------------------------------------------------
100 // Represents a type used as argument for some wxMethod
101 // ----------------------------------------------------------------------------
102 class wxArgumentType
: public wxType
106 wxArgumentType(const wxString
& type
, const wxString
& defVal
,
107 const wxString
& argName
= wxEmptyString
)
108 { SetTypeFromString(type
); SetDefaultValue(defVal
); m_strArgName
= argName
; }
110 void SetArgumentName(const wxString
& name
)
111 { m_strArgName
=name
.Strip(wxString::both
); }
112 wxString
GetArgumentName() const
113 { return m_strArgName
; }
115 void SetDefaultValue(const wxString
& defval
,
116 const wxString
& defvalForCmp
= wxEmptyString
);
117 wxString
GetDefaultValue() const
118 { return m_strDefaultValue
; }
120 // returns the default value used for comparisons
121 wxString
GetDefaultCleanValue() const
122 { return m_strDefaultValueForCmp
; }
124 bool HasDefaultValue() const
125 { return !m_strDefaultValue
.IsEmpty(); }
127 bool operator==(const wxArgumentType
& m
) const;
128 bool operator!=(const wxArgumentType
& m
) const
129 { return !(*this == m
); }
132 wxString m_strDefaultValue
;
134 // this string may differ from m_strDefaultValue if there were
135 // preprocessor substitutions or other "replacements" done to
136 // avoid false errors.
137 wxString m_strDefaultValueForCmp
;
140 wxString m_strArgName
;
143 extern wxArgumentType wxEmptyArgumentType
;
144 WX_DECLARE_OBJARRAY(wxArgumentType
, wxArgumentTypeArray
);
147 enum wxMethodAccessSpecifier
154 // ----------------------------------------------------------------------------
155 // Represents a single prototype of a class' member.
156 // ----------------------------------------------------------------------------
161 { m_bConst
=m_bVirtual
=m_bPureVirtual
=m_bStatic
=m_bDeprecated
=false;
162 m_nLine
=-1; m_nAvailability
=wxPORT_UNKNOWN
; m_access
=wxMAS_PUBLIC
; }
164 wxMethod(const wxType
& rettype
, const wxString
& name
,
165 const wxArgumentTypeArray
& arguments
,
166 bool isConst
, bool isStatic
, bool isVirtual
)
167 : m_retType(rettype
), m_strName(name
.Strip(wxString::both
)),
168 m_bConst(isConst
), m_bStatic(isStatic
), m_bVirtual(isVirtual
)
169 { SetArgumentTypes(arguments
); m_nLine
=-1; }
174 // bWithArgumentNames = output argument names?
175 // bCleanDefaultValues = output clean argument default values?
176 // bDeprecated = output [deprecated] next to deprecated methods?
177 // bAccessSpec = output [public], [protected] or [private] next to method?
179 // TODO: convert to readable flags this set of bools
180 wxString
GetAsString(bool bWithArgumentNames
= true,
181 bool bCleanDefaultValues
= false,
182 bool bDeprecated
= false,
183 bool bAccessSpec
= false) const;
185 // parser of the prototype:
186 // all these functions return strings with spaces stripped
187 wxType
GetReturnType() const
188 { return m_retType
; }
189 wxString
GetName() const
190 { return m_strName
; }
191 const wxArgumentTypeArray
& GetArgumentTypes() const
193 wxArgumentTypeArray
& GetArgumentTypes()
195 int GetLocation() const
197 int GetAvailability() const
198 { return m_nAvailability
; }
199 wxMethodAccessSpecifier
GetAccessSpecifier() const
204 bool IsStatic() const
205 { return m_bStatic
; }
206 bool IsVirtual() const
207 { return m_bVirtual
; }
208 bool IsPureVirtual() const
209 { return m_bPureVirtual
; }
213 { return m_retType
==wxEmptyType
&& !m_strName
.StartsWith("~"); }
215 { return m_retType
==wxEmptyType
&& m_strName
.StartsWith("~"); }
217 bool IsDeprecated() const
218 { return m_bDeprecated
; }
223 void SetReturnType(const wxType
& t
)
225 void SetName(const wxString
& name
)
227 void SetArgumentTypes(const wxArgumentTypeArray
& arr
)
229 void SetConst(bool c
= true)
231 void SetStatic(bool c
= true)
233 void SetVirtual(bool c
= true)
235 void SetPureVirtual(bool c
= true)
238 if (c
) m_bVirtual
=c
; // pure virtual => virtual
240 void SetDeprecated(bool c
= true)
242 void SetLocation(int lineNumber
)
243 { m_nLine
=lineNumber
; }
244 void SetAvailability(int nAvail
)
245 { m_nAvailability
=nAvail
; }
246 void SetAccessSpecifier(wxMethodAccessSpecifier spec
)
251 bool operator==(const wxMethod
&) const;
252 bool operator!=(const wxMethod
& m
) const
253 { return !(*this == m
); }
255 // this function works like operator== but tests everything:
259 // except for the method attributes (const,static,virtual,pureVirtual,deprecated)
260 bool MatchesExceptForAttributes(const wxMethod
& m
) const;
262 // returns true if this is a ctor which has default values for all its
263 // argument, thus is able to act also as default ctor
264 bool ActsAsDefaultCtor() const;
266 void Dump(wxTextOutputStream
& stream
) const;
271 wxArgumentTypeArray m_args
;
280 // m_nLine can be -1 if no location infos are available
283 // this is a combination of wxPORT_* flags (see wxPortId) or wxPORT_UNKNOWN
284 // if this method should be available for all wxWidgets ports.
285 // NOTE: this is not used for comparing wxMethod objects
286 // (gccXML never gives this kind of info).
289 // the access specifier for this method
290 wxMethodAccessSpecifier m_access
;
293 WX_DECLARE_OBJARRAY(wxMethod
, wxMethodArray
);
294 WX_DEFINE_ARRAY(const wxMethod
*, wxMethodPtrArray
);
297 // we need wxClassPtrArray to be defined _before_ wxClass itself,
298 // since wxClass uses wxClassPtrArray.
300 WX_DEFINE_ARRAY(const wxClass
*, wxClassPtrArray
);
302 class wxXmlInterface
;
305 // ----------------------------------------------------------------------------
306 // Represents a class of the wx API/interface.
307 // ----------------------------------------------------------------------------
312 wxClass(const wxString
& name
, const wxString
& headername
)
313 : m_strName(name
), m_strHeader(headername
) {}
318 void SetHeader(const wxString
& header
)
319 { m_strHeader
=header
; }
320 void SetName(const wxString
& name
)
322 void SetAvailability(int nAvail
)
323 { m_nAvailability
=nAvail
; }
324 void SetParent(unsigned int k
, const wxString
& name
)
325 { m_parents
[k
]=name
; }
330 { return !m_strName
.IsEmpty() && !m_methods
.IsEmpty(); }
332 bool IsValidCtorForThisClass(const wxMethod
& m
) const;
333 bool IsValidDtorForThisClass(const wxMethod
& m
) const;
335 wxString
GetName() const
336 { return m_strName
; }
337 wxString
GetHeader() const
338 { return m_strHeader
; }
339 wxString
GetNameWithoutTemplate() const;
341 unsigned int GetMethodCount() const
342 { return m_methods
.GetCount(); }
343 wxMethod
& GetMethod(unsigned int n
) const
344 { return m_methods
[n
]; }
345 wxMethod
& GetLastMethod() const
346 { return m_methods
.Last(); }
348 int GetAvailability() const
349 { return m_nAvailability
; }
351 //const wxClass *GetParent(unsigned int i) const
352 const wxString
& GetParent(unsigned int i
) const
353 { return m_parents
[i
]; }
354 unsigned int GetParentCount() const
355 { return m_parents
.GetCount(); }
359 void AddMethod(const wxMethod
& func
)
360 { m_methods
.Add(func
); }
362 void AddParent(const wxString
& parent
)//wxClass* parent)
363 { m_parents
.Add(parent
); }
365 // returns a single result (the first, which is also the only
366 // one if CheckConsistency() return true)
367 const wxMethod
* FindMethod(const wxMethod
& m
) const;
369 // like FindMethod() but this one searches also recursively in
370 // the parents of this class.
371 const wxMethod
* RecursiveUpwardFindMethod(const wxMethod
& m
,
372 const wxXmlInterface
* allclasses
) const;
374 // returns an array of pointers to the overloaded methods with the
376 wxMethodPtrArray
FindMethodsNamed(const wxString
& name
) const;
378 // like FindMethodsNamed() but this one searches also recursively in
379 // the parents of this class.
380 wxMethodPtrArray
RecursiveUpwardFindMethodsNamed(const wxString
& name
,
381 const wxXmlInterface
* allclasses
) const;
383 // dumps all methods to the given output stream
384 void Dump(wxTextOutputStream
& stream
) const;
387 bool CheckConsistency() const;
391 wxString m_strHeader
;
392 wxMethodArray m_methods
;
394 // name of the base classes: we store the names and not the pointers
395 // because this makes _much_ easier the parsing process!
396 // (basically because when parsing class X which derives from Y,
397 // we may have not parsed yet class Y!)
398 wxArrayString m_parents
;
400 // see the wxMethod::m_nAvailability field for more info
404 WX_DECLARE_OBJARRAY(wxClass
, wxClassArray
);
408 // ----------------------------------------------------------------------------
410 // ----------------------------------------------------------------------------
416 const wxClass
* FindClass(const wxString
& classname
) const
418 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
419 if (m_classes
[i
].GetName() == classname
)
420 return &m_classes
[i
];
424 void Dump(const wxString
& filename
);
426 const wxClassArray
& GetClasses() const
427 { return m_classes
; }
429 unsigned int GetClassesCount() const
430 { return m_classes
.GetCount(); }
432 unsigned int GetMethodCount() const
434 unsigned int methods
= 0;
435 for (unsigned i
=0; i
< m_classes
.GetCount(); i
++)
436 methods
+= m_classes
[i
].GetMethodCount();
440 // pass a full-path header filename:
441 wxClassPtrArray
FindClassesDefinedIn(const wxString
& headerfile
) const;
444 { /*wxPrint(".");*/ }
446 bool CheckParseResults() const;
449 wxClassArray m_classes
;
453 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
454 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
455 WX_DECLARE_HASH_MAP( unsigned long, wxString
,
456 wxIntegerHash
, wxIntegerEqual
,
459 WX_DECLARE_STRING_HASH_MAP( wxString
, wxStringHashMap
);
462 typedef std::basic_string
<char> stlString
;
463 typedef std::map
<unsigned long, stlString
> wxTypeIdHashMap
;
467 // ----------------------------------------------------------------------------
468 // Represents the real interface of wxWidgets
469 // Loads it from the XML produced by gccXML: http://www.gccxml.org
470 // ----------------------------------------------------------------------------
471 class wxXmlGccInterface
: public wxXmlInterface
476 // FIXME: we should retrieve this from the XML file!
477 // here we suppose the XML was created for the currently-running port
478 m_portId
= wxPlatformInfo::Get().GetPortId();
481 bool Parse(const wxString
& filename
);
482 bool ParseMethod(const wxXmlNode
*p
,
483 const wxTypeIdHashMap
& types
,
486 wxPortId
GetInterfacePort() const
489 wxString
GetInterfacePortName() const
490 { return wxPlatformInfo::GetPortIdName(m_portId
, false); }
493 // the port for which the gcc XML was generated
498 // ----------------------------------------------------------------------------
499 // Represents the interface of the doxygen headers of wxWidgets
500 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
501 // ----------------------------------------------------------------------------
502 class wxXmlDoxygenInterface
: public wxXmlInterface
505 wxXmlDoxygenInterface() {}
508 // Using wxXmlDocument::Load as is, the entire XML file is parsed
509 // and an entire tree of wxXmlNodes is built in memory.
510 // We need however only small portions of the Doxygen-generated XML: to speedup the
511 // processing we could detach the expat callbacks when we detect the beginning of a
512 // node we're not interested about, or just don't create a wxXmlNode for it!
513 // This needs a modification of wxXml API.
515 bool Parse(const wxString
& filename
);
516 bool ParseCompoundDefinition(const wxString
& filename
);
517 bool ParseMethod(const wxXmlNode
*, wxMethod
&, wxString
& header
);
519 // this class can take advantage of the preprocessor output to give
520 // a minor number of false positive warnings in the final comparison
521 void AddPreprocessorValue(const wxString
& name
, const wxString
& val
)
522 { m_preproc
[name
]=val
; }
525 wxStringHashMap m_preproc
;
530 #endif // _XMLPARSER_H_