Debian Codegraph @

cppchecker/cppchecksettingsdlg.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : cppchecksettingsdlg.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 "cppchecksettingsdlg.h"
27 #include "windowattrmanager.h"
28 #include <wx/msgdlg.h>
29 #include <wx/filedlg.h>
30 #include <wx/textdlg.h>
31 #include <wx/dirdlg.h>
32
33 CppCheckSettingsDialog::CppCheckSettingsDialog(wxWindow* parent, CppCheckSettings* settings, IConfigTool* conf,
34 const wxString& defaultpath, bool showDefsTab)
35 : CppCheckSettingsDialogBase(parent)
36 , m_settings(settings)
37 , m_conf(conf)
38 , m_defaultpath(defaultpath)
39 {
40 m_checkListExtraWarnings->Check(0, settings->GetStyle());
41 m_checkListExtraWarnings->Check(1, settings->GetPerformance());
42 m_checkListExtraWarnings->Check(2, settings->GetPortability());
43 m_checkListExtraWarnings->Check(3, settings->GetUnusedFunctions());
44 m_checkListExtraWarnings->Check(4, settings->GetMissingIncludes());
45 m_checkListExtraWarnings->Check(5, settings->GetInformation());
46 m_checkListExtraWarnings->Check(6, settings->GetPosixStandards());
47 m_checkListExtraWarnings->Check(7, settings->GetC99Standards());
48 m_checkListExtraWarnings->Check(8, settings->GetCpp11Standards());
49 m_cbOptionForce->SetValue(settings->GetForce());
50 m_cbJobs->SetValue(settings->GetJobs() > 1);
51 m_spinCtrlJobs->SetValue(settings->GetJobs());
52
53 m_listBoxExcludelist->Append(settings->GetExcludeFiles());
54
55 // The Suppressed Warnings strings are stored in a string/string map:
56 // e.g. <"cstyleCast", "C-style pointer casting">
57 // We display the second, so store the keys in a wxArrayInt so that we can identify each again
58 m_SuppressionsKeys.Clear();
59 wxStringMap_t::const_iterator iter = settings->GetSuppressedWarningsStrings1()->begin();
60 for(; iter != settings->GetSuppressedWarningsStrings1()->end(); ++iter) {
61 // First the checked ones
62 int index = m_checkListSuppress->Append((*iter).second);
63 m_checkListSuppress->Check(index, true);
64 m_SuppressionsKeys.Add((*iter).first);
65 }
66
67 for(iter = settings->GetSuppressedWarningsStrings0()->begin();
68 iter != settings->GetSuppressedWarningsStrings0()->end(); ++iter) {
69 // Then the unchecked ones
70 int index = m_checkListSuppress->Append((*iter).second);
71 m_checkListSuppress->Check(index, false);
72 m_SuppressionsKeys.Add((*iter).first);
73 }
74
75 m_listBoxIncludeDirs->Append(settings->GetIncludeDirs());
76 m_checkBoxSuppressSystemIncludes->SetValue(settings->GetSuppressSystemIncludes());
77 m_checkBoxSerialiseIncludeDirs->SetValue(settings->GetSaveIncludeDirs());
78
79 if(showDefsTab) {
80 m_listBoxDefinelist->Append(settings->GetDefinitions());
81 m_listBoxUndefineList->Append(settings->GetUndefines());
82 } else {
83 m_DefinesPanel->Hide(); // Don't show this panel unless its contents are valid i.e. we got here via a rt-click
84 // over a project
85 }
86
87 SetName("CppCheckSettingsDialog");
88 WindowAttrManager::Load(this);
89 }
90
91 void CppCheckSettingsDialog::OnBtnOK(wxCommandEvent& e)
92 {
93 m_settings->SetStyle(m_checkListExtraWarnings->IsChecked(0));
94 m_settings->SetPerformance(m_checkListExtraWarnings->IsChecked(1));
95 m_settings->SetPortability(m_checkListExtraWarnings->IsChecked(2));
96 m_settings->SetUnusedFunctions(m_checkListExtraWarnings->IsChecked(3));
97 m_settings->SetMissingIncludes(m_checkListExtraWarnings->IsChecked(4));
98 m_settings->SetInformation(m_checkListExtraWarnings->IsChecked(5));
99 m_settings->SetPosixStandards(m_checkListExtraWarnings->IsChecked(6));
100 m_settings->SetC99Standards(m_checkListExtraWarnings->IsChecked(7));
101 m_settings->SetCpp11Standards(m_checkListExtraWarnings->IsChecked(8));
102 m_settings->SetForce(m_cbOptionForce->IsChecked());
103 if(m_cbJobs->IsChecked()) {
104 m_settings->SetJobs(m_spinCtrlJobs->GetValue());
105 } else {
106 m_settings->SetJobs(1);
107 }
108 m_settings->SetCheckConfig(m_cbCheckConfig->IsChecked());
109
110 m_settings->SetExcludeFiles(m_listBoxExcludelist->GetStrings());
111
112 m_settings->SetSuppressedWarnings(m_checkListSuppress, m_SuppressionsKeys);
113 m_settings->SetSaveSuppressedWarnings(m_checkBoxSerialise->IsChecked());
114
115 m_settings->SetIncludeDirs(m_listBoxIncludeDirs->GetStrings());
116 m_settings->SetSuppressSystemIncludes(m_checkBoxSuppressSystemIncludes->IsChecked());
117 m_settings->SetSaveIncludeDirs(m_checkBoxSerialiseIncludeDirs->IsChecked());
118
119 m_settings->SetDefinitions(m_listBoxDefinelist->GetStrings());
120 m_settings->SetUndefines(m_listBoxUndefineList->GetStrings());
121
122 e.Skip();
123 }
124
125 CppCheckSettingsDialog::~CppCheckSettingsDialog() {}
126
127 void CppCheckSettingsDialog::OnChecksTickAll(wxCommandEvent& WXUNUSED(e))
128 {
129 for(size_t n = 0; n < m_checkListExtraWarnings->GetCount(); ++n) {
130 m_checkListExtraWarnings->Check(n);
131 }
132 }
133
134 void CppCheckSettingsDialog::OnChecksUntickAll(wxCommandEvent& WXUNUSED(e))
135 {
136 for(size_t n = 0; n < m_checkListExtraWarnings->GetCount(); ++n) {
137 m_checkListExtraWarnings->Check(n, false);
138 }
139 }
140
141 void CppCheckSettingsDialog::OnAddFile(wxCommandEvent& WXUNUSED(e))
142 {
143 wxString filter(wxString(_("C/C++ Files ")));
144 filter << wxT("(*.c;*.cpp)|*.c;*.cpp|") << wxString(_("All Files")) << wxT(" (") << wxFileSelectorDefaultWildcardStr
145 << wxT(")|") << wxFileSelectorDefaultWildcardStr;
146
147 wxFileDialog dlg(this, _("Add File(s):"), m_defaultpath, wxEmptyString, filter,
148 wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE, wxDefaultPosition);
149
150 if(dlg.ShowModal() == wxID_OK) {
151 wxArrayString paths;
152 dlg.GetPaths(paths);
153
154 m_listBoxExcludelist->Append(paths);
155 }
156 }
157
158 void CppCheckSettingsDialog::OnRemoveFile(wxCommandEvent& WXUNUSED(e))
159 {
160 int sel = m_listBoxExcludelist->GetSelection();
161 if(sel != wxNOT_FOUND) {
162 m_listBoxExcludelist->Delete((unsigned int)sel);
163 }
164 }
165
166 void CppCheckSettingsDialog::OnClearList(wxCommandEvent& e)
167 {
168 wxUnusedVar(e);
169 m_listBoxExcludelist->Clear();
170 }
171
172 void CppCheckSettingsDialog::OnAddSuppression(wxCommandEvent& WXUNUSED(e))
173 {
174 CppCheckAddSuppressionDialog dlg(this);
175 while(true) {
176 int ret = dlg.ShowModal();
177 if(ret != wxID_OK) {
178 return;
179 }
180 wxString key = dlg.GetKey()->GetValue();
181 if(m_settings->GetSuppressedWarningsStrings0()->count(key) ||
182 m_settings->GetSuppressedWarningsStrings1()->count(key)) {
183 int ans = wxMessageBox(_("There is already an entry with ID string. Try again?"), _("CppCheck"),
184 wxYES_NO | wxICON_QUESTION, this);
185 if(ans != wxID_YES && ans != wxYES) {
186 return;
187 } else {
188 dlg.GetKey()->Clear();
189 continue;
190 }
191 }
192
193 int index = m_checkListSuppress->Append(dlg.GetDescription()->GetValue()); // If we're here, it worked
194 m_checkListSuppress->Check(index, false);
195 m_settings->AddSuppressedWarning(dlg.GetKey()->GetValue(), dlg.GetDescription()->GetValue(), false);
196 m_SuppressionsKeys.Add(dlg.GetKey()->GetValue());
197 return;
198 }
199 }
200
201 void CppCheckSettingsDialog::OnRemoveSuppression(wxCommandEvent& WXUNUSED(e))
202 {
203 int ans = wxMessageBox(_("Really remove this warning suppression, rather than just unticking it?"), _("CppCheck"),
204 wxYES_NO | wxICON_QUESTION, this);
205 if(ans != wxID_YES && ans != wxYES) {
206 return;
207 }
208
209 int sel = m_checkListSuppress->GetSelection();
210 if(sel != wxNOT_FOUND) {
211 m_settings->RemoveSuppressedWarning(m_SuppressionsKeys.Item(sel));
212 m_checkListSuppress->Delete((unsigned int)sel);
213 m_SuppressionsKeys.RemoveAt(sel);
214 }
215 }
216
217 void CppCheckSettingsDialog::OnSuppressTickAll(wxCommandEvent& WXUNUSED(e))
218 {
219 for(size_t n = 0; n < m_checkListSuppress->GetCount(); ++n) {
220 m_checkListSuppress->Check(n);
221 }
222 }
223
224 void CppCheckSettingsDialog::OnSuppressUntickAll(wxCommandEvent& WXUNUSED(e))
225 {
226 for(size_t n = 0; n < m_checkListSuppress->GetCount(); ++n) {
227 m_checkListSuppress->Check(n, false);
228 }
229 }
230
231 void CppCheckSettingsDialog::OnAddIncludeDir(wxCommandEvent& WXUNUSED(e))
232 {
233 wxDirDialog dlg(this, _("Select the path containing include files"));
234 if(dlg.ShowModal() == wxID_OK) {
235 m_listBoxIncludeDirs->Append(dlg.GetPath());
236 }
237 }
238
239 void CppCheckSettingsDialog::OnRemoveIncludeDir(wxCommandEvent& WXUNUSED(e))
240 {
241 int sel = m_listBoxIncludeDirs->GetSelection();
242 if(sel != wxNOT_FOUND) {
243 m_listBoxIncludeDirs->Delete((unsigned int)sel);
244 }
245 }
246
247 void CppCheckSettingsDialog::OnAddDefinition(wxCommandEvent& WXUNUSED(e))
248 {
249 wxString newitem =
250 wxGetTextFromUser(_("Enter a definition e.g. 'FOO' or 'BAR=1' (not '-DFoo')"), "CodeLite", "", this);
251 if(!newitem.empty()) {
252 m_listBoxDefinelist->Append(newitem);
253 }
254 }
255
256 void CppCheckSettingsDialog::OnRemoveDefinition(wxCommandEvent& WXUNUSED(e))
257 {
258 int sel = m_listBoxDefinelist->GetSelection();
259 if(sel != wxNOT_FOUND) {
260 m_listBoxDefinelist->Delete((unsigned int)sel);
261 }
262 }
263
264 void CppCheckSettingsDialog::OnClearDefinitions(wxCommandEvent& e)
265 {
266 wxUnusedVar(e);
267 m_listBoxDefinelist->Clear();
268 }
269
270 void CppCheckSettingsDialog::OnAddUndefine(wxCommandEvent& WXUNUSED(e))
271 {
272 wxString newitem =
273 wxGetTextFromUser(_("Enter a definition NOT to check e.g. 'FOO' or 'BAR=1' (not '-UFoo')"), "CodeLite", "", this);
274 if(!newitem.empty()) {
275 m_listBoxUndefineList->Append(newitem);
276 }
277 }
278
279 void CppCheckSettingsDialog::OnRemoveUndefine(wxCommandEvent& WXUNUSED(e))
280 {
281 int sel = m_listBoxUndefineList->GetSelection();
282 if(sel != wxNOT_FOUND) {
283 m_listBoxUndefineList->Delete((unsigned int)sel);
284 }
285 }
286
287 void CppCheckSettingsDialog::OnClearUndefines(wxCommandEvent& e)
288 {
289 wxUnusedVar(e);
290 m_listBoxUndefineList->Clear();
291 }
292
293 void CppCheckSettingsDialog::OnChecksTickAllUI(wxUpdateUIEvent& e)
294 {
295 for(size_t n = 0; n < m_checkListExtraWarnings->GetCount(); ++n) {
296 if(!m_checkListExtraWarnings->IsChecked(n)) {
297 e.Enable(true);
298 return;
299 }
300 }
301 e.Enable(false);
302 }
303
304 void CppCheckSettingsDialog::OnChecksUntickAllUI(wxUpdateUIEvent& e)
305 {
306 for(size_t n = 0; n < m_checkListExtraWarnings->GetCount(); ++n) {
307 if(m_checkListExtraWarnings->IsChecked(n)) {
308 e.Enable(true);
309 return;
310 }
311 }
312 e.Enable(false);
313 }
314
315 void CppCheckSettingsDialog::OnJobsUpdateUI(wxUpdateUIEvent& e) { e.Enable(m_cbJobs->IsChecked()); }
316
317 void CppCheckSettingsDialog::OnRemoveFileUI(wxUpdateUIEvent& e)
318 {
319 e.Enable(m_listBoxExcludelist->GetSelection() != wxNOT_FOUND);
320 }
321
322 void CppCheckSettingsDialog::OnClearListUI(wxUpdateUIEvent& e) { e.Enable(!m_listBoxExcludelist->IsEmpty()); }
323
324 void CppCheckSettingsDialog::OnRemoveSuppressionUI(wxUpdateUIEvent& e)
325 {
326 e.Enable(m_checkListSuppress->GetSelection() != wxNOT_FOUND);
327 }
328
329 void CppCheckSettingsDialog::OnSuppressTickAllUI(wxUpdateUIEvent& e)
330 {
331 for(size_t n = 0; n < m_checkListSuppress->GetCount(); ++n) {
332 if(!m_checkListSuppress->IsChecked(n)) {
333 e.Enable(true);
334 return;
335 }
336 }
337 e.Enable(false);
338 }
339
340 void CppCheckSettingsDialog::OnSuppressUntickAllUI(wxUpdateUIEvent& e)
341 {
342 for(size_t n = 0; n < m_checkListSuppress->GetCount(); ++n) {
343 if(m_checkListSuppress->IsChecked(n)) {
344 e.Enable(true);
345 return;
346 }
347 }
348 e.Enable(false);
349 }
350
351 void CppCheckSettingsDialog::OnIncludeDirsUpdateUI(wxUpdateUIEvent& event)
352 {
353 bool enable(false);
354 int index = m_checkListExtraWarnings->FindString(_("Missing includes"));
355 if(index != wxNOT_FOUND) {
356 enable = m_checkListExtraWarnings->IsChecked(index);
357 }
358 // Only enable the IncludeDirs panel if the "Missing includes" warning is enabled
359 event.Enable(enable);
360 }
361
362 void CppCheckSettingsDialog::OnRemoveIncludeDirUI(wxUpdateUIEvent& event)
363 {
364 event.Enable(m_listBoxIncludeDirs->GetSelection() != wxNOT_FOUND);
365 }
366
367 void CppCheckSettingsDialog::OnRemoveDefinitionUI(wxUpdateUIEvent& e)
368 {
369 e.Enable(m_listBoxDefinelist->GetSelection() != wxNOT_FOUND);
370 }
371
372 void CppCheckSettingsDialog::OnClearDefinitionsUI(wxUpdateUIEvent& e) { e.Enable(!m_listBoxDefinelist->IsEmpty()); }
373
374 void CppCheckSettingsDialog::OnRemoveUndefineUI(wxUpdateUIEvent& e)
375 {
376 e.Enable(m_listBoxUndefineList->GetSelection() != wxNOT_FOUND);
377 }
378
379 void CppCheckSettingsDialog::OnClearUndefinesUI(wxUpdateUIEvent& e) { e.Enable(!m_listBoxUndefineList->IsEmpty()); }
380
381 void CppCheckAddSuppressionDialog::OnOKButtonUpdateUI(wxUpdateUIEvent& e)
382 {
383 e.Enable(!GetKey()->IsEmpty() && !GetDescription()->IsEmpty());
384 }