]> git.saurik.com Git - wxWidgets.git/blob - utils/ifacecheck/src/ifacecheck.cpp
prototype fixes (in particular fix missing default value initializers)
[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_SWITCH "p"
42 #define MODIFY_SWITCH "m"
43 #define DUMP_SWITCH "d"
44 #define HELP_SWITCH "h"
45 #define VERBOSE_SWITCH "v"
46
47 static const wxCmdLineEntryDesc g_cmdLineDesc[] =
48 {
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",
59 "be 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 },
64 wxCMD_LINE_DESC_END
65 };
66
67 class IfaceCheckApp : public wxAppConsole
68 {
69 public:
70 // don't use builtin cmd line parsing:
71 virtual bool OnInit() { m_modify=false; return true; }
72 virtual int OnRun();
73
74 bool Compare();
75 int CompareClasses(const wxClass* iface, const wxClassPtrArray& api);
76 void FixMethod(const wxString& header, const wxMethod* iface, const wxMethod* api);
77
78 void ShowProgress();
79 void PrintStatistics(long secs);
80
81 bool IsToProcess(const wxString& headername) const
82 {
83 if (m_strToMatch.IsEmpty())
84 return true;
85 return wxMatchWild(m_strToMatch, headername, false);
86 }
87
88 protected:
89 wxXmlGccInterface m_api; // "real" headers API
90 wxXmlDoxygenInterface m_interface; // doxygen-commented headers API
91
92 // was the MODIFY_SWITCH passed?
93 bool m_modify;
94
95 // if non-empty, then PROCESS_ONLY_SWITCH was passed and this is the
96 // wildcard expression to match
97 wxString m_strToMatch;
98 };
99
100 IMPLEMENT_APP_CONSOLE(IfaceCheckApp)
101
102 int IfaceCheckApp::OnRun()
103 {
104 long startTime = wxGetLocalTime(); // for timing purpose
105
106 // parse the command line...
107 wxCmdLineParser parser(g_cmdLineDesc, argc, argv);
108 bool ok = true;
109 switch (parser.Parse())
110 {
111 case -1:
112 // HELP_SWITCH was passed
113 return 0;
114
115 case 0:
116 if (parser.Found(VERBOSE_SWITCH))
117 g_verbose = true;
118
119 if (!m_api.Parse(parser.GetParam(0)) ||
120 !m_interface.Parse(parser.GetParam(1)))
121 return 1;
122
123 if (parser.Found(DUMP_SWITCH))
124 {
125 LogMessage("Dumping real API to '%s'...", API_DUMP_FILE);
126 m_api.Dump(API_DUMP_FILE);
127
128 LogMessage("Dumping interface API to '%s'...", INTERFACE_DUMP_FILE);
129 m_interface.Dump(INTERFACE_DUMP_FILE);
130 }
131 else
132 {
133 if (parser.Found(MODIFY_SWITCH))
134 m_modify = true;
135
136 if (parser.Found(PROCESS_ONLY_SWITCH, &m_strToMatch))
137 {
138 size_t len = m_strToMatch.Len();
139 if (m_strToMatch.StartsWith("\"") &&
140 m_strToMatch.EndsWith("\"") &&
141 len > 2)
142 m_strToMatch = m_strToMatch.Mid(1, len-2);
143 }
144
145 ok = Compare();
146 }
147
148 PrintStatistics(wxGetLocalTime() - startTime);
149 return ok ? 0 : 1;
150 }
151
152 return 1;
153 }
154
155 void IfaceCheckApp::ShowProgress()
156 {
157 wxPrint(".");
158 //fflush(stdout);
159 }
160
161 bool IfaceCheckApp::Compare()
162 {
163 const wxClassArray& interface = m_interface.GetClasses();
164 const wxClass* c;
165 wxClassPtrArray api;
166 int mcount = 0, ccount = 0;
167
168 LogMessage("Comparing the interface API to the real API (%d classes to compare)...",
169 interface.GetCount());
170
171 if (!m_strToMatch.IsEmpty())
172 LogMessage("Processing only header files matching '%s' expression.", m_strToMatch);
173
174 for (unsigned int i=0; i<interface.GetCount(); i++)
175 {
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();
179
180 if (!IsToProcess(header))
181 continue; // skip this one
182
183 wxString cname = interface[i].GetName();
184
185 api.Empty();
186
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);
192 if (c) api.Add(c);
193 c = m_api.FindClass(cname + "Base");
194 if (c) api.Add(c);
195
196 if (api.GetCount()>0) {
197
198 // there is a class with exactly the same name!
199 mcount += CompareClasses(&interface[i], api);
200
201 } else {
202
203 LogMessage("%s: couldn't find the real interface for the '%s' class",
204 header, cname);
205 ccount++;
206 }
207 }
208
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()));
213
214 return true;
215 }
216
217 int IfaceCheckApp::CompareClasses(const wxClass* iface, const wxClassPtrArray& api)
218 {
219 wxString searchedclasses;
220 const wxMethod *real;
221 int count = 0;
222
223 wxASSERT(iface && api.GetCount()>0);
224
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);
229
230 // shorten the name of the header so the log file is more readable
231 wxString header = wxFileName(iface->GetHeader()).GetFullName();
232
233 for (unsigned int i=0; i<iface->GetMethodCount(); i++)
234 {
235 const wxMethod& m = iface->GetMethod(i);
236 int matches = 0;
237
238 // search in the methods of the api classes provided
239 for (unsigned int j=0; j<api.GetCount(); j++)
240 {
241 real = api[j]->FindMethod(m);
242 if (real)
243 matches++; // there is a real matching prototype! It's ok!
244 }
245
246 if (matches == 0)
247 {
248 wxMethodPtrArray overloads;
249
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++)
253 {
254 wxMethodPtrArray results = api[j]->FindMethodNamed(m.GetName());
255
256 // append "results" array to "overloads"
257 WX_APPEND_ARRAY(overloads, results);
258 }
259
260 if (overloads.GetCount()==0)
261 {
262 LogMessage("%s: real '%s' class has no method '%s'",
263 header, searchedclasses, m.GetAsString());
264 // we've found no overloads
265 }
266 else
267 {
268 // first, output a warning
269 wxString warning = header;
270 if (overloads.GetCount()>1)
271 warning += wxString::Format(": in the real headers there are %d overloads of '%s' for "
272 "'%s' all with different signatures:\n",
273 overloads.GetCount(), m.GetName(), searchedclasses);
274 else
275 warning += wxString::Format(": in the real headers there is a method '%s' for '%s'"
276 " but has different signature:\n",
277 m.GetName(), searchedclasses);
278
279 warning += "\tdoxy header: " + m.GetAsString();
280 for (unsigned int j=0; j<overloads.GetCount(); j++)
281 warning += "\n\treal header: " + overloads[j]->GetAsString();
282
283 wxPrint(warning + "\n");
284 count++;
285
286 if (overloads.GetCount()>1)
287 {
288 // TODO: decide which of these overloads is the most "similar" to m
289 // and eventually modify it
290 if (m_modify)
291 wxPrint("\tmanual fix is required\n");
292 }
293 else
294 {
295 wxASSERT(overloads.GetCount() == 1);
296
297 if (m_modify)
298 {
299 wxPrint("\tfixing it...\n");
300
301 // try to modify it!
302 FixMethod(iface->GetHeader(), &m, overloads[0]);
303 }
304 }
305 }
306
307 count++;
308 }
309 }
310
311 return count;
312 }
313
314 void IfaceCheckApp::FixMethod(const wxString& header, const wxMethod* iface, const wxMethod* api)
315 {
316 wxASSERT(iface && api);
317
318 wxTextFile file;
319 if (!file.Open(header)) {
320 LogError("can't open the '%s' header file.", header);
321 return;
322 }
323
324 // GetLocation() returns the line where the last part of the prototype is placed:
325 int end = iface->GetLocation()-1;
326 if (end <= 0 || end >= (int)file.GetLineCount()) {
327 LogWarning("invalid location info for method '%s': %d.",
328 iface->GetAsString(), iface->GetLocation());
329 return;
330 }
331
332 if (!file.GetLine(end).Contains(iface->GetName())) {
333 LogWarning("invalid location info for method '%s': %d.",
334 iface->GetAsString(), iface->GetLocation());
335 return;
336 }
337
338 // find the start point of this prototype declaration:
339 int start = end-1;
340 while (start > 0 &&
341 !file.GetLine(start).Contains(";") &&
342 !file.GetLine(start).Contains("*/"))
343 start--;
344
345 if (start <= 0)
346 {
347 LogError("can't find the beginning of the declaration of '%s' method in '%s' header",
348 iface->GetAsString(), header);
349 return;
350 }
351
352 // start-th line contains either the declaration of another prototype
353 // or the closing tag */ of a doxygen comment; start one line below
354 start++;
355
356 // remove the old prototype
357 for (int i=start; i<=end; i++)
358 file.RemoveLine(start); // remove (end-start)-nth times the start-th line
359
360 #define INDENTATION_STR wxString(" ")
361
362 // if possible, add also the @deprecated tag in the doxygen comment
363 if (file.GetLine(start-1).Contains("*/") && api->IsDeprecated())
364 {
365 file.RemoveLine(start-1);
366 file.InsertLine(INDENTATION_STR + INDENTATION_STR +
367 "@deprecated @todo provide deprecation description", start-1);
368 file.InsertLine(INDENTATION_STR + "*/", start++);
369 }
370
371 wxMethod tmp(*api);
372
373 // discard API argument names and replace them with those parsed from doxygen XML:
374 const wxArgumentTypeArray& doxygenargs = iface->GetArgumentTypes();
375 const wxArgumentTypeArray& realargs = api->GetArgumentTypes();
376 if (realargs.GetCount() == doxygenargs.GetCount())
377 {
378 for (unsigned int j=0; j<doxygenargs.GetCount(); j++)
379 if (doxygenargs[j]==realargs[j])
380 realargs[j].SetArgumentName(doxygenargs[j].GetArgumentName());
381
382 tmp.SetArgumentTypes(realargs);
383 }
384
385 // insert the new one
386 file.InsertLine(INDENTATION_STR + tmp.GetAsString() + ";", start);
387
388 // now save the modification
389 if (!file.Write()) {
390 LogError("can't save the '%s' header file.", header);
391 return;
392 }
393 }
394
395 void IfaceCheckApp::PrintStatistics(long secs)
396 {
397 LogMessage("wx real headers contains declaration of %d classes (%d methods)",
398 m_api.GetClassesCount(), m_api.GetMethodCount());
399 LogMessage("wx interface headers contains declaration of %d classes (%d methods)",
400 m_interface.GetClassesCount(), m_interface.GetMethodCount());
401 LogMessage("total processing took %d seconds.", secs);
402 }
403