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 303 304 305 306 307 308 309
|
/* This file is part of KDevelop
Copyright 2013 Christoph Thielecke <crissi99@gmx.de>
Copyright 2016 Anton Anikin <anton.anikin@htower.ru>
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "parser.h"
#include "debug.h"
#include <interfaces/icore.h>
#include <interfaces/iuicontroller.h>
#include <shell/problem.h>
#include <sublime/message.h>
#include <language/editor/documentrange.h>
// KF
#include <KLocalizedString>
// Qt
#include <QApplication>
#include <QFile>
namespace cppcheck
{
/**
* Convert the value of \<verbose\> attribute of \<error\> element from cppcheck's
* XML-output to 'good-looking' HTML-version. This is necessary because the
* displaying of the original message is performed without line breaks - such
* tooltips are uncomfortable to read, and large messages will not fit into the
* screen.
*
* This function put the original message into \<html\> tag that automatically
* provides line wrapping by builtin capabilities of Qt library. The source text
* also can contain tokens '\012' (line break) - they are present in the case of
* source code examples. In such cases, the entire text between the first and
* last tokens (i.e. source code) is placed into \<pre\> tag.
*
* @param[in] input the original value of \<verbose\> attribute
* @return HTML version for displaying in problem's tooltip
*/
QString verboseMessageToHtml( const QString & input )
{
QString output(QStringLiteral("<html>%1</html>").arg(input.toHtmlEscaped()));
output.replace(QLatin1String("\\012"), QLatin1String("\n"));
if (output.count(QLatin1Char('\n')) >= 2) {
output.replace(output.indexOf(QLatin1Char('\n')), 1, QStringLiteral("<pre>") );
output.replace(output.lastIndexOf(QLatin1Char('\n')), 1, QStringLiteral("</pre><br>") );
}
return output;
}
CppcheckParser::CppcheckParser()
{
}
CppcheckParser::~CppcheckParser()
{
}
void CppcheckParser::clear()
{
m_stateStack.clear();
}
bool CppcheckParser::startElement()
{
State newState = Unknown;
qCDebug(KDEV_CPPCHECK) << "CppcheckParser::startElement: elem: " << qPrintable(name().toString());
if (name() == QLatin1String("results")) {
newState = Results;
}
else if (name() == QLatin1String("cppcheck")) {
newState = CppCheck;
}
else if (name() == QLatin1String("errors")) {
newState = Errors;
}
else if (name() == QLatin1String("location")) {
newState = Location;
if (attributes().hasAttribute(QStringLiteral("file")) && attributes().hasAttribute(QStringLiteral("line"))) {
QString errorFile = attributes().value(QStringLiteral("file")).toString();
// Usually when "file0" attribute exists it associated with source and
// attribute "file" associated with header).
// But sometimes cppcheck produces errors with "file" and "file0" attributes
// both associated with same *source* file. In such cases attribute "file" contains
// only file name, without full path. Therefore we should use "file0" instead "file".
if (!QFile::exists(errorFile) && attributes().hasAttribute(QStringLiteral("file0"))) {
errorFile = attributes().value(QStringLiteral("file0")).toString();
}
m_errorFiles += errorFile;
m_errorLines += attributes().value(QStringLiteral("line")).toString().toInt();
}
}
else if (name() == QLatin1String("error")) {
newState = Error;
m_errorSeverity = QStringLiteral("unknown");
m_errorInconclusive = false;
m_errorFiles.clear();
m_errorLines.clear();
m_errorMessage.clear();
m_errorVerboseMessage.clear();
if (attributes().hasAttribute(QStringLiteral("msg"))) {
m_errorMessage = attributes().value(QStringLiteral("msg")).toString();
}
if (attributes().hasAttribute(QStringLiteral("verbose"))) {
m_errorVerboseMessage = verboseMessageToHtml(attributes().value(QStringLiteral("verbose")).toString());
}
if (attributes().hasAttribute(QStringLiteral("severity"))) {
m_errorSeverity = attributes().value(QStringLiteral("severity")).toString();
}
if (attributes().hasAttribute(QStringLiteral("inconclusive"))) {
m_errorInconclusive = true;
}
}
else {
m_stateStack.push(m_stateStack.top());
return true;
}
m_stateStack.push(newState);
return true;
}
bool CppcheckParser::endElement(QVector<KDevelop::IProblem::Ptr>& problems)
{
qCDebug(KDEV_CPPCHECK) << "CppcheckParser::endElement: elem: " << qPrintable(name().toString());
State state = m_stateStack.pop();
switch (state) {
case CppCheck:
if (attributes().hasAttribute(QStringLiteral("version"))) {
qCDebug(KDEV_CPPCHECK) << "Cppcheck report version: " << attributes().value(QStringLiteral("version"));
}
break;
case Errors:
// errors finished
break;
case Error:
qCDebug(KDEV_CPPCHECK) << "CppcheckParser::endElement: new error elem: line: "
<< (m_errorLines.isEmpty() ? QStringLiteral("?") : QString::number(m_errorLines.first()))
<< " at " << (m_errorFiles.isEmpty() ? QStringLiteral("?") : m_errorFiles.first())
<< ", msg: " << m_errorMessage;
storeError(problems);
break;
case Results:
// results finished
break;
case Location:
break;
default:
break;
}
return true;
}
QVector<KDevelop::IProblem::Ptr> CppcheckParser::parse()
{
QVector<KDevelop::IProblem::Ptr> problems;
qCDebug(KDEV_CPPCHECK) << "CppcheckParser::parse!";
while (!atEnd()) {
int readNextVal = readNext();
switch (readNextVal) {
case StartDocument:
clear();
break;
case StartElement:
startElement();
break;
case EndElement:
endElement(problems);
break;
case Characters:
break;
default:
qCDebug(KDEV_CPPCHECK) << "CppcheckParser::startElement: case: " << readNextVal;
break;
}
}
qCDebug(KDEV_CPPCHECK) << "CppcheckParser::parse: end";
if (hasError()) {
switch (error()) {
case CustomError:
case UnexpectedElementError:
case NotWellFormedError: {
const QString messageText =
i18n("Cppcheck XML Parsing: error at line %1, column %2: %3", lineNumber(), columnNumber(), errorString());
auto* message = new Sublime::Message(messageText, Sublime::Message::Error);
KDevelop::ICore::self()->uiController()->postMessage(message);
break;
}
case NoError:
case PrematureEndOfDocumentError:
break;
}
}
return problems;
}
void CppcheckParser::storeError(QVector<KDevelop::IProblem::Ptr>& problems)
{
// Construct problem with using first location element
KDevelop::IProblem::Ptr problem = getProblem();
// Adds other <location> elements as diagnostics.
// This allows the user to track the problem.
for (int locationIdx = 1; locationIdx < m_errorFiles.size(); ++locationIdx) {
problem->addDiagnostic(getProblem(locationIdx));
}
problems.push_back(problem);
}
KDevelop::IProblem::Ptr CppcheckParser::getProblem(int locationIdx) const
{
KDevelop::IProblem::Ptr problem(new KDevelop::DetectedProblem(i18n("Cppcheck")));
QStringList messagePrefix;
QString errorMessage(m_errorMessage);
if (m_errorSeverity == QLatin1String("error")) {
problem->setSeverity(KDevelop::IProblem::Error);
}
else if (m_errorSeverity == QLatin1String("warning")) {
problem->setSeverity(KDevelop::IProblem::Warning);
}
else {
problem->setSeverity(KDevelop::IProblem::Hint);
messagePrefix.push_back(m_errorSeverity);
}
if (m_errorInconclusive) {
messagePrefix.push_back(QStringLiteral("inconclusive"));
}
if (!messagePrefix.isEmpty()) {
errorMessage = QStringLiteral("(%1) %2").arg(messagePrefix.join(QLatin1String(", ")), m_errorMessage);
}
problem->setDescription(errorMessage);
problem->setExplanation(m_errorVerboseMessage);
KDevelop::DocumentRange range;
if (locationIdx < 0 || locationIdx >= m_errorFiles.size()) {
range = KDevelop::DocumentRange::invalid();
} else {
range.document = KDevelop::IndexedString(m_errorFiles.at(locationIdx));
range.setBothLines(m_errorLines.at(locationIdx) - 1);
range.setBothColumns(0);
}
problem->setFinalLocation(range);
problem->setFinalLocationMode(KDevelop::IProblem::TrimmedLine);
return problem;
}
}
|