]> git.saurik.com Git - wxWidgets.git/blob - utils/ifacecheck/src/xmlparser.cpp
regenerated
[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 #include "wx/crt.h"
21 #endif
22
23 #include "wx/xml/xml.h"
24 #include "wx/wfstream.h"
25 #include "wx/hashmap.h"
26 #include "wx/filename.h"
27 #include "xmlparser.h"
28 #include <errno.h>
29
30 #include <wx/arrimpl.cpp>
31 WX_DEFINE_OBJARRAY(wxTypeArray)
32 WX_DEFINE_OBJARRAY(wxArgumentTypeArray)
33 WX_DEFINE_OBJARRAY(wxMethodArray)
34 WX_DEFINE_OBJARRAY(wxClassArray)
35
36
37 #define PROGRESS_RATE 1000 // each PROGRESS_RATE nodes processed print a dot
38 #define ESTIMATED_NUM_CLASSES 600 // used by both wxXmlInterface-derived classes to prealloc mem
39
40
41 // defined in ifacecheck.cpp
42 extern bool g_verbose;
43
44 // global variable:
45 bool g_bLogEnabled = true;
46
47
48
49 // ----------------------------------------------------------------------------
50 // wxType
51 // ----------------------------------------------------------------------------
52
53 wxType wxEmptyType;
54
55 void wxType::SetTypeFromString(const wxString& t)
56 {
57 /*
58 TODO: optimize the following code writing a single function
59 which works at char-level and does everything in a single pass
60 */
61
62 // clean the type string
63 // ---------------------
64
65 m_strType = t;
66
67 // [] is the same as * for gccxml
68 m_strType.Replace("[]", "*");
69 m_strType.Replace("long int", "long"); // in wx typically we never write "long int", just "long"
70 m_strType.Replace("long unsigned int", "unsigned long");
71 m_strType.Replace("short unsigned int", "unsigned short");
72
73 // make sure the * and & operator always use the same spacing rules
74 // (to make sure GetAsString() output is always consistent)
75 m_strType.Replace("*", "* ");
76 m_strType.Replace("&", "& ");
77 m_strType.Replace(" *", "*");
78 m_strType.Replace(" &", "&");
79
80 while (m_strType.Contains(" "))
81 m_strType.Replace(" ", " "); // do it once again
82
83 m_strType.Replace(" ,", ",");
84
85 // ADHOC-FIX
86 m_strType.Replace("_wxArraywxArrayStringBase", "wxString");
87 m_strType.Replace("ExitCode", "void*"); // used in wxThread stuff
88
89 m_strType = m_strType.Strip(wxString::both);
90
91
92
93 // clean the type string (this time for the comparison)
94 // ----------------------------------------------------
95
96 m_strTypeClean = m_strType; // begin with the already-cleaned string
97 m_strTypeClean.Replace("const", "");
98 m_strTypeClean.Replace("static", "");
99 m_strTypeClean.Replace("*", "");
100 m_strTypeClean.Replace("&", "");
101 m_strTypeClean.Replace("[]", "");
102 m_strTypeClean = m_strTypeClean.Strip(wxString::both);
103
104 // to avoid false errors types like wxStandardPaths and wxStandardPathsBase
105 // need to be considered as the same type
106 if (m_strTypeClean.EndsWith("Base"))
107 m_strTypeClean = m_strTypeClean.Left(m_strTypeClean.Len()-4);
108
109 // remove the namespace from the types; there's no problem of conflicts
110 // (except for templates) and this avoids tons of false warnings
111 if (m_strTypeClean.Contains("::") && !m_strTypeClean.Contains("<"))
112 m_strTypeClean = m_strTypeClean.Mid(m_strTypeClean.Find("::")+2);
113
114 // ADHOC-FIX:
115 m_strTypeClean.Replace("wxWindowID", "int");
116 }
117
118 bool wxType::IsOk() const
119 {
120 // NB: m_strType can contain the :: operator; think to e.g. the
121 // "reverse_iterator_impl<wxString::const_iterator>" type
122 // It can also contain commas, * and & operators etc
123
124 return !m_strTypeClean.IsEmpty();
125 }
126
127 bool wxType::operator==(const wxType& m) const
128 {
129 // brain-dead comparison:
130
131 if (m_strTypeClean == m.m_strTypeClean &&
132 IsConst() == m.IsConst() &&
133 IsStatic() == m.IsStatic() &&
134 IsPointer() == m.IsPointer() &&
135 IsReference() == m.IsReference())
136 return true;
137
138 if (g_verbose)
139 {
140 wxLogMessage("Type '%s' does not match type '%s'", m_strType, m.m_strType);
141 wxLogMessage(" => TypeClean %s / %s; IsConst %d / %d; IsStatic %d / %d; IsPointer %d / %d; IsReference %d / %d",
142 m_strTypeClean, m.m_strTypeClean, IsConst(), m.IsConst(),
143 IsStatic(), m.IsStatic(), IsPointer(), m.IsPointer(),
144 IsReference(), m.IsReference());
145 }
146
147 return false;
148 }
149
150
151 // ----------------------------------------------------------------------------
152 // wxArgumentType
153 // ----------------------------------------------------------------------------
154
155 void wxArgumentType::SetDefaultValue(const wxString& defval, const wxString& defvalForCmp)
156 {
157 m_strDefaultValue = defval.Strip(wxString::both);
158 m_strDefaultValueForCmp = defvalForCmp.IsEmpty() ?
159 m_strDefaultValue : defvalForCmp.Strip(wxString::both);
160
161
162 // clean the default argument strings
163 // ----------------------------------
164
165 // Note: we adjust the aesthetic form of the m_strDefaultValue string for the "modify mode"
166 // of ifacecheck: we may need to write it out in an interface header
167
168 wxString *p = NULL;
169 for (int i=0; i<2; i++) // to avoid copying&pasting the code!
170 {
171 if (i == 0) p = &m_strDefaultValue;
172 if (i == 1) p = &m_strDefaultValueForCmp;
173
174 if (*p == "0u" || *p == "0l") *p = "0";
175
176 p->Replace("0x000000001", "1");
177 p->Replace("\\000\\000\\000", ""); // fix for unicode strings:
178 p->Replace("\\011", "\\t");
179 p->Replace("e+0", "");
180 p->Replace("2147483647", "__INT_MAX__");
181
182 // ADHOC-FIX: for wxConv* default values
183 p->Replace("wxConvAuto(wxFONTENCODING_DEFAULT)", "wxConvAuto()");
184 p->Replace("wxGet_wxConvUTF8()", "wxConvUTF8");
185 p->Replace("wxGet_wxConvLocal()", "wxConvLocal");
186 }
187
188
189 // clean ONLY the default argument string specific for comparison
190 // --------------------------------------------------------------
191
192 if (m_strDefaultValueForCmp.StartsWith("wxT(") &&
193 m_strDefaultValueForCmp.EndsWith(")"))
194 {
195 // get rid of the wxT() part
196 unsigned int len = m_strDefaultValueForCmp.Len();
197 m_strDefaultValueForCmp = m_strDefaultValueForCmp.Mid(4,len-5);
198 }
199
200 // ADHOC-FIX:
201 // doxygen likes to put wxDateTime:: in front of all wxDateTime enums;
202 // fix this to avoid false positives
203 m_strDefaultValueForCmp.Replace("wxDateTime::", "");
204 m_strDefaultValueForCmp.Replace("wxStockGDI::", ""); // same story for some other classes
205 m_strDefaultValueForCmp.Replace("wxHelpEvent::", ""); // same story for some other classes
206 m_strDefaultValueForCmp.Replace("* GetColour(COLOUR_BLACK)", "*wxBLACK");
207
208 // ADHOC-FIX:
209 if (m_strDefaultValueForCmp.Contains("wxGetTranslation"))
210 m_strDefaultValueForCmp = "_(TOFIX)"; // TODO: wxGetTranslation gives problems to gccxml
211 }
212
213 bool wxArgumentType::operator==(const wxArgumentType& m) const
214 {
215 if ((const wxType&)(*this) != (const wxType&)m)
216 return false;
217
218 // check if the default values match
219 // ---------------------------------
220
221
222 // ADHOC-FIX:
223 // default values for style attributes of wxWindow-derived classes in gccxml appear as raw
224 // numbers; avoid false positives in this case!
225 if (m_strArgName == m.m_strArgName && m_strArgName == "style" &&
226 (m_strDefaultValueForCmp.IsNumber() || m.m_strDefaultValueForCmp.IsNumber()))
227 return true;
228
229 // fix for default values which were replaced by gcc-xml with their numeric values
230 // (at this point we know that m_strTypeClean == m.m_strTypeClean):
231 if (m_strTypeClean == "long" || m_strTypeClean == "int")
232 {
233 if ((m_strDefaultValueForCmp.IsNumber() && m.m_strDefaultValueForCmp.StartsWith("wx")) ||
234 (m.m_strDefaultValueForCmp.IsNumber() && m_strDefaultValueForCmp.StartsWith("wx")))
235 {
236 if (g_verbose)
237 {
238 wxLogMessage("Supposing '%s' default value to be the same of '%s'...",
239 m_strDefaultValueForCmp, m.m_strDefaultValueForCmp);
240 }
241
242 return true;
243 }
244 }
245 else if (m_strTypeClean == "float" || m_strTypeClean == "double")
246 // gccXML translates the default floating values in a hardly usable
247 // format; e.g. 25.2 => 2.51999999999999992894572642398998141288757324219e+1
248 // we avoid check on these...
249 return true;
250
251 if (m_strDefaultValueForCmp != m.m_strDefaultValueForCmp)
252 {
253 // maybe the default values are numbers.
254 // in this case gccXML gives as default values things like '-0x0000001' instead of just '-1'.
255 // To handle these cases, we try to convert the default value strings to numbers:
256 long def1val, def2val;
257 if (m_strDefaultValueForCmp.ToLong(&def1val, 0 /* auto-detect */) &&
258 m.m_strDefaultValueForCmp.ToLong(&def2val, 0 /* auto-detect */))
259 {
260 if (def1val == def2val)
261 return true; // the default values match
262 }
263
264 if (g_verbose)
265 {
266 wxLogMessage("Argument type '%s = %s' has different default value from '%s = %s'",
267 m_strType, m_strDefaultValueForCmp, m.m_strType, m.m_strDefaultValueForCmp);
268 }
269 return false;
270 }
271
272 // we deliberately avoid checks on the argument name
273
274 return true;
275 }
276
277
278 // ----------------------------------------------------------------------------
279 // wxMethod
280 // ----------------------------------------------------------------------------
281
282 bool wxMethod::IsOk() const
283 {
284 // NOTE: m_retType can be a wxEmptyType, and means that this method
285 // is a ctor or a dtor.
286 if (!m_retType.IsOk() && m_retType!=wxEmptyType) {
287 wxLogError("'%s' method has invalid return type: %s", m_retType.GetAsString());
288 return false;
289 }
290
291 if (m_strName.IsEmpty())
292 return false;
293
294 // a function can't be both const and static or virtual and static!
295 if ((m_bConst && m_bStatic) || ((m_bVirtual || m_bPureVirtual) && m_bStatic)) {
296 wxLogError("'%s' method can't be both const/static or virtual/static", m_strName);
297 return false;
298 }
299
300 wxASSERT(!m_bPureVirtual || (m_bPureVirtual && m_bVirtual));
301
302 for (unsigned int i=0; i<m_args.GetCount(); i++)
303 if (!m_args[i].IsOk()) {
304 wxLogError("'%s' method has invalid %d-th argument type: %s",
305 m_strName, i+1, m_args[i].GetAsString());
306 return false;
307 }
308
309 // NB: the default value of the arguments can contain pretty much everything
310 // (think to e.g. wxPoint(3+4/2,0) or *wxBLACK or someClass<type>)
311 // so we don't do any test on their contents
312 if (m_args.GetCount()>0)
313 {
314 bool previousArgHasDefault = m_args[0].HasDefaultValue();
315 for (unsigned int i=1; i<m_args.GetCount(); i++)
316 {
317 if (previousArgHasDefault && !m_args[i].HasDefaultValue()) {
318 wxLogError("'%s' method has %d-th argument which has no default value "
319 "(while the previous one had one!)",
320 m_strName, i+1);
321 return false;
322 }
323
324 previousArgHasDefault = m_args[i].HasDefaultValue();
325 }
326 }
327
328 return true;
329 }
330
331 bool wxMethod::MatchesExceptForAttributes(const wxMethod& m) const
332 {
333 if (GetReturnType() != m.GetReturnType() ||
334 GetName() != m.GetName())
335 {
336 if (g_verbose)
337 {
338 wxLogMessage("The method '%s' does not match method '%s'; different names/rettype", GetName(), m.GetName());
339 }
340 return false;
341 }
342
343 if (m_args.GetCount()!=m.m_args.GetCount()) {
344 if (g_verbose)
345 {
346 wxLogMessage("Method '%s' has %d arguments while '%s' has %d arguments",
347 m_strName, m_args.GetCount(), m_strName, m.m_args.GetCount());
348 }
349 return false;
350 }
351
352 // compare argument types
353 for (unsigned int i=0; i<m_args.GetCount(); i++)
354 if (m_args[i] != m.m_args[i])
355 return false;
356
357 return true;
358 }
359
360 bool wxMethod::ActsAsDefaultCtor() const
361 {
362 if (!IsCtor())
363 return false;
364
365 for (unsigned int i=0; i<m_args.GetCount(); i++)
366 if (!m_args[i].HasDefaultValue())
367 return false;
368
369 return true;
370 }
371
372 bool wxMethod::operator==(const wxMethod& m) const
373 {
374 // check attributes
375 if (IsConst() != m.IsConst() ||
376 IsStatic() != m.IsStatic() ||
377 IsVirtual() != m.IsVirtual() ||
378 IsPureVirtual() != m.IsPureVirtual() ||
379 IsDeprecated() != m.IsDeprecated() ||
380 GetAccessSpecifier() != m.GetAccessSpecifier())
381 {
382 if (g_verbose)
383 {
384 wxLogMessage("The method '%s' does not match method '%s'; different attributes", GetName(), m.GetName());
385 }
386
387 return false;
388 }
389
390 // check everything else
391 return MatchesExceptForAttributes(m);
392 }
393
394 wxString wxMethod::GetAsString(bool bWithArgumentNames, bool bCleanDefaultValues,
395 bool bDeprecated, bool bAccessSpec) const
396 {
397 wxString ret;
398
399 // NOTE: for return and argument types, never use wxType::GetAsCleanString
400 // since in that way we'd miss important decorators like &,*,const etc
401
402 if (m_retType!=wxEmptyType)
403 ret += m_retType.GetAsString() + " ";
404 //else; this is a ctor or dtor
405
406 ret += m_strName + "(";
407
408 for (unsigned int i=0; i<m_args.GetCount(); i++)
409 {
410 ret += m_args[i].GetAsString();
411
412 const wxString& name = m_args[i].GetArgumentName();
413 if (bWithArgumentNames && !name.IsEmpty())
414 ret += " " + name;
415
416 const wxString& def = bCleanDefaultValues ?
417 m_args[i].GetDefaultCleanValue() : m_args[i].GetDefaultValue();
418 if (!def.IsEmpty())
419 ret += " = " + def;
420
421 ret += ", ";
422 }
423
424 if (m_args.GetCount()>0)
425 ret = ret.Left(ret.Len()-2);
426
427 ret += ")";
428
429 if (m_bConst)
430 ret += " const";
431 if (m_bStatic)
432 ret = "static " + ret;
433 if (m_bVirtual || m_bPureVirtual)
434 ret = "virtual " + ret;
435 if (m_bPureVirtual)
436 ret += " = 0";
437 if (m_bDeprecated && bDeprecated)
438 ret += " [deprecated]";
439
440 if (bAccessSpec)
441 {
442 switch (m_access)
443 {
444 case wxMAS_PUBLIC:
445 ret += " [public]";
446 break;
447 case wxMAS_PROTECTED:
448 ret += " [protected]";
449 break;
450 case wxMAS_PRIVATE:
451 ret += " [private]";
452 break;
453 }
454 }
455
456 return ret;
457 }
458
459 void wxMethod::Dump(wxTextOutputStream& stream) const
460 {
461 stream << "[" + m_retType.GetAsString() + "]";
462 stream << "[" + m_strName + "]";
463
464 for (unsigned int i=0; i<m_args.GetCount(); i++)
465 stream << "[" + m_args[i].GetAsString() + " " + m_args[i].GetArgumentName() +
466 "=" + m_args[i].GetDefaultValue() + "]";
467
468 if (IsConst())
469 stream << " CONST";
470 if (IsStatic())
471 stream << " STATIC";
472 if (IsVirtual())
473 stream << " VIRTUAL";
474 if (IsPureVirtual())
475 stream << " PURE-VIRTUAL";
476 if (IsDeprecated())
477 stream << " DEPRECATED";
478
479 // no final newline
480 }
481
482 // ----------------------------------------------------------------------------
483 // wxClass
484 // ----------------------------------------------------------------------------
485
486 wxString wxClass::GetNameWithoutTemplate() const
487 {
488 // NB: I'm not sure this is the right terminology for this function!
489
490 if (m_strName.Contains("<"))
491 return m_strName.Left(m_strName.Find("<"));
492 return m_strName;
493 }
494
495 bool wxClass::IsValidCtorForThisClass(const wxMethod& m) const
496 {
497 // remember that e.g. the ctor for wxWritableCharTypeBuffer<wchar_t> is
498 // named wxWritableCharTypeBuffer, without the <...> part!
499
500 if (m.IsCtor() && m.GetName() == GetNameWithoutTemplate())
501 return true;
502
503 return false;
504 }
505
506 bool wxClass::IsValidDtorForThisClass(const wxMethod& m) const
507 {
508 if (m.IsDtor() && m.GetName() == "~" + GetNameWithoutTemplate())
509 return true;
510
511 return false;
512 }
513
514 void wxClass::Dump(wxTextOutputStream& out) const
515 {
516 out << m_strName + "\n";
517
518 for (unsigned int i=0; i<m_methods.GetCount(); i++) {
519
520 // dump all our methods
521 out << "|- ";
522 m_methods[i].Dump(out);
523 out << "\n";
524 }
525
526 out << "\n";
527 }
528
529 bool wxClass::CheckConsistency() const
530 {
531 for (unsigned int i=0; i<m_methods.GetCount(); i++)
532 for (unsigned int j=0; j<m_methods.GetCount(); j++)
533 if (i!=j && m_methods[i] == m_methods[j])
534 {
535 wxLogError("class %s has two methods with the same prototype: '%s'",
536 m_strName, m_methods[i].GetAsString());
537 return false;
538
539 // fix the problem?
540 //((wxClass*)this)->m_methods.RemoveAt(j);
541 //j--;
542 }
543
544 return true;
545 }
546
547 const wxMethod* wxClass::FindMethod(const wxMethod& m) const
548 {
549 for (unsigned int i=0; i<m_methods.GetCount(); i++)
550 if (m_methods[i] == m)
551 return &m_methods[i];
552 return NULL;
553 }
554
555 const wxMethod* wxClass::RecursiveUpwardFindMethod(const wxMethod& m,
556 const wxXmlInterface* allclasses) const
557 {
558 // first, search into *this
559 const wxMethod* ret = FindMethod(m);
560 if (ret)
561 return ret;
562
563 // then, search into its parents
564 for (unsigned int i=0; i<m_parents.GetCount(); i++)
565 {
566 // ignore non-wx-classes parents
567 // AD-HOC FIX: discard wxScrolledT_Helper parent as it always gives errors
568 if (m_parents[i].StartsWith("wx") && m_parents[i] != "wxScrolledT_Helper")
569 {
570 const wxClass *parent = allclasses->FindClass(m_parents[i]);
571 if (!parent) {
572 wxLogError("Could not find parent '%s' of class '%s'...",
573 m_parents[i], GetName());
574 return false;
575 }
576
577 const wxMethod *parentMethod = parent->RecursiveUpwardFindMethod(m, allclasses);
578 if (parentMethod)
579 return parentMethod;
580 }
581 }
582
583 // could not find anything even in parent classes...
584 return NULL;
585 }
586
587 wxMethodPtrArray wxClass::FindMethodsNamed(const wxString& name) const
588 {
589 wxMethodPtrArray ret;
590
591 for (unsigned int i=0; i<m_methods.GetCount(); i++)
592 if (m_methods[i].GetName() == name)
593 ret.Add(&m_methods[i]);
594
595 return ret;
596 }
597
598
599 wxMethodPtrArray wxClass::RecursiveUpwardFindMethodsNamed(const wxString& name,
600 const wxXmlInterface* allclasses) const
601 {
602 // first, search into *this
603 wxMethodPtrArray ret = FindMethodsNamed(name);
604 if (ret.GetCount()>0)
605 return ret; // stop here, don't look upward in the parents
606
607 // then, search into parents of this class
608 for (unsigned int i=0; i<m_parents.GetCount(); i++)
609 {
610 // AD-HOC FIX: discard wxScrolledT_Helper parent as it always gives errors
611 if (m_parents[i].StartsWith("wx") && m_parents[i] != "wxScrolledT_Helper")
612 {
613 const wxClass *parent = allclasses->FindClass(m_parents[i]);
614 if (!parent) {
615 wxLogError("Could not find parent '%s' of class '%s'...",
616 m_parents[i], GetName());
617 return false;
618 }
619
620 wxMethodPtrArray temp = parent->RecursiveUpwardFindMethodsNamed(name, allclasses);
621 WX_APPEND_ARRAY(ret, temp);
622 }
623 }
624
625 return ret;
626 }
627
628
629
630 // ----------------------------------------------------------------------------
631 // wxXmlInterface
632 // ----------------------------------------------------------------------------
633
634 WX_DEFINE_SORTED_ARRAY(wxClass*, wxSortedClassArray);
635
636 int CompareWxClassObjects(wxClass *item1, wxClass *item2)
637 {
638 // sort alphabetically
639 return item1->GetName().Cmp(item2->GetName());
640 }
641
642 void wxXmlInterface::Dump(const wxString& filename)
643 {
644 wxFFileOutputStream apioutput( filename );
645 wxTextOutputStream apiout( apioutput );
646
647 // dump the classes in alphabetical order
648 wxSortedClassArray sorted(CompareWxClassObjects);
649 sorted.Alloc(m_classes.GetCount());
650
651 unsigned i;
652 for (i=0; i<m_classes.GetCount(); i++)
653 sorted.Add(&m_classes[i]);
654
655 // now they have been sorted
656 for (i=0; i<sorted.GetCount(); i++)
657 sorted[i]->Dump(apiout);
658 }
659
660 bool wxXmlInterface::CheckConsistency() const
661 {
662 // this check can be quite slow, so do it only for debug releases:
663 //#ifdef __WXDEBUG__
664 for (unsigned int i=0; i<m_classes.GetCount(); i++)
665 {
666 if (!m_classes[i].CheckConsistency())
667 return false;
668
669 for (unsigned int j=0; j<m_classes.GetCount(); j++)
670 if (i!=j && m_classes[i].GetName() == m_classes[j].GetName())
671 {
672 wxLogError("two classes have the same name: %s",
673 m_classes[i].GetName());
674 return false;
675 }
676 }
677 //#endif
678
679 return true;
680 }
681
682 wxClassPtrArray wxXmlInterface::FindClassesDefinedIn(const wxString& headerfile) const
683 {
684 wxClassPtrArray ret;
685
686 for (unsigned int i=0; i<m_classes.GetCount(); i++)
687 if (m_classes[i].GetHeader() == headerfile)
688 ret.Add(&m_classes[i]);
689
690 return ret;
691 }
692
693
694 // ----------------------------------------------------------------------------
695 // wxXmlGccInterface helper declarations
696 // ----------------------------------------------------------------------------
697
698 // or-able flags for a toResolveTypeItem->attrib:
699 #define ATTRIB_CONST 1
700 #define ATTRIB_REFERENCE 2
701 #define ATTRIB_POINTER 4
702 #define ATTRIB_ARRAY 8
703
704 // it may sound strange but gccxml, in order to produce shorter ID names
705 // uses (after the underscore) characters in range 0-9 and a-z in the ID names;
706 // in order to be able to translate such strings into numbers using strtoul()
707 // we use as base 10 (possible digits) + 25 (possible characters) = 35
708 #define GCCXML_BASE 35
709
710 class toResolveTypeItem
711 {
712 public:
713 toResolveTypeItem() { attribs=0; }
714 toResolveTypeItem(unsigned int refID, unsigned int attribint)
715 : ref(refID), attribs(attribint) {}
716
717 unsigned long ref, // the referenced type's ID
718 attribs; // the attributes of this reference
719 };
720
721 #if 1
722
723 // for wxToResolveTypeHashMap, keys == gccXML IDs and values == toResolveTypeItem
724 WX_DECLARE_HASH_MAP( unsigned long, toResolveTypeItem,
725 wxIntegerHash, wxIntegerEqual,
726 wxToResolveTypeHashMap );
727
728 // for wxClassMemberIdHashMap, keys == gccXML IDs and values == wxClass which owns that member ID
729 WX_DECLARE_HASH_MAP( unsigned long, wxClass*,
730 wxIntegerHash, wxIntegerEqual,
731 wxClassMemberIdHashMap );
732
733 #else
734 #include <map>
735 typedef std::map<unsigned long, toResolveTypeItem> wxToResolveTypeHashMap;
736 #endif
737
738
739 // utility to parse gccXML ID values;
740 // this function is equivalent to wxString(str).Mid(1).ToULong(&id, GCCXML_BASE)
741 // but is a little bit faster
742 bool getID(unsigned long *id, const wxString& str)
743 {
744 const wxStringCharType * const start = str.wx_str()+1;
745 wxStringCharType *end;
746 #if wxUSE_UNICODE_WCHAR
747 unsigned long val = wcstoul(start, &end, GCCXML_BASE);
748 #else
749 unsigned long val = strtoul(start, &end, GCCXML_BASE);
750 #endif
751
752 // return true only if scan was stopped by the terminating NUL and
753 // if the string was not empty to start with and no under/overflow
754 // occurred:
755 if ( *end != '\0' || end == start || errno == ERANGE || errno == EINVAL )
756 return false;
757
758 *id = val;
759 return true;
760 }
761
762 // utility specialized to parse efficiently the gccXML list of IDs which occur
763 // in nodes like <Class> ones... i.e. numeric values separed by " _" token
764 bool getMemberIDs(wxClassMemberIdHashMap* map, wxClass* p, const wxString& str)
765 {
766 const wxStringCharType * const start = str.wx_str();
767 #if wxUSE_UNICODE_WCHAR
768 size_t len = wcslen(start);
769 #else
770 size_t len = strlen(start);
771 #endif
772
773 if (len == 0 || start[0] != '_')
774 return false;
775
776 const wxStringCharType *curpos = start,
777 *end = start + len;
778 wxStringCharType *nexttoken;
779
780 while (curpos < end)
781 {
782 // curpos always points to the underscore of the next token to parse:
783 #if wxUSE_UNICODE_WCHAR
784 unsigned long id = wcstoul(curpos+1, &nexttoken, GCCXML_BASE);
785 #else
786 unsigned long id = strtoul(curpos+1, &nexttoken, GCCXML_BASE);
787 #endif
788 if ( *nexttoken != ' ' || errno == ERANGE || errno == EINVAL )
789 return false;
790
791 // advance current position
792 curpos = nexttoken + 1;
793
794 // add this ID to the hashmap
795 wxClassMemberIdHashMap::value_type v(id, p);
796 map->insert(v);
797 }
798
799 return true;
800 }
801
802
803 // ----------------------------------------------------------------------------
804 // wxXmlGccInterface
805 // ----------------------------------------------------------------------------
806
807 bool wxXmlGccInterface::Parse(const wxString& filename)
808 {
809 wxXmlDocument doc;
810 wxXmlNode *child;
811 int nodes = 0;
812
813 wxLogMessage("Parsing %s...", filename);
814
815 if (!doc.Load(filename)) {
816 wxLogError("can't load %s", filename);
817 return false;
818 }
819
820 // start processing the XML file
821 if (doc.GetRoot()->GetName() != "GCC_XML") {
822 wxLogError("invalid root node for %s", filename);
823 return false;
824 }
825
826 wxString version = doc.GetRoot()->GetAttribute("cvs_revision");
827 bool old = false;
828
829 #define MIN_REVISION 120
830
831 if (!version.StartsWith("1."))
832 old = true;
833 if (!old)
834 {
835 unsigned long rev = 0;
836 if (!version.Mid(2).ToULong(&rev))
837 old = true;
838 else
839 if (rev < MIN_REVISION)
840 old = true;
841 }
842
843 if (old)
844 {
845 wxLogError("The version of GCC-XML used for the creation of %s is too old; "
846 "the cvs_revision attribute of the root node reports '%s', "
847 "minimal required is 1.%d.", filename, version, MIN_REVISION);
848 return false;
849 }
850
851 wxToResolveTypeHashMap toResolveTypes;
852 wxClassMemberIdHashMap members;
853 wxTypeIdHashMap types;
854 wxTypeIdHashMap files;
855 wxTypeIdHashMap typedefs;
856
857 // prealloc quite a lot of memory!
858 m_classes.Alloc(ESTIMATED_NUM_CLASSES);
859
860 // build a list of wx classes and in general of all existent types
861 child = doc.GetRoot()->GetChildren();
862 while (child)
863 {
864 const wxString& n = child->GetName();
865
866 unsigned long id = 0;
867 if (!getID(&id, child->GetAttribute("id")) || (id == 0 && n != "File")) {
868
869 // NOTE: <File> nodes can have an id == "f0"...
870
871 wxLogError("Invalid id for node %s: %s", n, child->GetAttribute("id"));
872 return false;
873 }
874
875 if (n == "Class")
876 {
877 wxString cname = child->GetAttribute("name");
878 if (cname.IsEmpty()) {
879 wxLogError("Invalid empty name for '%s' node", n);
880 return false;
881 }
882
883 // only register wx classes (do remember also the IDs of their members)
884 if (cname.StartsWith("wx"))
885 {
886 // NB: "file" attribute contains an ID value that we'll resolve later
887 m_classes.Add(wxClass(cname, child->GetAttribute("file")));
888
889 // the just-inserted class:
890 wxClass *newClass = &m_classes.Last();
891
892 // now get a list of the base classes:
893 wxXmlNode *baseNode = child->GetChildren();
894 while (baseNode)
895 {
896 // for now we store as "parents" only the parent IDs...
897 // later we will resolve them into full class names
898 if (baseNode->GetName() == "Base")
899 newClass->AddParent(baseNode->GetAttribute("type"));
900
901 baseNode = baseNode->GetNext();
902 }
903
904 const wxString& ids = child->GetAttribute("members");
905 if (ids.IsEmpty())
906 {
907 if (child->GetAttribute("incomplete") != "1") {
908 wxLogError("Invalid member IDs for '%s' class node: %s",
909 cname, child->GetAttribute("id"));
910 return false;
911 }
912 //else: don't warn the user; it looks like "incomplete" classes
913 // never have any member...
914 }
915 else
916 {
917 // decode the non-empty list of IDs:
918 if (!getMemberIDs(&members, newClass, ids)) {
919 wxLogError("Invalid member IDs for '%s' class node: %s",
920 cname, child->GetAttribute("id"));
921 return false;
922 }
923 }
924 }
925
926 // register this class also as possible return/argument type:
927 types[id] = cname;
928 }
929 else if (n == "Typedef")
930 {
931 unsigned long typeId = 0;
932 if (!getID(&typeId, child->GetAttribute("type"))) {
933 wxLogError("Invalid type for node %s: %s", n, child->GetAttribute("type"));
934 return false;
935 }
936
937 // this typedef node tell us that every type referenced with the
938 // "typeId" ID should be called with another name:
939 wxString name = child->GetAttribute("name");
940
941 // save this typedef in a separate hashmap...
942 typedefs[typeId] = name;
943
944 types[id] = name;
945 }
946 else if (n == "PointerType" || n == "ReferenceType" ||
947 n == "CvQualifiedType" || n == "ArrayType")
948 {
949 unsigned long type = 0;
950 if (!getID(&type, child->GetAttribute("type")) || type == 0) {
951 wxLogError("Invalid type for node %s: %s", n, child->GetAttribute("type"));
952 return false;
953 }
954
955 unsigned long attr = 0;
956 if (n == "PointerType")
957 attr = ATTRIB_POINTER;
958 else if (n == "ReferenceType")
959 attr = ATTRIB_REFERENCE;
960 else if (n == "CvQualifiedType" && child->GetAttribute("const") == "1")
961 attr = ATTRIB_CONST;
962 else if (n == "ArrayType")
963 attr = ATTRIB_ARRAY;
964
965 // these nodes make reference to other types... we'll resolve them later
966 toResolveTypes[id] = toResolveTypeItem(type, attr);
967 }
968 else if (n == "FunctionType" || n == "MethodType")
969 {
970 /*
971 TODO: parsing FunctionType and MethodType nodes is not as easy
972 as for other "simple" types.
973 */
974
975 wxString argstr;
976 wxXmlNode *arg = child->GetChildren();
977 while (arg)
978 {
979 if (arg->GetName() == "Argument")
980 argstr += arg->GetAttribute("type") + ", ";
981 arg = arg->GetNext();
982 }
983
984 if (argstr.Len() > 0)
985 argstr = argstr.Left(argstr.Len()-2); // remove final comma
986
987 // these nodes make reference to other types... we'll resolve them later
988 //toResolveTypes[id] = toResolveTypeItem(ret, 0);
989 //types[id] = child->GetAttribute("returns") + "(" + argstr + ")";
990
991 types[id] = "TOFIX"; // typically this type will be "fixed" thanks
992 // to a typedef later...
993 }
994 else if (n == "File")
995 {
996 if (!child->GetAttribute("id").StartsWith("f")) {
997 wxLogError("Unexpected file ID: %s", child->GetAttribute("id"));
998 return false;
999 }
1000
1001 // just ignore this node... all file IDs/names were already parsed
1002 files[id] = child->GetAttribute("name");
1003 }
1004 else
1005 {
1006 // we register everything else as a possible return/argument type:
1007 const wxString& name = child->GetAttribute("name");
1008
1009 if (!name.IsEmpty())
1010 {
1011 //typeIds.Add(id);
1012 //typeNames.Add(name);
1013 types[id] = name;
1014 }
1015 else
1016 {
1017 // this may happen with unnamed structs/union, special ctors,
1018 // or other exotic things which we are not interested to, since
1019 // they're never used as return/argument types by wxWidgets methods
1020
1021 if (g_verbose)
1022 {
1023 wxLogWarning("Type node '%s' with ID '%s' does not have name attribute",
1024 n, child->GetAttribute("id"));
1025 }
1026
1027 types[id] = "TOFIX";
1028 }
1029 }
1030
1031 child = child->GetNext();
1032
1033 // give feedback to the user about the progress...
1034 if ((++nodes%PROGRESS_RATE)==0) ShowProgress();
1035 }
1036
1037 // some nodes with IDs referenced by methods as return/argument types, do reference
1038 // in turn other nodes (see PointerType, ReferenceType and CvQualifierType above);
1039 // thus we need to resolve their name iteratively:
1040 while (toResolveTypes.size()>0)
1041 {
1042 if (g_verbose)
1043 {
1044 wxLogMessage("%d types were collected; %d types need yet to be resolved...",
1045 types.size(), toResolveTypes.size());
1046 }
1047
1048 for (wxToResolveTypeHashMap::iterator i = toResolveTypes.begin();
1049 i != toResolveTypes.end();)
1050 {
1051 unsigned long id = i->first;
1052 unsigned long referenced = i->second.ref;
1053
1054 wxTypeIdHashMap::iterator primary = types.find(referenced);
1055 if (primary != types.end())
1056 {
1057 // this to-resolve-type references a "primary" type
1058
1059 wxString newtype = primary->second;
1060 int attribs = i->second.attribs;
1061
1062 // attribs may contain a combination of ATTRIB_* flags:
1063 if (attribs & ATTRIB_CONST)
1064 newtype = "const " + newtype;
1065 if (attribs & ATTRIB_REFERENCE)
1066 newtype = newtype + "&";
1067 if (attribs & ATTRIB_POINTER)
1068 newtype = newtype + "*";
1069 if (attribs & ATTRIB_ARRAY)
1070 newtype = newtype + "[]";
1071
1072 // add the resolved type to the list of "primary" types
1073 if (newtype.Contains("TOFIX") && typedefs[id] != "")
1074 types[id] = typedefs[id]; // better use a typedef for this type!
1075 else
1076 types[id] = newtype;
1077
1078 // this one has been resolved; erase it through its iterator!
1079 toResolveTypes.erase(i);
1080
1081 // now iterator i is invalid; assign it again to the beginning
1082 i = toResolveTypes.begin();
1083 }
1084 else
1085 {
1086 // then search in the referenced types themselves:
1087 wxToResolveTypeHashMap::iterator idx2 = toResolveTypes.find(referenced);
1088 if (idx2 != toResolveTypes.end())
1089 {
1090 // merge this to-resolve-type with the idx2->second type
1091 i->second.ref = idx2->second.ref;
1092 i->second.attribs |= idx2->second.attribs;
1093
1094 // this type will eventually be solved in the next while() iteration
1095 i++;
1096 }
1097 else
1098 {
1099 wxLogError("Cannot solve '%d' reference type!", referenced);
1100 return false;
1101 }
1102 }
1103 }
1104 }
1105
1106 // resolve header names
1107 unsigned i;
1108 for (i=0; i<m_classes.GetCount(); i++)
1109 {
1110 unsigned long fileID = 0;
1111 if (!getID(&fileID, m_classes[i].GetHeader()) || fileID == 0) {
1112 wxLogError("invalid header id: %s", m_classes[i].GetHeader());
1113 return false;
1114 }
1115
1116 // search this file
1117 wxTypeIdHashMap::const_iterator idx = files.find(fileID);
1118 if (idx == files.end())
1119 {
1120 // this is an error!
1121 wxLogError("couldn't find file ID '%s'", m_classes[i].GetHeader());
1122 }
1123 else
1124 m_classes[i].SetHeader(idx->second);
1125 }
1126
1127 // resolve parent names
1128 for (i=0; i<m_classes.GetCount(); i++)
1129 {
1130 for (unsigned int k=0; k<m_classes[i].GetParentCount(); k++)
1131 {
1132 unsigned long id;
1133
1134 if (!getID(&id, m_classes[i].GetParent(k))) {
1135 wxLogError("invalid parent class ID for '%s'", m_classes[i].GetName());
1136 return false;
1137 }
1138
1139 wxTypeIdHashMap::const_iterator idx = types.find(id);
1140 if (idx == types.end())
1141 {
1142 // this is an error!
1143 wxLogError("couldn't find parent class ID '%d'", id);
1144 }
1145 else
1146 // replace k-th parent with its true name:
1147 m_classes[i].SetParent(k, idx->second);
1148 }
1149 }
1150
1151 // build the list of the wx methods
1152 child = doc.GetRoot()->GetChildren();
1153 while (child)
1154 {
1155 wxString n = child->GetName(), acc = child->GetAttribute("access");
1156
1157 // only register public&protected methods
1158 if ((acc == "public" || acc == "protected") &&
1159 (n == "Method" || n == "Constructor" || n == "Destructor" || n == "OperatorMethod"))
1160 {
1161 unsigned long id = 0;
1162 if (!getID(&id, child->GetAttribute("id"))) {
1163 wxLogError("invalid ID for node '%s' with ID '%s'", n, child->GetAttribute("id"));
1164 return false;
1165 }
1166
1167 wxClassMemberIdHashMap::const_iterator it = members.find(id);
1168 if (it != members.end())
1169 {
1170 wxClass *p = it->second;
1171
1172 // this <Method> node is a method of the i-th class!
1173 wxMethod newfunc;
1174 if (!ParseMethod(child, types, newfunc)) {
1175 wxLogError("The method '%s' could not be added to class '%s'",
1176 child->GetAttribute("demangled"), p->GetName());
1177 return false;
1178 }
1179
1180 // do some additional check that we can do only here:
1181
1182 if (newfunc.IsCtor() && !p->IsValidCtorForThisClass(newfunc)) {
1183 wxLogError("The method '%s' does not seem to be a ctor for '%s'",
1184 newfunc.GetName(), p->GetName());
1185 return false;
1186 }
1187 if (newfunc.IsDtor() && !p->IsValidDtorForThisClass(newfunc)) {
1188 wxLogError("The method '%s' does not seem to be a dtor for '%s'",
1189 newfunc.GetName(), p->GetName());
1190 return false;
1191 }
1192
1193 p->AddMethod(newfunc);
1194 }
1195 }
1196
1197 child = child->GetNext();
1198
1199 // give feedback to the user about the progress...
1200 if ((++nodes%PROGRESS_RATE)==0) ShowProgress();
1201 }
1202
1203 if (!CheckConsistency())
1204 return false; // the check failed
1205
1206 return true;
1207 }
1208
1209 bool wxXmlGccInterface::ParseMethod(const wxXmlNode *p,
1210 const wxTypeIdHashMap& types,
1211 wxMethod& m)
1212 {
1213 // get the real name
1214 wxString name = p->GetAttribute("name").Strip(wxString::both);
1215 if (p->GetName() == "Destructor")
1216 name = "~" + name;
1217 else if (p->GetName() == "OperatorMethod")
1218 name = "operator" + name;
1219
1220 // resolve return type
1221 wxType ret;
1222 unsigned long retid = 0;
1223 if (!getID(&retid, p->GetAttribute("returns")) || retid == 0)
1224 {
1225 if (p->GetName() != "Destructor" && p->GetName() != "Constructor") {
1226 wxLogError("Empty return ID for method '%s', with ID '%s'",
1227 name, p->GetAttribute("id"));
1228 return false;
1229 }
1230 }
1231 else
1232 {
1233 wxTypeIdHashMap::const_iterator retidx = types.find(retid);
1234 if (retidx == types.end()) {
1235 wxLogError("Could not find return type ID '%s'", retid);
1236 return false;
1237 }
1238
1239 ret = wxType(retidx->second);
1240 if (!ret.IsOk()) {
1241 wxLogError("Invalid return type '%s' for method '%s', with ID '%s'",
1242 retidx->second, name, p->GetAttribute("id"));
1243 return false;
1244 }
1245 }
1246
1247 // resolve argument types
1248 wxArgumentTypeArray argtypes;
1249 wxXmlNode *arg = p->GetChildren();
1250 while (arg)
1251 {
1252 if (arg->GetName() == "Argument")
1253 {
1254 unsigned long id = 0;
1255 if (!getID(&id, arg->GetAttribute("type")) || id == 0) {
1256 wxLogError("Invalid argument type ID '%s' for method '%s' with ID %s",
1257 arg->GetAttribute("type"), name, p->GetAttribute("id"));
1258 return false;
1259 }
1260
1261 wxTypeIdHashMap::const_iterator idx = types.find(id);
1262 if (idx == types.end()) {
1263 wxLogError("Could not find argument type ID '%s'", id);
1264 return false;
1265 }
1266
1267 argtypes.Add(wxArgumentType(idx->second,
1268 arg->GetAttribute("default"),
1269 arg->GetAttribute("name")));
1270 }
1271
1272 arg = arg->GetNext();
1273 }
1274
1275 m.SetReturnType(ret);
1276 m.SetName(name);
1277 m.SetArgumentTypes(argtypes);
1278 m.SetConst(p->GetAttribute("const") == "1");
1279 m.SetStatic(p->GetAttribute("static") == "1");
1280
1281 // NOTE: gccxml is smart enough to mark as virtual those functions
1282 // which are declared virtual in base classes but don't have
1283 // the "virtual" keyword explicitely indicated in the derived
1284 // classes... so we don't need any further logic for virtuals
1285
1286 m.SetVirtual(p->GetAttribute("virtual") == "1");
1287 m.SetPureVirtual(p->GetAttribute("pure_virtual") == "1");
1288 m.SetDeprecated(p->GetAttribute("attributes") == "deprecated");
1289
1290 // decode access specifier
1291 if (p->GetAttribute("access") == "public")
1292 m.SetAccessSpecifier(wxMAS_PUBLIC);
1293 else if (p->GetAttribute("access") == "protected")
1294 m.SetAccessSpecifier(wxMAS_PROTECTED);
1295 else if (p->GetAttribute("access") == "private")
1296 m.SetAccessSpecifier(wxMAS_PRIVATE);
1297
1298 if (!m.IsOk()) {
1299 wxLogError("The prototype '%s' is not valid!", m.GetAsString());
1300 return false;
1301 }
1302
1303 return true;
1304 }
1305
1306
1307
1308 // ----------------------------------------------------------------------------
1309 // wxXmlDoxygenInterface global helpers
1310 // ----------------------------------------------------------------------------
1311
1312 static wxString GetTextFromChildren(const wxXmlNode *n)
1313 {
1314 wxString text;
1315
1316 // consider the tree
1317 //
1318 // <a><b>this</b> is a <b>string</b></a>
1319 //
1320 // <a>
1321 // |- <b>
1322 // | |- this
1323 // |- is a
1324 // |- <b>
1325 // |- string
1326 //
1327 // unlike wxXmlNode::GetNodeContent() which would return " is a "
1328 // this function returns "this is a string"
1329
1330 wxXmlNode *ref = n->GetChildren();
1331 while (ref) {
1332 if (ref->GetType() == wxXML_ELEMENT_NODE)
1333 text += ref->GetNodeContent();
1334 else if (ref->GetType() == wxXML_TEXT_NODE)
1335 text += ref->GetContent();
1336 else
1337 wxLogWarning("Unexpected node type while getting text from '%s' node", n->GetName());
1338
1339 ref = ref->GetNext();
1340 }
1341
1342 return text;
1343 }
1344
1345 static bool HasTextNodeContaining(const wxXmlNode *parent, const wxString& name)
1346 {
1347 if (!parent)
1348 return false;
1349
1350 wxXmlNode *p = parent->GetChildren();
1351 while (p)
1352 {
1353 switch (p->GetType())
1354 {
1355 case wxXML_TEXT_NODE:
1356 if (p->GetContent() == name)
1357 return true;
1358 break;
1359
1360 case wxXML_ELEMENT_NODE:
1361 // recurse into this node...
1362 if (HasTextNodeContaining(p, name))
1363 return true;
1364 break;
1365
1366 default:
1367 // skip it
1368 break;
1369 }
1370
1371 p = p->GetNext();
1372 }
1373
1374 return false;
1375 }
1376
1377 static const wxXmlNode* FindNodeNamed(const wxXmlNode* parent, const wxString& name)
1378 {
1379 if (!parent)
1380 return NULL;
1381
1382 const wxXmlNode *p = parent->GetChildren();
1383 while (p)
1384 {
1385 if (p->GetName() == name)
1386 return p; // found!
1387
1388 // search recursively in the children of this node
1389 const wxXmlNode *ret = FindNodeNamed(p, name);
1390 if (ret)
1391 return ret;
1392
1393 p = p->GetNext();
1394 }
1395
1396 return NULL;
1397 }
1398
1399 int GetAvailabilityFor(const wxXmlNode *node)
1400 {
1401 // identify <onlyfor> custom XML tags
1402 const wxXmlNode* onlyfor = FindNodeNamed(node, "onlyfor");
1403 if (!onlyfor)
1404 return wxPORT_UNKNOWN;
1405
1406 wxArrayString ports = wxSplit(onlyfor->GetNodeContent(), ',');
1407 int nAvail = wxPORT_UNKNOWN;
1408 for (unsigned int i=0; i < ports.GetCount(); i++)
1409 {
1410 if (!ports[i].StartsWith("wx")) {
1411 wxLogError("unexpected port ID '%s'", ports[i]);
1412 return false;
1413 }
1414
1415 nAvail |= wxPlatformInfo::GetPortId(ports[i].Mid(2));
1416 }
1417
1418 return nAvail;
1419 }
1420
1421
1422 // ----------------------------------------------------------------------------
1423 // wxXmlDoxygenInterface
1424 // ----------------------------------------------------------------------------
1425
1426 bool wxXmlDoxygenInterface::Parse(const wxString& filename)
1427 {
1428 wxXmlDocument index;
1429 wxXmlNode *compound;
1430
1431 wxLogMessage("Parsing %s...", filename);
1432
1433 if (!index.Load(filename)) {
1434 wxLogError("can't load %s", filename);
1435 return false;
1436 }
1437
1438 // start processing the index:
1439 if (index.GetRoot()->GetName() != "doxygenindex") {
1440 wxLogError("invalid root node for %s", filename);
1441 return false;
1442 }
1443
1444 /*
1445 NB: we may need in future to do a version-check here if the
1446 format of the XML generated by doxygen changes.
1447 For now (doxygen version 1.5.5), this check is not required
1448 since AFAIK the XML format never changed since it was introduced.
1449 */
1450
1451 m_classes.Alloc(ESTIMATED_NUM_CLASSES);
1452
1453 // process files referenced by this index file
1454 compound = index.GetRoot()->GetChildren();
1455 while (compound)
1456 {
1457 if (compound->GetName() == "compound" &&
1458 compound->GetAttribute("kind") == "class")
1459 {
1460 wxString refid = compound->GetAttribute("refid");
1461
1462 wxFileName fn(filename);
1463 if (!ParseCompoundDefinition(fn.GetPath(wxPATH_GET_SEPARATOR) + refid + ".xml"))
1464 return false;
1465 }
1466
1467 compound = compound->GetNext();
1468 }
1469 //wxPrint("\n");
1470
1471 if (!CheckConsistency())
1472 return false; // the check failed
1473
1474 return true;
1475 }
1476
1477 bool wxXmlDoxygenInterface::ParseCompoundDefinition(const wxString& filename)
1478 {
1479 wxClassMemberIdHashMap parents;
1480 wxXmlDocument doc;
1481 wxXmlNode *child;
1482 int nodes = 0;
1483
1484 if (g_verbose)
1485 {
1486 wxLogMessage("Parsing %s...", filename);
1487 }
1488
1489 if (!doc.Load(filename)) {
1490 wxLogError("can't load %s", filename);
1491 return false;
1492 }
1493
1494 // start processing this compound definition XML
1495 if (doc.GetRoot()->GetName() != "doxygen") {
1496 wxLogError("invalid root node for %s", filename);
1497 return false;
1498 }
1499
1500 // build a list of wx classes
1501 child = doc.GetRoot()->GetChildren();
1502 while (child)
1503 {
1504 if (child->GetName() == "compounddef" &&
1505 child->GetAttribute("kind") == "class")
1506 {
1507 // parse this class
1508 wxClass klass;
1509 wxString absoluteFile, header;
1510
1511 wxXmlNode *subchild = child->GetChildren();
1512 while (subchild)
1513 {
1514 // NOTE: when documenting functions using the //@{ and //@}
1515 // tags to create function groups, doxygen puts the
1516 // contained methods into a "user-defined" section
1517 // so we _must_ use the "prot" attribute to distinguish
1518 // public/protected methods from private ones and cannot
1519 // rely on the kind="public" attribute of <sectiondef>
1520 if (subchild->GetName() == "sectiondef")
1521 {
1522 wxXmlNode *membernode = subchild->GetChildren();
1523 while (membernode)
1524 {
1525 const wxString& accessSpec = membernode->GetAttribute("prot");
1526
1527 // parse only public&protected functions:
1528 if (membernode->GetName() == "memberdef" &&
1529 membernode->GetAttribute("kind") == "function" &&
1530 (accessSpec == "public" || accessSpec == "protected"))
1531 {
1532
1533 wxMethod m;
1534 if (!ParseMethod(membernode, m, header)) {
1535 wxLogError("The method '%s' could not be added to class '%s'",
1536 m.GetName(), klass.GetName());
1537 return false;
1538 }
1539
1540 if (accessSpec == "public")
1541 m.SetAccessSpecifier(wxMAS_PUBLIC);
1542 else if (accessSpec == "protected")
1543 m.SetAccessSpecifier(wxMAS_PROTECTED);
1544 else if (accessSpec == "private")
1545 m.SetAccessSpecifier(wxMAS_PRIVATE);
1546
1547 if (absoluteFile.IsEmpty())
1548 absoluteFile = header;
1549 else if (header != absoluteFile)
1550 {
1551 wxLogError("The method '%s' is documented in a different "
1552 "file from others (which belong to '%s') ?",
1553 header, absoluteFile);
1554 return false;
1555 }
1556
1557 klass.AddMethod(m);
1558 }
1559
1560 membernode = membernode->GetNext();
1561 }
1562
1563 // all methods of this class were taken from the header "absoluteFile":
1564 klass.SetHeader(absoluteFile);
1565 }
1566 else if (subchild->GetName() == "compoundname")
1567 {
1568 klass.SetName(subchild->GetNodeContent());
1569 }
1570 /*else if (subchild->GetName() == "includes")
1571 {
1572 // NOTE: we'll get the header from the <location> tags
1573 // scattered inside <memberdef> tags instead of
1574 // this <includes> tag since it does not contain
1575 // the absolute path of the header
1576
1577 klass.SetHeader(subchild->GetNodeContent());
1578 }*/
1579 else if (subchild->GetName() == "detaileddescription")
1580 {
1581 // identify <onlyfor> custom XML tags
1582 klass.SetAvailability(GetAvailabilityFor(subchild));
1583 }
1584 else if (subchild->GetName() == "basecompoundref")
1585 {
1586 // add the name of this parent to the list of klass' parents
1587 klass.AddParent(subchild->GetNodeContent());
1588 }
1589
1590 subchild = subchild->GetNext();
1591 }
1592
1593 // add a new class
1594 if (klass.IsOk())
1595 {
1596 m_classes.Add(klass);
1597 }
1598 else if (g_verbose)
1599 {
1600 wxLogWarning("discarding class '%s' with %d methods...",
1601 klass.GetName(), klass.GetMethodCount());
1602 }
1603 }
1604
1605 child = child->GetNext();
1606
1607 // give feedback to the user about the progress...
1608 if ((++nodes%PROGRESS_RATE)==0) ShowProgress();
1609 }
1610
1611 return true;
1612 }
1613
1614 bool wxXmlDoxygenInterface::ParseMethod(const wxXmlNode* p, wxMethod& m, wxString& header)
1615 {
1616 wxArgumentTypeArray args;
1617 long line;
1618
1619 wxXmlNode *child = p->GetChildren();
1620 while (child)
1621 {
1622 if (child->GetName() == "name")
1623 m.SetName(child->GetNodeContent());
1624 else if (child->GetName() == "type")
1625 m.SetReturnType(wxType(GetTextFromChildren(child)));
1626 else if (child->GetName() == "param")
1627 {
1628 wxString typestr, namestr, defstr, arrstr;
1629 wxXmlNode *n = child->GetChildren();
1630 while (n)
1631 {
1632 if (n->GetName() == "type")
1633 // if the <type> node has children, they should be all TEXT and <ref> nodes
1634 // and we need to take the text they contain, in the order they appear
1635 typestr = GetTextFromChildren(n);
1636 else if (n->GetName() == "declname")
1637 namestr = GetTextFromChildren(n);
1638 else if (n->GetName() == "defval")
1639 defstr = GetTextFromChildren(n).Strip(wxString::both);
1640 else if (n->GetName() == "array")
1641 arrstr = GetTextFromChildren(n);
1642
1643 n = n->GetNext();
1644 }
1645
1646 if (typestr.IsEmpty()) {
1647 wxLogError("cannot find type node for a param in method '%s'", m.GetName());
1648 return false;
1649 }
1650
1651 wxArgumentType newarg(typestr + arrstr, defstr, namestr);
1652
1653 // can we use preprocessor output to transform the default value
1654 // into the same form which gets processed by wxXmlGccInterface?
1655 wxStringHashMap::const_iterator it = m_preproc.find(defstr);
1656 if (it != m_preproc.end())
1657 newarg.SetDefaultValue(defstr, it->second);
1658
1659 args.Add(newarg);
1660 }
1661 else if (child->GetName() == "location")
1662 {
1663 line = -1;
1664 if (child->GetAttribute("line").ToLong(&line))
1665 m.SetLocation((int)line);
1666 header = child->GetAttribute("file");
1667 }
1668 else if (child->GetName() == "detaileddescription")
1669 {
1670 // when a method has a @deprecated tag inside its description,
1671 // Doxygen outputs somewhere nested inside <detaileddescription>
1672 // a <xreftitle>Deprecated</xreftitle> tag.
1673 m.SetDeprecated(HasTextNodeContaining(child, "Deprecated"));
1674
1675 // identify <onlyfor> custom XML tags
1676 m.SetAvailability(GetAvailabilityFor(child));
1677 }
1678
1679 child = child->GetNext();
1680 }
1681
1682 m.SetArgumentTypes(args);
1683 m.SetConst(p->GetAttribute("const")=="yes");
1684 m.SetStatic(p->GetAttribute("static")=="yes");
1685
1686 // NOTE: Doxygen is smart enough to mark as virtual those functions
1687 // which are declared virtual in base classes but don't have
1688 // the "virtual" keyword explicitely indicated in the derived
1689 // classes... so we don't need any further logic for virtuals
1690
1691 m.SetVirtual(p->GetAttribute("virt")=="virtual");
1692 m.SetPureVirtual(p->GetAttribute("virt")=="pure-virtual");
1693
1694 if (!m.IsOk()) {
1695 wxLogError("The prototype '%s' is not valid!", m.GetAsString());
1696 return false;
1697 }
1698
1699 return true;
1700 }