1 /////////////////////////////////////////////////////////////////////////////
2 // Name: ifacecheck.cpp
3 // Purpose: Interface headers <=> real headers coherence checker
4 // Author: Francesco Montorsi
6 // Copyright: (c) 2008 Francesco Montorsi
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
17 // for all others, include the necessary headers
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"
29 // global verbosity flag
30 bool g_verbose
= false;
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 #define API_DUMP_FILE "dump.api.txt"
39 #define INTERFACE_DUMP_FILE "dump.interface.txt"
41 #define PROCESS_ONLY_OPTION "p"
42 #define USE_PREPROCESSOR_OPTION "u"
44 #define MODIFY_SWITCH "m"
45 #define DUMP_SWITCH "d"
46 #define HELP_SWITCH "h"
47 #define VERBOSE_SWITCH "v"
49 static const wxCmdLineEntryDesc g_cmdLineDesc
[] =
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",
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
},
72 class IfaceCheckLog
: public wxLog
77 virtual void DoLogText(const wxString
& msg
)
79 // send all messages to stdout (normal behaviour is to sent them to stderr)
85 class IfaceCheckApp
: public wxAppConsole
88 // don't use builtin cmd line parsing:
89 virtual bool OnInit() { m_modify
=false; return true; }
92 bool ParsePreprocessorOutput(const wxString
& filename
);
95 int CompareClasses(const wxClass
* iface
, const wxClass
* api
);
96 bool FixMethod(const wxString
& header
, const wxMethod
* iface
, const wxMethod
* api
);
97 bool StringContainsMethodName(const wxString
& str
, const wxMethod
* m
);
99 void PrintStatistics(long secs
);
101 bool IsToProcess(const wxString
& headername
) const
103 if (m_strToMatch
.IsEmpty())
105 return wxMatchWild(m_strToMatch
, headername
, false);
109 wxXmlGccInterface m_gccInterface
; // "real" headers API
110 wxXmlDoxygenInterface m_doxyInterface
; // doxygen-commented headers API
112 // was the MODIFY_SWITCH passed?
115 // if non-empty, then PROCESS_ONLY_OPTION was passed and this is the
116 // wildcard expression to match
117 wxString m_strToMatch
;
120 IMPLEMENT_APP_CONSOLE(IfaceCheckApp
)
122 int IfaceCheckApp::OnRun()
124 long startTime
= wxGetLocalTime(); // for timing purpose
126 wxCmdLineParser
parser(g_cmdLineDesc
, argc
, argv
);
128 wxString::Format("wxWidgets Interface checker utility (built %s against %s)",
129 __DATE__
, wxVERSION_STRING
));
131 // make the output more readable:
132 wxLog::SetActiveTarget(new IfaceCheckLog
);
133 wxLog::DisableTimestamp();
135 // parse the command line...
137 wxString preprocFile
;
138 switch (parser
.Parse())
141 if (parser
.Found(VERBOSE_SWITCH
))
144 // IMPORTANT: parsing #define values must be done _before_ actually
145 // parsing the GCC/doxygen XML files
146 if (parser
.Found(USE_PREPROCESSOR_OPTION
, &preprocFile
))
148 if (!ParsePreprocessorOutput(preprocFile
))
152 // in any case set basic std preprocessor #defines:
153 m_doxyInterface
.AddPreprocessorValue("NULL", "0");
155 // parse the two XML files which contain the real and the doxygen interfaces
156 // for wxWidgets API:
157 if (!m_gccInterface
.Parse(parser
.GetParam(0)) ||
158 !m_doxyInterface
.Parse(parser
.GetParam(1)))
161 if (parser
.Found(DUMP_SWITCH
))
163 wxLogMessage("Dumping real API to '%s'...", API_DUMP_FILE
);
164 m_gccInterface
.Dump(API_DUMP_FILE
);
166 wxLogMessage("Dumping interface API to '%s'...", INTERFACE_DUMP_FILE
);
167 m_doxyInterface
.Dump(INTERFACE_DUMP_FILE
);
171 if (parser
.Found(MODIFY_SWITCH
))
174 if (parser
.Found(PROCESS_ONLY_OPTION
, &m_strToMatch
))
176 size_t len
= m_strToMatch
.Len();
177 if (m_strToMatch
.StartsWith("\"") &&
178 m_strToMatch
.EndsWith("\"") &&
180 m_strToMatch
= m_strToMatch
.Mid(1, len
-2);
187 PrintStatistics(wxGetLocalTime() - startTime
);
191 wxPrintf("\nThis utility checks that the interface XML files created by Doxygen are in\n");
192 wxPrintf("synch with the real headers (whose contents are extracted by the gcc XML file).\n\n");
193 wxPrintf("The 'gccXML' parameter should be the wxapi.xml file created by the 'rungccxml.sh'\n");
194 wxPrintf("script which resides in 'utils/ifacecheck'.\n");
195 wxPrintf("The 'doxygenXML' parameter should be the index.xml file created by Doxygen\n");
196 wxPrintf("for the wxWidgets 'interface' folder.\n\n");
197 wxPrintf("Since the gcc XML file does not contain info about #defines, if you use\n");
198 wxPrintf("the -%s option, you'll get a smaller number of false warnings.\n",
199 USE_PREPROCESSOR_OPTION
);
201 // HELP_SWITCH was passed or a syntax error occurred
206 bool IfaceCheckApp::Compare()
208 const wxClassArray
& interfaces
= m_doxyInterface
.GetClasses();
210 int mcount
= 0, ccount
= 0;
212 wxLogMessage("Comparing the interface API to the real API (%d classes to compare)...",
213 interfaces
.GetCount());
215 if (!m_strToMatch
.IsEmpty())
217 wxLogMessage("Processing only header files matching '%s' expression.", m_strToMatch
);
220 for (unsigned int i
=0; i
<interfaces
.GetCount(); i
++)
222 // only compare the methods which are available for the port
223 // for which the gcc XML was produced
224 if (interfaces
[i
].GetAvailability() != wxPORT_UNKNOWN
&&
225 (interfaces
[i
].GetAvailability() & m_gccInterface
.GetInterfacePort()) == 0) {
229 wxLogMessage("skipping class '%s' since it's not available for the %s port.",
230 interfaces
[i
].GetName(), m_gccInterface
.GetInterfacePortName());
233 continue; // skip this method
236 // shorten the name of the header so the log file is more readable
237 // and also for calling IsToProcess() against it
238 wxString header
= wxFileName(interfaces
[i
].GetHeader()).GetFullName();
240 if (!IsToProcess(header
))
241 continue; // skip this one
243 wxString cname
= interfaces
[i
].GetName();
245 // search in the real headers for i-th interface class; we search for
246 // both class cname and cnameBase since in wxWidgets world tipically
247 // class cname is platform-specific while the real public interface of
248 // that class is part of the cnameBase class.
249 /*c = m_gccInterface.FindClass(cname + "Base");
252 c
= m_gccInterface
.FindClass(cname
);
255 // sometimes the platform-specific class is named "wxGeneric" + cname
257 c
= m_gccInterface
.FindClass("wxGeneric" + cname
.Mid(2));
260 c
= m_gccInterface
.FindClass("wxGtk" + cname
.Mid(2));
266 // there is a class with the same (logic) name!
267 mcount
+= CompareClasses(&interfaces
[i
], c
);
271 wxLogMessage("%s: couldn't find the real interface for the '%s' class",
277 wxLogMessage("%d on a total of %d methods (%.1f%%) of the interface headers do not exist in the real headers",
278 mcount
, m_doxyInterface
.GetMethodCount(), (float)(100.0 * mcount
/m_doxyInterface
.GetMethodCount()));
279 wxLogMessage("%d on a total of %d classes (%.1f%%) of the interface headers do not exist in the real headers",
280 ccount
, m_doxyInterface
.GetClassesCount(), (float)(100.0 * ccount
/m_doxyInterface
.GetClassesCount()));
285 int IfaceCheckApp::CompareClasses(const wxClass
* iface
, const wxClass
* api
)
287 const wxMethod
*real
;
290 wxASSERT(iface
&& api
);
292 // shorten the name of the header so the log file is more readable
293 wxString header
= wxFileName(iface
->GetHeader()).GetFullName();
295 for (unsigned int i
=0; i
<iface
->GetMethodCount(); i
++)
297 const wxMethod
& m
= iface
->GetMethod(i
);
299 // only compare the methods which are available for the port
300 // for which the gcc XML was produced
301 if (m
.GetAvailability() != wxPORT_UNKNOWN
&&
302 (m
.GetAvailability() & m_gccInterface
.GetInterfacePort()) == 0) {
306 wxLogMessage("skipping method '%s' since it's not available for the %s port.",
307 m
.GetAsString(), m_gccInterface
.GetInterfacePortName());
310 continue; // skip this method
313 // search in the methods of the api classes provided
314 real
= api
->RecursiveUpwardFindMethod(m
, &m_gccInterface
);
316 // avoid some false positives:
317 if (!real
&& m
.ActsAsDefaultCtor())
319 // build an artificial default ctor for this class:
321 temp
.GetArgumentTypes().Clear();
324 real
= api
->RecursiveUpwardFindMethod(temp
, &m_gccInterface
);
331 wxMethodPtrArray overloads
=
332 api
->RecursiveUpwardFindMethodsNamed(m
.GetName(), &m_gccInterface
);
334 // avoid false positives:
335 for (unsigned int k
=0; k
<overloads
.GetCount(); k
++)
336 if (overloads
[k
]->MatchesExceptForAttributes(m
) &&
337 m
.IsDeprecated() && !overloads
[k
]->IsDeprecated())
339 // maybe the iface method is marked as deprecated but the
340 // real method is not?
341 wxMethod
tmp(*overloads
[k
]);
342 tmp
.SetDeprecated(true);
346 // in this case, we can disregard this warning... the real
347 // method probably is included in WXWIN_COMPAT sections!
348 proceed
= false; // skip this method
352 #define HACK_TO_AUTO_CORRECT_ONLY_METHOD_ATTRIBUTES 0
353 #if HACK_TO_AUTO_CORRECT_ONLY_METHOD_ATTRIBUTES
354 for (unsigned int k
=0; k
<overloads
.GetCount(); k
++)
355 if (overloads
[k
]->MatchesExceptForAttributes(m
))
357 // fix default values of results[k]:
358 wxMethod
tmp(*overloads
[k
]);
359 tmp
.SetArgumentTypes(m
.GetArgumentTypes());
361 // modify interface header
362 if (FixMethod(iface
->GetHeader(), &m
, &tmp
))
364 wxLogMessage("Adjusted attributes of '%s' method", m
.GetAsString());
370 #endif // HACK_TO_AUTO_CORRECT_ONLY_METHOD_ATTRIBUTES
374 if (overloads
.GetCount()==0)
376 wxLogMessage("%s: real '%s' class and their parents have no method '%s'",
377 header
, api
->GetName(), m
.GetAsString());
378 // we've found no overloads
382 // first, output a warning
383 wxString warning
= header
;
384 if (overloads
.GetCount()>1)
385 warning
+= wxString::Format(": in the real headers there are %d overloads of '%s' for "
386 "'%s' all with different signatures:\n",
387 overloads
.GetCount(), m
.GetName(), api
->GetName());
389 warning
+= wxString::Format(": in the real headers there is a method '%s' for '%s'"
390 " but has different signature:\n",
391 m
.GetName(), api
->GetName());
394 // get a list of the prototypes with _all_ possible attributes:
395 warning
+= "\tdoxy header: " + m
.GetAsString(true, true, true, true);
396 for (unsigned int j
=0; j
<overloads
.GetCount(); j
++)
397 warning
+= "\n\treal header: " + overloads
[j
]->GetAsString(true, true, true, true);
399 wxLogWarning("%s", warning
);
402 if (overloads
.GetCount()>1)
404 // TODO: decide which of these overloads is the most "similar" to m
405 // and eventually modify it
408 wxLogWarning("\tmanual fix is required");
413 wxASSERT(overloads
.GetCount() == 1);
415 if (m_modify
|| m
.IsCtor())
417 wxLogWarning("\tfixing it...");
420 FixMethod(iface
->GetHeader(), &m
, overloads
[0]);
433 bool IfaceCheckApp::StringContainsMethodName(const wxString
& str
, const wxMethod
* m
)
435 return str
.Contains(m
->GetName()) ||
436 (m
->IsOperator() && str
.Contains("operator"));
439 bool IfaceCheckApp::FixMethod(const wxString
& header
, const wxMethod
* iface
, const wxMethod
* api
)
442 wxASSERT(iface
&& api
);
445 if (!file
.Open(header
)) {
446 wxLogError("\tcan't open the '%s' header file.", header
);
450 // GetLocation() returns the line where the last part of the prototype is placed;
451 // i.e. the line containing the semicolon at the end of the declaration.
452 int end
= iface
->GetLocation()-1;
453 if (end
<= 0 || end
>= (int)file
.GetLineCount()) {
454 wxLogWarning("\tinvalid location info for method '%s': %d.",
455 iface
->GetAsString(), iface
->GetLocation());
459 if (!file
.GetLine(end
).Contains(";")) {
460 wxLogWarning("\tinvalid location info for method '%s': %d.",
461 iface
->GetAsString(), iface
->GetLocation());
465 // is this a one-line prototype declaration?
466 bool founddecl
= false;
468 if (StringContainsMethodName(file
.GetLine(end
), iface
))
470 // yes, this prototype is all on this line:
476 start
= end
; // will be decremented inside the while{} loop below
478 // find the start point of this prototype declaration; i.e. the line
479 // containing the function name, which is also the line following
480 // the marker '*/' for the closure of the doxygen comment
483 start
--; // go up one line
485 if (StringContainsMethodName(file
.GetLine(start
), iface
))
488 while (start
> 0 && !founddecl
&&
489 !file
.GetLine(start
).Contains(";") &&
490 !file
.GetLine(start
).Contains("*/"));
493 if (start
<= 0 || !founddecl
)
495 wxLogError("\tcan't find the beginning of the declaration of '%s' method in '%s' header looking backwards from line %d; I arrived at %d and gave up",
496 iface
->GetAsString(), header
, end
+1 /* zero-based => 1-based */, start
);
500 // remove the old prototype
501 for (int k
=start
; k
<=end
; k
++)
502 file
.RemoveLine(start
); // remove (end-start)-nth times the start-th line
504 #define INDENTATION_STR wxString(" ")
506 // if possible, add also the @deprecated tag in the doxygen comment if it's missing
507 int deprecationOffset
= 0;
508 if (file
.GetLine(start
-1).Contains("*/") &&
509 (api
->IsDeprecated() && !iface
->IsDeprecated()))
511 file
.RemoveLine(start
-1);
512 file
.InsertLine(INDENTATION_STR
+ INDENTATION_STR
+
513 "@deprecated @todo provide deprecation description", start
-1);
514 file
.InsertLine(INDENTATION_STR
+ "*/", start
++);
516 // we have added a new line in the final balance
522 // discard gcc XML argument names and replace them with those parsed from doxygen XML;
523 // in this way we should avoid introducing doxygen warnings about cases where the argument
524 // 'xx' of the prototype is called 'yy' in the function's docs.
525 const wxArgumentTypeArray
& doxygenargs
= iface
->GetArgumentTypes();
526 const wxArgumentTypeArray
& realargs
= api
->GetArgumentTypes();
527 if (realargs
.GetCount() == doxygenargs
.GetCount())
529 for (j
=0; j
<doxygenargs
.GetCount(); j
++)
530 if (doxygenargs
[j
]==realargs
[j
])
532 realargs
[j
].SetArgumentName(doxygenargs
[j
].GetArgumentName());
534 if (realargs
[j
].GetDefaultValue().IsNumber() &&
535 doxygenargs
[j
].GetDefaultValue().StartsWith("wx"))
536 realargs
[j
].SetDefaultValue(doxygenargs
[j
].GetDefaultValue());
539 tmp
.SetArgumentTypes(realargs
);
542 #define WRAP_COLUMN 80
544 wxArrayString toinsert
;
545 toinsert
.Add(INDENTATION_STR
+ tmp
.GetAsString() + ";");
547 int nStartColumn
= toinsert
[0].Find('(');
548 wxASSERT(nStartColumn
!= wxNOT_FOUND
);
550 // wrap lines too long at comma boundaries
551 for (i
=0; i
<toinsert
.GetCount(); i
++)
553 size_t len
= toinsert
[i
].Len();
554 if (len
> WRAP_COLUMN
)
556 wxASSERT(i
== toinsert
.GetCount()-1);
559 wxString tmpleft
= toinsert
[i
].Left(WRAP_COLUMN
);
560 int comma
= tmpleft
.Find(',', true /* from end */);
561 if (comma
== wxNOT_FOUND
)
562 break; // break out of the for cycle...
564 toinsert
.Add(wxString(' ', nStartColumn
+1) +
565 toinsert
[i
].Right(len
-comma
-2)); // exclude the comma and the space after it
566 toinsert
[i
] = tmpleft
.Left(comma
+1); // include the comma
570 // insert the new lines
571 for (i
=0; i
<toinsert
.GetCount(); i
++)
572 file
.InsertLine(toinsert
[i
], start
+i
);
574 // now save the modification
576 wxLogError("\tcan't save the '%s' header file.", header
);
580 // how many lines did we add/remove in total?
581 int nOffset
= toinsert
.GetCount() + deprecationOffset
- (end
-start
+1);
587 wxLogMessage("\tthe final row offset for following methods is %d lines.", nOffset
);
590 // update the other method's locations for those methods which belong to the modified header
591 // and are placed _below_ the modified method
592 wxClassPtrArray cToUpdate
= m_doxyInterface
.FindClassesDefinedIn(header
);
593 for (i
=0; i
< cToUpdate
.GetCount(); i
++)
595 for (j
=0; j
< cToUpdate
[i
]->GetMethodCount(); j
++)
597 wxMethod
& m
= cToUpdate
[i
]->GetMethod(j
);
598 if (m
.GetLocation() > iface
->GetLocation())
600 // update the location of this method
601 m
.SetLocation(m
.GetLocation()+nOffset
);
609 bool IfaceCheckApp::ParsePreprocessorOutput(const wxString
& filename
)
612 if (!tf
.Open(filename
)) {
613 wxLogError("can't open the '%s' preprocessor output file.", filename
);
618 for (unsigned int i
=0; i
< tf
.GetLineCount(); i
++)
620 const wxString
& line
= tf
.GetLine(i
);
621 wxString defnameval
= line
.Mid(8); // what follows the "#define " string
623 // the format of this line should be:
624 // #define DEFNAME DEFVALUE
625 if (!line
.StartsWith("#define ")) {
626 wxLogError("unexpected content in '%s' at line %d.", filename
, i
+1);
630 if (defnameval
.Contains(" "))
633 wxString defname
= defnameval
.BeforeFirst(' ');
634 if (defname
.Contains("("))
635 continue; // this is a macro, skip it!
638 wxString defval
= defnameval
.AfterFirst(' ').Strip(wxString::both
);
639 if (defval
.StartsWith("(") && defval
.EndsWith(")"))
640 defval
= defval
.Mid(1, defval
.Len()-2);
642 // store this pair in the doxygen interface, where it can be useful
643 m_doxyInterface
.AddPreprocessorValue(defname
, defval
);
648 // it looks like the format of this line is:
650 // we are not interested to symbols #defined to nothing,
651 // so we just ignore this line.
655 wxLogMessage("Parsed %d preprocessor #defines from '%s' which will be used later...",
661 void IfaceCheckApp::PrintStatistics(long secs
)
663 // these stats, for what regards the gcc XML, are all referred to the wxWidgets
666 wxLogMessage("wx real headers contains declaration of %d classes (%d methods)",
667 m_gccInterface
.GetClassesCount(), m_gccInterface
.GetMethodCount());
668 wxLogMessage("wx interface headers contains declaration of %d classes (%d methods)",
669 m_doxyInterface
.GetClassesCount(), m_doxyInterface
.GetMethodCount());
671 // build a list of the undocumented wx classes
674 const wxClassArray
& arr
= m_gccInterface
.GetClasses();
675 for (unsigned int i
=0; i
<arr
.GetCount(); i
++) {
676 if (m_doxyInterface
.FindClass(arr
[i
].GetName()) == NULL
) {
677 list
+= arr
[i
].GetName() + ", ";
685 wxLogMessage("the list of the %d undocumented wx classes is: %s", undoc
, list
);
686 wxLogMessage("total processing took %d seconds.", secs
);