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)
262 LogMessage("%s: real '%s' class has no method '%s'",
263 header
, searchedclasses
, m
.GetAsString());
264 // we've found no overloads
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
);
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
);
279 warning
+= "\tdoxy header: " + m
.GetAsString();
280 for (unsigned int j
=0; j
<overloads
.GetCount(); j
++)
281 warning
+= "\n\treal header: " + overloads
[j
]->GetAsString();
283 wxPrint(warning
+ "\n");
286 if (overloads
.GetCount()>1)
288 // TODO: decide which of these overloads is the most "similar" to m
289 // and eventually modify it
291 wxPrint("\tmanual fix is required\n");
295 wxASSERT(overloads
.GetCount() == 1);
299 wxPrint("\tfixing it...\n");
302 FixMethod(iface
->GetHeader(), &m
, overloads
[0]);
314 void IfaceCheckApp::FixMethod(const wxString
& header
, const wxMethod
* iface
, const wxMethod
* api
)
316 wxASSERT(iface
&& api
);
319 if (!file
.Open(header
)) {
320 LogError("can't open the '%s' header file.", header
);
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());
332 if (!file
.GetLine(end
).Contains(iface
->GetName())) {
333 LogWarning("invalid location info for method '%s': %d.",
334 iface
->GetAsString(), iface
->GetLocation());
338 // find the start point of this prototype declaration:
341 !file
.GetLine(start
).Contains(";") &&
342 !file
.GetLine(start
).Contains("*/"))
347 LogError("can't find the beginning of the declaration of '%s' method in '%s' header",
348 iface
->GetAsString(), header
);
352 // start-th line contains either the declaration of another prototype
353 // or the closing tag */ of a doxygen comment; start one line below
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
360 #define INDENTATION_STR wxString(" ")
362 // if possible, add also the @deprecated tag in the doxygen comment
363 if (file
.GetLine(start
-1).Contains("*/") && api
->IsDeprecated())
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
++);
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())
378 for (unsigned int j
=0; j
<doxygenargs
.GetCount(); j
++)
379 if (doxygenargs
[j
]==realargs
[j
])
380 realargs
[j
].SetArgumentName(doxygenargs
[j
].GetArgumentName());
382 tmp
.SetArgumentTypes(realargs
);
385 // insert the new one
386 file
.InsertLine(INDENTATION_STR
+ tmp
.GetAsString() + ";", start
);
388 // now save the modification
390 LogError("can't save the '%s' header file.", header
);
395 void IfaceCheckApp::PrintStatistics(long secs
)
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
);