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 "xmlparser.h"
27 // global verbosity flag
28 bool g_verbose
= false;
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 #define API_DUMP_FILE "dump.api.txt"
37 #define INTERFACE_DUMP_FILE "dump.interface.txt"
39 #define MODIFY_SWITCH "m"
40 #define DUMP_SWITCH "dump"
41 #define HELP_SWITCH "h"
42 #define VERBOSE_SWITCH "v"
44 static const wxCmdLineEntryDesc g_cmdLineDesc
[] =
46 { wxCMD_LINE_SWITCH
, MODIFY_SWITCH
, "modify",
47 "modify the interface headers to match the real ones" },
48 { wxCMD_LINE_SWITCH
, "", DUMP_SWITCH
,
49 "dump both interface and API to plain text" },
50 { wxCMD_LINE_SWITCH
, HELP_SWITCH
, "help",
51 "show help message", wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
52 { wxCMD_LINE_SWITCH
, VERBOSE_SWITCH
, "verbose",
54 { wxCMD_LINE_PARAM
, NULL
, NULL
,
55 "gccXML", wxCMD_LINE_VAL_STRING
, wxCMD_LINE_OPTION_MANDATORY
},
56 { wxCMD_LINE_PARAM
, NULL
, NULL
,
57 "doxygenXML", wxCMD_LINE_VAL_STRING
, wxCMD_LINE_OPTION_MANDATORY
},
61 class IfaceCheckApp
: public wxAppConsole
64 // don't use builtin cmd line parsing:
65 virtual bool OnInit() { m_modify
=false; return true; }
69 int CompareClasses(const wxClass
* iface
, const wxClassPtrArray
& api
);
70 void FixMethod(const wxString
& header
, const wxMethod
* iface
, const wxMethod
* api
);
73 void PrintStatistics(long secs
);
76 wxXmlGccInterface m_api
; // "real" headers API
77 wxXmlDoxygenInterface m_interface
; // doxygen-commented headers API
79 // was the MODIFY_SWITCH passed?
83 IMPLEMENT_APP_CONSOLE(IfaceCheckApp
)
85 int IfaceCheckApp::OnRun()
87 long startTime
= wxGetLocalTime(); // for timing purpose
89 // parse the command line...
90 wxCmdLineParser
parser(g_cmdLineDesc
, argc
, argv
);
92 switch (parser
.Parse())
95 // HELP_SWITCH was passed
99 if (parser
.Found(VERBOSE_SWITCH
))
102 if (!m_api
.Parse(parser
.GetParam(0)) ||
103 !m_interface
.Parse(parser
.GetParam(1)))
106 if (parser
.Found(DUMP_SWITCH
))
108 LogMessage("Dumping real API to '%s'...", API_DUMP_FILE
);
109 m_api
.Dump(API_DUMP_FILE
);
111 LogMessage("Dumping interface API to '%s'...", INTERFACE_DUMP_FILE
);
112 m_interface
.Dump(INTERFACE_DUMP_FILE
);
116 if (parser
.Found(MODIFY_SWITCH
))
122 PrintStatistics(wxGetLocalTime() - startTime
);
129 void IfaceCheckApp::ShowProgress()
135 bool IfaceCheckApp::Compare()
137 const wxClassArray
& interface
= m_interface
.GetClasses();
140 int mcount
= 0, ccount
= 0;
142 LogMessage("Comparing the interface API to the real API (%d classes to compare)...",
143 interface
.GetCount());
145 for (unsigned int i
=0; i
<interface
.GetCount(); i
++)
147 wxString cname
= interface
[i
].GetName();
151 // search in the real headers for i-th interface class
152 // for both class cname and cnameBase as in wxWidgets world, most often
153 // class cname is platform-specific while the real public interface of
154 // that class is part of the cnameBase class.
155 c
= m_api
.FindClass(cname
);
157 c
= m_api
.FindClass(cname
+ "Base");
160 if (api
.GetCount()>0) {
162 // there is a class with exactly the same name!
163 mcount
+= CompareClasses(&interface
[i
], api
);
167 LogMessage("%s: couldn't find the real interface for the '%s' class",
168 interface
[i
].GetHeader(), cname
);
173 LogMessage("%d methods (%.1f%%) of the interface headers do not exist in the real headers",
174 mcount
, (float)(100.0 * mcount
/m_interface
.GetMethodCount()));
175 LogMessage("%d classes (%.1f%%) of the interface headers do not exist in the real headers",
176 ccount
, (float)(100.0 * ccount
/m_interface
.GetClassesCount()));
181 int IfaceCheckApp::CompareClasses(const wxClass
* iface
, const wxClassPtrArray
& api
)
183 wxString searchedclasses
;
184 const wxMethod
*real
;
187 wxASSERT(iface
&& api
.GetCount()>0);
189 // build a string with the names of the API classes compared to iface
190 for (unsigned int j
=0; j
<api
.GetCount(); j
++)
191 searchedclasses
+= "/" + api
[j
]->GetName();
192 searchedclasses
.Remove(0, 1);
195 // shorten the name of the header so the log file is more readable
196 wxString header
= iface
->GetHeader().AfterLast('/');
198 for (unsigned int i
=0; i
<iface
->GetMethodCount(); i
++)
200 const wxMethod
& m
= iface
->GetMethod(i
);
201 wxString tofind
= m
.GetAsString();
204 // search in the methods of the api classes provided
205 for (unsigned int j
=0; j
<api
.GetCount(); j
++)
207 real
= api
[j
]->FindMethod(m
);
210 // there is a matching prototype! It's ok!
211 //LogMessage("the doxygen method '%s' has a match in the real interface of '%s'!",
212 // tofind, api[j]->GetName());
219 wxMethodPtrArray overloads
;
221 // try searching for a method with the same name but with
222 // different return type / arguments / qualifiers
223 for (unsigned int j
=0; j
<api
.GetCount(); j
++)
225 wxMethodPtrArray results
= api
[j
]->FindMethodNamed(m
.GetName());
227 // append "results" array to "overloads"
228 WX_APPEND_ARRAY(overloads
, results
);
231 if (overloads
.GetCount()>1)
233 // TODO: decide which of these overloads is the most "similar" to m
234 // and eventually modify it
235 LogWarning("%s: there are %d overloads of method '%s' in the classes '%s' "
236 "all with different signatures; manual fix is required",
237 header
, overloads
.GetCount(), tofind
, searchedclasses
);
239 else if (overloads
.GetCount() == 1)
242 if (m_modify
) tmp
= "; fixing it...";
244 LogWarning("%s: the method '%s' of classes '%s' has a different signature%s",
245 header
, tofind
, searchedclasses
, tmp
);
249 FixMethod(iface
->GetHeader(), &m
, overloads
[0]);
253 LogMessage("%s: real '%s' class has no method '%s'",
254 header
, searchedclasses
, tofind
);
255 count
++; // count this type of warnings
263 void IfaceCheckApp::FixMethod(const wxString
& header
, const wxMethod
* iface
, const wxMethod
* api
)
265 wxASSERT(iface
&& api
);
268 if (!file
.Open(header
)) {
269 LogError("can't open the '%s' header file.", header
);
274 // fix iface signature to match that of "api" method:
275 file
.RemoveLine(iface
->GetLocation());
276 file
.InsertLine(api
->GetAsString(), iface
->GetLocation());
278 // GetLocation() returns the line where the last part of the prototype is placed:
279 int end
= iface
->GetLocation()-1;
281 // find the start point of this prototype declaration:
283 while (!file
.GetLine(start
).Contains(";") && !file
.GetLine(start
).Contains("*/"))
288 LogError("can't find the beginning of the declaration of '%s' method in '%s' header",
289 iface
->GetAsString(), header
);
294 // remove the old prototype
295 for (int i
=start
; i
<=end
; i
++)
298 // insert the new one
299 file
.InsertLine(" " + api
->GetAsString(), start
);
303 LogError("can't save the '%s' header file.", header
);
308 void IfaceCheckApp::PrintStatistics(long secs
)
310 LogMessage("wx real headers contains declaration of %d classes (%d methods)",
311 m_api
.GetClassesCount(), m_api
.GetMethodCount());
312 LogMessage("wx interface headers contains declaration of %d classes (%d methods)",
313 m_interface
.GetClassesCount(), m_interface
.GetMethodCount());
314 LogMessage("total processing took %d seconds.", secs
);
322 #include "wx/process.h"
323 #include "wx/unix/execute.h"
325 int wxAddProcessCallback(wxEndProcessData
*, int )