]> git.saurik.com Git - wxWidgets.git/blob - utils/ifacecheck/src/ifacecheck.cpp
BIG CHANGE: added parsing of base classes; now CompareClasses() is much smarter since...
[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 wxClass* 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
193 void IfaceCheckApp::ShowProgress()
194 {
195 wxPrint(".");
196 //fflush(stdout);
197 }
198
199 bool IfaceCheckApp::Compare()
200 {
201 const wxClassArray& interfaces = m_doxyInterface.GetClasses();
202 const wxClass* c;
203 int mcount = 0, ccount = 0;
204
205 LogMessage("Comparing the interface API to the real API (%d classes to compare)...",
206 interfaces.GetCount());
207
208 if (!m_strToMatch.IsEmpty())
209 LogMessage("Processing only header files matching '%s' expression.", m_strToMatch);
210
211 for (unsigned int i=0; i<interfaces.GetCount(); i++)
212 {
213 // only compare the methods which are available for the port
214 // for which the gcc XML was produced
215 if (interfaces[i].GetAvailability() != wxPORT_UNKNOWN &&
216 (interfaces[i].GetAvailability() & m_gccInterface.GetInterfacePort()) == 0) {
217
218 if (g_verbose)
219 LogMessage("skipping class '%s' since it's not available for the %s port.",
220 interfaces[i].GetName(), m_gccInterface.GetInterfacePortName());
221
222 continue; // skip this method
223 }
224
225 // shorten the name of the header so the log file is more readable
226 // and also for calling IsToProcess() against it
227 wxString header = wxFileName(interfaces[i].GetHeader()).GetFullName();
228
229 if (!IsToProcess(header))
230 continue; // skip this one
231
232 wxString cname = interfaces[i].GetName();
233
234 // search in the real headers for i-th interface class; we search for
235 // both class cname and cnameBase since in wxWidgets world tipically
236 // class cname is platform-specific while the real public interface of
237 // that class is part of the cnameBase class.
238 /*c = m_gccInterface.FindClass(cname + "Base");
239 if (c) api.Add(c);*/
240
241 c = m_gccInterface.FindClass(cname);
242 if (!c)
243 {
244 // sometimes the platform-specific class is named "wxGeneric" + cname
245 // or similar:
246 c = m_gccInterface.FindClass("wxGeneric" + cname.Mid(2));
247 if (!c)
248 {
249 c = m_gccInterface.FindClass("wxGtk" + cname.Mid(2));
250 }
251 }
252
253 if (c) {
254
255 // there is a class with the same (logic) name!
256 mcount += CompareClasses(&interfaces[i], c);
257
258 } else {
259
260 LogMessage("%s: couldn't find the real interface for the '%s' class",
261 header, cname);
262 ccount++;
263 }
264 }
265
266 LogMessage("%d methods (%.1f%%) of the interface headers do not exist in the real headers",
267 mcount, (float)(100.0 * mcount/m_doxyInterface.GetMethodCount()));
268 LogMessage("%d classes (%.1f%%) of the interface headers do not exist in the real headers",
269 ccount, (float)(100.0 * ccount/m_doxyInterface.GetClassesCount()));
270
271 return true;
272 }
273
274 int IfaceCheckApp::CompareClasses(const wxClass* iface, const wxClass* api)
275 {
276 const wxMethod *real;
277 int count = 0;
278
279 wxASSERT(iface && api);
280
281 // shorten the name of the header so the log file is more readable
282 wxString header = wxFileName(iface->GetHeader()).GetFullName();
283
284 for (unsigned int i=0; i<iface->GetMethodCount(); i++)
285 {
286 const wxMethod& m = iface->GetMethod(i);
287
288 // only compare the methods which are available for the port
289 // for which the gcc XML was produced
290 if (m.GetAvailability() != wxPORT_UNKNOWN &&
291 (m.GetAvailability() & m_gccInterface.GetInterfacePort()) == 0) {
292
293 if (g_verbose)
294 LogMessage("skipping method '%s' since it's not available for the %s port.",
295 m.GetAsString(), m_gccInterface.GetInterfacePortName());
296
297 continue; // skip this method
298 }
299
300 // search in the methods of the api classes provided
301 real = api->RecursiveUpwardFindMethod(m, &m_gccInterface);
302
303 if (real)
304 {
305 bool exit = false;
306 wxMethodPtrArray overloads =
307 api->RecursiveUpwardFindMethodsNamed(m.GetName(), &m_gccInterface);
308
309 #define HACK_TO_AUTO_CORRECT_ONLY_METHOD_ATTRIBUTES 0
310 #if HACK_TO_AUTO_CORRECT_ONLY_METHOD_ATTRIBUTES
311 for (unsigned int k=0; k<overloads.GetCount(); k++)
312 if (overloads[k]->MatchesExceptForAttributes(m) &&
313 overloads[k]->IsPureVirtual() == m.IsPureVirtual())
314 {
315 // fix default values of results[k]:
316 wxMethod tmp(*overloads[k]);
317 tmp.SetArgumentTypes(m.GetArgumentTypes());
318
319 // modify interface header
320 if (FixMethod(iface->GetHeader(), &m, &tmp))
321 LogMessage("Adjusted attributes of '%s' method", m.GetAsString());
322
323 exit = true;
324 break;
325 }
326
327 if (!exit)
328 {
329 #endif
330
331 if (overloads.GetCount()==0)
332 {
333 LogMessage("%s: real '%s' class and their parents have no method '%s'",
334 header, api->GetName(), m.GetAsString());
335 // we've found no overloads
336 }
337 else
338 {
339 // first, output a warning
340 wxString warning = header;
341 if (overloads.GetCount()>1)
342 warning += wxString::Format(": in the real headers there are %d overloads of '%s' for "
343 "'%s' all with different signatures:\n",
344 overloads.GetCount(), m.GetName(), api->GetName());
345 else
346 warning += wxString::Format(": in the real headers there is a method '%s' for '%s'"
347 " but has different signature:\n",
348 m.GetName(), api->GetName());
349
350 // get a list of the prototypes with _all_ possible attributes:
351 warning += "\tdoxy header: " + m.GetAsString(true, true, true, true);
352 for (unsigned int j=0; j<overloads.GetCount(); j++)
353 warning += "\n\treal header: " + overloads[j]->GetAsString(true, true, true, true);
354
355 wxPrint(warning + "\n");
356 count++;
357
358 if (overloads.GetCount()>1)
359 {
360 // TODO: decide which of these overloads is the most "similar" to m
361 // and eventually modify it
362 if (m_modify)
363 wxPrint("\tmanual fix is required\n");
364 }
365 else
366 {
367 wxASSERT(overloads.GetCount() == 1);
368
369 if (m_modify)
370 {
371 wxPrint("\tfixing it...\n");
372
373 // try to modify it!
374 FixMethod(iface->GetHeader(), &m, overloads[0]);
375 }
376 }
377 }
378
379 count++;
380
381 #if HACK_TO_AUTO_CORRECT_ONLY_METHOD_ATTRIBUTES
382 }
383 #endif
384 }
385 }
386
387 return count;
388 }
389
390 bool IfaceCheckApp::FixMethod(const wxString& header, const wxMethod* iface, const wxMethod* api)
391 {
392 wxASSERT(iface && api);
393
394 wxTextFile file;
395 if (!file.Open(header)) {
396 LogError("\tcan't open the '%s' header file.", header);
397 return false;
398 }
399
400 // GetLocation() returns the line where the last part of the prototype is placed:
401 int end = iface->GetLocation()-1;
402 if (end <= 0 || end >= (int)file.GetLineCount()) {
403 LogWarning("\tinvalid location info for method '%s': %d.",
404 iface->GetAsString(), iface->GetLocation());
405 return false;
406 }
407
408 if (!file.GetLine(end).Contains(";")) {
409 LogWarning("\tinvalid location info for method '%s': %d.",
410 iface->GetAsString(), iface->GetLocation());
411 return false;
412 }
413
414 // is this a one-line prototype declaration?
415 bool founddecl = false;
416 int start;
417 if (file.GetLine(end).Contains(iface->GetName()))
418 {
419 // yes, this prototype is all on this line:
420 start = end;
421 founddecl = true;
422 }
423 else
424 {
425 start = end-1;
426
427 // find the start point of this prototype declaration:
428 while (start > 0 &&
429 !file.GetLine(start).Contains(";") &&
430 !file.GetLine(start).Contains("*/"))
431 {
432 start--;
433
434 founddecl |= file.GetLine(start).Contains(iface->GetName());
435 }
436
437 // start-th line contains either the declaration of another prototype
438 // or the closing tag */ of a doxygen comment; start one line below
439 start++;
440 }
441
442 if (start <= 0 || !founddecl)
443 {
444 LogError("\tcan't find the beginning of the declaration of '%s' method in '%s' header looking backwards from line %d",
445 iface->GetAsString(), header, end);
446 return false;
447 }
448
449 // remove the old prototype
450 for (int i=start; i<=end; i++)
451 file.RemoveLine(start); // remove (end-start)-nth times the start-th line
452
453 #define INDENTATION_STR wxString(" ")
454
455 // if possible, add also the @deprecated tag in the doxygen comment if it's missing
456 int deprecationOffset = 0;
457 if (file.GetLine(start-1).Contains("*/") &&
458 (api->IsDeprecated() && !iface->IsDeprecated()))
459 {
460 file.RemoveLine(start-1);
461 file.InsertLine(INDENTATION_STR + INDENTATION_STR +
462 "@deprecated @todo provide deprecation description", start-1);
463 file.InsertLine(INDENTATION_STR + "*/", start++);
464
465 // we have added a new line in the final balance
466 deprecationOffset=1;
467 }
468
469 wxMethod tmp(*api);
470
471 // discard API argument names and replace them with those parsed from doxygen XML:
472 const wxArgumentTypeArray& doxygenargs = iface->GetArgumentTypes();
473 const wxArgumentTypeArray& realargs = api->GetArgumentTypes();
474 if (realargs.GetCount() == doxygenargs.GetCount())
475 {
476 for (unsigned int j=0; j<doxygenargs.GetCount(); j++)
477 if (doxygenargs[j]==realargs[j])
478 realargs[j].SetArgumentName(doxygenargs[j].GetArgumentName());
479
480 tmp.SetArgumentTypes(realargs);
481 }
482
483 #define WRAP_COLUMN 80
484
485 wxArrayString toinsert;
486 toinsert.Add(INDENTATION_STR + tmp.GetAsString() + ";");
487
488 int nStartColumn = toinsert[0].Find('(');
489 wxASSERT(nStartColumn != wxNOT_FOUND);
490
491 // wrap lines too long at comma boundaries
492 for (unsigned int i=0; i<toinsert.GetCount(); i++)
493 {
494 size_t len = toinsert[i].Len();
495 if (len > WRAP_COLUMN)
496 {
497 wxASSERT(i == toinsert.GetCount()-1);
498
499 // break this line
500 wxString tmpleft = toinsert[i].Left(WRAP_COLUMN);
501 int comma = tmpleft.Find(',', true /* from end */);
502 if (comma == wxNOT_FOUND)
503 break; // break out of the for cycle...
504
505 toinsert.Add(wxString(' ', nStartColumn+1) +
506 toinsert[i].Right(len-comma-2)); // exclude the comma and the space after it
507 toinsert[i] = tmpleft.Left(comma+1); // include the comma
508 }
509 }
510
511 // insert the new lines
512 for (unsigned int i=0; i<toinsert.GetCount(); i++)
513 file.InsertLine(toinsert[i], start+i);
514
515 // now save the modification
516 if (!file.Write()) {
517 LogError("\tcan't save the '%s' header file.", header);
518 return false;
519 }
520
521 // how many lines did we add/remove in total?
522 int nOffset = toinsert.GetCount() + deprecationOffset - (end-start+1);
523 if (nOffset == 0)
524 return false;
525
526 if (g_verbose)
527 LogMessage("\tthe final row offset for following methods is %d lines.", nOffset);
528
529 // update the other method's locations for those methods which belong to the modified header
530 // and are placed _below_ the modified method
531 wxClassPtrArray cToUpdate = m_doxyInterface.FindClassesDefinedIn(header);
532 for (unsigned int i=0; i < cToUpdate.GetCount(); i++)
533 {
534 for (unsigned int j=0; j < cToUpdate[i]->GetMethodCount(); j++)
535 {
536 wxMethod& m = cToUpdate[i]->GetMethod(j);
537 if (m.GetLocation() > iface->GetLocation())
538 {
539 // update the location of this method
540 m.SetLocation(m.GetLocation()+nOffset);
541 }
542 }
543 }
544
545 return true;
546 }
547
548 bool IfaceCheckApp::ParsePreprocessorOutput(const wxString& filename)
549 {
550 wxTextFile tf;
551 if (!tf.Open(filename)) {
552 LogError("can't open the '%s' preprocessor output file.", filename);
553 return false;
554 }
555
556 size_t useful = 0;
557 for (unsigned int i=0; i < tf.GetLineCount(); i++)
558 {
559 const wxString& line = tf.GetLine(i);
560 wxString defnameval = line.Mid(8); // what follows the "#define " string
561
562 // the format of this line should be:
563 // #define DEFNAME DEFVALUE
564 if (!line.StartsWith("#define ")) {
565 LogError("unexpected content in '%s' at line %d.", filename, i+1);
566 return false;
567 }
568
569 if (defnameval.Contains(" "))
570 {
571 // get DEFNAME
572 wxString defname = defnameval.BeforeFirst(' ');
573 if (defname.Contains("("))
574 continue; // this is a macro, skip it!
575
576 // get DEFVAL
577 wxString defval = defnameval.AfterFirst(' ').Strip(wxString::both);
578 if (defval.StartsWith("(") && defval.EndsWith(")"))
579 defval = defval.Mid(1, defval.Len()-2);
580
581 // store this pair in the doxygen interface, where it can be useful
582 m_doxyInterface.AddPreprocessorValue(defname, defval);
583 useful++;
584 }
585 else
586 {
587 // it looks like the format of this line is:
588 // #define DEFNAME
589 // we are not interested to symbols #defined to nothing,
590 // so we just ignore this line.
591 }
592 }
593
594 LogMessage("Parsed %d preprocessor #defines from '%s' which will be used later...",
595 useful, filename);
596
597 return true;
598 }
599
600 void IfaceCheckApp::PrintStatistics(long secs)
601 {
602 LogMessage("wx real headers contains declaration of %d classes (%d methods)",
603 m_gccInterface.GetClassesCount(), m_gccInterface.GetMethodCount());
604 LogMessage("wx interface headers contains declaration of %d classes (%d methods)",
605 m_doxyInterface.GetClassesCount(), m_doxyInterface.GetMethodCount());
606 LogMessage("total processing took %d seconds.", secs);
607 }
608