1 /////////////////////////////////////////////////////////////////////////////
2 // Name: ifacecheck.cpp
3 // Purpose: Interface headers <=> real headers coherence checker
4 // Author: Francesco Montorsi
7 // Copyright: (c) 2008 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
18 // 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 IfaceCheckApp
: public wxAppConsole
75 // don't use builtin cmd line parsing:
76 virtual bool OnInit() { m_modify
=false; return true; }
79 bool ParsePreprocessorOutput(const wxString
& filename
);
82 int CompareClasses(const wxClass
* iface
, const wxClass
* api
);
83 bool FixMethod(const wxString
& header
, const wxMethod
* iface
, const wxMethod
* api
);
86 void PrintStatistics(long secs
);
88 bool IsToProcess(const wxString
& headername
) const
90 if (m_strToMatch
.IsEmpty())
92 return wxMatchWild(m_strToMatch
, headername
, false);
96 wxXmlGccInterface m_gccInterface
; // "real" headers API
97 wxXmlDoxygenInterface m_doxyInterface
; // doxygen-commented headers API
99 // was the MODIFY_SWITCH passed?
102 // if non-empty, then PROCESS_ONLY_OPTION was passed and this is the
103 // wildcard expression to match
104 wxString m_strToMatch
;
107 IMPLEMENT_APP_CONSOLE(IfaceCheckApp
)
109 int IfaceCheckApp::OnRun()
111 long startTime
= wxGetLocalTime(); // for timing purpose
113 wxCmdLineParser
parser(g_cmdLineDesc
, argc
, argv
);
115 wxString::Format("wxWidgets Interface checker utility (built %s against %s)",
116 __DATE__
, wxVERSION_STRING
));
118 // parse the command line...
120 wxString preprocFile
;
121 switch (parser
.Parse())
124 if (parser
.Found(VERBOSE_SWITCH
))
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
))
131 if (!ParsePreprocessorOutput(preprocFile
))
135 // in any case set basic std preprocessor #defines:
136 m_doxyInterface
.AddPreprocessorValue("NULL", "0");
138 //g_bLogEnabled = false;
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)))
146 g_bLogEnabled
= true;
148 if (parser
.Found(DUMP_SWITCH
))
150 LogMessage("Dumping real API to '%s'...", API_DUMP_FILE
);
151 m_gccInterface
.Dump(API_DUMP_FILE
);
153 LogMessage("Dumping interface API to '%s'...", INTERFACE_DUMP_FILE
);
154 m_doxyInterface
.Dump(INTERFACE_DUMP_FILE
);
158 if (parser
.Found(MODIFY_SWITCH
))
161 if (parser
.Found(PROCESS_ONLY_OPTION
, &m_strToMatch
))
163 size_t len
= m_strToMatch
.Len();
164 if (m_strToMatch
.StartsWith("\"") &&
165 m_strToMatch
.EndsWith("\"") &&
167 m_strToMatch
= m_strToMatch
.Mid(1, len
-2);
174 PrintStatistics(wxGetLocalTime() - startTime
);
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
);
188 // HELP_SWITCH was passed or a syntax error occurred
193 void IfaceCheckApp::ShowProgress()
199 bool IfaceCheckApp::Compare()
201 const wxClassArray
& interfaces
= m_doxyInterface
.GetClasses();
203 int mcount
= 0, ccount
= 0;
205 LogMessage("Comparing the interface API to the real API (%d classes to compare)...",
206 interfaces
.GetCount());
208 if (!m_strToMatch
.IsEmpty())
209 LogMessage("Processing only header files matching '%s' expression.", m_strToMatch
);
211 for (unsigned int i
=0; i
<interfaces
.GetCount(); i
++)
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) {
219 LogMessage("skipping class '%s' since it's not available for the %s port.",
220 interfaces
[i
].GetName(), m_gccInterface
.GetInterfacePortName());
222 continue; // skip this method
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();
229 if (!IsToProcess(header
))
230 continue; // skip this one
232 wxString cname
= interfaces
[i
].GetName();
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");
241 c
= m_gccInterface
.FindClass(cname
);
244 // sometimes the platform-specific class is named "wxGeneric" + cname
246 c
= m_gccInterface
.FindClass("wxGeneric" + cname
.Mid(2));
249 c
= m_gccInterface
.FindClass("wxGtk" + cname
.Mid(2));
255 // there is a class with the same (logic) name!
256 mcount
+= CompareClasses(&interfaces
[i
], c
);
260 LogMessage("%s: couldn't find the real interface for the '%s' class",
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()));
274 int IfaceCheckApp::CompareClasses(const wxClass
* iface
, const wxClass
* api
)
276 const wxMethod
*real
;
279 wxASSERT(iface
&& api
);
281 // shorten the name of the header so the log file is more readable
282 wxString header
= wxFileName(iface
->GetHeader()).GetFullName();
284 for (unsigned int i
=0; i
<iface
->GetMethodCount(); i
++)
286 const wxMethod
& m
= iface
->GetMethod(i
);
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) {
294 LogMessage("skipping method '%s' since it's not available for the %s port.",
295 m
.GetAsString(), m_gccInterface
.GetInterfacePortName());
297 continue; // skip this method
300 // search in the methods of the api classes provided
301 real
= api
->RecursiveUpwardFindMethod(m
, &m_gccInterface
);
306 wxMethodPtrArray overloads
=
307 api
->RecursiveUpwardFindMethodsNamed(m
.GetName(), &m_gccInterface
);
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())
315 // fix default values of results[k]:
316 wxMethod
tmp(*overloads
[k
]);
317 tmp
.SetArgumentTypes(m
.GetArgumentTypes());
319 // modify interface header
320 if (FixMethod(iface
->GetHeader(), &m
, &tmp
))
321 LogMessage("Adjusted attributes of '%s' method", m
.GetAsString());
331 if (overloads
.GetCount()==0)
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
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());
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());
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);
355 wxPrint(warning
+ "\n");
358 if (overloads
.GetCount()>1)
360 // TODO: decide which of these overloads is the most "similar" to m
361 // and eventually modify it
363 wxPrint("\tmanual fix is required\n");
367 wxASSERT(overloads
.GetCount() == 1);
371 wxPrint("\tfixing it...\n");
374 FixMethod(iface
->GetHeader(), &m
, overloads
[0]);
381 #if HACK_TO_AUTO_CORRECT_ONLY_METHOD_ATTRIBUTES
390 bool IfaceCheckApp::FixMethod(const wxString
& header
, const wxMethod
* iface
, const wxMethod
* api
)
392 wxASSERT(iface
&& api
);
395 if (!file
.Open(header
)) {
396 LogError("\tcan't open the '%s' header file.", header
);
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());
408 if (!file
.GetLine(end
).Contains(";")) {
409 LogWarning("\tinvalid location info for method '%s': %d.",
410 iface
->GetAsString(), iface
->GetLocation());
414 // is this a one-line prototype declaration?
415 bool founddecl
= false;
417 if (file
.GetLine(end
).Contains(iface
->GetName()))
419 // yes, this prototype is all on this line:
427 // find the start point of this prototype declaration:
429 !file
.GetLine(start
).Contains(";") &&
430 !file
.GetLine(start
).Contains("*/"))
434 founddecl
|= file
.GetLine(start
).Contains(iface
->GetName());
437 // start-th line contains either the declaration of another prototype
438 // or the closing tag */ of a doxygen comment; start one line below
442 if (start
<= 0 || !founddecl
)
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
);
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
453 #define INDENTATION_STR wxString(" ")
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()))
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
++);
465 // we have added a new line in the final balance
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())
476 for (unsigned int j
=0; j
<doxygenargs
.GetCount(); j
++)
477 if (doxygenargs
[j
]==realargs
[j
])
478 realargs
[j
].SetArgumentName(doxygenargs
[j
].GetArgumentName());
480 tmp
.SetArgumentTypes(realargs
);
483 #define WRAP_COLUMN 80
485 wxArrayString toinsert
;
486 toinsert
.Add(INDENTATION_STR
+ tmp
.GetAsString() + ";");
488 int nStartColumn
= toinsert
[0].Find('(');
489 wxASSERT(nStartColumn
!= wxNOT_FOUND
);
491 // wrap lines too long at comma boundaries
492 for (unsigned int i
=0; i
<toinsert
.GetCount(); i
++)
494 size_t len
= toinsert
[i
].Len();
495 if (len
> WRAP_COLUMN
)
497 wxASSERT(i
== toinsert
.GetCount()-1);
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...
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
511 // insert the new lines
512 for (unsigned int i
=0; i
<toinsert
.GetCount(); i
++)
513 file
.InsertLine(toinsert
[i
], start
+i
);
515 // now save the modification
517 LogError("\tcan't save the '%s' header file.", header
);
521 // how many lines did we add/remove in total?
522 int nOffset
= toinsert
.GetCount() + deprecationOffset
- (end
-start
+1);
527 LogMessage("\tthe final row offset for following methods is %d lines.", nOffset
);
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
++)
534 for (unsigned int j
=0; j
< cToUpdate
[i
]->GetMethodCount(); j
++)
536 wxMethod
& m
= cToUpdate
[i
]->GetMethod(j
);
537 if (m
.GetLocation() > iface
->GetLocation())
539 // update the location of this method
540 m
.SetLocation(m
.GetLocation()+nOffset
);
548 bool IfaceCheckApp::ParsePreprocessorOutput(const wxString
& filename
)
551 if (!tf
.Open(filename
)) {
552 LogError("can't open the '%s' preprocessor output file.", filename
);
557 for (unsigned int i
=0; i
< tf
.GetLineCount(); i
++)
559 const wxString
& line
= tf
.GetLine(i
);
560 wxString defnameval
= line
.Mid(8); // what follows the "#define " string
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);
569 if (defnameval
.Contains(" "))
572 wxString defname
= defnameval
.BeforeFirst(' ');
573 if (defname
.Contains("("))
574 continue; // this is a macro, skip it!
577 wxString defval
= defnameval
.AfterFirst(' ').Strip(wxString::both
);
578 if (defval
.StartsWith("(") && defval
.EndsWith(")"))
579 defval
= defval
.Mid(1, defval
.Len()-2);
581 // store this pair in the doxygen interface, where it can be useful
582 m_doxyInterface
.AddPreprocessorValue(defname
, defval
);
587 // it looks like the format of this line is:
589 // we are not interested to symbols #defined to nothing,
590 // so we just ignore this line.
594 LogMessage("Parsed %d preprocessor #defines from '%s' which will be used later...",
600 void IfaceCheckApp::PrintStatistics(long secs
)
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
);