Debian Codegraph @

cppchecker/cppcheck_settings.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : cppcheck_settings.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 <wx/xrc/xmlres.h>
27 #include "wx/checklst.h"
28 #include <memory>
29 #include <wx/stdpaths.h>
30 #include <wx/tokenzr.h>
31 #include "cppcheck_settings.h"
32 #include "plugin.h"
33 #include <stdio.h>
34 #include "workspace.h"
35
36 // Some macros needed
37 #ifdef __WXMSW__
38 #define PIPE_NAME "\\\\.\\pipe\\cppchecker_%s"
39 #else
40 #define PIPE_NAME "/tmp/cppchecker.%s.sock"
41 #endif
42
43 //------------------------------------------------------
44
45 const wxEventType wxEVT_CPPCHECKJOB_STATUS_MESSAGE = XRCID("cppcheck_status_message");
46 const wxEventType wxEVT_CPPCHECKJOB_CHECK_COMPLETED = XRCID("cppcheck_check_completed");
47 const wxEventType wxEVT_CPPCHECKJOB_REPORT = XRCID("cppcheck_report");
48
49 //------------------------------------------------------
50 CppCheckSettings::CppCheckSettings()
51 : m_Style(true)
52 , m_Performance(true)
53 , m_Portability(true)
54 , m_UnusedFunctions(true)
55 , m_MissingIncludes(true)
56 , m_Information(true)
57 , m_PosixStandards(true)
58 , m_C99Standards(true)
59 , m_Cpp11Standards(true)
60 , m_Force(false)
61 , m_Jobs(1)
62 , m_CheckConfig(false)
63 , m_saveSuppressedWarnings(false)
64 , m_SuppressSystemIncludes(false)
65 , m_saveIncludeDirs(false)
66 {
67 }
68
69 void CppCheckSettings::Serialize(Archive& arch)
70 {
71 arch.Write(wxT("option.style"), m_Style);
72 arch.Write(wxT("option.performance"), m_Performance);
73 arch.Write(wxT("option.portability"), m_Portability);
74 arch.Write(wxT("option.unusedFunctions"), m_UnusedFunctions);
75 arch.Write(wxT("option.missingIncludes"), m_MissingIncludes);
76 arch.Write(wxT("option.information"), m_Information);
77 arch.Write(wxT("option.posixStandards"), m_PosixStandards);
78 arch.Write(wxT("option.c99Standards"), m_C99Standards);
79 arch.Write(wxT("option.cpp11Standards"), m_Cpp11Standards);
80 arch.Write(wxT("option.force"), m_Force);
81 arch.Write(wxT("option.jobs"), m_Jobs);
82 arch.Write(wxT("m_excludeFiles"), m_excludeFiles);
83
84 if(m_saveSuppressedWarnings) {
85 arch.Write(wxT("SuppressedWarningsStrings0"), m_SuppressedWarnings0);
86 arch.Write(wxT("SuppressedWarningsStrings1"), m_SuppressedWarnings1);
87 // and cache the new values
88 m_SuppressedWarningsOrig0.clear();
89 m_SuppressedWarningsOrig1.clear();
90 m_SuppressedWarningsOrig0.insert(m_SuppressedWarnings0.begin(), m_SuppressedWarnings0.end());
91 m_SuppressedWarningsOrig1.insert(m_SuppressedWarnings1.begin(), m_SuppressedWarnings1.end());
92 } else {
93 // Saving nothing would lose the original values; so save the cached originals
94 arch.Write(wxT("SuppressedWarningsStrings0"), m_SuppressedWarningsOrig0);
95 arch.Write(wxT("SuppressedWarningsStrings1"), m_SuppressedWarningsOrig1);
96 }
97
98 if(m_saveIncludeDirs) {
99 arch.Write(wxT("ExtraIncludeDirs"), m_IncludeDirs);
100 arch.Write(wxT("SuppressSystemIncludes"), m_SuppressSystemIncludes);
101 }
102 }
103
104 void CppCheckSettings::DeSerialize(Archive& arch)
105 {
106 arch.Read(wxT("option.style"), m_Style);
107 arch.Read(wxT("option.performance"), m_Performance);
108 arch.Read(wxT("option.portability"), m_Portability);
109 arch.Read(wxT("option.unusedFunctions"), m_UnusedFunctions);
110 arch.Read(wxT("option.missingIncludes"), m_MissingIncludes);
111 arch.Read(wxT("option.information"), m_Information);
112 arch.Read(wxT("option.posixStandards"), m_PosixStandards);
113 arch.Read(wxT("option.c99Standards"), m_C99Standards);
114 arch.Read(wxT("option.cpp11Standards"), m_Cpp11Standards);
115 arch.Read(wxT("option.force"), m_Force);
116 arch.Read(wxT("option.jobs"), m_Jobs);
117
118 arch.Read(wxT("m_excludeFiles"), m_excludeFiles);
119
120 arch.Read(wxT("SuppressedWarningsStrings0"), m_SuppressedWarnings0); // Unchecked ones
121 arch.Read(wxT("SuppressedWarningsStrings1"), m_SuppressedWarnings1); // Checked ones
122
123 arch.Read(wxT("ExtraIncludeDirs"), m_IncludeDirs);
124 arch.Read(wxT("SuppressSystemIncludes"), m_SuppressSystemIncludes);
125 m_saveIncludeDirs = m_IncludeDirs.GetCount() > 0; // If there are saved dirs, this must have been set last time
126 }
127
128 void CppCheckSettings::SetDefaultSuppressedWarnings()
129 {
130 if(m_SuppressedWarnings0.empty() && m_SuppressedWarnings1.empty()) {
131 // The first time the Settings dlg is run, none will have been saved; so add the standard ones by hand,
132 // unchecked
133 m_SuppressedWarnings0.insert(std::pair<wxString, wxString>(wxT("cstyleCast"), wxT("C-style pointer casting")));
134 m_SuppressedWarnings0.insert(
135 std::pair<wxString, wxString>(wxT("uninitMemberVar"), wxT("...is not initialized in the constructor")));
136 m_SuppressedWarnings0.insert(
137 std::pair<wxString, wxString>(wxT("variableHidingEnum"), wxT("...hides enumerator with same name")));
138 m_SuppressedWarnings0.insert(
139 std::pair<wxString, wxString>(wxT("variableScope"), wxT("The scope of the variable...can be reduced")));
140 }
141
142 // Now duplicate them, so that the originals can be serialised if the user doesn't want to save any changes
143 // Done outside the 'if' statement, as we want it also to happen for deserialised data
144 m_SuppressedWarningsOrig0.clear();
145 m_SuppressedWarningsOrig1.clear();
146 m_SuppressedWarningsOrig0.insert(m_SuppressedWarnings0.begin(), m_SuppressedWarnings0.end());
147 m_SuppressedWarningsOrig1.insert(m_SuppressedWarnings1.begin(), m_SuppressedWarnings1.end());
148 }
149
150 void CppCheckSettings::SetSuppressedWarnings(wxCheckListBox* clb, const wxArrayString& keys)
151 {
152 wxCHECK_RET(clb->GetCount() == keys.GetCount(), wxT("Mismatched counts"));
153
154 m_SuppressedWarnings0.clear();
155 m_SuppressedWarnings1.clear();
156 for(size_t n = 0; n < clb->GetCount(); ++n) {
157 AddSuppressedWarning(keys.Item(n), clb->GetString(n), clb->IsChecked(n));
158 }
159 }
160
161 void CppCheckSettings::AddSuppressedWarning(const wxString& key, const wxString& label, bool checked)
162 {
163 if(checked) {
164 m_SuppressedWarnings1.insert(std::pair<wxString, wxString>(key, label));
165 } else {
166 m_SuppressedWarnings0.insert(std::pair<wxString, wxString>(key, label));
167 }
168 }
169
170 void CppCheckSettings::RemoveSuppressedWarning(const wxString& key)
171 {
172 m_SuppressedWarnings0.erase(key);
173 m_SuppressedWarnings1.erase(key);
174 }
175
176 wxString CppCheckSettings::GetOptions() const
177 {
178 wxString options;
179 if(GetStyle()) {
180 options << wxT(" --enable=style ");
181 }
182 if(GetPerformance()) {
183 options << wxT(" --enable=performance ");
184 }
185 if(GetPortability()) {
186 options << wxT(" --enable=portability ");
187 }
188 if(GetUnusedFunctions()) {
189 options << wxT(" --enable=unusedFunction ");
190 }
191 if(GetMissingIncludes()) {
192 options << wxT(" --enable=missingInclude ");
193 }
194 if(GetInformation()) {
195 options << wxT(" --enable=information ");
196 }
197 if(GetPosixStandards()) {
198 options << wxT(" --std=posix ");
199 }
200 if(GetC99Standards()) {
201 options << wxT(" --std=c99 ");
202 }
203 if(GetCpp11Standards()) {
204 options << wxT(" --std=c++11 ");
205 }
206 if(GetForce()) {
207 options << wxT("--force ");
208 }
209 if(GetJobs() > 1) {
210 options << wxT("-j") << GetJobs() << " ";
211 }
212 if(GetCheckConfig()) {
213 options << wxT("--check-config "); // Though this turns off other checks, afaict it does not harm to emit them
214 }
215
216 // Now add any ticked suppressedwarnings
217 wxStringMap_t::const_iterator iter = m_SuppressedWarnings1.begin();
218 for(; iter != m_SuppressedWarnings1.end(); ++iter) {
219 options << wxT(" --suppress=") << (*iter).first << " ";
220 }
221
222 // IncludeDirs
223 for(size_t n = 0; n < m_IncludeDirs.GetCount(); ++n) {
224 wxString item = m_IncludeDirs.Item(n);
225 item.Trim().Trim(false);
226 if(!item.empty()) {
227 options << " -I" << item << " ";
228 }
229 }
230 if(m_SuppressSystemIncludes) {
231 options << wxT(" --suppress=") << "missingIncludeSystem ";
232 }
233
234 // (Un)Definitions
235 for(size_t n = 0; n < m_definitions.GetCount(); ++n) {
236 wxString item = m_definitions.Item(n);
237 item.Trim().Trim(false);
238 if(!item.empty()) {
239 options << " -D" << item << " ";
240 }
241 }
242 for(size_t n = 0; n < m_undefines.GetCount(); ++n) {
243 wxString item = m_undefines.Item(n);
244 item.Trim().Trim(false);
245 if(!item.empty()) {
246 options << " -U" << item << " ";
247 }
248 }
249
250 options << wxT(" --template gcc ");
251 return options;
252 }
253
254 void CppCheckSettings::LoadProjectSpecificSettings(ProjectPtr project)
255 {
256 wxString rawData;
257 wxArrayString definitions, undefines;
258 if(project) {
259 rawData = project->GetPluginData("CppCheck");
260 }
261
262 wxArrayString configurations = wxStringTokenize(rawData, ";", wxTOKEN_RET_EMPTY_ALL);
263 if(configurations.GetCount() == 2) { // It'll either be empty or 2
264 if(!configurations.Item(0).empty()) {
265 definitions = wxStringTokenize(configurations.Item(0), ",");
266 }
267 if(!configurations.Item(1).empty()) {
268 undefines = wxStringTokenize(configurations.Item(1), ",");
269 }
270 }
271 // Note that we set these even if project == NULL, as this will clear any stale values
272 SetDefinitions(definitions);
273 SetUndefines(undefines);
274 }