]> git.saurik.com Git - wxWidgets.git/blob - utils/ifacecheck/src/ifacecheck.cpp
make m_strDefaultValueForCmp always non-empty; this simplifies the code; fix wxMethod...
[wxWidgets.git] / utils / ifacecheck / src / ifacecheck.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: ifacecheck.cpp
3 // Purpose: Interface headers <=> real headers coherence checker
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/app.h"
21 #endif
22
23 #include "wx/cmdline.h"
24 #include "wx/textfile.h"
25 #include "wx/filename.h"
26 #include "wx/stopwatch.h" // for wxGetLocalTime
27 #include "xmlparser.h"
28
29 // global verbosity flag
30 bool g_verbose = false;
31
32
33
34 // ----------------------------------------------------------------------------
35 // IfaceCheckApp
36 // ----------------------------------------------------------------------------
37
38 #define API_DUMP_FILE "dump.api.txt"
39 #define INTERFACE_DUMP_FILE "dump.interface.txt"
40
41 #define PROCESS_ONLY_OPTION "p"
42 #define USE_PREPROCESSOR_OPTION "u"
43
44 #define MODIFY_SWITCH "m"
45 #define DUMP_SWITCH "d"
46 #define HELP_SWITCH "h"
47 #define VERBOSE_SWITCH "v"
48
49 static const wxCmdLineEntryDesc g_cmdLineDesc[] =
50 {
51 { wxCMD_LINE_OPTION, USE_PREPROCESSOR_OPTION, "use-preproc",
52 "uses the preprocessor output to increase the checker accuracy",
53 wxCMD_LINE_VAL_STRING, wxCMD_LINE_NEEDS_SEPARATOR },
54 { wxCMD_LINE_OPTION, PROCESS_ONLY_OPTION, "process-only",
55 "processes only header files matching the given wildcard",
56 wxCMD_LINE_VAL_STRING, wxCMD_LINE_NEEDS_SEPARATOR },
57 { wxCMD_LINE_SWITCH, MODIFY_SWITCH, "modify",
58 "modify the interface headers to match the real ones" },
59 { wxCMD_LINE_SWITCH, DUMP_SWITCH, "dump",
60 "dump both interface and API to plain text dump.*.txt files" },
61 { wxCMD_LINE_SWITCH, HELP_SWITCH, "help",
62 "show help message", wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
63 { wxCMD_LINE_SWITCH, VERBOSE_SWITCH, "verbose",
64 "be verbose" },
65 { wxCMD_LINE_PARAM, NULL, NULL,
66 "gccXML", wxCMD_LINE_VAL_STRING, wxCMD_LINE_OPTION_MANDATORY },
67 { wxCMD_LINE_PARAM, NULL, NULL,
68 "doxygenXML", wxCMD_LINE_VAL_STRING, wxCMD_LINE_OPTION_MANDATORY },
69 wxCMD_LINE_DESC_END
70 };
71
72 class IfaceCheckApp : public wxAppConsole
73 {
74 public:
75 // don't use builtin cmd line parsing:
76 virtual bool OnInit() { m_modify=false; return true; }
77 virtual int OnRun();
78
79 bool ParsePreprocessorOutput(const wxString& filename);
80
81 bool Compare();
82 int CompareClasses(const wxClass* iface, const wxClassPtrArray& api);
83 bool FixMethod(const wxString& header, const wxMethod* iface, const wxMethod* api);
84
85 void ShowProgress();
86 void PrintStatistics(long secs);
87
88 bool IsToProcess(const wxString& headername) const
89 {
90 if (m_strToMatch.IsEmpty())
91 return true;
92 return wxMatchWild(m_strToMatch, headername, false);
93 }
94
95 protected:
96 wxXmlGccInterface m_gccInterface; // "real" headers API
97 wxXmlDoxygenInterface m_doxyInterface; // doxygen-commented headers API
98
99 // was the MODIFY_SWITCH passed?
100 bool m_modify;
101
102 // if non-empty, then PROCESS_ONLY_OPTION was passed and this is the
103 // wildcard expression to match
104 wxString m_strToMatch;
105 };
106
107 IMPLEMENT_APP_CONSOLE(IfaceCheckApp)
108
109 int IfaceCheckApp::OnRun()
110 {
111 long startTime = wxGetLocalTime(); // for timing purpose
112
113 wxCmdLineParser parser(g_cmdLineDesc, argc, argv);
114 parser.SetLogo(
115 wxString::Format("wxWidgets Interface checker utility (built %s against %s)",
116 __DATE__, wxVERSION_STRING));
117
118 // parse the command line...
119 bool ok = true;
120 wxString preprocFile;
121 switch (parser.Parse())
122 {
123 case 0:
124 if (parser.Found(VERBOSE_SWITCH))
125 g_verbose = true;
126
127 // IMPORTANT: parsing #define values must be done _before_ actually
128 // parsing the GCC/doxygen XML files
129 if (parser.Found(USE_PREPROCESSOR_OPTION, &preprocFile))
130 {
131 if (!ParsePreprocessorOutput(preprocFile))
132 return 1;
133 }
134
135 // in any case set basic std preprocessor #defines:
136 m_doxyInterface.AddPreprocessorValue("NULL", "0");
137
138 g_bLogEnabled = false;
139
140 // parse the two XML files which contain the real and the doxygen interfaces
141 // for wxWidgets API:
142 if (!m_gccInterface.Parse(parser.GetParam(0)) ||
143 !m_doxyInterface.Parse(parser.GetParam(1)))
144 return 1;
145
146 g_bLogEnabled = true;
147
148 if (parser.Found(DUMP_SWITCH))
149 {
150 LogMessage("Dumping real API to '%s'...", API_DUMP_FILE);
151 m_gccInterface.Dump(API_DUMP_FILE);
152
153 LogMessage("Dumping interface API to '%s'...", INTERFACE_DUMP_FILE);
154 m_doxyInterface.Dump(INTERFACE_DUMP_FILE);
155 }
156 else
157 {
158 if (parser.Found(MODIFY_SWITCH))
159 m_modify = true;
160
161 if (parser.Found(PROCESS_ONLY_OPTION, &m_strToMatch))
162 {
163 size_t len = m_strToMatch.Len();
164 if (m_strToMatch.StartsWith("\"") &&
165 m_strToMatch.EndsWith("\"") &&
166 len > 2)
167 m_strToMatch = m_strToMatch.Mid(1, len-2);
168 }
169
170
171 ok = Compare();
172 }
173
174 PrintStatistics(wxGetLocalTime() - startTime);
175 return ok ? 0 : 1;
176
177 default:
178 wxPrintf("\nThis utility checks that the interface XML files created by Doxygen are in\n");
179 wxPrintf("synch with the real headers (whose contents are extracted by the gcc XML file).\n\n");
180 wxPrintf("The 'gccXML' parameter should be the wxapi.xml file created by the 'rungccxml.sh'\n");
181 wxPrintf("script which resides in 'utils/ifacecheck'.\n");
182 wxPrintf("The 'doxygenXML' parameter should be the index.xml file created by Doxygen\n");
183 wxPrintf("for the wxWidgets 'interface' folder.\n\n");
184 wxPrintf("Since the gcc XML file does not contain info about #defines, if you use\n");
185 wxPrintf("the -%s option, you'll get a smaller number of false warnings.\n",
186 USE_PREPROCESSOR_OPTION);
187
188 // HELP_SWITCH was passed or a syntax error occurred
189 return 0;
190 }
191
192 return 1;
193 }
194
195 void IfaceCheckApp::ShowProgress()
196 {
197 wxPrint(".");
198 //fflush(stdout);
199 }
200
201 bool IfaceCheckApp::Compare()
202 {
203 const wxClassArray& interface = m_doxyInterface.GetClasses();
204 const wxClass* c;
205 wxClassPtrArray api;
206 int mcount = 0, ccount = 0;
207
208 LogMessage("Comparing the interface API to the real API (%d classes to compare)...",
209 interface.GetCount());
210
211 if (!m_strToMatch.IsEmpty())
212 LogMessage("Processing only header files matching '%s' expression.", m_strToMatch);
213
214 for (unsigned int i=0; i<interface.GetCount(); i++)
215 {
216 // only compare the methods which are available for the port
217 // for which the gcc XML was produced
218 if (interface[i].GetAvailability() != wxPORT_UNKNOWN &&
219 (interface[i].GetAvailability() & m_gccInterface.GetInterfacePort()) == 0) {
220
221 if (g_verbose)
222 LogMessage("skipping class '%s' since it's not available for the %s port.",
223 interface[i].GetName(), m_gccInterface.GetInterfacePortName());
224
225 continue; // skip this method
226 }
227
228 // shorten the name of the header so the log file is more readable
229 // and also for calling IsToProcess() against it
230 wxString header = wxFileName(interface[i].GetHeader()).GetFullName();
231
232 if (!IsToProcess(header))
233 continue; // skip this one
234
235 wxString cname = interface[i].GetName();
236
237 api.Empty();
238
239 // search in the real headers for i-th interface class; we search for
240 // both class cname and cnameBase since in wxWidgets world tipically
241 // class cname is platform-specific while the real public interface of
242 // that class is part of the cnameBase class.
243 c = m_gccInterface.FindClass(cname);
244 if (c) api.Add(c);
245 c = m_gccInterface.FindClass(cname + "Base");
246 if (c) api.Add(c);
247
248 // sometimes the platform-specific class is named "wxGeneric" + cname
249 // or similar:
250 c = m_gccInterface.FindClass("wxGeneric" + cname.Mid(2));
251 if (c) api.Add(c);
252 c = m_gccInterface.FindClass("wxGtk" + cname.Mid(2));
253 if (c) api.Add(c);
254
255 if (api.GetCount()>0) {
256
257 // there is a class with exactly the same name!
258 mcount += CompareClasses(&interface[i], api);
259
260 } else {
261
262 LogMessage("%s: couldn't find the real interface for the '%s' class",
263 header, cname);
264 ccount++;
265 }
266 }
267
268 LogMessage("%d methods (%.1f%%) of the interface headers do not exist in the real headers",
269 mcount, (float)(100.0 * mcount/m_doxyInterface.GetMethodCount()));
270 LogMessage("%d classes (%.1f%%) of the interface headers do not exist in the real headers",
271 ccount, (float)(100.0 * ccount/m_doxyInterface.GetClassesCount()));
272
273 return true;
274 }
275
276 int IfaceCheckApp::CompareClasses(const wxClass* iface, const wxClassPtrArray& api)
277 {
278 wxString searchedclasses;
279 const wxMethod *real;
280 int count = 0;
281
282 wxASSERT(iface && api.GetCount()>0);
283
284 // build a string with the names of the API classes compared to iface
285 for (unsigned int j=0; j<api.GetCount(); j++)
286 searchedclasses += "/" + api[j]->GetName();
287 searchedclasses.Remove(0, 1);
288
289 // shorten the name of the header so the log file is more readable
290 wxString header = wxFileName(iface->GetHeader()).GetFullName();
291
292 for (unsigned int i=0; i<iface->GetMethodCount(); i++)
293 {
294 const wxMethod& m = iface->GetMethod(i);
295 int matches = 0;
296
297 // only compare the methods which are available for the port
298 // for which the gcc XML was produced
299 if (m.GetAvailability() != wxPORT_UNKNOWN &&
300 (m.GetAvailability() & m_gccInterface.GetInterfacePort()) == 0) {
301
302 if (g_verbose)
303 LogMessage("skipping method '%s' since it's not available for the %s port.",
304 m.GetAsString(), m_gccInterface.GetInterfacePortName());
305
306 continue; // skip this method
307 }
308
309 // search in the methods of the api classes provided
310 for (unsigned int j=0; j<api.GetCount(); j++)
311 {
312 real = api[j]->FindMethod(m);
313 if (real)
314 matches++; // there is a real matching prototype! It's ok!
315 }
316
317 if (matches == 0)
318 {
319 bool exit = false;
320 wxMethodPtrArray overloads;
321
322 // try searching for methods with the same name but with
323 // different return type / arguments / qualifiers
324 for (unsigned int j=0; j<api.GetCount(); j++)
325 {
326 wxMethodPtrArray results = api[j]->FindMethodsNamed(m.GetName());
327
328 // append "results" array to "overloads"
329 WX_APPEND_ARRAY(overloads, results);
330
331
332 #define HACK_TO_AUTO_CORRECT_ONLY_VIRTUAL_AND_CONST_ATTRIBUTES 1
333 #if HACK_TO_AUTO_CORRECT_ONLY_VIRTUAL_AND_CONST_ATTRIBUTES
334 for (unsigned int k=0; k<results.GetCount(); k++)
335 if (results[k]->MatchesExceptForAttributes(m) &&
336 results[k]->IsPureVirtual() == m.IsPureVirtual())
337 {
338 // fix default values of results[k]:
339 wxMethod tmp(*results[k]);
340 tmp.SetArgumentTypes(m.GetArgumentTypes());
341
342 // modify interface header
343 if (FixMethod(iface->GetHeader(), &m, &tmp))
344 LogMessage("Adjusted attributes of '%s' method", m.GetAsString());
345
346 exit = true;
347 break;
348 }
349
350 if (exit)
351 break;
352 #endif
353 }
354
355 #if HACK_TO_AUTO_CORRECT_ONLY_VIRTUAL_AND_CONST_ATTRIBUTES
356 if (!exit)
357 {
358 #endif
359
360 if (overloads.GetCount()==0)
361 {
362 /*
363 TODO: sometimes the interface headers re-document a method
364 inherited from a base class even if the real header does
365 not actually re-implement it.
366 To avoid false positives, we'd need to search in the base classes
367 of api[] classes and search for a matching method.
368 */
369 LogMessage("%s: real '%s' class has no method '%s'",
370 header, searchedclasses, m.GetAsString());
371 // we've found no overloads
372 }
373 else
374 {
375 // first, output a warning
376 wxString warning = header;
377 if (overloads.GetCount()>1)
378 warning += wxString::Format(": in the real headers there are %d overloads of '%s' for "
379 "'%s' all with different signatures:\n",
380 overloads.GetCount(), m.GetName(), searchedclasses);
381 else
382 warning += wxString::Format(": in the real headers there is a method '%s' for '%s'"
383 " but has different signature:\n",
384 m.GetName(), searchedclasses);
385
386 // get a list of the prototypes with _all_ possible attributes:
387 warning += "\tdoxy header: " + m.GetAsString(true, true, true);
388 for (unsigned int j=0; j<overloads.GetCount(); j++)
389 warning += "\n\treal header: " + overloads[j]->GetAsString(true, true, true);
390
391 wxPrint(warning + "\n");
392 count++;
393
394 if (overloads.GetCount()>1)
395 {
396 // TODO: decide which of these overloads is the most "similar" to m
397 // and eventually modify it
398 if (m_modify)
399 wxPrint("\tmanual fix is required\n");
400 }
401 else
402 {
403 wxASSERT(overloads.GetCount() == 1);
404
405 if (m_modify)
406 {
407 wxPrint("\tfixing it...\n");
408
409 // try to modify it!
410 FixMethod(iface->GetHeader(), &m, overloads[0]);
411 }
412 }
413 }
414
415 count++;
416
417 #if HACK_TO_AUTO_CORRECT_ONLY_VIRTUAL_AND_CONST_ATTRIBUTES
418 }
419 #endif
420 }
421 }
422
423 return count;
424 }
425
426 bool IfaceCheckApp::FixMethod(const wxString& header, const wxMethod* iface, const wxMethod* api)
427 {
428 wxASSERT(iface && api);
429
430 wxTextFile file;
431 if (!file.Open(header)) {
432 LogError("\tcan't open the '%s' header file.", header);
433 return false;
434 }
435
436 // GetLocation() returns the line where the last part of the prototype is placed:
437 int end = iface->GetLocation()-1;
438 if (end <= 0 || end >= (int)file.GetLineCount()) {
439 LogWarning("\tinvalid location info for method '%s': %d.",
440 iface->GetAsString(), iface->GetLocation());
441 return false;
442 }
443
444 if (!file.GetLine(end).Contains(";")) {
445 LogWarning("\tinvalid location info for method '%s': %d.",
446 iface->GetAsString(), iface->GetLocation());
447 return false;
448 }
449
450 // is this a one-line prototype declaration?
451 bool founddecl = false;
452 int start;
453 if (file.GetLine(end).Contains(iface->GetName()))
454 {
455 // yes, this prototype is all on this line:
456 start = end;
457 founddecl = true;
458 }
459 else
460 {
461 start = end-1;
462
463 // find the start point of this prototype declaration:
464 while (start > 0 &&
465 !file.GetLine(start).Contains(";") &&
466 !file.GetLine(start).Contains("*/"))
467 {
468 start--;
469
470 founddecl |= file.GetLine(start).Contains(iface->GetName());
471 }
472
473 // start-th line contains either the declaration of another prototype
474 // or the closing tag */ of a doxygen comment; start one line below
475 start++;
476 }
477
478 if (start <= 0 || !founddecl)
479 {
480 LogError("\tcan't find the beginning of the declaration of '%s' method in '%s' header looking backwards from line %d",
481 iface->GetAsString(), header, end);
482 return false;
483 }
484
485 // remove the old prototype
486 for (int i=start; i<=end; i++)
487 file.RemoveLine(start); // remove (end-start)-nth times the start-th line
488
489 #define INDENTATION_STR wxString(" ")
490
491 // if possible, add also the @deprecated tag in the doxygen comment if it's missing
492 int deprecationOffset = 0;
493 if (file.GetLine(start-1).Contains("*/") &&
494 (api->IsDeprecated() && !iface->IsDeprecated()))
495 {
496 file.RemoveLine(start-1);
497 file.InsertLine(INDENTATION_STR + INDENTATION_STR +
498 "@deprecated @todo provide deprecation description", start-1);
499 file.InsertLine(INDENTATION_STR + "*/", start++);
500
501 // we have added a new line in the final balance
502 deprecationOffset=1;
503 }
504
505 wxMethod tmp(*api);
506
507 // discard API argument names and replace them with those parsed from doxygen XML:
508 const wxArgumentTypeArray& doxygenargs = iface->GetArgumentTypes();
509 const wxArgumentTypeArray& realargs = api->GetArgumentTypes();
510 if (realargs.GetCount() == doxygenargs.GetCount())
511 {
512 for (unsigned int j=0; j<doxygenargs.GetCount(); j++)
513 if (doxygenargs[j]==realargs[j])
514 realargs[j].SetArgumentName(doxygenargs[j].GetArgumentName());
515
516 tmp.SetArgumentTypes(realargs);
517 }
518
519 #define WRAP_COLUMN 80
520
521 wxArrayString toinsert;
522 toinsert.Add(INDENTATION_STR + tmp.GetAsString() + ";");
523
524 int nStartColumn = toinsert[0].Find('(');
525 wxASSERT(nStartColumn != wxNOT_FOUND);
526
527 // wrap lines too long at comma boundaries
528 for (unsigned int i=0; i<toinsert.GetCount(); i++)
529 {
530 size_t len = toinsert[i].Len();
531 if (len > WRAP_COLUMN)
532 {
533 wxASSERT(i == toinsert.GetCount()-1);
534
535 // break this line
536 wxString tmpleft = toinsert[i].Left(WRAP_COLUMN);
537 int comma = tmpleft.Find(',', true /* from end */);
538 if (comma == wxNOT_FOUND)
539 break; // break out of the for cycle...
540
541 toinsert.Add(wxString(' ', nStartColumn+1) +
542 toinsert[i].Right(len-comma-2)); // exclude the comma and the space after it
543 toinsert[i] = tmpleft.Left(comma+1); // include the comma
544 }
545 }
546
547 // insert the new lines
548 for (unsigned int i=0; i<toinsert.GetCount(); i++)
549 file.InsertLine(toinsert[i], start+i);
550
551 // now save the modification
552 if (!file.Write()) {
553 LogError("\tcan't save the '%s' header file.", header);
554 return false;
555 }
556
557 // how many lines did we add/remove in total?
558 int nOffset = toinsert.GetCount() + deprecationOffset - (end-start+1);
559 if (nOffset == 0)
560 return false;
561
562 if (g_verbose)
563 LogMessage("\tthe final row offset for following methods is %d lines.", nOffset);
564
565 // update the other method's locations for those methods which belong to the modified header
566 // and are placed _below_ the modified method
567 wxClassPtrArray cToUpdate = m_doxyInterface.FindClassesDefinedIn(header);
568 for (unsigned int i=0; i < cToUpdate.GetCount(); i++)
569 {
570 for (unsigned int j=0; j < cToUpdate[i]->GetMethodCount(); j++)
571 {
572 wxMethod& m = cToUpdate[i]->GetMethod(j);
573 if (m.GetLocation() > iface->GetLocation())
574 {
575 // update the location of this method
576 m.SetLocation(m.GetLocation()+nOffset);
577 }
578 }
579 }
580
581 return true;
582 }
583
584 bool IfaceCheckApp::ParsePreprocessorOutput(const wxString& filename)
585 {
586 wxTextFile tf;
587 if (!tf.Open(filename)) {
588 LogError("can't open the '%s' preprocessor output file.", filename);
589 return false;
590 }
591
592 size_t useful = 0;
593 for (unsigned int i=0; i < tf.GetLineCount(); i++)
594 {
595 const wxString& line = tf.GetLine(i);
596 wxString defnameval = line.Mid(8); // what follows the "#define " string
597
598 // the format of this line should be:
599 // #define DEFNAME DEFVALUE
600 if (!line.StartsWith("#define ")) {
601 LogError("unexpected content in '%s' at line %d.", filename, i+1);
602 return false;
603 }
604
605 if (defnameval.Contains(" "))
606 {
607 // get DEFNAME
608 wxString defname = defnameval.BeforeFirst(' ');
609 if (defname.Contains("("))
610 continue; // this is a macro, skip it!
611
612 // get DEFVAL
613 wxString defval = defnameval.AfterFirst(' ').Strip(wxString::both);
614 if (defval.StartsWith("(") && defval.EndsWith(")"))
615 defval = defval.Mid(1, defval.Len()-2);
616
617 // store this pair in the doxygen interface, where it can be useful
618 m_doxyInterface.AddPreprocessorValue(defname, defval);
619 useful++;
620 }
621 else
622 {
623 // it looks like the format of this line is:
624 // #define DEFNAME
625 // we are not interested to symbols #defined to nothing,
626 // so we just ignore this line.
627 }
628 }
629
630 LogMessage("Parsed %d preprocessor #defines from '%s' which will be used later...",
631 useful, filename);
632
633 return true;
634 }
635
636 void IfaceCheckApp::PrintStatistics(long secs)
637 {
638 LogMessage("wx real headers contains declaration of %d classes (%d methods)",
639 m_gccInterface.GetClassesCount(), m_gccInterface.GetMethodCount());
640 LogMessage("wx interface headers contains declaration of %d classes (%d methods)",
641 m_doxyInterface.GetClassesCount(), m_doxyInterface.GetMethodCount());
642 LogMessage("total processing took %d seconds.", secs);
643 }
644