Commit 4bc61eca authored by Andre Hartmann's avatar Andre Hartmann Committed by André Hartmann

Custom Error Parser

Allow setting the following items from outside:
* capture regular expression,
* file name, line number and message capture position and
* whether to parse stdout, stderr or both

The parser functions can be unit-tested by running (Debug build of Qt
Creator needed):

qtcreator -test ProjectExplorer,testCustomOutputParsers

The data is passed to the custom parser in
CustomToolChain::outputParser().

The parser information is stored in toolchains.xml together with the
custom toolchain. A configuration widget is provided to set up and test
the regular expression against a sample error message.

Change-Id: I6191df3c44432943e0aeb16c48d8e79d35845d2e
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent 9249175a
/****************************************************************************
**
** Copyright (C) 2013 Andre Hartmann.
** Contact: aha_1980@gmx.de
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "customparser.h"
#include "task.h"
#include "projectexplorerconstants.h"
#include <utils/qtcassert.h>
#include <QString>
using namespace ProjectExplorer;
CustomParserSettings::CustomParserSettings() :
fileNameCap(1),
lineNumberCap(2),
messageCap(3)
{ }
bool CustomParserSettings::operator !=(const CustomParserSettings &other) const
{
if (errorPattern == other.errorPattern)
return false;
if (fileNameCap == other.fileNameCap)
return false;
if (lineNumberCap == other.lineNumberCap)
return false;
if (messageCap == other.messageCap)
return false;
return true;
}
CustomParser::CustomParser(const CustomParserSettings &settings) :
m_parserChannels(ParseBothChannels)
{
setObjectName(QLatin1String("CustomParser"));
setSettings(settings);
}
CustomParser::~CustomParser()
{
}
void CustomParser::setErrorPattern(const QString &errorPattern)
{
m_errorRegExp.setPattern(errorPattern);
m_errorRegExp.setMinimal(true);
QTC_CHECK(m_errorRegExp.isValid());
}
QString CustomParser::errorPattern() const
{
return m_errorRegExp.pattern();
}
int CustomParser::lineNumberCap() const
{
return m_lineNumberCap;
}
void CustomParser::setLineNumberCap(int lineNumberCap)
{
m_lineNumberCap = lineNumberCap;
}
int CustomParser::fileNameCap() const
{
return m_fileNameCap;
}
void CustomParser::setFileNameCap(int fileNameCap)
{
m_fileNameCap = fileNameCap;
}
int CustomParser::messageCap() const
{
return m_messageCap;
}
void CustomParser::setMessageCap(int messageCap)
{
m_messageCap = messageCap;
}
void CustomParser::stdError(const QString &line)
{
if (m_parserChannels & ParseStdErrChannel)
if (parseLine(line))
return;
IOutputParser::stdError(line);
}
void CustomParser::stdOutput(const QString &line)
{
if (m_parserChannels & ParseStdOutChannel)
if (parseLine(line))
return;
IOutputParser::stdOutput(line);
}
void CustomParser::setSettings(const CustomParserSettings &settings)
{
setErrorPattern(settings.errorPattern);
setFileNameCap(settings.fileNameCap);
setLineNumberCap(settings.lineNumberCap);
setMessageCap(settings.messageCap);
}
bool CustomParser::parseLine(const QString &rawLine)
{
if (m_errorRegExp.isEmpty())
return false;
if (m_errorRegExp.indexIn(rawLine.trimmed()) == -1)
return false;
const Utils::FileName fileName =
Utils::FileName::fromUserInput(m_errorRegExp.cap(m_fileNameCap));
const int lineNumber = m_errorRegExp.cap(m_lineNumberCap).toInt();
const QString message = m_errorRegExp.cap(m_messageCap);
emit addTask(Task(Task::Error, message, fileName, lineNumber, Constants::TASK_CATEGORY_COMPILE));
return true;
}
// Unit tests:
#ifdef WITH_TESTS
# include <QTest>
# include "projectexplorer.h"
# include "metatypedeclarations.h"
# include "outputparser_test.h"
using namespace Utils;
void ProjectExplorerPlugin::testCustomOutputParsers_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<OutputParserTester::Channel>("inputChannel");
QTest::addColumn<QString>("pattern");
QTest::addColumn<int>("fileNameCap");
QTest::addColumn<int>("lineNumberCap");
QTest::addColumn<int>("messageCap");
QTest::addColumn<QString>("childStdOutLines");
QTest::addColumn<QString>("childStdErrLines");
QTest::addColumn<QList<ProjectExplorer::Task> >("tasks");
QTest::addColumn<QString>("outputLines");
const Core::Id categoryCompile = Core::Id(Constants::TASK_CATEGORY_COMPILE);
const QString simplePattern = QLatin1String("^([a-z]+\\.[a-z]+):(\\d+): error: ([^\\s].+)$");
const Utils::FileName fileName = Utils::FileName::fromUserInput(QLatin1String("main.c"));
QTest::newRow("empty pattern")
<< QString::fromLatin1("Sometext")
<< OutputParserTester::STDOUT
<< QString::fromLatin1("")
<< 1 << 2 << 3
<< QString::fromLatin1("Sometext\n") << QString()
<< QList<ProjectExplorer::Task>()
<< QString();
QTest::newRow("pass-through stdout")
<< QString::fromLatin1("Sometext")
<< OutputParserTester::STDOUT
<< simplePattern
<< 1 << 2 << 3
<< QString::fromLatin1("Sometext\n") << QString()
<< QList<ProjectExplorer::Task>()
<< QString();
QTest::newRow("pass-through stderr")
<< QString::fromLatin1("Sometext")
<< OutputParserTester::STDERR
<< simplePattern
<< 1 << 2 << 3
<< QString() << QString::fromLatin1("Sometext\n")
<< QList<ProjectExplorer::Task>()
<< QString();
const QString simpleError = QLatin1String("main.c:9: error: `sfasdf' undeclared (first use this function)");
const QString message = QLatin1String("`sfasdf' undeclared (first use this function)");
QTest::newRow("simple error")
<< simpleError
<< OutputParserTester::STDERR
<< simplePattern
<< 1 << 2 << 3
<< QString() << QString()
<< (QList<ProjectExplorer::Task>()
<< Task(Task::Error, message, fileName, 9, categoryCompile)
)
<< QString();
const QString simpleError2 = QLatin1String("Error: main.c:19: `sfasdf' undeclared (first use this function)");
const QString simplePattern2 = QLatin1String("^Error: ([a-z]+\\.[a-z]+):(\\d+): ([^\\s].+)$");
const int lineNumber2 = 19;
QTest::newRow("another simple error on stderr")
<< simpleError2
<< OutputParserTester::STDERR
<< simplePattern2
<< 1 << 2 << 3
<< QString() << QString()
<< (QList<ProjectExplorer::Task>()
<< Task(Task::Error, message, fileName, lineNumber2, categoryCompile)
)
<< QString();
QTest::newRow("another simple error on stdout")
<< simpleError2
<< OutputParserTester::STDOUT
<< simplePattern2
<< 1 << 2 << 3
<< QString() << QString()
<< (QList<ProjectExplorer::Task>()
<< Task(Task::Error, message, fileName, lineNumber2, categoryCompile)
)
<< QString();
const QString unitTestError = QLatin1String("../LedDriver/LedDriverTest.c:63: FAIL: Expected 0x0080 Was 0xffff");
const FileName unitTestFileName = FileName::fromUserInput(QLatin1String("../LedDriver/LedDriverTest.c"));
const QString unitTestMessage = QLatin1String("Expected 0x0080 Was 0xffff");
const QString unitTestPattern = QLatin1String("^([^:]+):(\\d+): FAIL: ([^\\s].+)$");
const int unitTestLineNumber = 63;
QTest::newRow("unit test error")
<< unitTestError
<< OutputParserTester::STDOUT
<< unitTestPattern
<< 1 << 2 << 3
<< QString() << QString()
<< (QList<ProjectExplorer::Task>()
<< Task(Task::Error, unitTestMessage, unitTestFileName, unitTestLineNumber, categoryCompile)
)
<< QString();
}
void ProjectExplorerPlugin::testCustomOutputParsers()
{
QFETCH(QString, input);
QFETCH(OutputParserTester::Channel, inputChannel);
QFETCH(QString, pattern);
QFETCH(int, fileNameCap);
QFETCH(int, lineNumberCap);
QFETCH(int, messageCap);
QFETCH(QString, childStdOutLines);
QFETCH(QString, childStdErrLines);
QFETCH(QList<Task>, tasks);
QFETCH(QString, outputLines);
CustomParser *parser = new CustomParser;
parser->setErrorPattern(pattern);
parser->setFileNameCap(fileNameCap);
parser->setLineNumberCap(lineNumberCap);
parser->setMessageCap(messageCap);
OutputParserTester testbench;
testbench.appendOutputParser(parser);
testbench.testParsing(input, inputChannel,
tasks, childStdOutLines, childStdErrLines,
outputLines);
}
#endif
/****************************************************************************
**
** Copyright (C) 2013 Andre Hartmann.
** Contact: aha_1980@gmx.de
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CUSTOMPARSER_H
#define CUSTOMPARSER_H
#include "ioutputparser.h"
#include <projectexplorer/task.h>
#include <QRegExp>
namespace ProjectExplorer {
class CustomParserSettings
{
public:
CustomParserSettings();
bool operator !=(const CustomParserSettings &other) const;
QString errorPattern;
int fileNameCap;
int lineNumberCap;
int messageCap;
};
class CustomParser : public ProjectExplorer::IOutputParser
{
public:
enum CustomParserChannels {
ParseNoChannel = 0,
ParseStdErrChannel = 1,
ParseStdOutChannel = 2,
ParseBothChannels = 3
};
CustomParser(const CustomParserSettings &settings = CustomParserSettings());
~CustomParser();
void stdError(const QString &line);
void stdOutput(const QString &line);
void setSettings(const CustomParserSettings &settings);
void setErrorPattern(const QString &errorPattern);
QString errorPattern() const;
int fileNameCap() const;
void setFileNameCap(int fileNameCap);
int lineNumberCap() const;
void setLineNumberCap(int lineNumberCap);
int messageCap() const;
void setMessageCap(int messageCap);
private:
bool parseLine(const QString &rawLine);
QRegExp m_errorRegExp;
int m_fileNameCap;
int m_lineNumberCap;
int m_messageCap;
CustomParserChannels m_parserChannels;
};
} // namespace ProjectExplorer
#endif // CUSTOMPARSER_H
/****************************************************************************
**
** Copyright (C) 2013 Andre Hartmann.
** Contact: aha_1980@gmx.de
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "customparserconfigdialog.h"
#include "ui_customparserconfigdialog.h"
#include <QPushButton>
#include <QRegExp>
CustomParserConfigDialog::CustomParserConfigDialog(QDialog *parent) :
QDialog(parent),
ui(new Ui::CustomParserConfigDialog)
{
ui->setupUi(this);
connect(ui->errorPattern, SIGNAL(textChanged(QString)), this, SLOT(changed()));
connect(ui->errorMessage, SIGNAL(textChanged(QString)), this, SLOT(changed()));
connect(ui->fileNameCap, SIGNAL(valueChanged(int)), this, SLOT(changed()));
connect(ui->lineNumberCap, SIGNAL(valueChanged(int)), this, SLOT(changed()));
connect(ui->messageCap, SIGNAL(valueChanged(int)), this, SLOT(changed()));
changed();
m_dirty = false;
}
CustomParserConfigDialog::~CustomParserConfigDialog()
{
delete ui;
}
void CustomParserConfigDialog::setExampleSettings()
{
setErrorPattern(QLatin1String("#error (.*):(\\d+): (.*)$"));
setFileNameCap(1);
setLineNumberCap(2);
setMessageCap(3);
ui->errorMessage->setText(QLatin1String("#error /home/user/src/test.c:891: Unknown identifier `test`"));
}
void CustomParserConfigDialog::setSettings(const ProjectExplorer::CustomParserSettings &settings)
{
if (settings.errorPattern.isEmpty()) {
setExampleSettings();
return;
}
setErrorPattern(settings.errorPattern);
setFileNameCap(settings.fileNameCap);
setLineNumberCap(settings.lineNumberCap);
setMessageCap(settings.messageCap);
}
ProjectExplorer::CustomParserSettings CustomParserConfigDialog::settings() const
{
ProjectExplorer::CustomParserSettings result;
result.errorPattern = errorPattern();
result.fileNameCap = fileNameCap();
result.lineNumberCap = lineNumberCap();
result.messageCap = messageCap();
return result;
}
void CustomParserConfigDialog::setErrorPattern(const QString &errorPattern)
{
ui->errorPattern->setText(errorPattern);
}
QString CustomParserConfigDialog::errorPattern() const
{
return ui->errorPattern->text();
}
void CustomParserConfigDialog::setFileNameCap(int fileNameCap)
{
ui->fileNameCap->setValue(fileNameCap);
}
int CustomParserConfigDialog::fileNameCap() const
{
return ui->fileNameCap->value();
}
void CustomParserConfigDialog::setLineNumberCap(int lineNumberCap)
{
ui->lineNumberCap->setValue(lineNumberCap);
}
int CustomParserConfigDialog::lineNumberCap() const
{
return ui->lineNumberCap->value();
}
void CustomParserConfigDialog::setMessageCap(int messageCap)
{
ui->messageCap->setValue(messageCap);
}
int CustomParserConfigDialog::messageCap() const
{
return ui->messageCap->value();
}
bool CustomParserConfigDialog::isDirty() const
{
return m_dirty;
}
void CustomParserConfigDialog::changed()
{
QRegExp rx;
rx.setPattern(ui->errorPattern->text());
rx.setMinimal(true);
QPalette palette;
palette.setColor(QPalette::Text, rx.isValid() ? Qt::black : Qt::red);
ui->errorPattern->setPalette(palette);
ui->errorPattern->setToolTip(rx.isValid() ? QString() : rx.errorString());
int pos = rx.indexIn(ui->errorMessage->text());
if (rx.isEmpty() || !rx.isValid() || pos < 0) {
QString error = QLatin1String("<font color=\"red\">") + tr("Not applicable: ");
if (rx.isEmpty())
error += tr("Pattern is empty.");
else if (!rx.isValid())
error += rx.errorString();
else
error += tr("Pattern does not match the error message.");
ui->fileNameTest->setText(error);
ui->lineNumberTest->setText(error);
ui->messageTest->setText(error);
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
return;
}
ui->fileNameTest->setText(rx.cap(ui->fileNameCap->value()));
ui->lineNumberTest->setText(rx.cap(ui->lineNumberCap->value()));
ui->messageTest->setText(rx.cap(ui->messageCap->value()));
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
m_dirty = true;
}
/****************************************************************************
**
** Copyright (C) 2013 Andre Hartmann.
** Contact: aha_1980@gmx.de
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CUSTOMPARSERCONFIGDIALOG_H
#define CUSTOMPARSERCONFIGDIALOG_H
#include "customparser.h"
#include <QDialog>
namespace Ui {
class CustomParserConfigDialog;
}
class CustomParserConfigDialog : public QDialog