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