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 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
18 // for all others, include the necessary headers
22 #include "wx/xml/xml.h"
23 #include "wx/wfstream.h"
24 #include "wx/hashmap.h"
25 #include "wx/filename.h"
26 #include "xmlparser.h"
29 #include <wx/arrimpl.cpp>
30 WX_DEFINE_OBJARRAY(wxTypeArray
)
31 WX_DEFINE_OBJARRAY(wxArgumentTypeArray
)
32 WX_DEFINE_OBJARRAY(wxMethodArray
)
33 WX_DEFINE_OBJARRAY(wxClassArray
)
36 #define PROGRESS_RATE 1000 // each PROGRESS_RATE nodes processed print a dot
37 #define ESTIMATED_NUM_CLASSES 600 // used by both wxXmlInterface-derived classes to prealloc mem
40 // defined in ifacecheck.cpp
41 extern bool g_verbose
;
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
51 void wxType::SetTypeFromString(const wxString
& t
)
54 TODO: optimize the following code writing a single function
55 which works at char-level and does everything in a single pass
60 // [] is the same as * for gccxml
61 m_strType
.Replace("[]", "*");
62 m_strType
.Replace("long int", "long"); // in wx typically we never write "long int", just "long"
64 // make sure the * and & operator always use the same spacing rules
65 // (to make sure GetAsString() output is always consistent)
66 m_strType
.Replace("*", "* ");
67 m_strType
.Replace("&", "& ");
68 m_strType
.Replace(" *", "*");
69 m_strType
.Replace(" &", "&");
71 while (m_strType
.Contains(" "))
72 m_strType
.Replace(" ", " "); // do it once again
74 m_strType
.Replace(" ,", ",");
76 m_strType
= m_strType
.Strip(wxString::both
);
79 bool wxType::IsOk() const
81 // NB: m_strType can contain the :: operator; think to e.g. the
82 // "reverse_iterator_impl<wxString::const_iterator>" type
83 // It can also contain commas, * and & operators etc
85 return !GetClean().IsEmpty();
88 wxString
wxType::GetClean() const
90 wxString
ret(m_strType
);
91 ret
.Replace("const", "");
92 ret
.Replace("static", "");
95 ret
.Replace("[]", "");
96 return ret
.Strip(wxString::both
);
99 bool wxType::operator==(const wxType
& m
) const
101 // brain-dead comparison:
103 if (GetClean() == m
.GetClean() &&
104 IsConst() == m
.IsConst() &&
105 IsStatic() == m
.IsStatic() &&
106 IsPointer() == m
.IsPointer() &&
107 IsReference() == m
.IsReference())
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 void wxArgumentType::SetDefaultValue(const wxString
& defval
, const wxString
& defvalForCmp
)
120 m_strDefaultValue
=defval
.Strip(wxString::both
);
121 m_strDefaultValueForCmp
=defvalForCmp
.Strip(wxString::both
);
123 // in order to make valid&simple comparison on argument defaults,
124 // we reduce some of the multiple forms in which the same things may appear
126 if (m_strDefaultValue
== "0u")
127 m_strDefaultValue
= "0";
130 m_strDefaultValue.Replace("0", "NULL");
132 m_strDefaultValue.Replace("NULL", "0");
135 if (m_strDefaultValue
.Contains("wxGetTranslation"))
136 m_strDefaultValue
= "_(TOFIX)"; // TODO: wxGetTranslation gives problems to gccxml
139 bool wxArgumentType::operator==(const wxArgumentType
& m
) const
141 if ((const wxType
&)(*this) != (const wxType
&)m
)
144 const wxString
& def1
= m_strDefaultValueForCmp
.IsEmpty() ? m_strDefaultValue
: m_strDefaultValueForCmp
;
145 const wxString
& def2
= m
.m_strDefaultValueForCmp
.IsEmpty() ? m
.m_strDefaultValue
: m
.m_strDefaultValueForCmp
;
149 // maybe the default values are numbers.
150 // in this case gccXML gives as default values things like '-0x0000001' instead of just '-1'.
151 // To handle these cases, we try to convert the default value strings to numbers:
152 long def1val
, def2val
;
153 if (def1
.ToLong(&def1val
, 0 /* auto-detect */) &&
154 def2
.ToLong(&def2val
, 0 /* auto-detect */))
156 if (def1val
== def2val
)
157 return true; // the default values match
163 // we deliberately avoid checks on the argument name
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 bool wxMethod::IsOk() const
175 // NOTE: m_retType can be a wxEmptyType, and means that this method
176 // is a ctor or a dtor.
177 if (!m_retType
.IsOk() && m_retType
!=wxEmptyType
) {
178 LogError("'%s' method has invalid return type: %s", m_retType
.GetAsString());
182 if (m_strName
.IsEmpty())
185 // a function can't be both const and static or virtual and static!
186 if ((m_bConst
&& m_bStatic
) || ((m_bVirtual
|| m_bPureVirtual
) && m_bStatic
)) {
187 LogError("'%s' method can't be both const/static or virtual/static", m_strName
);
191 wxASSERT(!m_bPureVirtual
|| (m_bPureVirtual
&& m_bVirtual
));
193 for (unsigned int i
=0; i
<m_args
.GetCount(); i
++)
194 if (!m_args
[i
].IsOk()) {
195 LogError("'%s' method has invalid %d-th argument type: %s",
196 m_strName
, i
+1, m_args
[i
].GetAsString());
200 // NB: the default value of the arguments can contain pretty much everything
201 // (think to e.g. wxPoint(3+4/2,0) or *wxBLACK or someClass<type>)
202 // so we don't do any test on their contents
203 if (m_args
.GetCount()>0)
205 bool previousArgHasDefault
= m_args
[0].HasDefaultValue();
206 for (unsigned int i
=1; i
<m_args
.GetCount(); i
++)
208 if (previousArgHasDefault
&& !m_args
[i
].HasDefaultValue()) {
209 LogError("'%s' method has %d-th argument which has no default value "
210 "(while the previous one had one!)",
215 previousArgHasDefault
= m_args
[i
].HasDefaultValue();
222 bool wxMethod::operator==(const wxMethod
& m
) const
224 if (GetReturnType() != m
.GetReturnType() ||
225 GetName() != m
.GetName() ||
226 IsConst() != m
.IsConst() ||
227 IsStatic() != m
.IsStatic() ||
228 IsVirtual() != m
.IsVirtual() ||
229 IsPureVirtual() != m
.IsPureVirtual() ||
230 IsDeprecated() != m
.IsDeprecated())
233 if (m_args
.GetCount()!=m
.m_args
.GetCount())
236 for (unsigned int i
=0; i
<m_args
.GetCount(); i
++)
237 if (m_args
[i
] != m
.m_args
[i
])
243 wxString
wxMethod::GetAsString(bool bWithArgumentNames
) const
247 if (m_retType
!=wxEmptyType
)
248 ret
+= m_retType
.GetAsString() + " ";
249 //else; this is a ctor or dtor
251 ret
+= m_strName
+ "(";
253 for (unsigned int i
=0; i
<m_args
.GetCount(); i
++)
255 ret
+= m_args
[i
].GetAsString();
257 const wxString
& name
= m_args
[i
].GetArgumentName();
258 if (bWithArgumentNames
&& !name
.IsEmpty())
261 const wxString
& def
= m_args
[i
].GetDefaultValue();
268 if (m_args
.GetCount()>0)
269 ret
= ret
.Left(ret
.Len()-2);
276 ret
= "static " + ret
;
277 if (m_bVirtual
|| m_bPureVirtual
)
278 ret
= "virtual " + ret
;
282 // in doxygen headers we don't need wxDEPRECATED:
284 // ret = "wxDEPRECATED( " + ret + " )";
289 void wxMethod::Dump(wxTextOutputStream
& stream
) const
291 stream
<< "[" + m_retType
.GetAsString() + "]";
292 stream
<< "[" + m_strName
+ "]";
294 for (unsigned int i
=0; i
<m_args
.GetCount(); i
++)
295 stream
<< "[" + m_args
[i
].GetAsString() + " " + m_args
[i
].GetArgumentName() +
296 "=" + m_args
[i
].GetDefaultValue() + "]";
303 stream
<< " VIRTUAL";
305 stream
<< " PURE-VIRTUAL";
307 stream
<< " DEPRECATED";
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
316 wxString
wxClass::GetNameWithoutTemplate() const
318 // NB: I'm not sure this is the right terminology for this function!
320 if (m_strName
.Contains("<"))
321 return m_strName
.Left(m_strName
.Find("<"));
325 bool wxClass::IsValidCtorForThisClass(const wxMethod
& m
) const
327 // remember that e.g. the ctor for wxWritableCharTypeBuffer<wchar_t> is
328 // named wxWritableCharTypeBuffer, without the <...> part!
330 if (m
.IsCtor() && m
.GetName() == GetNameWithoutTemplate())
336 bool wxClass::IsValidDtorForThisClass(const wxMethod
& m
) const
338 if (m
.IsDtor() && m
.GetName() == "~" + GetNameWithoutTemplate())
344 void wxClass::Dump(wxTextOutputStream
& out
) const
346 out
<< m_strName
+ "\n";
348 for (unsigned int i
=0; i
<m_methods
.GetCount(); i
++) {
350 // dump all our methods
352 m_methods
[i
].Dump(out
);
359 bool wxClass::CheckConsistency() const
361 for (unsigned int i
=0; i
<m_methods
.GetCount(); i
++)
362 for (unsigned int j
=0; j
<m_methods
.GetCount(); j
++)
363 if (i
!=j
&& m_methods
[i
] == m_methods
[j
])
365 LogError("class %s has two methods with the same prototype: '%s'",
366 m_strName
, m_methods
[i
].GetAsString());
373 const wxMethod
* wxClass::FindMethod(const wxMethod
& m
) const
375 for (unsigned int i
=0; i
<m_methods
.GetCount(); i
++)
376 if (m_methods
[i
] == m
)
377 return &m_methods
[i
];
381 wxMethodPtrArray
wxClass::FindMethodNamed(const wxString
& name
) const
383 wxMethodPtrArray ret
;
385 for (unsigned int i
=0; i
<m_methods
.GetCount(); i
++)
386 if (m_methods
[i
].GetName() == name
)
387 ret
.Add(&m_methods
[i
]);
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
397 WX_DEFINE_SORTED_ARRAY(wxClass
*, wxSortedClassArray
);
399 int CompareWxClassObjects(wxClass
*item1
, wxClass
*item2
)
401 // sort alphabetically
402 return item1
->GetName().Cmp(item2
->GetName());
405 void wxXmlInterface::Dump(const wxString
& filename
)
407 wxFFileOutputStream
apioutput( filename
);
408 wxTextOutputStream
apiout( apioutput
);
410 // dump the classes in alphabetical order
411 wxSortedClassArray
sorted(CompareWxClassObjects
);
412 sorted
.Alloc(m_classes
.GetCount());
413 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
414 sorted
.Add(&m_classes
[i
]);
416 // now they have been sorted
417 for (unsigned int i
=0; i
<sorted
.GetCount(); i
++)
418 sorted
[i
]->Dump(apiout
);
421 bool wxXmlInterface::CheckParseResults() const
423 // this check can be quite slow, so do it only for debug releases:
425 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
426 if (!m_classes
[i
].CheckConsistency())
433 wxClassPtrArray
wxXmlInterface::FindClassesDefinedIn(const wxString
& headerfile
) const
437 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
438 if (m_classes
[i
].GetHeader() == headerfile
)
439 ret
.Add(&m_classes
[i
]);
445 // ----------------------------------------------------------------------------
446 // wxXmlGccInterface helper declarations
447 // ----------------------------------------------------------------------------
449 #define ATTRIB_CONST 1
450 #define ATTRIB_REFERENCE 2
451 #define ATTRIB_POINTER 4
452 #define ATTRIB_ARRAY 8
454 #define GCCXML_BASE 35
456 class toResolveTypeItem
459 toResolveTypeItem() { attribs
=0; }
460 toResolveTypeItem(unsigned int refID
, unsigned int attribint
)
461 : ref(refID
), attribs(attribint
) {}
463 unsigned long ref
, attribs
;
468 // for wxToResolveTypeHashMap, keys == gccXML IDs and values == toResolveTypeItem
469 WX_DECLARE_HASH_MAP( unsigned long, toResolveTypeItem
,
470 wxIntegerHash
, wxIntegerEqual
,
471 wxToResolveTypeHashMap
);
473 // for wxClassMemberIdHashMap, keys == gccXML IDs and values == wxClass which owns that member ID
474 WX_DECLARE_HASH_MAP( unsigned long, wxClass
*,
475 wxIntegerHash
, wxIntegerEqual
,
476 wxClassMemberIdHashMap
);
479 typedef std::map
<unsigned long, toResolveTypeItem
> wxToResolveTypeHashMap
;
483 // utility to parse gccXML ID values;
484 // this function is equivalent to wxString(str).Mid(1).ToULong(&id, GCCXML_BASE)
485 // but is a little bit faster
486 bool getID(unsigned long *id
, const wxStringCharType
* str
)
488 wxStringCharType
*end
;
489 #if wxUSE_UNICODE_UTF8
490 unsigned long val
= strtoul(str
+1, &end
, GCCXML_BASE
);
492 unsigned long val
= wcstoul(str
+1, &end
, GCCXML_BASE
);
495 // return true only if scan was stopped by the terminating NUL and
496 // if the string was not empty to start with and no under/overflow
498 if ( *end
!= '\0' || end
== str
+1 || errno
== ERANGE
|| errno
== EINVAL
)
505 // utility specialized to parse efficiently the gccXML list of IDs which occur
506 // in nodes like <Class> ones... i.e. numeric values separed by " _" token
507 bool getMemberIDs(wxClassMemberIdHashMap
* map
, wxClass
* p
, const wxStringCharType
* str
)
509 #if wxUSE_UNICODE_UTF8
510 size_t len
= strlen(str
);
512 size_t len
= wcslen(str
);
515 if (len
== 0 || str
[0] != '_')
518 const wxStringCharType
*curpos
= str
,
520 wxStringCharType
*nexttoken
;
524 // curpos always points to the underscore of the next token to parse:
525 #if wxUSE_UNICODE_UTF8
526 unsigned long id
= strtoul(curpos
+1, &nexttoken
, GCCXML_BASE
);
528 unsigned long id
= wcstoul(curpos
+1, &nexttoken
, GCCXML_BASE
);
530 if ( *nexttoken
!= ' ' || errno
== ERANGE
|| errno
== EINVAL
)
533 // advance current position
534 curpos
= nexttoken
+ 1;
536 // add this ID to the hashmap
537 wxClassMemberIdHashMap::value_type
v(id
, p
);
545 // ----------------------------------------------------------------------------
547 // ----------------------------------------------------------------------------
549 bool wxXmlGccInterface::Parse(const wxString
& filename
)
555 LogMessage("Parsing %s...", filename
);
557 if (!doc
.Load(filename
)) {
558 LogError("can't load %s", filename
);
562 // start processing the XML file
563 if (doc
.GetRoot()->GetName() != "GCC_XML") {
564 LogError("invalid root node for %s", filename
);
568 wxToResolveTypeHashMap toResolveTypes
;
569 wxClassMemberIdHashMap members
;
570 wxTypeIdHashMap types
;
571 wxTypeIdHashMap files
;
573 // prealloc quite a lot of memory!
574 m_classes
.Alloc(ESTIMATED_NUM_CLASSES
);
576 // build a list of wx classes and in general of all existent types
577 child
= doc
.GetRoot()->GetChildren();
580 const wxString
& n
= child
->GetName();
582 unsigned long id
= 0;
583 if (!getID(&id
, child
->GetAttribute("id")) || (id
== 0 && n
!= "File")) {
585 // NOTE: <File> nodes can have an id == "f0"...
587 LogError("Invalid id for node %s: %s", n
, child
->GetAttribute("id"));
593 wxString cname
= child
->GetAttribute("name");
594 if (cname
.IsEmpty()) {
595 LogError("Invalid empty name for '%s' node", n
);
599 // only register wx classes (do remember also the IDs of their members)
600 if (cname
.StartsWith("wx"))
602 // NB: "file" attribute contains an ID value that we'll resolve later
603 m_classes
.Add(wxClass(cname
, child
->GetAttribute("file")));
605 const wxString
& ids
= child
->GetAttribute("members");
608 if (child
->GetAttribute("incomplete") != "1") {
609 LogError("Invalid member IDs for '%s' class node: %s",
610 cname
, child
->GetAttribute("id"));
613 //else: don't warn the user; it looks like "incomplete" classes
614 // never have any member...
618 // decode the non-empty list of IDs:
619 if (!getMemberIDs(&members
, &m_classes
.Last(), ids
)) {
620 LogError("Invalid member IDs for '%s' class node: %s",
621 cname
, child
->GetAttribute("id"));
627 // register this class also as possible return/argument type:
630 else if (n
== "PointerType" || n
== "ReferenceType" ||
631 n
== "CvQualifiedType" || n
== "ArrayType")
633 unsigned long type
= 0;
634 if (!getID(&type
, child
->GetAttribute("type")) || type
== 0) {
635 LogError("Invalid type for node %s: %s", n
, child
->GetAttribute("type"));
639 unsigned long attr
= 0;
640 if (n
== "PointerType")
641 attr
= ATTRIB_POINTER
;
642 else if (n
== "ReferenceType")
643 attr
= ATTRIB_REFERENCE
;
644 else if (n
== "CvQualifiedType" && child
->GetAttribute("const") == "1")
646 else if (n
== "ArrayType")
649 // these nodes make reference to other types... we'll resolve them later
650 toResolveTypes
[id
] = toResolveTypeItem(type
, attr
);
652 else if (n
== "FunctionType" || n
== "MethodType")
655 TODO: parsing FunctionType and MethodType nodes is not as easy
656 as for other "simple" types.
660 wxXmlNode
*arg
= child
->GetChildren();
663 if (arg
->GetName() == "Argument")
664 argstr
+= arg
->GetAttribute("type") + ", ";
665 arg
= arg
->GetNext();
668 if (argstr
.Len() > 0)
669 argstr
= argstr
.Left(argstr
.Len()-2);
671 // these nodes make reference to other types... we'll resolve them later
672 //toResolveTypes[id] = toResolveTypeItem(ret, 0);
673 types
[id
] = child
->GetAttribute("returns") + "(" + argstr
+ ")";
675 else if (n
== "File")
677 if (!child
->GetAttribute("id").StartsWith("f")) {
678 LogError("Unexpected file ID: %s", child
->GetAttribute("id"));
682 // just ignore this node... all file IDs/names were already parsed
683 files
[id
] = child
->GetAttribute("name");
687 // we register everything else as a possible return/argument type:
688 const wxString
& name
= child
->GetAttribute("name");
693 //typeNames.Add(name);
698 // this may happen with unnamed structs/union, special ctors,
699 // or other exotic things which we are not interested to, since
700 // they're never used as return/argument types by wxWidgets methods
703 LogWarning("Type node '%s' with ID '%s' does not have name attribute",
704 n
, child
->GetAttribute("id"));
710 child
= child
->GetNext();
712 // give feedback to the user about the progress...
713 if ((++nodes%PROGRESS_RATE
)==0) ShowProgress();
716 // some nodes with IDs referenced by methods as return/argument types, do reference
717 // in turn o ther nodes (see PointerType, ReferenceType and CvQualifierType above);
718 // thus we need to resolve their name iteratively:
719 while (toResolveTypes
.size()>0)
722 LogMessage("%d types were collected; %d types need yet to be resolved...",
723 types
.size(), toResolveTypes
.size());
725 for (wxToResolveTypeHashMap::iterator i
= toResolveTypes
.begin();
726 i
!= toResolveTypes
.end();)
728 unsigned long id
= i
->first
;
729 unsigned long referenced
= i
->second
.ref
;
731 wxTypeIdHashMap::iterator primary
= types
.find(referenced
);
732 if (primary
!= types
.end())
734 // this to-resolve-type references a "primary" type
736 wxString newtype
= primary
->second
;
737 int attribs
= i
->second
.attribs
;
739 // attribs may contain a combination of ATTRIB_* flags:
740 if (attribs
& ATTRIB_CONST
)
741 newtype
= "const " + newtype
;
742 if (attribs
& ATTRIB_REFERENCE
)
743 newtype
= newtype
+ "&";
744 if (attribs
& ATTRIB_POINTER
)
745 newtype
= newtype
+ "*";
746 if (attribs
& ATTRIB_ARRAY
)
747 newtype
= newtype
+ "[]";
749 // add the resolved type to the list of "primary" types
752 // this one has been resolved; erase it through its iterator!
753 toResolveTypes
.erase(i
);
755 // now iterator i is invalid; assign it again to the beginning
756 i
= toResolveTypes
.begin();
760 // then search in the referenced types themselves:
761 wxToResolveTypeHashMap::iterator idx2
= toResolveTypes
.find(referenced
);
762 if (idx2
!= toResolveTypes
.end())
764 // merge this to-resolve-type with the idx2->second type
765 i
->second
.ref
= idx2
->second
.ref
;
766 i
->second
.attribs
|= idx2
->second
.attribs
;
768 // this type will eventually be solved in the next while() iteration
773 LogError("Cannot solve '%s' reference type!", referenced
);
780 // resolve header names
781 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
783 unsigned long fileID
= 0;
784 if (!getID(&fileID
, m_classes
[i
].GetHeader()) || fileID
== 0) {
785 LogError("invalid header id: %s", m_classes
[i
].GetHeader());
790 wxTypeIdHashMap::const_iterator idx
= files
.find(fileID
);
791 if (idx
== files
.end())
794 LogError("couldn't find file ID '%s'", m_classes
[i
].GetHeader());
797 m_classes
[i
].SetHeader(idx
->second
);
800 // build the list of the wx methods
801 child
= doc
.GetRoot()->GetChildren();
804 wxString n
= child
->GetName();
806 // only register public methods
807 if (child
->GetAttribute("access") == "public" &&
808 (n
== "Method" || n
== "Constructor" || n
== "Destructor" || n
== "OperatorMethod"))
810 unsigned long id
= 0;
811 if (!getID(&id
, child
->GetAttribute("id"))) {
812 LogError("invalid ID for node '%s' with ID '%s'", n
, child
->GetAttribute("id"));
816 wxClassMemberIdHashMap::const_iterator it
= members
.find(id
);
817 if (it
!= members
.end())
819 wxClass
*p
= it
->second
;
821 // this <Method> node is a method of the i-th class!
823 if (!ParseMethod(child
, types
, newfunc
)) {
824 LogError("The method '%s' could not be added to class '%s'",
825 child
->GetAttribute("demangled"), p
->GetName());
829 if (newfunc
.IsCtor() && !p
->IsValidCtorForThisClass(newfunc
)) {
830 LogError("The method '%s' does not seem to be a ctor for '%s'",
831 newfunc
.GetName(), p
->GetName());
834 if (newfunc
.IsDtor() && !p
->IsValidDtorForThisClass(newfunc
)) {
835 LogError("The method '%s' does not seem to be a dtor for '%s'",
836 newfunc
.GetName(), p
->GetName());
840 p
->AddMethod(newfunc
);
844 child
= child
->GetNext();
846 // give feedback to the user about the progress...
847 if ((++nodes%PROGRESS_RATE
)==0) ShowProgress();
850 if (!CheckParseResults())
856 bool wxXmlGccInterface::ParseMethod(const wxXmlNode
*p
,
857 const wxTypeIdHashMap
& types
,
861 wxString name
= p
->GetAttribute("name").Strip(wxString::both
);
862 if (p
->GetName() == "Destructor")
864 else if (p
->GetName() == "OperatorMethod")
865 name
= "operator" + name
;
867 // resolve return type
869 unsigned long retid
= 0;
870 if (!getID(&retid
, p
->GetAttribute("returns")) || retid
== 0)
872 if (p
->GetName() != "Destructor" && p
->GetName() != "Constructor") {
873 LogError("Empty return ID for method '%s', with ID '%s'",
874 name
, p
->GetAttribute("id"));
880 wxTypeIdHashMap::const_iterator retidx
= types
.find(retid
);
881 if (retidx
== types
.end()) {
882 LogError("Could not find return type ID '%s'", retid
);
886 ret
= wxType(retidx
->second
);
888 LogError("Invalid return type '%s' for method '%s', with ID '%s'",
889 retidx
->second
, name
, p
->GetAttribute("id"));
894 // resolve argument types
895 wxArgumentTypeArray argtypes
;
896 wxXmlNode
*arg
= p
->GetChildren();
899 if (arg
->GetName() == "Argument")
901 unsigned long id
= 0;
902 if (!getID(&id
, arg
->GetAttribute("type")) || id
== 0) {
903 LogError("Invalid argument type ID '%s' for method '%s' with ID %s",
904 arg
->GetAttribute("type"), name
, p
->GetAttribute("id"));
908 wxTypeIdHashMap::const_iterator idx
= types
.find(id
);
909 if (idx
== types
.end()) {
910 LogError("Could not find argument type ID '%s'", id
);
914 argtypes
.Add(wxArgumentType(idx
->second
, arg
->GetAttribute("default")));
917 arg
= arg
->GetNext();
920 m
.SetReturnType(ret
);
922 m
.SetArgumentTypes(argtypes
);
923 m
.SetConst(p
->GetAttribute("const") == "1");
924 m
.SetStatic(p
->GetAttribute("static") == "1");
926 // NOTE: gccxml is smart enough to mark as virtual those functions
927 // which are declared virtual in base classes but don't have
928 // the "virtual" keyword explicitely indicated in the derived
929 // classes... so we don't need any further logic for virtuals
931 m
.SetVirtual(p
->GetAttribute("virtual") == "1");
932 m
.SetPureVirtual(p
->GetAttribute("pure_virtual") == "1");
933 m
.SetDeprecated(p
->GetAttribute("attributes") == "deprecated");
936 LogError("The prototype '%s' is not valid!", m
.GetAsString());
945 // ----------------------------------------------------------------------------
946 // wxXmlDoxygenInterface global helpers
947 // ----------------------------------------------------------------------------
949 static wxString
GetTextFromChildren(const wxXmlNode
*n
)
955 // <a><b>this</b> is a <b>string</b></a>
964 // unlike wxXmlNode::GetNodeContent() which would return " is a "
965 // this function returns "this is a string"
967 wxXmlNode
*ref
= n
->GetChildren();
969 if (ref
->GetType() == wxXML_ELEMENT_NODE
)
970 text
+= ref
->GetNodeContent();
971 else if (ref
->GetType() == wxXML_TEXT_NODE
)
972 text
+= ref
->GetContent();
974 LogWarning("Unexpected node type while getting text from '%s' node", n
->GetName());
976 ref
= ref
->GetNext();
982 static bool HasTextNodeContaining(const wxXmlNode
*parent
, const wxString
& name
)
987 wxXmlNode
*p
= parent
->GetChildren();
990 switch (p
->GetType())
992 case wxXML_TEXT_NODE
:
993 if (p
->GetContent() == name
)
997 case wxXML_ELEMENT_NODE
:
998 // recurse into this node...
999 if (HasTextNodeContaining(p
, name
))
1014 static const wxXmlNode
* FindNodeNamed(const wxXmlNode
* parent
, const wxString
& name
)
1019 const wxXmlNode
*p
= parent
->GetChildren();
1022 if (p
->GetName() == name
)
1025 // search recursively in the children of this node
1026 const wxXmlNode
*ret
= FindNodeNamed(p
, name
);
1036 int GetAvailabilityFor(const wxXmlNode
*node
)
1038 // identify <onlyfor> custom XML tags
1039 const wxXmlNode
* onlyfor
= FindNodeNamed(node
, "onlyfor");
1041 return wxPORT_UNKNOWN
;
1043 wxArrayString ports
= wxSplit(onlyfor
->GetNodeContent(), ',');
1044 int nAvail
= wxPORT_UNKNOWN
;
1045 for (unsigned int i
=0; i
< ports
.GetCount(); i
++)
1047 if (!ports
[i
].StartsWith("wx")) {
1048 LogError("unexpected port ID '%s'", ports
[i
]);
1052 nAvail
|= wxPlatformInfo::GetPortId(ports
[i
].Mid(2));
1059 // ----------------------------------------------------------------------------
1060 // wxXmlDoxygenInterface
1061 // ----------------------------------------------------------------------------
1063 bool wxXmlDoxygenInterface::Parse(const wxString
& filename
)
1065 wxXmlDocument index
;
1066 wxXmlNode
*compound
;
1068 LogMessage("Parsing %s...", filename
);
1070 if (!index
.Load(filename
)) {
1071 LogError("can't load %s", filename
);
1075 // start processing the index:
1076 if (index
.GetRoot()->GetName() != "doxygenindex") {
1077 LogError("invalid root node for %s", filename
);
1081 m_classes
.Alloc(ESTIMATED_NUM_CLASSES
);
1083 // process files referenced by this index file
1084 compound
= index
.GetRoot()->GetChildren();
1087 if (compound
->GetName() == "compound" &&
1088 compound
->GetAttribute("kind") == "class")
1090 wxString refid
= compound
->GetAttribute("refid");
1092 wxFileName
fn(filename
);
1093 if (!ParseCompoundDefinition(fn
.GetPath(wxPATH_GET_SEPARATOR
) + refid
+ ".xml"))
1097 compound
= compound
->GetNext();
1101 if (!CheckParseResults())
1107 bool wxXmlDoxygenInterface::ParseCompoundDefinition(const wxString
& filename
)
1114 LogMessage("Parsing %s...", filename
);
1116 if (!doc
.Load(filename
)) {
1117 LogError("can't load %s", filename
);
1121 // start processing this compound definition XML
1122 if (doc
.GetRoot()->GetName() != "doxygen") {
1123 LogError("invalid root node for %s", filename
);
1127 // build a list of wx classes
1128 child
= doc
.GetRoot()->GetChildren();
1131 if (child
->GetName() == "compounddef" &&
1132 child
->GetAttribute("kind") == "class")
1136 wxString absoluteFile
, header
;
1138 wxXmlNode
*subchild
= child
->GetChildren();
1141 if (subchild
->GetName() == "sectiondef" &&
1142 subchild
->GetAttribute("kind") == "public-func")
1145 wxXmlNode
*membernode
= subchild
->GetChildren();
1148 if (membernode
->GetName() == "memberdef" &&
1149 membernode
->GetAttribute("kind") == "function")
1153 if (!ParseMethod(membernode
, m
, header
)) {
1154 LogError("The method '%s' could not be added to class '%s'",
1155 m
.GetName(), klass
.GetName());
1159 if (absoluteFile
.IsEmpty())
1160 absoluteFile
= header
;
1161 else if (header
!= absoluteFile
)
1163 LogError("The method '%s' is documented in a different "
1164 "file from others (which belong to '%s') ?",
1165 header
, absoluteFile
);
1172 membernode
= membernode
->GetNext();
1175 // all methods of this class were taken from the header "absoluteFile":
1176 klass
.SetHeader(absoluteFile
);
1178 else if (subchild
->GetName() == "compoundname")
1180 klass
.SetName(subchild
->GetNodeContent());
1182 /*else if (subchild->GetName() == "includes")
1184 // NOTE: we'll get the header from the <location> tags
1185 // scattered inside <memberdef> tags instead of
1186 // this <includes> tag since it does not contain
1187 // the absolute path of the header
1189 klass.SetHeader(subchild->GetNodeContent());
1191 else if (subchild
->GetName() == "detaileddescription")
1193 // identify <onlyfor> custom XML tags
1194 klass
.SetAvailability(GetAvailabilityFor(subchild
));
1197 subchild
= subchild
->GetNext();
1202 m_classes
.Add(klass
);
1204 LogWarning("discarding class '%s' with %d methods...",
1205 klass
.GetName(), klass
.GetMethodCount());
1208 child
= child
->GetNext();
1210 // give feedback to the user about the progress...
1211 if ((++nodes%PROGRESS_RATE
)==0) ShowProgress();
1217 bool wxXmlDoxygenInterface::ParseMethod(const wxXmlNode
* p
, wxMethod
& m
, wxString
& header
)
1219 wxArgumentTypeArray args
;
1222 wxXmlNode
*child
= p
->GetChildren();
1225 if (child
->GetName() == "name")
1226 m
.SetName(child
->GetNodeContent());
1227 else if (child
->GetName() == "type")
1228 m
.SetReturnType(wxType(GetTextFromChildren(child
)));
1229 else if (child
->GetName() == "param")
1231 wxString typestr
, namestr
, defstr
, arrstr
;
1232 wxXmlNode
*n
= child
->GetChildren();
1235 if (n
->GetName() == "type")
1236 // if the <type> node has children, they should be all TEXT and <ref> nodes
1237 // and we need to take the text they contain, in the order they appear
1238 typestr
= GetTextFromChildren(n
);
1239 else if (n
->GetName() == "declname")
1240 namestr
= GetTextFromChildren(n
);
1241 else if (n
->GetName() == "defval")
1242 defstr
= GetTextFromChildren(n
).Strip(wxString::both
);
1243 else if (n
->GetName() == "array")
1244 arrstr
= GetTextFromChildren(n
);
1249 if (typestr
.IsEmpty()) {
1250 LogError("cannot find type node for a param in method '%s'", m
.GetName());
1254 wxArgumentType
newarg(typestr
+ arrstr
, defstr
, namestr
);
1256 // can we use preprocessor output to transform the default value
1257 // into the same form which gets processed by wxXmlGccInterface?
1258 wxStringHashMap::const_iterator it
= m_preproc
.find(defstr
);
1259 if (it
!= m_preproc
.end())
1260 newarg
.SetDefaultValue(defstr
, it
->second
);
1264 else if (child
->GetName() == "location")
1267 if (child
->GetAttribute("line").ToLong(&line
))
1268 m
.SetLocation((int)line
);
1269 header
= child
->GetAttribute("file");
1271 else if (child
->GetName() == "detaileddescription")
1273 // when a method has a @deprecated tag inside its description,
1274 // Doxygen outputs somewhere nested inside <detaileddescription>
1275 // a <xreftitle>Deprecated</xreftitle> tag.
1276 m
.SetDeprecated(HasTextNodeContaining(child
, "Deprecated"));
1278 // identify <onlyfor> custom XML tags
1279 m
.SetAvailability(GetAvailabilityFor(child
));
1282 child
= child
->GetNext();
1285 m
.SetArgumentTypes(args
);
1286 m
.SetConst(p
->GetAttribute("const")=="yes");
1287 m
.SetStatic(p
->GetAttribute("static")=="yes");
1289 // NOTE: Doxygen is smart enough to mark as virtual those functions
1290 // which are declared virtual in base classes but don't have
1291 // the "virtual" keyword explicitely indicated in the derived
1292 // classes... so we don't need any further logic for virtuals
1294 m
.SetVirtual(p
->GetAttribute("virt")=="virtual");
1295 m
.SetPureVirtual(p
->GetAttribute("virt")=="pure-virtual");
1298 LogError("The prototype '%s' is not valid!", m
.GetAsString());