]> git.saurik.com Git - wxWidgets.git/blob - utils/ifacecheck/src/xmlparser.cpp
mention that both gccxml and doxygen are smart enough to mark as virtual functions...
[wxWidgets.git] / utils / ifacecheck / src / xmlparser.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: xmlparser.cpp
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 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 // for all others, include the necessary headers
19 #ifndef WX_PRECOMP
20 #endif
21
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"
27 #include <errno.h>
28
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)
34
35
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
38
39
40 // defined in ifacecheck.cpp
41 extern bool g_verbose;
42
43
44
45 // ----------------------------------------------------------------------------
46 // wxType
47 // ----------------------------------------------------------------------------
48
49 wxType wxEmptyType;
50
51 void wxType::SetTypeFromString(const wxString& t)
52 {
53 /*
54 TODO: optimize the following code writing a single function
55 which works at char-level and does everything in a single pass
56 */
57
58 m_strType = t;
59
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"
63
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(" &", "&");
70
71 while (m_strType.Contains(" "))
72 m_strType.Replace(" ", " "); // do it once again
73
74 m_strType.Replace(" ,", ",");
75
76 m_strType = m_strType.Strip(wxString::both);
77 }
78
79 bool wxType::IsOk() const
80 {
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
84
85 return !GetClean().IsEmpty();
86 }
87
88 wxString wxType::GetClean() const
89 {
90 wxString ret(m_strType);
91 ret.Replace("const", "");
92 ret.Replace("static", "");
93 ret.Replace("*", "");
94 ret.Replace("&", "");
95 ret.Replace("[]", "");
96 return ret.Strip(wxString::both);
97 }
98
99 bool wxType::operator==(const wxType& m) const
100 {
101 // brain-dead comparison:
102
103 if (GetClean() == m.GetClean() &&
104 IsConst() == m.IsConst() &&
105 IsStatic() == m.IsStatic() &&
106 IsPointer() == m.IsPointer() &&
107 IsReference() == m.IsReference())
108 return true;
109
110 return false;
111 }
112
113
114 // ----------------------------------------------------------------------------
115 // wxArgumentType
116 // ----------------------------------------------------------------------------
117
118 void wxArgumentType::SetDefaultValue(const wxString& defval)
119 {
120 m_strDefaultValue=defval.Strip(wxString::both);
121
122 // in order to make valid&simple comparison on argument defaults,
123 // we reduce some of the multiple forms in which the same things may appear
124 // to a single form:
125 m_strDefaultValue.Replace("0u", "0");
126
127 if (IsPointer())
128 m_strDefaultValue.Replace("0", "NULL");
129 else
130 m_strDefaultValue.Replace("NULL", "0");
131
132
133 if (m_strDefaultValue.Contains("wxGetTranslation"))
134 m_strDefaultValue = "_(TOFIX)"; // TODO: wxGetTranslation gives problems to gccxml
135 }
136
137 bool wxArgumentType::operator==(const wxArgumentType& m) const
138 {
139 if ((const wxType&)(*this) != (const wxType&)m)
140 return false;
141
142 if (m_strDefaultValue != m.m_strDefaultValue)
143 return false;
144
145 // we deliberately avoid checks on the argument name
146
147 return true;
148 }
149
150
151 // ----------------------------------------------------------------------------
152 // wxMethod
153 // ----------------------------------------------------------------------------
154
155 bool wxMethod::IsOk() const
156 {
157 // NOTE: m_retType can be a wxEmptyType, and means that this method
158 // is a ctor or a dtor.
159 if (!m_retType.IsOk() && m_retType!=wxEmptyType) {
160 LogError("'%s' method has invalid return type: %s", m_retType.GetAsString());
161 return false;
162 }
163
164 if (m_strName.IsEmpty())
165 return false;
166
167 // a function can't be both const and static or virtual and static!
168 if ((m_bConst && m_bStatic) || ((m_bVirtual || m_bPureVirtual) && m_bStatic)) {
169 LogError("'%s' method can't be both const/static or virtual/static", m_strName);
170 return false;
171 }
172
173 wxASSERT((m_bVirtual && m_bPureVirtual) || !m_bVirtual);
174
175 for (unsigned int i=0; i<m_args.GetCount(); i++)
176 if (!m_args[i].IsOk()) {
177 LogError("'%s' method has invalid %d-th argument type: %s",
178 m_strName, i+1, m_args[i].GetAsString());
179 return false;
180 }
181
182 // NB: the default value of the arguments can contain pretty much everything
183 // (think to e.g. wxPoint(3+4/2,0) or *wxBLACK or someClass<type>)
184 // so we don't do any test on their contents
185 if (m_args.GetCount()>0)
186 {
187 bool previousArgHasDefault = m_args[0].HasDefaultValue();
188 for (unsigned int i=1; i<m_args.GetCount(); i++)
189 {
190 if (previousArgHasDefault && !m_args[i].HasDefaultValue()) {
191 LogError("'%s' method has %d-th argument which has no default value "
192 "(while the previous one had one!)",
193 m_strName, i+1);
194 return false;
195 }
196
197 previousArgHasDefault = m_args[i].HasDefaultValue();
198 }
199 }
200
201 return true;
202 }
203
204 bool wxMethod::operator==(const wxMethod& m) const
205 {
206 if (GetReturnType() != m.GetReturnType() ||
207 GetName() != m.GetName() ||
208 IsConst() != m.IsConst() ||
209 IsStatic() != m.IsStatic() ||
210 IsVirtual() != m.IsVirtual() ||
211 IsPureVirtual() != m.IsPureVirtual() ||
212 IsDeprecated() != m.IsDeprecated())
213 return false;
214
215 if (m_args.GetCount()!=m.m_args.GetCount())
216 return false;
217
218 for (unsigned int i=0; i<m_args.GetCount(); i++)
219 if (m_args[i] != m.m_args[i])
220 return false;
221
222 return true;
223 }
224
225 wxString wxMethod::GetAsString(bool bWithArgumentNames) const
226 {
227 wxString ret;
228
229 if (m_retType!=wxEmptyType)
230 ret += m_retType.GetAsString() + " ";
231 //else; this is a ctor or dtor
232
233 ret += m_strName + "(";
234
235 for (unsigned int i=0; i<m_args.GetCount(); i++)
236 {
237 ret += m_args[i].GetAsString();
238
239 const wxString& name = m_args[i].GetArgumentName();
240 if (bWithArgumentNames && !name.IsEmpty())
241 ret += " " + name;
242
243 const wxString& def = m_args[i].GetDefaultValue();
244 if (!def.IsEmpty())
245 ret += " = " + def;
246
247 ret += ", ";
248 }
249
250 if (m_args.GetCount()>0)
251 ret = ret.Left(ret.Len()-2);
252
253 ret += ")";
254
255 if (m_bConst)
256 ret += " const";
257 if (m_bStatic)
258 ret = "static " + ret;
259 if (m_bVirtual || m_bPureVirtual)
260 ret = "virtual " + ret;
261 if (m_bPureVirtual)
262 ret = ret + " = 0";
263
264 // in doxygen headers we don't need wxDEPRECATED:
265 //if (m_bDeprecated)
266 // ret = "wxDEPRECATED( " + ret + " )";
267
268 return ret;
269 }
270
271 void wxMethod::Dump(wxTextOutputStream& stream) const
272 {
273 stream << "[" + m_retType.GetAsString() + "]";
274 stream << "[" + m_strName + "]";
275
276 for (unsigned int i=0; i<m_args.GetCount(); i++)
277 stream << "[" + m_args[i].GetAsString() + " " + m_args[i].GetArgumentName() +
278 "=" + m_args[i].GetDefaultValue() + "]";
279
280 if (IsConst())
281 stream << " CONST";
282 if (IsStatic())
283 stream << " STATIC";
284 if (IsVirtual())
285 stream << " VIRTUAL";
286 if (IsPureVirtual())
287 stream << " PURE-VIRTUAL";
288 if (IsDeprecated())
289 stream << " DEPRECATED";
290
291 // no final newline
292 }
293
294 // ----------------------------------------------------------------------------
295 // wxClass
296 // ----------------------------------------------------------------------------
297
298 wxString wxClass::GetNameWithoutTemplate() const
299 {
300 // NB: I'm not sure this is the right terminology for this function!
301
302 if (m_strName.Contains("<"))
303 return m_strName.Left(m_strName.Find("<"));
304 return m_strName;
305 }
306
307 bool wxClass::IsValidCtorForThisClass(const wxMethod& m) const
308 {
309 // remember that e.g. the ctor for wxWritableCharTypeBuffer<wchar_t> is
310 // named wxWritableCharTypeBuffer, without the <...> part!
311
312 if (m.IsCtor() && m.GetName() == GetNameWithoutTemplate())
313 return true;
314
315 return false;
316 }
317
318 bool wxClass::IsValidDtorForThisClass(const wxMethod& m) const
319 {
320 if (m.IsDtor() && m.GetName() == "~" + GetNameWithoutTemplate())
321 return true;
322
323 return false;
324 }
325
326 void wxClass::Dump(wxTextOutputStream& out) const
327 {
328 out << m_strName + "\n";
329
330 for (unsigned int i=0; i<m_methods.GetCount(); i++) {
331
332 // dump all our methods
333 out << "|- ";
334 m_methods[i].Dump(out);
335 out << "\n";
336 }
337
338 out << "\n";
339 }
340
341 bool wxClass::CheckConsistency() const
342 {
343 for (unsigned int i=0; i<m_methods.GetCount(); i++)
344 for (unsigned int j=0; j<m_methods.GetCount(); j++)
345 if (i!=j && m_methods[i] == m_methods[j])
346 {
347 LogError("class %s has two methods with the same prototype: '%s'",
348 m_strName, m_methods[i].GetAsString());
349 return false;
350 }
351
352 return true;
353 }
354
355 const wxMethod* wxClass::FindMethod(const wxMethod& m) const
356 {
357 for (unsigned int i=0; i<m_methods.GetCount(); i++)
358 if (m_methods[i] == m)
359 return &m_methods[i];
360 return NULL;
361 }
362
363 wxMethodPtrArray wxClass::FindMethodNamed(const wxString& name) const
364 {
365 wxMethodPtrArray ret;
366
367 for (unsigned int i=0; i<m_methods.GetCount(); i++)
368 if (m_methods[i].GetName() == name)
369 ret.Add(&m_methods[i]);
370
371 return ret;
372 }
373
374
375 // ----------------------------------------------------------------------------
376 // wxXmlInterface
377 // ----------------------------------------------------------------------------
378
379 WX_DEFINE_SORTED_ARRAY(wxClass*, wxSortedClassArray);
380
381 int CompareWxClassObjects(wxClass *item1, wxClass *item2)
382 {
383 // sort alphabetically
384 return item1->GetName().Cmp(item2->GetName());
385 }
386
387 void wxXmlInterface::Dump(const wxString& filename)
388 {
389 wxFFileOutputStream apioutput( filename );
390 wxTextOutputStream apiout( apioutput );
391
392 // dump the classes in alphabetical order
393 wxSortedClassArray sorted(CompareWxClassObjects);
394 sorted.Alloc(m_classes.GetCount());
395 for (unsigned int i=0; i<m_classes.GetCount(); i++)
396 sorted.Add(&m_classes[i]);
397
398 // now they have been sorted
399 for (unsigned int i=0; i<sorted.GetCount(); i++)
400 sorted[i]->Dump(apiout);
401 }
402
403 bool wxXmlInterface::CheckParseResults() const
404 {
405 // this check can be quite slow, so do it only for debug releases:
406 #ifdef __WXDEBUG__
407 for (unsigned int i=0; i<m_classes.GetCount(); i++)
408 if (!m_classes[i].CheckConsistency())
409 return false;
410 #endif
411
412 return true;
413 }
414
415 wxClassPtrArray wxXmlInterface::FindClassesDefinedIn(const wxString& headerfile) const
416 {
417 wxClassPtrArray ret;
418
419 for (unsigned int i=0; i<m_classes.GetCount(); i++)
420 if (m_classes[i].GetHeader() == headerfile)
421 ret.Add(&m_classes[i]);
422
423 return ret;
424 }
425
426
427 // ----------------------------------------------------------------------------
428 // wxXmlGccInterface helper declarations
429 // ----------------------------------------------------------------------------
430
431 #define ATTRIB_CONST 1
432 #define ATTRIB_REFERENCE 2
433 #define ATTRIB_POINTER 4
434 #define ATTRIB_ARRAY 8
435
436 #define GCCXML_BASE 35
437
438 class toResolveTypeItem
439 {
440 public:
441 toResolveTypeItem() { attribs=0; }
442 toResolveTypeItem(unsigned int refID, unsigned int attribint)
443 : ref(refID), attribs(attribint) {}
444
445 unsigned long ref, attribs;
446 };
447
448 #if 1
449
450 // for wxToResolveTypeHashMap, keys == gccXML IDs and values == toResolveTypeItem
451 WX_DECLARE_HASH_MAP( unsigned long, toResolveTypeItem,
452 wxIntegerHash, wxIntegerEqual,
453 wxToResolveTypeHashMap );
454
455 // for wxClassMemberIdHashMap, keys == gccXML IDs and values == wxClass which owns that member ID
456 WX_DECLARE_HASH_MAP( unsigned long, wxClass*,
457 wxIntegerHash, wxIntegerEqual,
458 wxClassMemberIdHashMap );
459 #else
460 #include <map>
461 typedef std::map<unsigned long, toResolveTypeItem> wxToResolveTypeHashMap;
462 #endif
463
464
465 // utility to parse gccXML ID values;
466 // this function is equivalent to wxString(str).Mid(1).ToULong(&id, GCCXML_BASE)
467 // but is a little bit faster
468 bool getID(unsigned long *id, const wxStringCharType* str)
469 {
470 wxStringCharType *end;
471 #if wxUSE_UNICODE_UTF8
472 unsigned long val = strtoul(str+1, &end, GCCXML_BASE);
473 #else
474 unsigned long val = wcstoul(str+1, &end, GCCXML_BASE);
475 #endif
476
477 // return true only if scan was stopped by the terminating NUL and
478 // if the string was not empty to start with and no under/overflow
479 // occurred:
480 if ( *end != '\0' || end == str+1 || errno == ERANGE || errno == EINVAL )
481 return false;
482
483 *id = val;
484 return true;
485 }
486
487 // utility specialized to parse efficiently the gccXML list of IDs which occur
488 // in nodes like <Class> ones... i.e. numeric values separed by " _" token
489 bool getMemberIDs(wxClassMemberIdHashMap* map, wxClass* p, const wxStringCharType* str)
490 {
491 #if wxUSE_UNICODE_UTF8
492 size_t len = strlen(str);
493 #else
494 size_t len = wcslen(str);
495 #endif
496
497 if (len == 0 || str[0] != '_')
498 return false;
499
500 const wxStringCharType *curpos = str,
501 *end = str + len;
502 wxStringCharType *nexttoken;
503
504 while (curpos < end)
505 {
506 // curpos always points to the underscore of the next token to parse:
507 #if wxUSE_UNICODE_UTF8
508 unsigned long id = strtoul(curpos+1, &nexttoken, GCCXML_BASE);
509 #else
510 unsigned long id = wcstoul(curpos+1, &nexttoken, GCCXML_BASE);
511 #endif
512 if ( *nexttoken != ' ' || errno == ERANGE || errno == EINVAL )
513 return false;
514
515 // advance current position
516 curpos = nexttoken + 1;
517
518 // add this ID to the hashmap
519 wxClassMemberIdHashMap::value_type v(id, p);
520 map->insert(v);
521 }
522
523 return true;
524 }
525
526
527 // ----------------------------------------------------------------------------
528 // wxXmlGccInterface
529 // ----------------------------------------------------------------------------
530
531 bool wxXmlGccInterface::Parse(const wxString& filename)
532 {
533 wxXmlDocument doc;
534 wxXmlNode *child;
535 int nodes = 0;
536
537 LogMessage("Parsing %s...", filename);
538
539 if (!doc.Load(filename)) {
540 LogError("can't load %s", filename);
541 return false;
542 }
543
544 // start processing the XML file
545 if (doc.GetRoot()->GetName() != "GCC_XML") {
546 LogError("invalid root node for %s", filename);
547 return false;
548 }
549
550 wxToResolveTypeHashMap toResolveTypes;
551 wxClassMemberIdHashMap members;
552 wxTypeIdHashMap types;
553 wxTypeIdHashMap files;
554
555 // prealloc quite a lot of memory!
556 m_classes.Alloc(ESTIMATED_NUM_CLASSES);
557
558 // build a list of wx classes and in general of all existent types
559 child = doc.GetRoot()->GetChildren();
560 while (child)
561 {
562 const wxString& n = child->GetName();
563
564 unsigned long id = 0;
565 if (!getID(&id, child->GetAttribute("id")) || (id == 0 && n != "File")) {
566
567 // NOTE: <File> nodes can have an id == "f0"...
568
569 LogError("Invalid id for node %s: %s", n, child->GetAttribute("id"));
570 return false;
571 }
572
573 if (n == "Class")
574 {
575 wxString cname = child->GetAttribute("name");
576 if (cname.IsEmpty()) {
577 LogError("Invalid empty name for '%s' node", n);
578 return false;
579 }
580
581 // only register wx classes (do remember also the IDs of their members)
582 if (cname.StartsWith("wx"))
583 {
584 // NB: "file" attribute contains an ID value that we'll resolve later
585 m_classes.Add(wxClass(cname, child->GetAttribute("file")));
586
587 const wxString& ids = child->GetAttribute("members");
588 if (ids.IsEmpty())
589 {
590 if (child->GetAttribute("incomplete") != "1") {
591 LogError("Invalid member IDs for '%s' class node: %s",
592 cname, child->GetAttribute("id"));
593 return false;
594 }
595 //else: don't warn the user; it looks like "incomplete" classes
596 // never have any member...
597 }
598 else
599 {
600 // decode the non-empty list of IDs:
601 if (!getMemberIDs(&members, &m_classes.Last(), ids)) {
602 LogError("Invalid member IDs for '%s' class node: %s",
603 cname, child->GetAttribute("id"));
604 return false;
605 }
606 }
607 }
608
609 // register this class also as possible return/argument type:
610 types[id] = cname;
611 }
612 else if (n == "PointerType" || n == "ReferenceType" ||
613 n == "CvQualifiedType" || n == "ArrayType")
614 {
615 unsigned long type = 0;
616 if (!getID(&type, child->GetAttribute("type")) || type == 0) {
617 LogError("Invalid type for node %s: %s", n, child->GetAttribute("type"));
618 return false;
619 }
620
621 unsigned long attr = 0;
622 if (n == "PointerType")
623 attr = ATTRIB_POINTER;
624 else if (n == "ReferenceType")
625 attr = ATTRIB_REFERENCE;
626 else if (n == "CvQualifiedType" && child->GetAttribute("const") == "1")
627 attr = ATTRIB_CONST;
628 else if (n == "ArrayType")
629 attr = ATTRIB_ARRAY;
630
631 // these nodes make reference to other types... we'll resolve them later
632 toResolveTypes[id] = toResolveTypeItem(type, attr);
633 }
634 else if (n == "FunctionType" || n == "MethodType")
635 {
636 /*
637 TODO: parsing FunctionType and MethodType nodes is not as easy
638 as for other "simple" types.
639 */
640
641 wxString argstr;
642 wxXmlNode *arg = child->GetChildren();
643 while (arg)
644 {
645 if (arg->GetName() == "Argument")
646 argstr += arg->GetAttribute("type") + ", ";
647 arg = arg->GetNext();
648 }
649
650 if (argstr.Len() > 0)
651 argstr = argstr.Left(argstr.Len()-2);
652
653 // these nodes make reference to other types... we'll resolve them later
654 //toResolveTypes[id] = toResolveTypeItem(ret, 0);
655 types[id] = child->GetAttribute("returns") + "(" + argstr + ")";
656 }
657 else if (n == "File")
658 {
659 if (!child->GetAttribute("id").StartsWith("f")) {
660 LogError("Unexpected file ID: %s", child->GetAttribute("id"));
661 return false;
662 }
663
664 // just ignore this node... all file IDs/names were already parsed
665 files[id] = child->GetAttribute("name");
666 }
667 else
668 {
669 // we register everything else as a possible return/argument type:
670 const wxString& name = child->GetAttribute("name");
671
672 if (!name.IsEmpty())
673 {
674 //typeIds.Add(id);
675 //typeNames.Add(name);
676 types[id] = name;
677 }
678 else
679 {
680 // this may happen with unnamed structs/union, special ctors,
681 // or other exotic things which we are not interested to, since
682 // they're never used as return/argument types by wxWidgets methods
683
684 if (g_verbose)
685 LogWarning("Type node '%s' with ID '%s' does not have name attribute",
686 n, child->GetAttribute("id"));
687
688 types[id] = "TOFIX";
689 }
690 }
691
692 child = child->GetNext();
693
694 // give feedback to the user about the progress...
695 if ((++nodes%PROGRESS_RATE)==0) ShowProgress();
696 }
697
698 // some nodes with IDs referenced by methods as return/argument types, do reference
699 // in turn o ther nodes (see PointerType, ReferenceType and CvQualifierType above);
700 // thus we need to resolve their name iteratively:
701 while (toResolveTypes.size()>0)
702 {
703 if (g_verbose)
704 LogMessage("%d types were collected; %d types need yet to be resolved...",
705 types.size(), toResolveTypes.size());
706
707 for (wxToResolveTypeHashMap::iterator i = toResolveTypes.begin();
708 i != toResolveTypes.end();)
709 {
710 unsigned long id = i->first;
711 unsigned long referenced = i->second.ref;
712
713 wxTypeIdHashMap::iterator primary = types.find(referenced);
714 if (primary != types.end())
715 {
716 // this to-resolve-type references a "primary" type
717
718 wxString newtype = primary->second;
719 int attribs = i->second.attribs;
720
721 // attribs may contain a combination of ATTRIB_* flags:
722 if (attribs & ATTRIB_CONST)
723 newtype = "const " + newtype;
724 if (attribs & ATTRIB_REFERENCE)
725 newtype = newtype + "&";
726 if (attribs & ATTRIB_POINTER)
727 newtype = newtype + "*";
728 if (attribs & ATTRIB_ARRAY)
729 newtype = newtype + "[]";
730
731 // add the resolved type to the list of "primary" types
732 types[id] = newtype;
733
734 // this one has been resolved; erase it through its iterator!
735 toResolveTypes.erase(i);
736
737 // now iterator i is invalid; assign it again to the beginning
738 i = toResolveTypes.begin();
739 }
740 else
741 {
742 // then search in the referenced types themselves:
743 wxToResolveTypeHashMap::iterator idx2 = toResolveTypes.find(referenced);
744 if (idx2 != toResolveTypes.end())
745 {
746 // merge this to-resolve-type with the idx2->second type
747 i->second.ref = idx2->second.ref;
748 i->second.attribs |= idx2->second.attribs;
749
750 // this type will eventually be solved in the next while() iteration
751 i++;
752 }
753 else
754 {
755 LogError("Cannot solve '%s' reference type!", referenced);
756 return false;
757 }
758 }
759 }
760 }
761
762 // resolve header names
763 for (unsigned int i=0; i<m_classes.GetCount(); i++)
764 {
765 unsigned long fileID = 0;
766 if (!getID(&fileID, m_classes[i].GetHeader()) || fileID == 0) {
767 LogError("invalid header id: %s", m_classes[i].GetHeader());
768 return false;
769 }
770
771 // search this file
772 wxTypeIdHashMap::const_iterator idx = files.find(fileID);
773 if (idx == files.end())
774 {
775 // this is an error!
776 LogError("couldn't find file ID '%s'", m_classes[i].GetHeader());
777 }
778 else
779 m_classes[i].SetHeader(idx->second);
780 }
781
782 // build the list of the wx methods
783 child = doc.GetRoot()->GetChildren();
784 while (child)
785 {
786 wxString n = child->GetName();
787
788 // only register public methods
789 if (child->GetAttribute("access") == "public" &&
790 (n == "Method" || n == "Constructor" || n == "Destructor" || n == "OperatorMethod"))
791 {
792 unsigned long id = 0;
793 if (!getID(&id, child->GetAttribute("id"))) {
794 LogError("invalid ID for node '%s' with ID '%s'", n, child->GetAttribute("id"));
795 return false;
796 }
797
798 wxClassMemberIdHashMap::const_iterator it = members.find(id);
799 if (it != members.end())
800 {
801 wxClass *p = it->second;
802
803 // this <Method> node is a method of the i-th class!
804 wxMethod newfunc;
805 if (!ParseMethod(child, types, newfunc)) {
806 LogError("The method '%s' could not be added to class '%s'",
807 child->GetAttribute("demangled"), p->GetName());
808 return false;
809 }
810
811 if (newfunc.IsCtor() && !p->IsValidCtorForThisClass(newfunc)) {
812 LogError("The method '%s' does not seem to be a ctor for '%s'",
813 newfunc.GetName(), p->GetName());
814 return false;
815 }
816 if (newfunc.IsDtor() && !p->IsValidDtorForThisClass(newfunc)) {
817 LogError("The method '%s' does not seem to be a dtor for '%s'",
818 newfunc.GetName(), p->GetName());
819 return false;
820 }
821
822 p->AddMethod(newfunc);
823 }
824 }
825
826 child = child->GetNext();
827
828 // give feedback to the user about the progress...
829 if ((++nodes%PROGRESS_RATE)==0) ShowProgress();
830 }
831
832 if (!CheckParseResults())
833 return false;
834
835 return true;
836 }
837
838 bool wxXmlGccInterface::ParseMethod(const wxXmlNode *p,
839 const wxTypeIdHashMap& types,
840 wxMethod& m)
841 {
842 // get the real name
843 wxString name = p->GetAttribute("name").Strip(wxString::both);
844 if (p->GetName() == "Destructor")
845 name = "~" + name;
846 else if (p->GetName() == "OperatorMethod")
847 name = "operator" + name;
848
849 // resolve return type
850 wxType ret;
851 unsigned long retid = 0;
852 if (!getID(&retid, p->GetAttribute("returns")) || retid == 0)
853 {
854 if (p->GetName() != "Destructor" && p->GetName() != "Constructor") {
855 LogError("Empty return ID for method '%s', with ID '%s'",
856 name, p->GetAttribute("id"));
857 return false;
858 }
859 }
860 else
861 {
862 wxTypeIdHashMap::const_iterator retidx = types.find(retid);
863 if (retidx == types.end()) {
864 LogError("Could not find return type ID '%s'", retid);
865 return false;
866 }
867
868 ret = wxType(retidx->second);
869 if (!ret.IsOk()) {
870 LogError("Invalid return type '%s' for method '%s', with ID '%s'",
871 retidx->second, name, p->GetAttribute("id"));
872 return false;
873 }
874 }
875
876 // resolve argument types
877 wxArgumentTypeArray argtypes;
878 wxXmlNode *arg = p->GetChildren();
879 while (arg)
880 {
881 if (arg->GetName() == "Argument")
882 {
883 unsigned long id = 0;
884 if (!getID(&id, arg->GetAttribute("type")) || id == 0) {
885 LogError("Invalid argument type ID '%s' for method '%s' with ID %s",
886 arg->GetAttribute("type"), name, p->GetAttribute("id"));
887 return false;
888 }
889
890 wxTypeIdHashMap::const_iterator idx = types.find(id);
891 if (idx == types.end()) {
892 LogError("Could not find argument type ID '%s'", id);
893 return false;
894 }
895
896 argtypes.Add(wxArgumentType(idx->second, arg->GetAttribute("default")));
897 }
898
899 arg = arg->GetNext();
900 }
901
902 m.SetReturnType(ret);
903 m.SetName(name);
904 m.SetArgumentTypes(argtypes);
905 m.SetConst(p->GetAttribute("const") == "1");
906 m.SetStatic(p->GetAttribute("static") == "1");
907
908 // NOTE: gccxml is smart enough to mark as virtual those functions
909 // which are declared virtual in base classes but don't have
910 // the "virtual" keyword explicitely indicated in the derived
911 // classes... so we don't need any further logic for virtuals
912
913 m.SetVirtual(p->GetAttribute("virtual") == "1");
914 m.SetPureVirtual(p->GetAttribute("pure_virtual") == "1");
915 m.SetDeprecated(p->GetAttribute("attributes") == "deprecated");
916
917 if (!m.IsOk()) {
918 LogError("The prototype '%s' is not valid!", m.GetAsString());
919 return false;
920 }
921
922 return true;
923 }
924
925
926 // ----------------------------------------------------------------------------
927 // wxXmlDoxygenInterface
928 // ----------------------------------------------------------------------------
929
930 bool wxXmlDoxygenInterface::Parse(const wxString& filename)
931 {
932 wxXmlDocument index;
933 wxXmlNode *compound;
934
935 LogMessage("Parsing %s...", filename);
936
937 if (!index.Load(filename)) {
938 LogError("can't load %s", filename);
939 return false;
940 }
941
942 // start processing the index:
943 if (index.GetRoot()->GetName() != "doxygenindex") {
944 LogError("invalid root node for %s", filename);
945 return false;
946 }
947
948 m_classes.Alloc(ESTIMATED_NUM_CLASSES);
949
950 // process files referenced by this index file
951 compound = index.GetRoot()->GetChildren();
952 while (compound)
953 {
954 if (compound->GetName() == "compound" &&
955 compound->GetAttribute("kind") == "class")
956 {
957 wxString refid = compound->GetAttribute("refid");
958
959 wxFileName fn(filename);
960 if (!ParseCompoundDefinition(fn.GetPath(wxPATH_GET_SEPARATOR) + refid + ".xml"))
961 return false;
962 }
963
964 compound = compound->GetNext();
965 }
966 //wxPrint("\n");
967
968 if (!CheckParseResults())
969 return false;
970
971 return true;
972 }
973
974 bool wxXmlDoxygenInterface::ParseCompoundDefinition(const wxString& filename)
975 {
976 wxXmlDocument doc;
977 wxXmlNode *child;
978 int nodes = 0;
979
980 if (g_verbose)
981 LogMessage("Parsing %s...", filename);
982
983 if (!doc.Load(filename)) {
984 LogError("can't load %s", filename);
985 return false;
986 }
987
988 // start processing this compound definition XML
989 if (doc.GetRoot()->GetName() != "doxygen") {
990 LogError("invalid root node for %s", filename);
991 return false;
992 }
993
994 // build a list of wx classes
995 child = doc.GetRoot()->GetChildren();
996 while (child)
997 {
998 if (child->GetName() == "compounddef" &&
999 child->GetAttribute("kind") == "class")
1000 {
1001 // parse this class
1002 wxClass klass;
1003 wxString absoluteFile, header;
1004
1005 wxXmlNode *subchild = child->GetChildren();
1006 while (subchild)
1007 {
1008 if (subchild->GetName() == "sectiondef" &&
1009 subchild->GetAttribute("kind") == "public-func")
1010 {
1011
1012 wxXmlNode *membernode = subchild->GetChildren();
1013 while (membernode)
1014 {
1015 if (membernode->GetName() == "memberdef" &&
1016 membernode->GetAttribute("kind") == "function")
1017 {
1018
1019 wxMethod m;
1020 if (!ParseMethod(membernode, m, header)) {
1021 LogError("The method '%s' could not be added to class '%s'",
1022 m.GetName(), klass.GetName());
1023 return false;
1024 }
1025
1026 if (absoluteFile.IsEmpty())
1027 absoluteFile = header;
1028 else if (header != absoluteFile)
1029 {
1030 LogError("The method '%s' is documented in a different "
1031 "file from others (which belong to '%s') ?",
1032 header, absoluteFile);
1033 return false;
1034 }
1035
1036 klass.AddMethod(m);
1037 }
1038
1039 membernode = membernode->GetNext();
1040 }
1041
1042 // all methods of this class were taken from the header "absoluteFile":
1043 klass.SetHeader(absoluteFile);
1044 }
1045 else if (subchild->GetName() == "compoundname")
1046 {
1047 klass.SetName(subchild->GetNodeContent());
1048 }
1049 /*else if (subchild->GetName() == "includes")
1050 {
1051 // NOTE: we'll get the header from the <location> tags
1052 // scattered inside <memberdef> tags instead of
1053 // this <includes> tag since it does not contain
1054 // the absolute path of the header
1055
1056 klass.SetHeader(subchild->GetNodeContent());
1057 }*/
1058
1059 subchild = subchild->GetNext();
1060 }
1061
1062 // add a new class
1063 if (klass.IsOk())
1064 m_classes.Add(klass);
1065 else if (g_verbose)
1066 LogWarning("discarding class '%s' with %d methods...",
1067 klass.GetName(), klass.GetMethodCount());
1068 }
1069
1070 child = child->GetNext();
1071
1072 // give feedback to the user about the progress...
1073 if ((++nodes%PROGRESS_RATE)==0) ShowProgress();
1074 }
1075
1076 return true;
1077 }
1078
1079 static wxString GetTextFromChildren(const wxXmlNode *n)
1080 {
1081 wxString text;
1082
1083 // consider the tree
1084 //
1085 // <a><b>this</b> is a <b>string</b></a>
1086 //
1087 // <a>
1088 // |- <b>
1089 // | |- this
1090 // |- is a
1091 // |- <b>
1092 // |- string
1093 //
1094 // unlike wxXmlNode::GetNodeContent() which would return " is a "
1095 // this function returns "this is a string"
1096
1097 wxXmlNode *ref = n->GetChildren();
1098 while (ref) {
1099 if (ref->GetType() == wxXML_ELEMENT_NODE)
1100 text += ref->GetNodeContent();
1101 else if (ref->GetType() == wxXML_TEXT_NODE)
1102 text += ref->GetContent();
1103 else
1104 LogWarning("Unexpected node type while getting text from '%s' node", n->GetName());
1105
1106 ref = ref->GetNext();
1107 }
1108
1109 return text;
1110 }
1111
1112 static bool HasTextNodeContaining(const wxXmlNode *parent, const wxString& name)
1113 {
1114 wxXmlNode *p = parent->GetChildren();
1115 while (p)
1116 {
1117 switch (p->GetType())
1118 {
1119 case wxXML_TEXT_NODE:
1120 if (p->GetContent() == name)
1121 return true;
1122 break;
1123
1124 case wxXML_ELEMENT_NODE:
1125 // recurse into this node...
1126 if (HasTextNodeContaining(p, name))
1127 return true;
1128 break;
1129
1130 default:
1131 // skip it
1132 break;
1133 }
1134
1135 p = p->GetNext();
1136 }
1137
1138 return false;
1139 }
1140
1141 bool wxXmlDoxygenInterface::ParseMethod(const wxXmlNode* p, wxMethod& m, wxString& header)
1142 {
1143 wxArgumentTypeArray args;
1144 long line;
1145
1146 wxXmlNode *child = p->GetChildren();
1147 while (child)
1148 {
1149 if (child->GetName() == "name")
1150 m.SetName(child->GetNodeContent());
1151 else if (child->GetName() == "type")
1152 m.SetReturnType(wxType(GetTextFromChildren(child)));
1153 else if (child->GetName() == "param")
1154 {
1155 wxString typestr, namestr, defstr, arrstr;
1156 wxXmlNode *n = child->GetChildren();
1157 while (n)
1158 {
1159 if (n->GetName() == "type")
1160 // if the <type> node has children, they should be all TEXT and <ref> nodes
1161 // and we need to take the text they contain, in the order they appear
1162 typestr = GetTextFromChildren(n);
1163 else if (n->GetName() == "declname")
1164 namestr = GetTextFromChildren(n);
1165 else if (n->GetName() == "defval")
1166 defstr = GetTextFromChildren(n);
1167 else if (n->GetName() == "array")
1168 arrstr = GetTextFromChildren(n);
1169
1170 n = n->GetNext();
1171 }
1172
1173 if (typestr.IsEmpty()) {
1174 LogError("cannot find type node for a param in method '%s'", m.GetName());
1175 return false;
1176 }
1177
1178 args.Add(wxArgumentType(typestr + arrstr, defstr, namestr));
1179 }
1180 else if (child->GetName() == "location")
1181 {
1182 line = -1;
1183 if (child->GetAttribute("line").ToLong(&line))
1184 m.SetLocation((int)line);
1185 header = child->GetAttribute("file");
1186 }
1187 else if (child->GetName() == "detaileddescription")
1188 {
1189 // when a method has a @deprecated tag inside its description,
1190 // Doxygen outputs somewhere nested inside <detaileddescription>
1191 // a <xreftitle>Deprecated</xreftitle> tag.
1192 m.SetDeprecated(HasTextNodeContaining(child, "Deprecated"));
1193 }
1194
1195 child = child->GetNext();
1196 }
1197
1198 m.SetArgumentTypes(args);
1199 m.SetConst(p->GetAttribute("const")=="yes");
1200 m.SetStatic(p->GetAttribute("static")=="yes");
1201
1202 // NOTE: Doxygen is smart enough to mark as virtual those functions
1203 // which are declared virtual in base classes but don't have
1204 // the "virtual" keyword explicitely indicated in the derived
1205 // classes... so we don't need any further logic for virtuals
1206
1207 m.SetVirtual(p->GetAttribute("virt")=="virtual");
1208 m.SetPureVirtual(p->GetAttribute("virt")=="pure-virtual");
1209
1210 if (!m.IsOk()) {
1211 LogError("The prototype '%s' is not valid!", m.GetAsString());
1212 return false;
1213 }
1214
1215 return true;
1216 }