Debian Codegraph @

cppchecker/cppchecker.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : cppchecker.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 #include "cppchecker.h"
27
28 #include "Notebook.h"
29 #include "clKeyboardManager.h"
30 #include "cl_process.h"
31 #include "cppcheckreportpage.h"
32 #include "cppchecksettingsdlg.h"
33 #include "event_notifier.h"
34 #include "file_logger.h"
35 #include "fileextmanager.h"
36 #include "globals.h"
37 #include "imanager.h"
38 #include "macros.h"
39 #include "processreaderthread.h"
40 #include "procutils.h"
41 #include "project.h"
42 #include "workspace.h"
43
44 #include <wx/app.h>
45 #include <wx/dir.h>
46 #include <wx/ffile.h>
47 #include <wx/imaglist.h>
48 #include <wx/log.h>
49 #include <wx/menu.h>
50 #include <wx/msgdlg.h>
51 #include <wx/process.h>
52 #include <wx/sstream.h>
53 #include <wx/stdpaths.h>
54 #include <wx/tokenzr.h>
55 #include <wx/xml/xml.h>
56 #include <wx/xrc/xmlres.h>
57
58 static CppCheckPlugin* thePlugin = NULL;
59
60 // Define the plugin entry point
61 CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager)
62 {
63 if(thePlugin == 0) {
64 thePlugin = new CppCheckPlugin(manager);
65 }
66 return thePlugin;
67 }
68
69 CL_PLUGIN_API PluginInfo* GetPluginInfo()
70 {
71 static PluginInfo info;
72 info.SetAuthor("Eran Ifrah & Jérémie (jfouche)");
73 info.SetName("CppChecker");
74 info.SetDescription(_("CppChecker integration for CodeLite IDE"));
75 info.SetVersion("v1.0");
76 return &info;
77 }
78
79 CL_PLUGIN_API int GetPluginInterfaceVersion() { return PLUGIN_INTERFACE_VERSION; }
80
81 CppCheckPlugin::CppCheckPlugin(IManager* manager)
82 : IPlugin(manager)
83 , m_cppcheckProcess(NULL)
84 , m_canRestart(true)
85 , m_explorerSepItem(NULL)
86 , m_workspaceSepItem(NULL)
87 , m_projectSepItem(NULL)
88 , m_view(NULL)
89 , m_analysisInProgress(false)
90 , m_fileCount(0)
91 , m_fileProcessed(1)
92 {
93 FileExtManager::Init();
94
95 Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &CppCheckPlugin::OnCppCheckReadData, this);
96 Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &CppCheckPlugin::OnCppCheckTerminated, this);
97
98 m_longName = _("CppCheck integration for CodeLite IDE");
99 m_shortName = "CppCheck";
100
101 // Load settings
102 m_mgr->GetConfigTool()->ReadObject("CppCheck", &m_settings);
103 // Now set default suppressions if none have been serialised
104 m_settings.SetDefaultSuppressedWarnings();
105 // NB we can't load any project-specific settings here, as the workspace won't yet have loaded. We do it just before
106 // they're used
107
108 // Connect events
109 m_mgr->GetTheApp()->Connect(XRCID("cppcheck_settings_item"), wxEVT_COMMAND_MENU_SELECTED,
110 wxCommandEventHandler(CppCheckPlugin::OnSettingsItem), NULL, (wxEvtHandler*)this);
111 m_mgr->GetTheApp()->Connect(XRCID("cppcheck_settings_item_project"), wxEVT_COMMAND_MENU_SELECTED,
112 wxCommandEventHandler(CppCheckPlugin::OnSettingsItemProject), NULL,
113 (wxEvtHandler*)this);
114 m_mgr->GetTheApp()->Connect(XRCID("cppcheck_editor_item"), wxEVT_COMMAND_MENU_SELECTED,
115 wxCommandEventHandler(CppCheckPlugin::OnCheckFileEditorItem), NULL,
116 (wxEvtHandler*)this);
117 m_mgr->GetTheApp()->Connect(XRCID("cppcheck_fileexplorer_item"), wxEVT_COMMAND_MENU_SELECTED,
118 wxCommandEventHandler(CppCheckPlugin::OnCheckFileExplorerItem), NULL,
119 (wxEvtHandler*)this);
120 m_mgr->GetTheApp()->Connect(XRCID("cppcheck_workspace_item"), wxEVT_COMMAND_MENU_SELECTED,
121 wxCommandEventHandler(CppCheckPlugin::OnCheckWorkspaceItem), NULL, (wxEvtHandler*)this);
122 m_mgr->GetTheApp()->Connect(XRCID("cppcheck_project_item"), wxEVT_COMMAND_MENU_SELECTED,
123 wxCommandEventHandler(CppCheckPlugin::OnCheckProjectItem), NULL, (wxEvtHandler*)this);
124
125 EventNotifier::Get()->Bind(wxEVT_WORKSPACE_CLOSED, &CppCheckPlugin::OnWorkspaceClosed, this);
126
127 EventNotifier::Get()->Bind(wxEVT_CONTEXT_MENU_EDITOR, &CppCheckPlugin::OnEditorContextMenu, this);
128
129 clKeyboardManager::Get()->AddAccelerator(_("CppCheck"),
130 { { "cppcheck_editor_item", _("Check current file") },
131 { "cppcheck_project_item", _("Check current file's project") },
132 { "cppcheck_workspace_item", _("Check workspace") } });
133
134 m_view = new CppCheckReportPage(m_mgr->GetOutputPaneNotebook(), m_mgr, this);
135
136 auto book = m_mgr->GetOutputPaneNotebook();
137 auto images = book->GetBitmaps();
138 book->AddPage(m_view, _("CppCheck"), false, images->Add("check-all"));
139 m_tabHelper.reset(new clTabTogglerHelper(_("CppCheck"), m_view, "", NULL));
140 m_tabHelper->SetOutputTabBmp(images->Add("check-all"));
141 }
142
143 CppCheckPlugin::~CppCheckPlugin() {}
144
145 void CppCheckPlugin::CreateToolBar(clToolBarGeneric* toolbar) { wxUnusedVar(toolbar); }
146
147 void CppCheckPlugin::CreatePluginMenu(wxMenu* pluginsMenu)
148 {
149 wxMenu* menu = new wxMenu();
150 menu->Append(XRCID("cppcheck_editor_item"), _("Check current file"));
151 menu->Append(XRCID("cppcheck_project_item"), _("Check current file's project"));
152 menu->Append(XRCID("cppcheck_workspace_item"), _("Check workspace"));
153 menu->AppendSeparator();
154
155 wxMenuItem* item =
156 new wxMenuItem(menu, XRCID("cppcheck_settings_item"), _("Settings"), wxEmptyString, wxITEM_NORMAL);
157 menu->Append(item);
158 pluginsMenu->Append(wxID_ANY, _("CppCheck"), menu);
159 }
160
161 void CppCheckPlugin::HookPopupMenu(wxMenu* menu, MenuType type)
162 {
163 if(type == MenuTypeFileExplorer) {
164 if(!menu->FindItem(XRCID("CPPCHECK_EXPLORER_POPUP"))) {
165 m_explorerSepItem = menu->PrependSeparator();
166 menu->Prepend(XRCID("CPPCHECK_EXPLORER_POPUP"), _("CppCheck"), CreateFileExplorerPopMenu());
167 }
168
169 } else if(type == MenuTypeFileView_Workspace) {
170 if(!menu->FindItem(XRCID("CPPCHECK_WORKSPACE_POPUP"))) {
171 m_workspaceSepItem = menu->PrependSeparator();
172 menu->Prepend(XRCID("CPPCHECK_WORKSPACE_POPUP"), _("CppCheck"), CreateWorkspacePopMenu());
173 }
174
175 } else if(type == MenuTypeFileView_Project) {
176 if(!menu->FindItem(XRCID("CPPCHECK_PROJECT_POPUP"))) {
177 m_projectSepItem = menu->PrependSeparator();
178 menu->Prepend(XRCID("CPPCHECK_PROJECT_POPUP"), _("CppCheck"), CreateProjectPopMenu());
179 }
180 }
181 }
182
183 void CppCheckPlugin::UnPlug()
184 {
185 m_tabHelper.reset(NULL);
186 Unbind(wxEVT_ASYNC_PROCESS_OUTPUT, &CppCheckPlugin::OnCppCheckReadData, this);
187 Unbind(wxEVT_ASYNC_PROCESS_TERMINATED, &CppCheckPlugin::OnCppCheckTerminated, this);
188
189 m_mgr->GetTheApp()->Disconnect(XRCID("cppcheck_settings_item"), wxEVT_COMMAND_MENU_SELECTED,
190 wxCommandEventHandler(CppCheckPlugin::OnSettingsItem), NULL, (wxEvtHandler*)this);
191 m_mgr->GetTheApp()->Disconnect(XRCID("cppcheck_settings_item_project"), wxEVT_COMMAND_MENU_SELECTED,
192 wxCommandEventHandler(CppCheckPlugin::OnSettingsItemProject), NULL,
193 (wxEvtHandler*)this);
194 m_mgr->GetTheApp()->Disconnect(XRCID("cppcheck_editor_item"), wxEVT_COMMAND_MENU_SELECTED,
195 wxCommandEventHandler(CppCheckPlugin::OnCheckFileEditorItem), NULL,
196 (wxEvtHandler*)this);
197 m_mgr->GetTheApp()->Disconnect(XRCID("cppcheck_fileexplorer_item"), wxEVT_COMMAND_MENU_SELECTED,
198 wxCommandEventHandler(CppCheckPlugin::OnCheckFileExplorerItem), NULL,
199 (wxEvtHandler*)this);
200 m_mgr->GetTheApp()->Disconnect(XRCID("cppcheck_workspace_item"), wxEVT_COMMAND_MENU_SELECTED,
201 wxCommandEventHandler(CppCheckPlugin::OnCheckWorkspaceItem), NULL,
202 (wxEvtHandler*)this);
203 m_mgr->GetTheApp()->Disconnect(XRCID("cppcheck_project_item"), wxEVT_COMMAND_MENU_SELECTED,
204 wxCommandEventHandler(CppCheckPlugin::OnCheckProjectItem), NULL,
205 (wxEvtHandler*)this);
206
207 EventNotifier::Get()->Unbind(wxEVT_CONTEXT_MENU_EDITOR, &CppCheckPlugin::OnEditorContextMenu, this);
208 EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_CLOSED, &CppCheckPlugin::OnWorkspaceClosed, this);
209
210 // before this plugin is un-plugged we must remove the tab we added
211 for(size_t i = 0; i < m_mgr->GetOutputPaneNotebook()->GetPageCount(); i++) {
212 if(m_view == m_mgr->GetOutputPaneNotebook()->GetPage(i)) {
213 m_mgr->GetOutputPaneNotebook()->RemovePage(i);
214 break;
215 }
216 }
217 m_view->Destroy();
218
219 // terminate the cppcheck daemon
220 wxDELETE(m_cppcheckProcess);
221 }
222
223 wxMenu* CppCheckPlugin::CreateFileExplorerPopMenu()
224 {
225 // Create the popup menu for the file explorer
226 // The only menu that we are interested in is the file explorer menu
227 wxMenu* menu = new wxMenu();
228 wxMenuItem* item(NULL);
229
230 item = new wxMenuItem(menu, XRCID("cppcheck_fileexplorer_item"), _("Run CppCheck"), wxEmptyString, wxITEM_NORMAL);
231 menu->Append(item);
232
233 item = new wxMenuItem(menu, XRCID("cppcheck_settings_item"), _("Settings"), wxEmptyString, wxITEM_NORMAL);
234 menu->Append(item);
235
236 return menu;
237 }
238
239 wxMenu* CppCheckPlugin::CreateEditorPopMenu()
240 {
241 // Create the popup menu for the file explorer
242 // The only menu that we are interested in is the file explorer menu
243 wxMenu* menu = new wxMenu();
244 wxMenuItem* item(NULL);
245
246 item = new wxMenuItem(menu, XRCID("cppcheck_editor_item"), _("Run CppCheck"), wxEmptyString, wxITEM_NORMAL);
247 menu->Append(item);
248
249 item = new wxMenuItem(menu, XRCID("cppcheck_settings_item"), _("Settings"), wxEmptyString, wxITEM_NORMAL);
250 menu->Append(item);
251
252 return menu;
253 }
254
255 wxMenu* CppCheckPlugin::CreateProjectPopMenu()
256 {
257 wxMenu* menu = new wxMenu();
258 wxMenuItem* item(NULL);
259
260 item = new wxMenuItem(menu, XRCID("cppcheck_project_item"), _("Run CppCheck"), wxEmptyString, wxITEM_NORMAL);
261 menu->Append(item);
262
263 item = new wxMenuItem(menu, XRCID("cppcheck_settings_item_project"), _("Settings"), wxEmptyString, wxITEM_NORMAL);
264 menu->Append(item);
265
266 return menu;
267 }
268
269 wxMenu* CppCheckPlugin::CreateWorkspacePopMenu()
270 {
271 wxMenu* menu = new wxMenu();
272 wxMenuItem* item(NULL);
273
274 item = new wxMenuItem(menu, XRCID("cppcheck_workspace_item"), _("Run CppCheck"), wxEmptyString, wxITEM_NORMAL);
275 menu->Append(item);
276
277 item = new wxMenuItem(menu, XRCID("cppcheck_settings_item"), _("Settings"), wxEmptyString, wxITEM_NORMAL);
278 menu->Append(item);
279
280 return menu;
281 }
282
283 void CppCheckPlugin::OnCheckFileEditorItem(wxCommandEvent& e)
284 {
285 if(m_cppcheckProcess) {
286 clLogMessage(_("CppCheckPlugin: CppCheck is currently busy please wait for it to complete the current check"));
287 return;
288 }
289
290 IEditor* editor = m_mgr->GetActiveEditor();
291 if(editor) {
292 m_filelist.Add(editor->GetFileName().GetFullPath());
293 }
294
295 DoStartTest();
296 }
297
298 void CppCheckPlugin::OnCheckFileExplorerItem(wxCommandEvent& e)
299 {
300 if(m_cppcheckProcess) {
301 clLogMessage(_("CppCheckPlugin: CppCheck is currently busy please wait for it to complete the current check"));
302 return;
303 }
304
305 TreeItemInfo item = m_mgr->GetSelectedTreeItemInfo(TreeFileExplorer);
306 for(size_t i = 0; i < item.m_paths.GetCount(); ++i) {
307 if(wxDir::Exists(item.m_paths.Item(i))) {
308 // directory
309 GetFileListFromDir(item.m_paths.Item(i));
310 } else {
311 // filename
312 m_filelist.Add(item.m_paths.Item(i));
313 }
314 }
315 DoStartTest();
316 }
317
318 void CppCheckPlugin::OnCheckWorkspaceItem(wxCommandEvent& e)
319 {
320 if(m_cppcheckProcess) {
321 clLogMessage(_("CppCheckPlugin: CppCheck is currently busy please wait for it to complete the current check"));
322 return;
323 }
324
325 if(!m_mgr->GetWorkspace() || !m_mgr->IsWorkspaceOpen()) {
326 return;
327 }
328
329 // retrieve complete list of source files of the workspace
330 wxArrayString projects;
331 wxString err_msg;
332 std::vector<wxFileName> tmpfiles;
333 m_mgr->GetWorkspace()->GetProjectList(projects);
334
335 for(size_t i = 0; i < projects.GetCount(); i++) {
336 ProjectPtr proj = m_mgr->GetWorkspace()->FindProjectByName(projects.Item(i), err_msg);
337 if(proj) {
338 proj->GetFilesAsVectorOfFileName(tmpfiles);
339 }
340 }
341
342 // only C/C++ files
343 for(size_t i = 0; i < tmpfiles.size(); i++) {
344 if(FileExtManager::GetType(tmpfiles.at(i).GetFullPath()) == FileExtManager::TypeSourceC ||
345 FileExtManager::GetType(tmpfiles.at(i).GetFullPath()) == FileExtManager::TypeSourceCpp) {
346 m_filelist.Add(tmpfiles.at(i).GetFullPath());
347 }
348 }
349 DoStartTest();
350 }
351
352 ProjectPtr CppCheckPlugin::FindSelectedProject()
353 {
354 ProjectPtr proj = NULL;
355
356 if(!m_mgr->GetWorkspace() || !m_mgr->IsWorkspaceOpen()) {
357 return proj;
358 }
359
360 TreeItemInfo item = m_mgr->GetSelectedTreeItemInfo(TreeFileView);
361 if(item.m_itemType == ProjectItem::TypeProject) {
362 wxString project_name(item.m_text);
363 wxString err_msg;
364 proj = m_mgr->GetWorkspace()->FindProjectByName(project_name, err_msg);
365 }
366
367 return proj;
368 }
369
370 void CppCheckPlugin::OnCheckProjectItem(wxCommandEvent& e)
371 {
372 if(m_cppcheckProcess) {
373 clLogMessage(_("CppCheckPlugin: CppCheck is currently busy please wait for it to complete the current check"));
374 return;
375 }
376
377 if(!m_mgr->GetWorkspace() || !m_mgr->IsWorkspaceOpen()) {
378 return;
379 }
380
381 ProjectPtr proj = FindSelectedProject();
382 if(!proj) {
383 // try the current editor's project
384 auto activeEditor = clGetManager()->GetActiveEditor();
385 if(activeEditor) {
386 wxString projectName = clGetManager()->GetProjectNameByFile(activeEditor->GetFileName().GetFullPath());
387 proj = m_mgr->GetWorkspace()->GetProject(projectName);
388 }
389
390 // if we are still null, return
391 if(!proj) {
392 return;
393 }
394 }
395
396 // retrieve complete list of source files of the workspace
397 std::vector<wxFileName> tmpfiles;
398 proj->GetFilesAsVectorOfFileName(tmpfiles);
399
400 // only C/C++ files
401 for(size_t i = 0; i < tmpfiles.size(); i++) {
402 if(FileExtManager::GetType(tmpfiles.at(i).GetFullPath()) == FileExtManager::TypeSourceC ||
403 FileExtManager::GetType(tmpfiles.at(i).GetFullPath()) == FileExtManager::TypeSourceCpp) {
404 m_filelist.Add(tmpfiles.at(i).GetFullPath());
405 }
406 }
407
408 DoStartTest(proj);
409 }
410
411 void CppCheckPlugin::OnCppCheckTerminated(clProcessEvent& e)
412 {
413 m_filelist.Clear();
414 wxDELETE(m_cppcheckProcess);
415
416 m_view->PrintStatusMessage();
417 m_view->GotoFirstError();
418 }
419
420 void CppCheckPlugin::OnSettingsItem(wxCommandEvent& WXUNUSED(e)) { DoSettingsItem(); }
421
422 void CppCheckPlugin::OnSettingsItemProject(wxCommandEvent& WXUNUSED(e))
423 {
424 ProjectPtr proj = FindSelectedProject();
425 DoSettingsItem(proj);
426 }
427
428 void CppCheckPlugin::DoSettingsItem(ProjectPtr project /*= NULL*/)
429 {
430 // Find the default path for the CppCheckSettingsDialog's wxFileDialog
431 wxString defaultpath;
432 IEditor* ed = m_mgr->GetActiveEditor();
433 if(ed && ed->GetFileName().IsOk()) {
434 defaultpath = ed->GetFileName().GetPath();
435 }
436
437 // If there's an active project, first load any project-specific settings: definitions and undefines
438 // (We couldn't do that with the rest of the settings as the workspace hadn't yet been loaded)
439 m_settings.LoadProjectSpecificSettings(
440 project); // NB we still do this if !project, as that will clear any stale settings
441
442 CppCheckSettingsDialog dlg(m_mgr->GetTheApp()->GetTopWindow(), &m_settings, m_mgr->GetConfigTool(), defaultpath,
443 project.Get() != NULL);
444 if(dlg.ShowModal() == wxID_OK) {
445 m_mgr->GetConfigTool()->WriteObject("CppCheck", &m_settings);
446 if(project) {
447 // Also save any project-specific settings: definitions and undefines
448 wxString definitions = wxJoin(m_settings.GetDefinitions(), ',');
449 wxString undefines = wxJoin(m_settings.GetUndefines(), ',');
450 if(!(definitions.empty() && undefines.empty())) {
451 project->SetPluginData("CppCheck", definitions + ';' + undefines);
452 } else {
453 project->SetPluginData("CppCheck", "");
454 }
455 }
456 }
457 }
458
459 void CppCheckPlugin::GetFileListFromDir(const wxString& root)
460 {
461 m_filelist.Clear();
462 wxArrayString tmparr;
463 wxDir::GetAllFiles(root, &tmparr);
464
465 for(size_t i = 0; i < tmparr.GetCount(); i++) {
466 switch(FileExtManager::GetType(tmparr.Item(i))) {
467 case FileExtManager::TypeSourceC:
468 case FileExtManager::TypeSourceCpp: {
469 m_filelist.Add(tmparr.Item(i));
470 break;
471 }
472
473 default:
474 break;
475 }
476 }
477 }
478
479 void CppCheckPlugin::DoProcess(ProjectPtr proj)
480 {
481 wxString command = DoGetCommand(proj);
482 m_view->AppendLine(wxString::Format(_("Starting cppcheck: %s\n"), command.c_str()));
483
484 size_t flags = IProcessCreateDefault | IProcessWrapInShell;
485 #if defined(__WXMSW__)
486 // Under Windows, we set the working directory to the binary folder
487 // so the configurtion files can be found
488 clDEBUG() << "CppCheck: Working directory:" << clStandardPaths::Get().GetBinFolder() << endl;
489 clDEBUG() << "CppCheck: Command:" << command << endl;
490 m_cppcheckProcess = CreateAsyncProcess(this, command, flags, clStandardPaths::Get().GetBinFolder());
491 #elif defined(__WXOSX__)
492 clDEBUG() << "CppCheck: Working directory:" << clStandardPaths::Get().GetDataDir() << endl;
493 clDEBUG() << "CppCheck: Command:" << command << endl;
494 m_cppcheckProcess = CreateAsyncProcess(this, command, flags, clStandardPaths::Get().GetDataDir());
495 #else
496 m_cppcheckProcess = CreateAsyncProcess(this, command, flags);
497 #endif
498 if(!m_cppcheckProcess) {
499 wxMessageBox(_("Failed to launch codelite_cppcheck process!"), _("Warning"), wxOK | wxCENTER | wxICON_WARNING);
500 return;
501 }
502 }
503
504 /**
505 * Ensure that the CppCheck tab is visible
506 */
507 void CppCheckPlugin::SetTabVisible(bool clearContent)
508 {
509 // Make sure that the Output pane is visible
510 wxAuiManager* aui = m_mgr->GetDockingManager();
511 if(aui) {
512 wxAuiPaneInfo& info = aui->GetPane("Output View");
513 if(info.IsOk() && !info.IsShown()) {
514 info.Show();
515 aui->Update();
516 }
517 }
518
519 // Set the focus to the CppCheck tab
520 Notebook* book = m_mgr->GetOutputPaneNotebook();
521 if(book->GetPageText((size_t)book->GetSelection()) != "CppCheck") {
522 for(size_t i = 0; i < book->GetPageCount(); i++) {
523 if(book->GetPageText(i) == "CppCheck") {
524 book->SetSelection(i);
525 break;
526 }
527 }
528 }
529
530 // clear the view contents
531 if(clearContent) {
532 m_view->Clear();
533 m_fileCount = m_filelist.GetCount();
534 m_fileProcessed = 1;
535 }
536 }
537
538 void CppCheckPlugin::StopAnalysis()
539 {
540 // Clear the files queue
541 if(m_cppcheckProcess) {
542 // terminate the m_cppcheckProcess
543 m_cppcheckProcess->Terminate();
544 }
545 }
546
547 size_t CppCheckPlugin::GetProgress()
548 {
549 double progress = (((double)m_fileProcessed) / (double)m_fileCount) * 100;
550 return (size_t)progress;
551 }
552
553 void CppCheckPlugin::RemoveExcludedFiles()
554 {
555 wxArrayString exclude = m_settings.GetExcludeFiles();
556
557 wxArrayString tmpfiles(m_filelist);
558 m_filelist.Clear();
559
560 for(size_t i = 0; i < tmpfiles.GetCount(); i++) {
561 wxFileName fn(tmpfiles.Item(i));
562 if(exclude.Index(fn.GetFullPath()) == wxNOT_FOUND) {
563 // file does not exist in the excluded files list
564 // add it
565 m_filelist.Add(tmpfiles.Item(i));
566 }
567 }
568 }
569
570 void CppCheckPlugin::OnWorkspaceClosed(clWorkspaceEvent& e)
571 {
572 m_view->Clear();
573 e.Skip();
574 }
575
576 void CppCheckPlugin::DoStartTest(ProjectPtr proj /*=NULL*/)
577 {
578 RemoveExcludedFiles();
579 if(!m_filelist.GetCount()) {
580 wxMessageBox(_("No files to check"), "CppCheck", wxOK | wxCENTRE, m_mgr->GetTheApp()->GetTopWindow());
581 return;
582 }
583 SetTabVisible(true);
584 m_view->Clear();
585 m_view->SetGaugeRange(m_filelist.GetCount());
586
587 // We need to load any project-specific settings: definitions and undefines
588 // (We couldn't do that with the rest of the settings as the workspace hadn't yet been loaded)
589 m_settings.LoadProjectSpecificSettings(proj); // NB we still do this if !proj, as that will clear any stale settings
590
591 // Start the test
592 DoProcess(proj);
593 }
594
595 wxString CppCheckPlugin::DoGetCommand(ProjectPtr proj)
596 {
597 // Linux / Mac way: spawn the process and execute the command
598 wxString cmd, path;
599 path = clStandardPaths::Get().GetBinaryFullPath("codelite_cppcheck");
600 ::WrapWithQuotes(path);
601
602 wxString fileList = DoGenerateFileList();
603 if(fileList.IsEmpty()) {
604 return "";
605 }
606
607 // build the command
608 cmd << path << " ";
609 cmd << m_settings.GetOptions();
610
611 // Append here project specifc search paths
612 if(proj) {
613 wxArrayString projectSearchPaths = proj->GetIncludePaths();
614 for(size_t i = 0; i < projectSearchPaths.GetCount(); ++i) {
615 wxFileName fnIncPath(projectSearchPaths.Item(i), "");
616 wxString includePath = fnIncPath.GetPath();
617 ::WrapWithQuotes(includePath);
618 cmd << " -I" << includePath;
619 }
620
621 wxArrayString projMacros = proj->GetPreProcessors();
622 for(size_t i = 0; i < projMacros.GetCount(); ++i) {
623 cmd << " -D" << projMacros.Item(i);
624 }
625 }
626
627 cmd << " --file-list=";
628 ::WrapWithQuotes(fileList);
629 cmd << fileList << " ";
630 clDEBUG() << "cppcheck command:" << cmd << endl;
631 return cmd;
632 }
633
634 wxString CppCheckPlugin::DoGenerateFileList()
635 {
636 // create temporary file and save the file there
637 wxFileName fnFileList(clCxxWorkspaceST::Get()->GetPrivateFolder(), "cppcheck.list");
638
639 // create temporary file and save the file there
640 wxFFile file(fnFileList.GetFullPath(), "w+b");
641 if(!file.IsOpened()) {
642 wxMessageBox(_("Failed to open temporary file ") + fnFileList.GetFullPath(), _("Warning"),
643 wxOK | wxCENTER | wxICON_WARNING);
644 return wxEmptyString;
645 }
646
647 wxString content;
648 for(size_t i = 0; i < m_filelist.GetCount(); i++) {
649 content << m_filelist.Item(i) << "\n";
650 }
651
652 file.Write(content);
653 file.Flush();
654 file.Close();
655 return fnFileList.GetFullPath();
656 }
657
658 void CppCheckPlugin::OnCppCheckReadData(clProcessEvent& e)
659 {
660 e.Skip();
661 m_view->AppendLine(e.GetOutput());
662 }
663
664 void CppCheckPlugin::OnEditorContextMenu(clContextMenuEvent& event)
665 {
666 event.Skip();
667 IEditor* editor = m_mgr->GetActiveEditor();
668 CHECK_PTR_RET(editor);
669
670 if(FileExtManager::IsCxxFile(editor->GetFileName())) {
671 event.GetMenu()->Append(wxID_ANY, _("CppCheck"), CreateEditorPopMenu());
672 }
673 }