forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppchecker.cpp
More file actions
301 lines (258 loc) · 10 KB
/
Copy pathcppchecker.cpp
File metadata and controls
301 lines (258 loc) · 10 KB
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 Eran Ifrah
// file name : cppchecker.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "cppchecker.h"
#include "AsyncProcess/processreaderthread.h"
#include "IWorkspace.h"
#include "Keyboard/clKeyboardManager.h"
#include "Notebook.h"
#include "Platform/Platform.hpp"
#include "StringUtils.h"
#include "build_settings_config.h"
#include "clAnsiEscapeCodeColourBuilder.hpp"
#include "clWorkspaceManager.h"
#include "cl_process.h"
#include "codelite_events.h"
#include "cppchecksettingsdlg.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "fileextmanager.h"
#include "globals.h"
#include "imanager.h"
#include "macros.h"
#include "procutils.h"
#include "project.h"
#include "shell_command.h"
#include "workspace.h"
#include <wx/app.h>
#include <wx/dir.h>
#include <wx/ffile.h>
#include <wx/imaglist.h>
#include <wx/log.h>
#include <wx/menu.h>
#include <wx/msgdlg.h>
#include <wx/process.h>
#include <wx/sstream.h>
#include <wx/stdpaths.h>
#include <wx/tokenzr.h>
#include <wx/xml/xml.h>
#include <wx/xrc/xmlres.h>
// Define the plugin entry point
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager)
{
return new CppCheckPlugin(manager);
}
CL_PLUGIN_API PluginInfo* GetPluginInfo()
{
static PluginInfo info;
info.SetAuthor("Eran Ifrah & Jérémie (jfouche)");
info.SetName("CppChecker");
info.SetDescription(_("CppChecker integration for CodeLite IDE"));
info.SetVersion("v2.0");
return &info;
}
CL_PLUGIN_API int GetPluginInterfaceVersion() { return PLUGIN_INTERFACE_VERSION; }
CppCheckPlugin::CppCheckPlugin(IManager* manager)
: IPlugin(manager)
{
FileExtManager::Init();
m_longName = _("CppCheck integration for CodeLite IDE");
m_shortName = "CppCheck";
// NB we can't load any project-specific settings here, as the workspace won't yet have loaded. We do it just before
// they're used
// Connect events
Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &CppCheckPlugin::OnCppCheckReadData, this);
Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &CppCheckPlugin::OnCppCheckTerminated, this);
m_mgr->GetTheApp()->Bind(wxEVT_MENU, &CppCheckPlugin::OnRun, this, XRCID("run_cppcheck"));
m_mgr->GetTheApp()->Bind(wxEVT_MENU, &CppCheckPlugin::OnSettings, this, XRCID("cppcheck_settings_item"));
EventNotifier::Get()->Bind(wxEVT_GET_IS_BUILD_IN_PROGRESS, &CppCheckPlugin::OnIsBuildInProgress, this);
EventNotifier::Get()->Bind(wxEVT_STOP_BUILD, &CppCheckPlugin::OnStopRun, this);
EventNotifier::Get()->Bind(wxEVT_WORKSPACE_CLOSED, &CppCheckPlugin::OnWorkspaceClosed, this);
clKeyboardManager::Get()->AddAccelerator(_("CppCheck"), { { "run_cppcheck", _("Run cppcheck...") } });
}
CppCheckPlugin::~CppCheckPlugin() {}
void CppCheckPlugin::CreateToolBar(clToolBarGeneric* toolbar) { wxUnusedVar(toolbar); }
void CppCheckPlugin::CreatePluginMenu(wxMenu* pluginsMenu)
{
wxMenu* menu = new wxMenu();
menu->Append(XRCID("run_cppcheck"), _("Run cppcheck..."));
menu->AppendSeparator();
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)
{
wxUnusedVar(menu);
wxUnusedVar(type);
}
void CppCheckPlugin::UnPlug()
{
Unbind(wxEVT_ASYNC_PROCESS_OUTPUT, &CppCheckPlugin::OnCppCheckReadData, this);
Unbind(wxEVT_ASYNC_PROCESS_TERMINATED, &CppCheckPlugin::OnCppCheckTerminated, this);
m_mgr->GetTheApp()->Unbind(wxEVT_MENU, &CppCheckPlugin::OnRun, this, XRCID("run_cppcheck"));
m_mgr->GetTheApp()->Unbind(wxEVT_MENU, &CppCheckPlugin::OnSettings, this, XRCID("cppcheck_settings_item"));
EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_CLOSED, &CppCheckPlugin::OnWorkspaceClosed, this);
// terminate the cppcheck daemon
wxDELETE(m_cppcheckProcess);
m_runStartedByUser = false;
}
void CppCheckPlugin::OnCppCheckTerminated(clProcessEvent& e)
{
wxDELETE(m_cppcheckProcess);
m_runStartedByUser = false;
NotifyStopped();
}
void CppCheckPlugin::DoRun()
{
wxString command = DoGetCommand();
if (command.empty()) {
return;
}
// notify about starting build process.
// we pass the selected compiler in the event
clBuildEvent eventStarted(wxEVT_BUILD_PROCESS_STARTED);
auto default_compiler = BuildSettingsConfigST::Get()->GetDefaultCompiler(wxEmptyString);
if (default_compiler) {
eventStarted.SetToolchain(BuildSettingsConfigST::Get()->GetDefaultCompiler(wxEmptyString)->GetName());
} else {
// use the default compiler known to CodeLite
eventStarted.SetToolchain("gnu g++");
}
EventNotifier::Get()->AddPendingEvent(eventStarted);
// Notify about build process started
clBuildEvent eventStart(wxEVT_BUILD_STARTED);
EventNotifier::Get()->AddPendingEvent(eventStart);
AddOutputLine(command + "\n");
size_t flags = IProcessCreateDefault | IProcessWrapInShell;
m_cppcheckProcess = ::CreateAsyncProcess(this, command, flags);
if (!m_cppcheckProcess) {
wxMessageBox(_("Failed to launch cppcheck process.\nMake sure its installed and in your PATH"), _("Warning"),
wxOK | wxCENTER | wxICON_WARNING);
return;
}
m_runStartedByUser = true;
}
wxString CppCheckPlugin::DoGetCommand()
{
// Linux / Mac way: spawn the process and execute the command
const auto cppcheck = ThePlatform->Which("cppcheck");
if (!cppcheck) {
::wxMessageBox(_("Could not locate \"cppcheck\". Please install it and try again"), "CodeLite",
wxICON_WARNING | wxOK | wxOK_DEFAULT | wxCENTRE);
return wxEmptyString;
}
wxString command = clConfig::Get().Read("cppcheck/command", CPPCHECK_DEFAULT_COMMAND);
wxString workspace_path;
wxString current_file;
auto workspace = clWorkspaceManager::Get().GetWorkspace();
if (workspace) {
if (workspace->IsRemote()) {
workspace_path = wxFileName(workspace->GetFileName()).GetPath(false, wxPATH_UNIX);
} else {
workspace_path = wxFileName(workspace->GetFileName()).GetPath();
}
}
if (clGetManager()->GetActiveEditor()) {
current_file = clGetManager()->GetActiveEditor()->GetRemotePathOrLocal();
}
// replace the place holders
command.Replace("${cppcheck}", StringUtils::WrapWithDoubleQuotes(*cppcheck));
command.Replace("${WorkspacePath}", StringUtils::WrapWithDoubleQuotes(workspace_path));
command.Replace("${CurrentFileFullPath}", StringUtils::WrapWithDoubleQuotes(current_file));
wxString cmd;
auto lines = ::wxStringTokenize(command, "\n", wxTOKEN_STRTOK);
for (auto& line : lines) {
line.Trim().Trim(false);
line = line.BeforeFirst('#');
if (line.empty()) {
continue;
}
// Create the cache dir if required
wxString cache_dir;
if (line.StartsWith("--cppcheck-build-dir=", &cache_dir)) {
cache_dir.Trim().Trim(false).Replace("\"", "");
wxFileName::Mkdir(cache_dir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
}
cmd << line << " ";
}
cmd.Trim();
if (cmd.empty()) {
::wxMessageBox(_("Cannot run cppcheck. Empty command"), "CodeLite",
wxICON_WARNING | wxOK | wxOK_DEFAULT | wxCENTRE);
return wxEmptyString;
}
return cmd;
}
void CppCheckPlugin::OnCppCheckReadData(clProcessEvent& e)
{
e.Skip();
AddOutputLine(e.GetOutputRaw());
}
void CppCheckPlugin::OnSettings(wxCommandEvent& event)
{
wxUnusedVar(event);
CppCheckSettingsDialog dlg{ EventNotifier::Get()->TopFrame() };
dlg.ShowModal();
}
void CppCheckPlugin::OnRun(wxCommandEvent& event)
{
wxUnusedVar(event);
DoRun();
}
void CppCheckPlugin::OnWorkspaceClosed(clWorkspaceEvent& e) { e.Skip(); }
void CppCheckPlugin::AddOutputLine(const wxString& message)
{
clBuildEvent eventAddLine(wxEVT_BUILD_PROCESS_ADDLINE);
eventAddLine.SetString(message);
EventNotifier::Get()->AddPendingEvent(eventAddLine);
}
void CppCheckPlugin::OnIsBuildInProgress(clBuildEvent& event)
{
if (!m_runStartedByUser) {
event.Skip();
return;
}
event.SetIsRunning(m_cppcheckProcess != nullptr);
}
void CppCheckPlugin::OnStopRun(clBuildEvent& event)
{
if (!m_runStartedByUser) {
event.Skip();
return;
}
m_runStartedByUser = false;
if (m_cppcheckProcess) {
wxDELETE(m_cppcheckProcess);
}
NotifyStopped();
}
void CppCheckPlugin::NotifyStopped()
{
clBuildEvent eventStopped(wxEVT_BUILD_ENDED);
EventNotifier::Get()->AddPendingEvent(eventStopped);
clBuildEvent eventProcessStopped(wxEVT_BUILD_PROCESS_ENDED);
EventNotifier::Get()->AddPendingEvent(eventProcessStopped);
}