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/stopwatch.h" // for wxGetLocalTime
26 #include "xmlparser.h"
28 // global verbosity flag
29 bool g_verbose
= false;
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 #define API_DUMP_FILE "dump.api.txt"
38 #define INTERFACE_DUMP_FILE "dump.interface.txt"
40 #define MODIFY_SWITCH "m"
41 #define DUMP_SWITCH "dump"
42 #define HELP_SWITCH "h"
43 #define VERBOSE_SWITCH "v"
45 static const wxCmdLineEntryDesc g_cmdLineDesc
[] =
47 { wxCMD_LINE_SWITCH
, MODIFY_SWITCH
, "modify",
48 "modify the interface headers to match the real ones" },
49 { wxCMD_LINE_SWITCH
, "", DUMP_SWITCH
,
50 "dump both interface and API to plain text" },
51 { wxCMD_LINE_SWITCH
, HELP_SWITCH
, "help",
52 "show help message", wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
53 { wxCMD_LINE_SWITCH
, VERBOSE_SWITCH
, "verbose",
55 { wxCMD_LINE_PARAM
, NULL
, NULL
,
56 "gccXML", wxCMD_LINE_VAL_STRING
, wxCMD_LINE_OPTION_MANDATORY
},
57 { wxCMD_LINE_PARAM
, NULL
, NULL
,
58 "doxygenXML", wxCMD_LINE_VAL_STRING
, wxCMD_LINE_OPTION_MANDATORY
},
62 class IfaceCheckApp
: public wxAppConsole
65 // don't use builtin cmd line parsing:
66 virtual bool OnInit() { m_modify
=false; return true; }
70 int CompareClasses(const wxClass
* iface
, const wxClassPtrArray
& api
);
71 void FixMethod(const wxString
& header
, const wxMethod
* iface
, const wxMethod
* api
);
74 void PrintStatistics(long secs
);
77 wxXmlGccInterface m_api
; // "real" headers API
78 wxXmlDoxygenInterface m_interface
; // doxygen-commented headers API
80 // was the MODIFY_SWITCH passed?
84 IMPLEMENT_APP_CONSOLE(IfaceCheckApp
)
86 int IfaceCheckApp::OnRun()
88 long startTime
= wxGetLocalTime(); // for timing purpose
90 // parse the command line...
91 wxCmdLineParser
parser(g_cmdLineDesc
, argc
, argv
);
93 switch (parser
.Parse())
96 // HELP_SWITCH was passed
100 if (parser
.Found(VERBOSE_SWITCH
))
103 if (!m_api
.Parse(parser
.GetParam(0)) ||
104 !m_interface
.Parse(parser
.GetParam(1)))
107 if (parser
.Found(DUMP_SWITCH
))
109 LogMessage("Dumping real API to '%s'...", API_DUMP_FILE
);
110 m_api
.Dump(API_DUMP_FILE
);
112 LogMessage("Dumping interface API to '%s'...", INTERFACE_DUMP_FILE
);
113 m_interface
.Dump(INTERFACE_DUMP_FILE
);
117 if (parser
.Found(MODIFY_SWITCH
))
123 PrintStatistics(wxGetLocalTime() - startTime
);
130 void IfaceCheckApp::ShowProgress()
136 bool IfaceCheckApp::Compare()
138 const wxClassArray
& interface
= m_interface
.GetClasses();
141 int mcount
= 0, ccount
= 0;
143 LogMessage("Comparing the interface API to the real API (%d classes to compare)...",
144 interface
.GetCount());
146 for (unsigned int i
=0; i
<interface
.GetCount(); i
++)
148 wxString cname
= interface
[i
].GetName();
152 // search in the real headers for i-th interface class
153 // for both class cname and cnameBase as in wxWidgets world, most often
154 // class cname is platform-specific while the real public interface of
155 // that class is part of the cnameBase class.
156 c
= m_api
.FindClass(cname
);
158 c
= m_api
.FindClass(cname
+ "Base");
161 if (api
.GetCount()>0) {
163 // there is a class with exactly the same name!
164 mcount
+= CompareClasses(&interface
[i
], api
);
168 // shorten the name of the header so the log file is more readable
169 wxString header
= interface
[i
].GetHeader().AfterLast('/');
171 LogMessage("%s: couldn't find the real interface for the '%s' class",
177 LogMessage("%d methods (%.1f%%) of the interface headers do not exist in the real headers",
178 mcount
, (float)(100.0 * mcount
/m_interface
.GetMethodCount()));
179 LogMessage("%d classes (%.1f%%) of the interface headers do not exist in the real headers",
180 ccount
, (float)(100.0 * ccount
/m_interface
.GetClassesCount()));
185 int IfaceCheckApp::CompareClasses(const wxClass
* iface
, const wxClassPtrArray
& api
)
187 wxString searchedclasses
;
188 const wxMethod
*real
;
191 wxASSERT(iface
&& api
.GetCount()>0);
193 // build a string with the names of the API classes compared to iface
194 for (unsigned int j
=0; j
<api
.GetCount(); j
++)
195 searchedclasses
+= "/" + api
[j
]->GetName();
196 searchedclasses
.Remove(0, 1);
199 // shorten the name of the header so the log file is more readable
200 wxString header
= iface
->GetHeader().AfterLast('/');
202 for (unsigned int i
=0; i
<iface
->GetMethodCount(); i
++)
204 const wxMethod
& m
= iface
->GetMethod(i
);
205 const wxString
& tofind
= m
.GetAsString();
208 // search in the methods of the api classes provided
209 for (unsigned int j
=0; j
<api
.GetCount(); j
++)
211 real
= api
[j
]->FindMethod(m
);
214 // there is a matching prototype! It's ok!
215 //LogMessage("the doxygen method '%s' has a match in the real interface of '%s'!",
216 // tofind, api[j]->GetName());
223 wxMethodPtrArray overloads
;
225 // try searching for a method with the same name but with
226 // different return type / arguments / qualifiers
227 for (unsigned int j
=0; j
<api
.GetCount(); j
++)
229 wxMethodPtrArray results
= api
[j
]->FindMethodNamed(m
.GetName());
231 // append "results" array to "overloads"
232 WX_APPEND_ARRAY(overloads
, results
);
235 if (overloads
.GetCount()>1)
237 // TODO: decide which of these overloads is the most "similar" to m
238 // and eventually modify it
239 LogWarning("%s: there are %d overloads of method '%s' in the classes '%s' "
240 "all with different signatures; manual fix is required",
241 header
, overloads
.GetCount(), tofind
, searchedclasses
);
243 else if (overloads
.GetCount() == 1)
246 if (m_modify
) tmp
= "; fixing it...";
248 LogWarning("%s: the method '%s' of classes '%s' has a different signature%s",
249 header
, tofind
, searchedclasses
, tmp
);
254 FixMethod(iface
->GetHeader(), &m
, overloads
[0]);
258 LogMessage("%s: real '%s' class has no method '%s'",
259 header
, searchedclasses
, tofind
);
260 count
++; // count this type of warnings
268 void IfaceCheckApp::FixMethod(const wxString
& header
, const wxMethod
* iface
, const wxMethod
* api
)
270 wxASSERT(iface
&& api
);
273 if (!file
.Open(header
)) {
274 LogError("can't open the '%s' header file.", header
);
278 // GetLocation() returns the line where the last part of the prototype is placed:
279 int end
= iface
->GetLocation()-1;
280 if (end
<= 0 || end
>= (int)file
.GetLineCount()) {
281 LogWarning("invalid location info for method '%s': %d.",
282 iface
->GetAsString(), iface
->GetLocation());
286 // find the start point of this prototype declaration:
289 !file
.GetLine(start
).Contains(";") &&
290 !file
.GetLine(start
).Contains("*/"))
295 LogError("can't find the beginning of the declaration of '%s' method in '%s' header",
296 iface
->GetAsString(), header
);
300 // remove the old prototype
301 for (int i
=start
; i
<=end
; i
++)
302 file
.RemoveLine(start
); // remove (end-start)-nth times the start-th line
304 #define INDENTATION_STR wxString(" ")
306 // if possible, add also the @deprecated tag in the doxygen comment
307 if (file
.GetLine(start
-1).Contains("*/") && api
->IsDeprecated())
309 file
.RemoveLine(start
-1);
310 file
.InsertLine(INDENTATION_STR
+ INDENTATION_STR
+
311 "@deprecated @todo provide deprecation description", start
-1);
312 file
.InsertLine(INDENTATION_STR
+ "*/", start
++);
315 // insert the new one
316 file
.InsertLine(INDENTATION_STR
+ api
->GetAsString() + ";", start
);
318 // now save the modification
320 LogError("can't save the '%s' header file.", header
);
325 void IfaceCheckApp::PrintStatistics(long secs
)
327 LogMessage("wx real headers contains declaration of %d classes (%d methods)",
328 m_api
.GetClassesCount(), m_api
.GetMethodCount());
329 LogMessage("wx interface headers contains declaration of %d classes (%d methods)",
330 m_interface
.GetClassesCount(), m_interface
.GetMethodCount());
331 LogMessage("total processing took %d seconds.", secs
);