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 302
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 The CodeLite Team
// file name : cppcheckreportpage.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 "cppcheckreportpage.h"
#include <wx/tokenzr.h>
#include "cppchecker.h"
#include "plugin.h"
#include <wx/regex.h>
#include <wx/log.h>
#include "event_notifier.h"
#include "lexer_configuration.h"
#include "editor_config.h"
static size_t sErrorCount(0);
#define CPPCHECK_ERROR_MARKER 3
#define CPPCHECK_ERROR_MARKER_CURRENT 4
CppCheckReportPage::CppCheckReportPage(wxWindow* parent, IManager* mgr, CppCheckPlugin* plugin)
: CppCheckReportBasePage(parent)
, m_mgr(mgr)
, m_plugin(plugin)
{
DoInitStyle();
EventNotifier::Get()->Connect(
wxEVT_CL_THEME_CHANGED, wxCommandEventHandler(CppCheckReportPage::OnThemeChanged), NULL, this);
}
CppCheckReportPage::~CppCheckReportPage()
{
EventNotifier::Get()->Disconnect(
wxEVT_CL_THEME_CHANGED, wxCommandEventHandler(CppCheckReportPage::OnThemeChanged), NULL, this);
}
void CppCheckReportPage::OnClearReportUI(wxUpdateUIEvent& event)
{
event.Enable(m_stc->GetLength() > 0 && !m_plugin->AnalysisInProgress());
}
void CppCheckReportPage::OnStopCheckingUI(wxUpdateUIEvent& event) { event.Enable(m_plugin->AnalysisInProgress()); }
void CppCheckReportPage::Clear()
{
m_stc->SetReadOnly(false);
m_stc->ClearAll();
m_stc->SetReadOnly(true);
m_mgr->SetStatusMessage("");
sErrorCount = 0;
}
void CppCheckReportPage::OnStopChecking(wxCommandEvent& event)
{
m_plugin->StopAnalysis();
m_mgr->SetStatusMessage("CppCheck Stopped");
}
void CppCheckReportPage::OnClearReport(wxCommandEvent& event) { Clear(); }
void CppCheckReportPage::AppendLine(const wxString& line)
{
wxString tmpLine(line);
// Locate status messages:
// 6/7 files checked 85% done
static wxRegEx reProgress(wxT("([0-9]+)/([0-9]+)( files checked )([0-9]+%)( done)"));
static wxRegEx reFileName(wxT("(Checking )([a-zA-Z:]{0,2}[ a-zA-Z\\.0-9_/\\+\\-]+ *)"));
// Locate the progress messages and update our progress bar
wxArrayString arrLines = wxStringTokenize(tmpLine, wxT("\n\r"), wxTOKEN_STRTOK);
for(size_t i = 0; i < arrLines.GetCount(); i++) {
if(reProgress.Matches(arrLines.Item(i))) {
// Get the current progress
wxString currentLine = reProgress.GetMatch(arrLines.Item(i), 1);
long fileNo(0);
currentLine.ToLong(&fileNo);
}
if(reFileName.Matches(arrLines.Item(i))) {
// Get the file name
wxString filename = reFileName.GetMatch(arrLines.Item(i), 2);
m_mgr->SetStatusMessage("CppCheck: checking file " + filename);
}
}
// Remove progress messages from the printed output
reProgress.ReplaceAll(&tmpLine, wxEmptyString);
tmpLine.Replace(wxT("\r"), wxT(""));
tmpLine.Replace(wxT("\n\n"), wxT("\n"));
m_stc->SetReadOnly(false);
m_stc->AppendText(tmpLine);
m_stc->SetReadOnly(true);
m_stc->ScrollToLine(m_stc->GetLineCount() - 1);
}
// Lexing function
// int CppCheckReportPage::ColorLine ( int, const char *text, size_t &start, size_t &len )
//{
// wxString txt(text, wxConvUTF8);
//
// if(txt.StartsWith(wxT("Checking "))) {
// return wxSTC_LEX_GCC_MAKE_ENTER;
// }
//
// static wxRegEx gccPattern(wxT("^([^ ][a-zA-Z:]{0,2}[ a-zA-Z\\.0-9_/\\+\\-]+ *)(:)([0-9]*)(:)([a-zA-Z ]*)"));
//
// static int fileIndex = 1;
//// static int lineIndex = 3;
// static int severityIndex = 4;
//
// if(gccPattern.Matches(txt)) {
// wxString severity = gccPattern.GetMatch(txt, severityIndex);
// gccPattern.GetMatch(&start, &len, fileIndex);
//
// sErrorCount ++;
//
// if(severity == wxT("error")) {
// return wxSTC_LEX_GCC_ERROR;
// } else {
// return wxSTC_LEX_GCC_WARNING;
// }
// }
//
// // file: line: severity
// return wxSTC_LEX_GCC_DEFAULT;
//}
//
void CppCheckReportPage::OnOpenFile(wxStyledTextEvent& e)
{
static wxRegEx gccPattern(wxT("^([^ ][a-zA-Z:]{0,2}[ a-zA-Z\\.0-9_/\\+\\-]+ *)(:)([0-9]*)(:)([a-zA-Z ]*)"));
static int fileIndex = 1;
static int lineIndex = 3;
wxString txt = m_stc->GetLine(m_stc->LineFromPosition(e.GetPosition()));
if(gccPattern.Matches(txt)) {
wxString file = gccPattern.GetMatch(txt, fileIndex);
wxString lineNumber = gccPattern.GetMatch(txt, lineIndex);
if(file.IsEmpty() == false) {
long n(0);
lineNumber.ToLong(&n);
// Zero based line number
if(n)
n--;
m_mgr->OpenFile(file, wxEmptyString, n);
}
}
}
size_t CppCheckReportPage::GetErrorCount() const { return sErrorCount; }
void CppCheckReportPage::PrintStatusMessage()
{
wxString statusLine;
sErrorCount = 0;
wxString text = m_stc->GetText();
wxArrayString lines = ::wxStringTokenize(text, "\n", wxTOKEN_RET_EMPTY_ALL);
static wxRegEx gccPattern(wxT("^([^ ][a-zA-Z:]{0,2}[ a-zA-Z\\.0-9_/\\+\\-]+ *)(:)([0-9]*)(:)([a-zA-Z ]*)"));
for(size_t i = 0; i < lines.GetCount(); ++i) {
if(gccPattern.Matches(lines.Item(i))) {
m_stc->MarkerAdd(i, CPPCHECK_ERROR_MARKER);
++sErrorCount;
}
}
statusLine << wxT("===== ");
statusLine << _("cppcheck analysis ended. Found ") << sErrorCount << _(" possible errors");
statusLine << wxT("=====");
AppendLine(statusLine);
SetMessage(_("Done"));
}
bool CppCheckReportPage::FindPrevMarker(bool gotoMatch)
{
int mask = (1 << CPPCHECK_ERROR_MARKER);
int nPos = m_stc->GetCurrentPos();
int nLine = m_stc->LineFromPosition(nPos);
int nFoundLine = m_stc->MarkerPrevious(nLine - 1, mask);
if(nFoundLine == wxNOT_FOUND) {
return false;
} else {
if(!gotoMatch) {
return true;
}
int matchPos = m_stc->PositionFromLine(nFoundLine);
m_stc->SetCurrentPos(matchPos);
}
m_stc->SetFirstVisibleLine(nFoundLine);
m_stc->MarkerDeleteAll(CPPCHECK_ERROR_MARKER_CURRENT);
m_stc->MarkerAdd(nFoundLine, CPPCHECK_ERROR_MARKER_CURRENT);
return true;
}
bool CppCheckReportPage::FindNextMarker(bool gotoMatch)
{
int markerMask = (1 << CPPCHECK_ERROR_MARKER);
int nPos = m_stc->GetCurrentPos();
int nLine = m_stc->LineFromPosition(nPos);
int nFoundLine = m_stc->MarkerNext(nLine + 1, markerMask);
if(nFoundLine == wxNOT_FOUND) {
return false;
} else {
if(!gotoMatch) {
return true;
}
int matchPos = m_stc->PositionFromLine(nFoundLine);
m_stc->SetCurrentPos(matchPos);
}
m_stc->SetFirstVisibleLine(nFoundLine);
m_stc->MarkerDeleteAll(CPPCHECK_ERROR_MARKER_CURRENT);
m_stc->MarkerAdd(nFoundLine, CPPCHECK_ERROR_MARKER_CURRENT);
return true;
}
void CppCheckReportPage::SetGaugeRange(int range) { wxUnusedVar(range); }
void CppCheckReportPage::SetMessage(const wxString& msg) { m_mgr->SetStatusMessage(msg); }
void CppCheckReportPage::DoInitStyle()
{
m_stc->SetReadOnly(true);
m_stc->MarkerDefine(CPPCHECK_ERROR_MARKER, wxSTC_MARK_ARROW, *wxRED, *wxRED);
m_stc->MarkerDefine(CPPCHECK_ERROR_MARKER_CURRENT, wxSTC_MARK_BACKGROUND, "PINK", "PINK");
m_stc->MarkerSetAlpha(CPPCHECK_ERROR_MARKER_CURRENT, 70);
LexerConf::Ptr_t config = EditorConfigST::Get()->GetLexer("text");
if(config) {
config->Apply(m_stc, true);
m_stc->HideSelection(true);
} else {
// Initialize the output text style
m_stc->SetLexer(wxSTC_LEX_NULL);
m_stc->StyleClearAll();
m_stc->HideSelection(true);
// Use font
for(int i = 0; i <= wxSTC_STYLE_DEFAULT; i++) {
wxFont defFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
defFont.SetFamily(wxFONTFAMILY_TELETYPE);
m_stc->StyleSetBackground(i, DrawingUtils::GetOutputPaneBgColour());
m_stc->StyleSetForeground(i, DrawingUtils::GetOutputPaneFgColour());
m_stc->StyleSetFont(i, defFont);
}
}
}
void CppCheckReportPage::OnThemeChanged(wxCommandEvent& e)
{
e.Skip();
DoInitStyle();
}
void CppCheckReportPage::OnStyleNeeded(wxStyledTextEvent& event) {}
void CppCheckReportPage::OnDown(wxCommandEvent& event)
{
wxUnusedVar(event);
FindNextMarker();
}
void CppCheckReportPage::OnUp(wxCommandEvent& event)
{
wxUnusedVar(event);
FindPrevMarker();
}
void CppCheckReportPage::OnDownUI(wxUpdateUIEvent& event) { event.Enable(FindNextMarker(false)); }
void CppCheckReportPage::OnUpUI(wxUpdateUIEvent& event) { event.Enable(FindPrevMarker(false)); }
void CppCheckReportPage::GotoFirstError() { FindNextMarker(true); }
|