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_SWITCH "p"
42 #define MODIFY_SWITCH "m"
43 #define DUMP_SWITCH "d"
44 #define HELP_SWITCH "h"
45 #define VERBOSE_SWITCH "v"
47 static const wxCmdLineEntryDesc g_cmdLineDesc
[] =
49 { wxCMD_LINE_OPTION
, PROCESS_ONLY_SWITCH
, "process-only",
50 "processes only header files matching the given wildcard",
51 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_NEEDS_SEPARATOR
},
52 { wxCMD_LINE_SWITCH
, MODIFY_SWITCH
, "modify",
53 "modify the interface headers to match the real ones" },
54 { wxCMD_LINE_SWITCH
, DUMP_SWITCH
, "dump",
55 "dump both interface and API to plain text dump.*.txt files" },
56 { wxCMD_LINE_SWITCH
, HELP_SWITCH
, "help",
57 "show help message", wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
58 { wxCMD_LINE_SWITCH
, VERBOSE_SWITCH
, "verbose",
60 { wxCMD_LINE_PARAM
, NULL
, NULL
,
61 "gccXML", wxCMD_LINE_VAL_STRING
, wxCMD_LINE_OPTION_MANDATORY
},
62 { wxCMD_LINE_PARAM
, NULL
, NULL
,
63 "doxygenXML", wxCMD_LINE_VAL_STRING
, wxCMD_LINE_OPTION_MANDATORY
},
67 class IfaceCheckApp
: public wxAppConsole
70 // don't use builtin cmd line parsing:
71 virtual bool OnInit() { m_modify
=false; return true; }
75 int CompareClasses(const wxClass
* iface
, const wxClassPtrArray
& api
);
76 void FixMethod(const wxString
& header
, const wxMethod
* iface
, const wxMethod
* api
);
79 void PrintStatistics(long secs
);
81 bool IsToProcess(const wxString
& headername
) const
83 if (m_strToMatch
.IsEmpty())
85 return wxMatchWild(m_strToMatch
, headername
, false);
89 wxXmlGccInterface m_api
; // "real" headers API
90 wxXmlDoxygenInterface m_interface
; // doxygen-commented headers API
92 // was the MODIFY_SWITCH passed?
95 // if non-empty, then PROCESS_ONLY_SWITCH was passed and this is the
96 // wildcard expression to match
97 wxString m_strToMatch
;
100 IMPLEMENT_APP_CONSOLE(IfaceCheckApp
)
102 int IfaceCheckApp::OnRun()
104 long startTime
= wxGetLocalTime(); // for timing purpose
106 // parse the command line...
107 wxCmdLineParser
parser(g_cmdLineDesc
, argc
, argv
);
109 switch (parser
.Parse())
112 // HELP_SWITCH was passed
116 if (parser
.Found(VERBOSE_SWITCH
))
119 if (!m_api
.Parse(parser
.GetParam(0)) ||
120 !m_interface
.Parse(parser
.GetParam(1)))
123 if (parser
.Found(DUMP_SWITCH
))
125 LogMessage("Dumping real API to '%s'...", API_DUMP_FILE
);
126 m_api
.Dump(API_DUMP_FILE
);
128 LogMessage("Dumping interface API to '%s'...", INTERFACE_DUMP_FILE
);
129 m_interface
.Dump(INTERFACE_DUMP_FILE
);
133 if (parser
.Found(MODIFY_SWITCH
))
136 if (parser
.Found(PROCESS_ONLY_SWITCH
, &m_strToMatch
))
138 size_t len
= m_strToMatch
.Len();
139 if (m_strToMatch
.StartsWith("\"") &&
140 m_strToMatch
.EndsWith("\"") &&
142 m_strToMatch
= m_strToMatch
.Mid(1, len
-2);
148 PrintStatistics(wxGetLocalTime() - startTime
);
155 void IfaceCheckApp::ShowProgress()
161 bool IfaceCheckApp::Compare()
163 const wxClassArray
& interface
= m_interface
.GetClasses();
166 int mcount
= 0, ccount
= 0;
168 LogMessage("Comparing the interface API to the real API (%d classes to compare)...",
169 interface
.GetCount());
171 if (!m_strToMatch
.IsEmpty())
172 LogMessage("Processing only header files matching '%s' expression.", m_strToMatch
);
174 for (unsigned int i
=0; i
<interface
.GetCount(); i
++)
176 // shorten the name of the header so the log file is more readable
177 // and also for calling IsToProcess() against it
178 wxString header
= wxFileName(interface
[i
].GetHeader()).GetFullName();
180 if (!IsToProcess(header
))
181 continue; // skip this one
183 wxString cname
= interface
[i
].GetName();
187 // search in the real headers for i-th interface class
188 // for both class cname and cnameBase as in wxWidgets world, most often
189 // class cname is platform-specific while the real public interface of
190 // that class is part of the cnameBase class.
191 c
= m_api
.FindClass(cname
);
193 c
= m_api
.FindClass(cname
+ "Base");
196 if (api
.GetCount()>0) {
198 // there is a class with exactly the same name!
199 mcount
+= CompareClasses(&interface
[i
], api
);
203 LogMessage("%s: couldn't find the real interface for the '%s' class",
209 LogMessage("%d methods (%.1f%%) of the interface headers do not exist in the real headers",
210 mcount
, (float)(100.0 * mcount
/m_interface
.GetMethodCount()));
211 LogMessage("%d classes (%.1f%%) of the interface headers do not exist in the real headers",
212 ccount
, (float)(100.0 * ccount
/m_interface
.GetClassesCount()));
217 int IfaceCheckApp::CompareClasses(const wxClass
* iface
, const wxClassPtrArray
& api
)
219 wxString searchedclasses
;
220 const wxMethod
*real
;
223 wxASSERT(iface
&& api
.GetCount()>0);
225 // build a string with the names of the API classes compared to iface
226 for (unsigned int j
=0; j
<api
.GetCount(); j
++)
227 searchedclasses
+= "/" + api
[j
]->GetName();
228 searchedclasses
.Remove(0, 1);
230 // shorten the name of the header so the log file is more readable
231 wxString header
= wxFileName(iface
->GetHeader()).GetFullName();
233 for (unsigned int i
=0; i
<iface
->GetMethodCount(); i
++)
235 const wxMethod
& m
= iface
->GetMethod(i
);
238 // search in the methods of the api classes provided
239 for (unsigned int j
=0; j
<api
.GetCount(); j
++)
241 real
= api
[j
]->FindMethod(m
);
243 matches
++; // there is a real matching prototype! It's ok!
248 wxMethodPtrArray overloads
;
250 // try searching for a method with the same name but with
251 // different return type / arguments / qualifiers
252 for (unsigned int j
=0; j
<api
.GetCount(); j
++)
254 wxMethodPtrArray results
= api
[j
]->FindMethodNamed(m
.GetName());
256 // append "results" array to "overloads"
257 WX_APPEND_ARRAY(overloads
, results
);
260 if (overloads
.GetCount()==0)
263 TODO: sometimes the interface headers re-document a method
264 inherited from a base class even if the real header does
265 not actually re-implement it.
266 To avoid false positives, we'd need to search in the base classes
267 of api[] classes and search for a matching method.
269 LogMessage("%s: real '%s' class has no method '%s'",
270 header
, searchedclasses
, m
.GetAsString());
271 // we've found no overloads
275 // first, output a warning
276 wxString warning
= header
;
277 if (overloads
.GetCount()>1)
278 warning
+= wxString::Format(": in the real headers there are %d overloads of '%s' for "
279 "'%s' all with different signatures:\n",
280 overloads
.GetCount(), m
.GetName(), searchedclasses
);
282 warning
+= wxString::Format(": in the real headers there is a method '%s' for '%s'"
283 " but has different signature:\n",
284 m
.GetName(), searchedclasses
);
286 warning
+= "\tdoxy header: " + m
.GetAsString();
287 for (unsigned int j
=0; j
<overloads
.GetCount(); j
++)
288 warning
+= "\n\treal header: " + overloads
[j
]->GetAsString();
290 wxPrint(warning
+ "\n");
293 if (overloads
.GetCount()>1)
295 // TODO: decide which of these overloads is the most "similar" to m
296 // and eventually modify it
298 wxPrint("\tmanual fix is required\n");
302 wxASSERT(overloads
.GetCount() == 1);
306 wxPrint("\tfixing it...\n");
309 FixMethod(iface
->GetHeader(), &m
, overloads
[0]);
321 void IfaceCheckApp::FixMethod(const wxString
& header
, const wxMethod
* iface
, const wxMethod
* api
)
323 wxASSERT(iface
&& api
);
326 if (!file
.Open(header
)) {
327 LogError("\tcan't open the '%s' header file.", header
);
331 // GetLocation() returns the line where the last part of the prototype is placed:
332 int end
= iface
->GetLocation()-1;
333 if (end
<= 0 || end
>= (int)file
.GetLineCount()) {
334 LogWarning("\tinvalid location info for method '%s': %d.",
335 iface
->GetAsString(), iface
->GetLocation());
339 if (!file
.GetLine(end
).Contains(";")) {
340 LogWarning("\tinvalid location info for method '%s': %d.",
341 iface
->GetAsString(), iface
->GetLocation());
345 // find the start point of this prototype declaration:
347 bool founddecl
= false;
349 !file
.GetLine(start
).Contains(";") &&
350 !file
.GetLine(start
).Contains("*/"))
354 founddecl
|= file
.GetLine(start
).Contains(iface
->GetName());
357 if (start
<= 0 || !founddecl
)
359 LogError("\tcan't find the beginning of the declaration of '%s' method in '%s' header",
360 iface
->GetAsString(), header
);
364 // start-th line contains either the declaration of another prototype
365 // or the closing tag */ of a doxygen comment; start one line below
368 // remove the old prototype
369 for (int i
=start
; i
<=end
; i
++)
370 file
.RemoveLine(start
); // remove (end-start)-nth times the start-th line
372 #define INDENTATION_STR wxString(" ")
374 // if possible, add also the @deprecated tag in the doxygen comment if it's missing
375 int deprecationOffset
= 0;
376 if (file
.GetLine(start
-1).Contains("*/") &&
377 (api
->IsDeprecated() && !iface
->IsDeprecated()))
379 file
.RemoveLine(start
-1);
380 file
.InsertLine(INDENTATION_STR
+ INDENTATION_STR
+
381 "@deprecated @todo provide deprecation description", start
-1);
382 file
.InsertLine(INDENTATION_STR
+ "*/", start
++);
384 // we have added a new line in the final balance
390 // discard API argument names and replace them with those parsed from doxygen XML:
391 const wxArgumentTypeArray
& doxygenargs
= iface
->GetArgumentTypes();
392 const wxArgumentTypeArray
& realargs
= api
->GetArgumentTypes();
393 if (realargs
.GetCount() == doxygenargs
.GetCount())
395 for (unsigned int j
=0; j
<doxygenargs
.GetCount(); j
++)
396 if (doxygenargs
[j
]==realargs
[j
])
397 realargs
[j
].SetArgumentName(doxygenargs
[j
].GetArgumentName());
399 tmp
.SetArgumentTypes(realargs
);
402 #define WRAP_COLUMN 80
404 wxArrayString toinsert
;
405 toinsert
.Add(INDENTATION_STR
+ tmp
.GetAsString() + ";");
407 int nStartColumn
= toinsert
[0].Find('(');
408 wxASSERT(nStartColumn
!= wxNOT_FOUND
);
410 // wrap lines too long at comma boundaries
411 for (unsigned int i
=0; i
<toinsert
.GetCount(); i
++)
413 size_t len
= toinsert
[i
].Len();
414 if (len
> WRAP_COLUMN
)
416 wxASSERT(i
== toinsert
.GetCount()-1);
419 wxString tmpleft
= toinsert
[i
].Left(WRAP_COLUMN
);
420 int comma
= tmpleft
.Find(',', true /* from end */);
421 if (comma
== wxNOT_FOUND
)
422 break; // break out of the for cycle...
424 toinsert
.Add(wxString(' ', nStartColumn
+1) +
425 toinsert
[i
].Right(len
-comma
-2)); // exclude the comma and the space after it
426 toinsert
[i
] = tmpleft
.Left(comma
+1); // include the comma
430 // insert the new lines
431 for (unsigned int i
=0; i
<toinsert
.GetCount(); i
++)
432 file
.InsertLine(toinsert
[i
], start
+i
);
434 // now save the modification
436 LogError("\tcan't save the '%s' header file.", header
);
440 // how many lines did we add/remove in total?
441 int nOffset
= toinsert
.GetCount() + deprecationOffset
- (end
-start
+1);
446 LogMessage("\tthe final row offset for following methods is %d lines.", nOffset
);
448 // update the other method's locations for those methods which belong to the modified header
449 // and are placed _below_ the modified method
450 wxClassPtrArray cToUpdate
= m_interface
.FindClassesDefinedIn(header
);
451 for (unsigned int i
=0; i
< cToUpdate
.GetCount(); i
++)
453 for (unsigned int j
=0; j
< cToUpdate
[i
]->GetMethodCount(); j
++)
455 wxMethod
& m
= cToUpdate
[i
]->GetMethod(j
);
456 if (m
.GetLocation() > iface
->GetLocation())
458 // update the location of this method
459 m
.SetLocation(m
.GetLocation()+nOffset
);
465 void IfaceCheckApp::PrintStatistics(long secs
)
467 LogMessage("wx real headers contains declaration of %d classes (%d methods)",
468 m_api
.GetClassesCount(), m_api
.GetMethodCount());
469 LogMessage("wx interface headers contains declaration of %d classes (%d methods)",
470 m_interface
.GetClassesCount(), m_interface
.GetMethodCount());
471 LogMessage("total processing took %d seconds.", secs
);