Debian Codegraph @

cppchecker/cppcheckreportpage.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : cppcheckreportpage.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 "cppcheckreportpage.h"
27
28 #include "cppchecker.h"
29 #include "editor_config.h"
30 #include "event_notifier.h"
31 #include "lexer_configuration.h"
32 #include "plugin.h"
33
34 #include <wx/log.h>
35 #include <wx/regex.h>
36 #include <wx/tokenzr.h>
37
38 static size_t sErrorCount(0);
39
40 #define CPPCHECK_ERROR_MARKER 3
41 #define CPPCHECK_ERROR_MARKER_CURRENT 4
42
43 CppCheckReportPage::CppCheckReportPage(wxWindow* parent, IManager* mgr, CppCheckPlugin* plugin)
44 : CppCheckReportBasePage(parent)
45 , m_mgr(mgr)
46 , m_plugin(plugin)
47 {
48 DoInitStyle();
49 EventNotifier::Get()->Connect(wxEVT_CL_THEME_CHANGED, wxCommandEventHandler(CppCheckReportPage::OnThemeChanged),
50 NULL, this);
51 }
52
53 CppCheckReportPage::~CppCheckReportPage()
54 {
55 EventNotifier::Get()->Disconnect(wxEVT_CL_THEME_CHANGED, wxCommandEventHandler(CppCheckReportPage::OnThemeChanged),
56 NULL, this);
57 }
58
59 void CppCheckReportPage::OnClearReportUI(wxUpdateUIEvent& event)
60 {
61 event.Enable(m_stc->GetLength() > 0 && !m_plugin->AnalysisInProgress());
62 }
63
64 void CppCheckReportPage::OnStopCheckingUI(wxUpdateUIEvent& event) { event.Enable(m_plugin->AnalysisInProgress()); }
65
66 void CppCheckReportPage::Clear()
67 {
68 m_stc->SetReadOnly(false);
69 m_stc->ClearAll();
70 m_stc->SetReadOnly(true);
71
72 m_mgr->SetStatusMessage("");
73 sErrorCount = 0;
74 }
75
76 void CppCheckReportPage::OnStopChecking(wxCommandEvent& event)
77 {
78 m_plugin->StopAnalysis();
79 m_mgr->SetStatusMessage("CppCheck Stopped");
80 }
81
82 void CppCheckReportPage::OnClearReport(wxCommandEvent& event) { Clear(); }
83
84 void CppCheckReportPage::AppendLine(const wxString& line)
85 {
86 wxString tmpLine(line);
87
88 // Locate status messages:
89 // 6/7 files checked 85% done
90 static wxRegEx reProgress(wxT("([0-9]+)/([0-9]+)( files checked )([0-9]+%)( done)"));
91 static wxRegEx reFileName(wxT("(Checking )([a-zA-Z:]{0,2}[ a-zA-Z\\.0-9_/\\+\\-]+ *)"));
92
93 // Locate the progress messages and update our progress bar
94 wxArrayString arrLines = wxStringTokenize(tmpLine, wxT("\n\r"), wxTOKEN_STRTOK);
95 for(size_t i = 0; i < arrLines.GetCount(); i++) {
96
97 if(reProgress.Matches(arrLines.Item(i))) {
98
99 // Get the current progress
100 wxString currentLine = reProgress.GetMatch(arrLines.Item(i), 1);
101
102 long fileNo(0);
103 currentLine.ToLong(&fileNo);
104 }
105
106 if(reFileName.Matches(arrLines.Item(i))) {
107
108 // Get the file name
109 wxString filename = reFileName.GetMatch(arrLines.Item(i), 2);
110 m_mgr->SetStatusMessage("CppCheck: checking file " + filename);
111 }
112 }
113
114 // Remove progress messages from the printed output
115 reProgress.ReplaceAll(&tmpLine, wxEmptyString);
116 tmpLine.Replace(wxT("\r"), wxT(""));
117 tmpLine.Replace(wxT("\n\n"), wxT("\n"));
118
119 m_stc->SetReadOnly(false);
120 m_stc->AppendText(tmpLine);
121 m_stc->SetReadOnly(true);
122
123 m_stc->ScrollToLine(m_stc->GetLineCount() - 1);
124 }
125
126 // Lexing function
127 // int CppCheckReportPage::ColorLine ( int, const char *text, size_t &start, size_t &len )
128 //{
129 // wxString txt(text, wxConvUTF8);
130 //
131 // if(txt.StartsWith(wxT("Checking "))) {
132 // return wxSTC_LEX_GCC_MAKE_ENTER;
133 // }
134 //
135 // static wxRegEx gccPattern(wxT("^([^ ][a-zA-Z:]{0,2}[ a-zA-Z\\.0-9_/\\+\\-]+ *)(:)([0-9]*)(:)([a-zA-Z ]*)"));
136 //
137 // static int fileIndex = 1;
138 //// static int lineIndex = 3;
139 // static int severityIndex = 4;
140 //
141 // if(gccPattern.Matches(txt)) {
142 // wxString severity = gccPattern.GetMatch(txt, severityIndex);
143 // gccPattern.GetMatch(&start, &len, fileIndex);
144 //
145 // sErrorCount ++;
146 //
147 // if(severity == wxT("error")) {
148 // return wxSTC_LEX_GCC_ERROR;
149 // } else {
150 // return wxSTC_LEX_GCC_WARNING;
151 // }
152 // }
153 //
154 // // file: line: severity
155 // return wxSTC_LEX_GCC_DEFAULT;
156 //}
157 //
158 void CppCheckReportPage::OnOpenFile(wxStyledTextEvent& e) { DoOpenLine(m_stc->LineFromPosition(e.GetPosition())); }
159
160 size_t CppCheckReportPage::GetErrorCount() const { return sErrorCount; }
161
162 void CppCheckReportPage::PrintStatusMessage()
163 {
164 wxString statusLine;
165 sErrorCount = 0;
166 wxString text = m_stc->GetText();
167 wxArrayString lines = ::wxStringTokenize(text, "\n", wxTOKEN_RET_EMPTY_ALL);
168 static wxRegEx gccPattern(wxT("^([^ ][a-zA-Z:]{0,2}[ a-zA-Z\\.0-9_/\\+\\-]+ *)(:)([0-9]*)(:)([a-zA-Z ]*)"));
169 for(size_t i = 0; i < lines.GetCount(); ++i) {
170 if(gccPattern.Matches(lines.Item(i))) {
171 m_stc->MarkerAdd(i, CPPCHECK_ERROR_MARKER);
172 ++sErrorCount;
173 }
174 }
175
176 statusLine << wxT("===== ");
177 statusLine << _("cppcheck analysis ended. Found ") << sErrorCount << _(" possible errors");
178 statusLine << wxT("=====");
179
180 AppendLine(statusLine);
181 SetMessage(_("Done"));
182 }
183
184 bool CppCheckReportPage::FindPrevMarker(bool gotoMatch)
185 {
186 int mask = (1 << CPPCHECK_ERROR_MARKER);
187 int nPos = m_stc->GetCurrentPos();
188 int nLine = m_stc->LineFromPosition(nPos);
189 int nFoundLine = m_stc->MarkerPrevious(nLine - 1, mask);
190 if(nFoundLine == wxNOT_FOUND) {
191 return false;
192 } else {
193 if(!gotoMatch) {
194 return true;
195 }
196 int matchPos = m_stc->PositionFromLine(nFoundLine);
197 m_stc->SetCurrentPos(matchPos);
198 }
199
200 m_stc->SetFirstVisibleLine(nFoundLine);
201 m_stc->MarkerDeleteAll(CPPCHECK_ERROR_MARKER_CURRENT);
202 m_stc->MarkerAdd(nFoundLine, CPPCHECK_ERROR_MARKER_CURRENT);
203 DoOpenLine(nFoundLine);
204 return true;
205 }
206
207 bool CppCheckReportPage::FindNextMarker(bool gotoMatch)
208 {
209 int markerMask = (1 << CPPCHECK_ERROR_MARKER);
210 int nPos = m_stc->GetCurrentPos();
211 int nLine = m_stc->LineFromPosition(nPos);
212 int nFoundLine = m_stc->MarkerNext(nLine + 1, markerMask);
213 if(nFoundLine == wxNOT_FOUND) {
214 return false;
215 } else {
216 if(!gotoMatch) {
217 return true;
218 }
219 int matchPos = m_stc->PositionFromLine(nFoundLine);
220 m_stc->SetCurrentPos(matchPos);
221 }
222
223 m_stc->SetFirstVisibleLine(nFoundLine);
224 m_stc->MarkerDeleteAll(CPPCHECK_ERROR_MARKER_CURRENT);
225 m_stc->MarkerAdd(nFoundLine, CPPCHECK_ERROR_MARKER_CURRENT);
226 DoOpenLine(nFoundLine);
227 return true;
228 }
229
230 void CppCheckReportPage::SetGaugeRange(int range) { wxUnusedVar(range); }
231
232 void CppCheckReportPage::SetMessage(const wxString& msg) { m_mgr->SetStatusMessage(msg); }
233
234 void CppCheckReportPage::DoInitStyle()
235 {
236 m_stc->SetReadOnly(true);
237 m_stc->MarkerDefine(CPPCHECK_ERROR_MARKER, wxSTC_MARK_ARROW, *wxRED, *wxRED);
238 m_stc->MarkerDefine(CPPCHECK_ERROR_MARKER_CURRENT, wxSTC_MARK_BACKGROUND, "PINK", "PINK");
239 m_stc->MarkerSetAlpha(CPPCHECK_ERROR_MARKER_CURRENT, 70);
240
241 LexerConf::Ptr_t config = EditorConfigST::Get()->GetLexer("text");
242 if(config) {
243 config->Apply(m_stc, true);
244 m_stc->HideSelection(true);
245
246 } else {
247 // Initialize the output text style
248 m_stc->SetLexer(wxSTC_LEX_NULL);
249 m_stc->StyleClearAll();
250 m_stc->HideSelection(true);
251
252 // Use font
253 for(int i = 0; i <= wxSTC_STYLE_DEFAULT; i++) {
254 wxFont defFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
255 defFont.SetFamily(wxFONTFAMILY_TELETYPE);
256 m_stc->StyleSetBackground(i, DrawingUtils::GetOutputPaneBgColour());
257 m_stc->StyleSetForeground(i, DrawingUtils::GetOutputPaneFgColour());
258 m_stc->StyleSetFont(i, defFont);
259 }
260 }
261 }
262
263 void CppCheckReportPage::OnThemeChanged(wxCommandEvent& e)
264 {
265 e.Skip();
266 DoInitStyle();
267 }
268
269 void CppCheckReportPage::OnStyleNeeded(wxStyledTextEvent& event) {}
270
271 void CppCheckReportPage::OnDown(wxCommandEvent& event)
272 {
273 wxUnusedVar(event);
274 FindNextMarker();
275 }
276
277 void CppCheckReportPage::OnUp(wxCommandEvent& event)
278 {
279 wxUnusedVar(event);
280 FindPrevMarker();
281 }
282 void CppCheckReportPage::OnDownUI(wxUpdateUIEvent& event) { event.Enable(FindNextMarker(false)); }
283 void CppCheckReportPage::OnUpUI(wxUpdateUIEvent& event) { event.Enable(FindPrevMarker(false)); }
284 void CppCheckReportPage::GotoFirstError() { FindNextMarker(true); }
285
286 void CppCheckReportPage::DoOpenLine(int outputLine)
287 {
288 static wxRegEx gccPattern(wxT("^([^ ][a-zA-Z:]{0,2}[ a-zA-Z\\.0-9_/\\+\\-]+ *)(:)([0-9]*)(:)([a-zA-Z ]*)"));
289 static int fileIndex = 1;
290 static int lineIndex = 3;
291
292 wxString txt = m_stc->GetLine(outputLine);
293
294 if(gccPattern.Matches(txt)) {
295 wxString file = gccPattern.GetMatch(txt, fileIndex);
296 wxString lineNumber = gccPattern.GetMatch(txt, lineIndex);
297
298 if(file.IsEmpty() == false) {
299 long n(0);
300 lineNumber.ToCLong(&n);
301
302 // Zero based line number
303 if(n)
304 n--;
305 m_mgr->OpenFile(file, wxEmptyString, n);
306 }
307 }
308 }