Commit c3aac762 authored by Tobias Hunger's avatar Tobias Hunger

CMake: Parse cmake syntax errors

Improve the CMakeParser so that it picks up the most common
errors/warnings that cmake throws when running into errors while
parsing its CMakeLists.txt files.

Change-Id: Ie3774695418f13aa3869d78802c1a552e6fd384e
Reviewed-by: default avatarTim Jenssen <tim.jenssen@theqtcompany.com>
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent 36118a5c
......@@ -36,6 +36,7 @@ using namespace ProjectExplorer;
const char COMMON_ERROR_PATTERN[] = "^CMake Error at (.*):([0-9]*) \\((.*)\\):";
const char NEXT_SUBERROR_PATTERN[] = "^CMake Error in (.*):";
const char LOCATION_LINE_PATTERN[] = ":(\\d+):(?:(\\d+))?$";
CMakeParser::CMakeParser()
{
......@@ -46,41 +47,82 @@ CMakeParser::CMakeParser()
m_nextSubError.setPattern(QLatin1String(NEXT_SUBERROR_PATTERN));
m_nextSubError.setMinimal(true);
QTC_CHECK(m_nextSubError.isValid());
m_locationLine.setPattern(QLatin1String(LOCATION_LINE_PATTERN));
QTC_CHECK(m_locationLine.isValid());
appendOutputParser(new GnuMakeParser());
}
void CMakeParser::stdError(const QString &line)
{
QString trimmedLine = rightTrimmed(line);
if (trimmedLine.isEmpty() && !m_lastTask.isNull()) {
if (trimmedLine.endsWith(QLatin1String("in cmake code at")))
qDebug() << "Break";
switch (m_expectTripleLineErrorData) {
case NONE:
if (trimmedLine.isEmpty() && !m_lastTask.isNull()) {
if (m_skippedFirstEmptyLine)
doFlush();
else
m_skippedFirstEmptyLine = true;
return;
}
if (m_skippedFirstEmptyLine)
m_skippedFirstEmptyLine = false;
if (m_commonError.indexIn(trimmedLine) != -1) {
m_lastTask = Task(Task::Error, QString(), Utils::FileName::fromUserInput(m_commonError.cap(1)),
m_commonError.cap(2).toInt(), Constants::TASK_CATEGORY_BUILDSYSTEM);
m_lines = 1;
return;
} else if (m_nextSubError.indexIn(trimmedLine) != -1) {
m_lastTask = Task(Task::Error, QString(), Utils::FileName::fromUserInput(m_nextSubError.cap(1)), -1,
Constants::TASK_CATEGORY_BUILDSYSTEM);
m_lines = 1;
return;
} else if (trimmedLine.startsWith(QLatin1String(" ")) && !m_lastTask.isNull()) {
if (!m_lastTask.description.isEmpty())
m_lastTask.description.append(QLatin1Char(' '));
m_lastTask.description.append(trimmedLine.trimmed());
++m_lines;
return;
} else if (trimmedLine.endsWith(QLatin1String("in cmake code at"))) {
m_expectTripleLineErrorData = LINE_LOCATION;
doFlush();
else
m_skippedFirstEmptyLine = true;
m_lastTask = Task(trimmedLine.contains(QLatin1String("Error")) ? Task::Error : Task::Warning,
QString(), Utils::FileName(), -1, Constants::TASK_CATEGORY_BUILDSYSTEM);
return;
}
IOutputParser::stdError(line);
return;
}
if (m_skippedFirstEmptyLine)
m_skippedFirstEmptyLine = false;
if (m_commonError.indexIn(trimmedLine) != -1) {
m_lastTask = Task(Task::Error, QString(), Utils::FileName::fromUserInput(m_commonError.cap(1)),
m_commonError.cap(2).toInt(), Constants::TASK_CATEGORY_BUILDSYSTEM);
m_lines = 1;
case LINE_LOCATION:
{
QRegularExpressionMatch m = m_locationLine.match(trimmedLine);
QTC_CHECK(m.hasMatch());
m_lastTask.file = Utils::FileName::fromUserInput(trimmedLine.mid(0, m.capturedStart()));
m_lastTask.line = m.captured(1).toInt();
m_expectTripleLineErrorData = LINE_DESCRIPTION;
}
return;
} else if (m_nextSubError.indexIn(trimmedLine) != -1) {
m_lastTask = Task(Task::Error, QString(), Utils::FileName::fromUserInput(m_nextSubError.cap(1)), -1,
Constants::TASK_CATEGORY_BUILDSYSTEM);
m_lines = 1;
case LINE_DESCRIPTION:
m_lastTask.description = trimmedLine;
if (trimmedLine.endsWith(QLatin1Char('\"')))
m_expectTripleLineErrorData = LINE_DESCRIPTION2;
else {
m_expectTripleLineErrorData = NONE;
doFlush();
}
return;
} else if (trimmedLine.startsWith(QLatin1String(" ")) && !m_lastTask.isNull()) {
if (!m_lastTask.description.isEmpty())
m_lastTask.description.append(QLatin1Char(' '));
m_lastTask.description.append(trimmedLine.trimmed());
++m_lines;
case LINE_DESCRIPTION2:
m_lastTask.description.append(QLatin1Char('\n'));
m_lastTask.description.append(trimmedLine);
m_expectTripleLineErrorData = NONE;
doFlush();
return;
}
IOutputParser::stdError(line);
}
void CMakeParser::doFlush()
......@@ -183,6 +225,33 @@ void CMakeProjectPlugin::testCMakeParser_data()
Utils::FileName::fromUserInput(QLatin1String("src/1/CMakeLists.txt")), 8,
categoryBuild))
<< QString();
QTest::newRow("cmake error")
<< QString::fromLatin1("CMake Error: Error in cmake code at\n"
"/test/path/CMakeLists.txt:9:\n"
"Parse error. Expected \"(\", got newline with text \"\n"
"\".")
<< OutputParserTester::STDERR
<< QString() << QString()
<< (QList<ProjectExplorer::Task>()
<< Task(Task::Error,
QLatin1String("Parse error. Expected \"(\", got newline with text \"\n\"."),
Utils::FileName::fromUserInput(QLatin1String("/test/path/CMakeLists.txt")), 9,
categoryBuild))
<< QString();
QTest::newRow("cmake warning")
<< QString::fromLatin1("Syntax Warning in cmake code at\n"
"/test/path/CMakeLists.txt:9:15\n"
"Argument not separated from preceding token by whitespace.")
<< OutputParserTester::STDERR
<< QString() << QString()
<< (QList<ProjectExplorer::Task>()
<< Task(Task::Warning,
QLatin1String("Argument not separated from preceding token by whitespace."),
Utils::FileName::fromUserInput(QLatin1String("/test/path/CMakeLists.txt")), 9,
categoryBuild))
<< QString();
}
void CMakeProjectPlugin::testCMakeParser()
......
......@@ -28,6 +28,8 @@
#include <projectexplorer/ioutputparser.h>
#include <projectexplorer/task.h>
#include <QRegularExpression>
namespace CMakeProjectManager {
namespace Internal {
......@@ -43,13 +45,17 @@ protected:
void doFlush() override;
private:
enum TripleLineError { NONE, LINE_LOCATION, LINE_DESCRIPTION, LINE_DESCRIPTION2 };
TripleLineError m_expectTripleLineErrorData = NONE;
ProjectExplorer::Task m_lastTask;
QRegExp m_commonError;
QRegExp m_nextSubError;
QRegularExpression m_locationLine;
bool m_skippedFirstEmptyLine = false;
int m_lines = 0;
};
} // namespace CMakeProjectManager
} // namespace Internal
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment