1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
#include <wx/app.h>
#include "processreaderthread.h"
#include <wx/imaglist.h>
#include <wx/ffile.h>
#include "imanager.h"
#include "procutils.h"
#include "cppcheckreportpage.h"
#include "cppchecksettingsdlg.h"
#include <wx/process.h>
#include <wx/dir.h>
#include "notebook_ex.h"
#include "workspace.h"
#include "project.h"
#include "fileextmanager.h"
#include "jobqueue.h"
#include "cppchecker.h"
#include "cl_process.h"
#include <wx/stdpaths.h>
#include <wx/menu.h>
#include <wx/xrc/xmlres.h>
#include <wx/xml/xml.h>
#include <wx/sstream.h>
#include <wx/log.h>
static CppCheckPlugin* thePlugin = NULL;
//Define the plugin entry point
extern "C" EXPORT IPlugin *CreatePlugin(IManager *manager)
{
if (thePlugin == 0) {
thePlugin = new CppCheckPlugin(manager);
}
return thePlugin;
}
extern "C" EXPORT PluginInfo GetPluginInfo()
{
PluginInfo info;
info.SetAuthor(wxT("Eran Ifrah & Jérémie (jfouche)"));
info.SetName(wxT("CppChecker"));
info.SetDescription(wxT("CppCheker integration for CodeLite IDE"));
info.SetVersion(wxT("v1.0"));
return info;
}
extern "C" EXPORT int GetPluginInterfaceVersion()
{
return PLUGIN_INTERFACE_VERSION;
}
BEGIN_EVENT_TABLE(CppCheckPlugin, wxEvtHandler)
EVT_COMMAND(wxID_ANY, wxEVT_PROC_DATA_READ, CppCheckPlugin::OnCppCheckReadData)
EVT_COMMAND(wxID_ANY, wxEVT_PROC_TERMINATED, CppCheckPlugin::OnCppCheckTerminated)
END_EVENT_TABLE()
CppCheckPlugin::CppCheckPlugin(IManager *manager)
: IPlugin(manager)
, m_cppcheckProcess(NULL)
, m_canRestart( true )
, m_explorerSepItem(NULL)
, m_workspaceSepItem(NULL)
, m_projectSepItem(NULL)
, m_view(NULL)
, m_analysisInProgress(false)
, m_fileCount(0)
, m_fileProcessed(1)
{
FileExtManager::Init();
m_longName = wxT("CppCheck intergration for CodeLite IDE");
m_shortName = wxT("CppCheck");
// Load settings
m_mgr->GetConfigTool()->ReadObject(wxT("CppCheck"), &m_settings);
// Connect events
m_mgr->GetTheApp()->Connect(XRCID("cppcheck_settings_item"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CppCheckPlugin::OnSettingsItem), NULL, (wxEvtHandler*)this);
m_mgr->GetTheApp()->Connect(XRCID("cppcheck_fileexplorer_item"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CppCheckPlugin::OnCheckFileExplorerItem), NULL, (wxEvtHandler*)this);
m_mgr->GetTheApp()->Connect(XRCID("cppcheck_workspace_item"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CppCheckPlugin::OnCheckWorkspaceItem), NULL, (wxEvtHandler*)this);
m_mgr->GetTheApp()->Connect(XRCID("cppcheck_project_item"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CppCheckPlugin::OnCheckProjectItem), NULL, (wxEvtHandler*)this);
m_mgr->GetTheApp()->Connect(wxEVT_WORKSPACE_CLOSED, wxCommandEventHandler(CppCheckPlugin::OnWorkspaceClosed),NULL, this);
m_view = new CppCheckReportPage(m_mgr->GetOutputPaneNotebook(), m_mgr, this);
wxBookCtrlBase *book = m_mgr->GetOutputPaneNotebook();
m_mgr->GetOutputPaneNotebook()->AddPage(m_view, wxT("CppCheck"), false, LoadBitmapFile(wxT("cppcheck.png")));
}
CppCheckPlugin::~CppCheckPlugin()
{
m_mgr->GetTheApp()->Disconnect(XRCID("cppcheck_settings_item"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CppCheckPlugin::OnSettingsItem), NULL, (wxEvtHandler*)this);
m_mgr->GetTheApp()->Disconnect(XRCID("cppcheck_fileexplorer_item"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CppCheckPlugin::OnCheckFileExplorerItem), NULL, (wxEvtHandler*)this);
m_mgr->GetTheApp()->Disconnect(XRCID("cppcheck_workspace_item"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CppCheckPlugin::OnCheckWorkspaceItem), NULL, (wxEvtHandler*)this);
m_mgr->GetTheApp()->Disconnect(XRCID("cppcheck_project_item"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CppCheckPlugin::OnCheckProjectItem), NULL, (wxEvtHandler*)this);
m_mgr->GetTheApp()->Disconnect(wxEVT_WORKSPACE_CLOSED, wxCommandEventHandler(CppCheckPlugin::OnWorkspaceClosed),NULL, this);
}
clToolBar *CppCheckPlugin::CreateToolBar(wxWindow *parent)
{
// Create the toolbar to be used by the plugin
clToolBar *tb(NULL);
return tb;
}
void CppCheckPlugin::CreatePluginMenu(wxMenu *pluginsMenu)
{
wxMenu* menu = new wxMenu();
wxMenuItem* item = new wxMenuItem(menu, XRCID("cppcheck_settings_item"), _("Settings"), wxEmptyString, wxITEM_NORMAL);
menu->Append(item);
pluginsMenu->Append(wxID_ANY, _("CppCheck"), menu);
}
void CppCheckPlugin::HookPopupMenu(wxMenu *menu, MenuType type)
{
if (type == MenuTypeEditor) {
//TODO::Append items for the editor context menu
} else if (type == MenuTypeFileExplorer) {
if (!menu->FindItem(XRCID("CPPCHECK_EXPLORER_POPUP"))) {
m_explorerSepItem = menu->PrependSeparator();
menu->Prepend(XRCID("CPPCHECK_EXPLORER_POPUP"), wxT("CppCheck"), CreateFileExplorerPopMenu());
}
} else if (type == MenuTypeFileView_Workspace) {
if (!menu->FindItem(XRCID("CPPCHECK_WORKSPACE_POPUP"))) {
m_workspaceSepItem = menu->PrependSeparator();
menu->Prepend(XRCID("CPPCHECK_WORKSPACE_POPUP"), wxT("CppCheck"), CreateWorkspacePopMenu());
}
} else if (type == MenuTypeFileView_Project) {
if (!menu->FindItem(XRCID("CPPCHECK_PROJECT_POPUP"))) {
m_projectSepItem = menu->PrependSeparator();
menu->Prepend(XRCID("CPPCHECK_PROJECT_POPUP"), wxT("CppCheck"), CreateProjectPopMenu());
}
}
}
void CppCheckPlugin::UnHookPopupMenu(wxMenu *menu, MenuType type)
{
if (type == MenuTypeEditor) {
//TODO::Unhook items for the editor context menu
} else if (type == MenuTypeFileExplorer) {
wxMenuItem *item = menu->FindItem(XRCID("CPPCHECK_EXPLORER_POPUP"));
if (item) {
menu->Destroy(item);
menu->Destroy(m_explorerSepItem);
m_explorerSepItem = NULL;
}
} else if (type == MenuTypeFileView_Workspace) {
wxMenuItem *item = menu->FindItem(XRCID("CPPCHECK_WORKSPACE_POPUP"));
if (item) {
menu->Destroy(item);
menu->Destroy(m_workspaceSepItem);
m_workspaceSepItem = NULL;
}
} else if (type == MenuTypeFileView_Project) {
wxMenuItem *item = menu->FindItem(XRCID("CPPCHECK_PROJECT_POPUP"));
if (item) {
menu->Destroy(item);
menu->Destroy(m_projectSepItem);
m_projectSepItem = NULL;
}
}
}
void CppCheckPlugin::UnPlug()
{
// before this plugin is un-plugged we must remove the tab we added
for (size_t i=0; i<m_mgr->GetOutputPaneNotebook()->GetPageCount(); i++) {
if (m_view == m_mgr->GetOutputPaneNotebook()->GetPage(i)) {
m_mgr->GetOutputPaneNotebook()->RemovePage(i);
m_view->Destroy();
break;
}
}
// terminate the cppcheck daemon
if ( m_cppcheckProcess ) {
wxLogMessage(wxT("CppCheckPlugin: Terminating cppcheck daemon..."));
delete m_cppcheckProcess;
m_cppcheckProcess = NULL;
}
}
wxMenu* CppCheckPlugin::CreateFileExplorerPopMenu()
{
//Create the popup menu for the file explorer
//The only menu that we are interseted is the file explorer menu
wxMenu* menu = new wxMenu();
wxMenuItem *item(NULL);
item = new wxMenuItem(menu, XRCID("cppcheck_fileexplorer_item"), wxT("Run CppCheck"), wxEmptyString, wxITEM_NORMAL);
menu->Append(item);
return menu;
}
wxMenu* CppCheckPlugin::CreateProjectPopMenu()
{
wxMenu* menu = new wxMenu();
wxMenuItem *item(NULL);
item = new wxMenuItem(menu, XRCID("cppcheck_project_item"), wxT("Run CppCheck"), wxEmptyString, wxITEM_NORMAL);
menu->Append(item);
return menu;
}
wxMenu* CppCheckPlugin::CreateWorkspacePopMenu()
{
wxMenu* menu = new wxMenu();
wxMenuItem *item(NULL);
item = new wxMenuItem(menu, XRCID("cppcheck_workspace_item"), wxT("Run CppCheck"), wxEmptyString, wxITEM_NORMAL);
menu->Append(item);
return menu;
}
void CppCheckPlugin::OnCheckFileExplorerItem(wxCommandEvent& e)
{
if ( m_cppcheckProcess ) {
wxLogMessage(wxT("CppCheckPlugin: CppCheck is currently busy please wait for it to complete the current check"));
return;
}
TreeItemInfo item = m_mgr->GetSelectedTreeItemInfo(TreeFileExplorer);
if ( item.m_fileName.IsDir() ) {
GetFileListFromDir( item.m_fileName.GetFullPath() );
} else {
m_filelist.Add( item.m_fileName.GetFullPath() );
}
DoStartTest();
}
void CppCheckPlugin::OnCheckWorkspaceItem(wxCommandEvent& e)
{
if ( m_cppcheckProcess ) {
wxLogMessage(wxT("CppCheckPlugin: CppCheck is currently busy please wait for it to complete the current check"));
return;
}
if ( !m_mgr->GetWorkspace() || !m_mgr->IsWorkspaceOpen() ) {
return;
}
TreeItemInfo item = m_mgr->GetSelectedTreeItemInfo(TreeFileView);
if ( item.m_itemType == ProjectItem::TypeWorkspace ) {
// retrieve complete list of source files of the workspace
wxArrayString projects;
wxString err_msg;
std::vector< wxFileName > tmpfiles;
m_mgr->GetWorkspace()->GetProjectList(projects);
for (size_t i=0; i< projects.GetCount(); i++) {
ProjectPtr proj = m_mgr->GetWorkspace()->FindProjectByName(projects.Item(i), err_msg);
if ( proj ) {
proj->GetFiles(tmpfiles, true);
}
}
// only C/C++ files
for (size_t i=0; i< tmpfiles.size(); i++ ) {
if (FileExtManager::GetType( tmpfiles.at(i).GetFullPath() ) == FileExtManager::TypeSourceC ||
FileExtManager::GetType( tmpfiles.at(i).GetFullPath() ) == FileExtManager::TypeSourceCpp) {
m_filelist.Add( tmpfiles.at(i).GetFullPath() );
}
}
}
DoStartTest();
}
void CppCheckPlugin::OnCheckProjectItem(wxCommandEvent& e)
{
if ( m_cppcheckProcess ) {
wxLogMessage(wxT("CppCheckPlugin: CppCheck is currently busy please wait for it to complete the current check"));
return;
}
if ( !m_mgr->GetWorkspace() || !m_mgr->IsWorkspaceOpen() ) {
return;
}
TreeItemInfo item = m_mgr->GetSelectedTreeItemInfo(TreeFileView);
if ( item.m_itemType == ProjectItem::TypeProject) {
// retrieve complete list of source files of the workspace
wxString project_name (item.m_text);
wxString err_msg;
std::vector< wxFileName > tmpfiles;
ProjectPtr proj = m_mgr->GetWorkspace()->FindProjectByName(project_name, err_msg);
if ( !proj ) {
return;
}
proj->GetFiles(tmpfiles, true);
// only C/C++ files
for (size_t i=0; i< tmpfiles.size(); i++ ) {
if ( FileExtManager::GetType( tmpfiles.at(i).GetFullPath() ) == FileExtManager::TypeSourceC ||
FileExtManager::GetType( tmpfiles.at(i).GetFullPath() ) == FileExtManager::TypeSourceCpp ) {
m_filelist.Add( tmpfiles.at(i).GetFullPath() );
}
}
}
DoStartTest();
}
void CppCheckPlugin::OnCppCheckTerminated(wxCommandEvent& e)
{
m_filelist.Clear();
delete m_cppcheckProcess;
m_cppcheckProcess = NULL;
m_view->PrintStatusMessage();
}
/**
*
*/
void CppCheckPlugin::OnSettingsItem(wxCommandEvent &e)
{
CppCheckSettingsDialog dlg(m_mgr->GetTheApp()->GetTopWindow(), &m_settings, m_mgr->GetConfigTool());
if (dlg.ShowModal() == wxID_OK) {
m_mgr->GetConfigTool()->WriteObject(wxT("CppCheck"), &m_settings);
}
}
void CppCheckPlugin::GetFileListFromDir(const wxString& root)
{
m_filelist.Clear();
wxArrayString tmparr;
wxDir::GetAllFiles(root, &tmparr);
for (size_t i=0; i<tmparr.GetCount(); i++) {
switch (FileExtManager::GetType( tmparr.Item(i) ) ) {
case FileExtManager::TypeSourceC:
case FileExtManager::TypeSourceCpp: {
m_filelist.Add( tmparr.Item(i) );
break;
}
default:
break;
}
}
}
void CppCheckPlugin::DoProcess()
{
wxString command = DoGetCommand();
wxLogMessage(wxT("Starting cppcheck: %s"), command.c_str());
m_cppcheckProcess = CreateAsyncProcess(this, command);
if (!m_cppcheckProcess ) {
wxMessageBox(_("Failed to launch codelite_cppcheck process!"), _("Warning"), wxOK|wxCENTER|wxICON_WARNING);
return;
}
}
/**
* Ensure that the CppCheck tab is visible
*/
void CppCheckPlugin::SetTabVisible(bool clearContent)
{
// Make sure that the Output pane is visible
wxAuiManager *aui = m_mgr->GetDockingManager();
if (aui) {
wxAuiPaneInfo &info = aui->GetPane(wxT("Output View"));
if (info.IsOk() && !info.IsShown()) {
info.Show();
aui->Update();
}
}
// Set the focus to the CppCheck tab
wxBookCtrlBase *book = m_mgr->GetOutputPaneNotebook();
if (book->GetPageText((size_t)book->GetSelection()) != wxT("CppCheck")) {
for (size_t i=0; i<book->GetPageCount(); i++) {
if (book->GetPageText(i) == wxT("CppCheck")) {
book->SetSelection(i);
break;
}
}
}
// clear the view contents
if ( clearContent ) {
m_view->Clear();
m_fileCount = m_filelist.GetCount();
m_fileProcessed = 1;
}
}
void CppCheckPlugin::StopAnalysis()
{
// Clear the files queue
if (m_cppcheckProcess) {
// terminate the m_cppcheckProcess
m_cppcheckProcess->Terminate();
}
}
size_t CppCheckPlugin::GetProgress()
{
double progress = (((double) m_fileProcessed) / (double) m_fileCount) * 100;
return (size_t) progress;
}
void CppCheckPlugin::RemoveExcludedFiles()
{
wxArrayString exclude = m_settings.GetExcludeFiles();
wxArrayString tmpfiles ( m_filelist );
m_filelist.Clear();
for ( size_t i=0; i<tmpfiles.GetCount(); i++ ) {
wxFileName fn ( tmpfiles.Item(i) );
if ( exclude.Index(fn.GetFullPath()) == wxNOT_FOUND ) {
// file does not exist in the excluded files list
// add it
m_filelist.Add( tmpfiles.Item(i) );
}
}
}
void CppCheckPlugin::OnWorkspaceClosed(wxCommandEvent& e)
{
m_view->Clear();
e.Skip();
}
void CppCheckPlugin::DoStartTest()
{
RemoveExcludedFiles();
SetTabVisible(true);
m_view->Clear();
m_view->SetGaugeRange(m_filelist.GetCount());
// Start the test
DoProcess();
}
wxString CppCheckPlugin::DoGetCommand()
{
// Linux / Mac way: spawn the process and execute the command
wxString cmd, path;
#if defined (__WXMAC__)
path = wxStandardPaths::Get().GetDataDir();
#else // Linux / Windows
path = wxStandardPaths::Get().GetPluginsDir();
#endif
path << wxFileName::GetPathSeparator() << wxT("codelite_cppcheck");
#ifdef __WXMSW__
path << wxT(".exe");
#endif
wxString fileList = DoGenerateFileList();
if(fileList.IsEmpty())
return wxT("");
// build the command
cmd << wxT("\"") << path << wxT("\" ");
cmd << m_settings.GetOptions();
cmd << wxT(" --file-list=");
cmd << wxT("\"") << fileList << wxT("\"");
return cmd;
}
wxString CppCheckPlugin::DoGenerateFileList()
{
//create temporary file and save the file there
wxString wspPath = m_mgr->GetWorkspace()->GetWorkspaceFileName().GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR);
wxString list_file( wspPath );
list_file << wxT("cppcheck.list");
//create temporary file and save the file there
wxFFile file(list_file, wxT("w+b"));
if (!file.IsOpened()) {
wxMessageBox(_("Failed to open temporary file ") + list_file, _("Warning"), wxOK|wxCENTER|wxICON_WARNING);
return wxEmptyString;
}
wxString content;
for(size_t i=0; i<m_filelist.GetCount(); i++) {
content << m_filelist.Item(i) << wxT("\n");
}
file.Write( content );
file.Flush();
file.Close();
return list_file;
}
void CppCheckPlugin::OnCppCheckReadData(wxCommandEvent& e)
{
e.Skip();
ProcessEventData *ped = (ProcessEventData *) e.GetClientData();
m_view->AppendLine( ped->GetData() );
delete ped;
}
|