Commit c3aac762 authored by Tobias Hunger's avatar Tobias Hunger
Browse files

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; ...@@ -36,6 +36,7 @@ using namespace ProjectExplorer;
const char COMMON_ERROR_PATTERN[] = "^CMake Error at (.*):([0-9]*) \\((.*)\\):"; const char COMMON_ERROR_PATTERN[] = "^CMake Error at (.*):([0-9]*) \\((.*)\\):";
const char NEXT_SUBERROR_PATTERN[] = "^CMake Error in (.*):"; const char NEXT_SUBERROR_PATTERN[] = "^CMake Error in (.*):";
const char LOCATION_LINE_PATTERN[] = ":(\\d+):(?:(\\d+))?$";
CMakeParser::CMakeParser() CMakeParser::CMakeParser()
{ {
...@@ -46,41 +47,82 @@ CMakeParser::CMakeParser() ...@@ -46,41 +47,82 @@ CMakeParser::CMakeParser()
m_nextSubError.setPattern(QLatin1String(NEXT_SUBERROR_PATTERN)); m_nextSubError.setPattern(QLatin1String(NEXT_SUBERROR_PATTERN));
m_nextSubError.setMinimal(true); m_nextSubError.setMinimal(true);
QTC_CHECK(m_nextSubError.isValid()); QTC_CHECK(m_nextSubError.isValid());
m_locationLine.setPattern(QLatin1String(LOCATION_LINE_PATTERN));
QTC_CHECK(m_locationLine.isValid());
appendOutputParser(new GnuMakeParser()); appendOutputParser(new GnuMakeParser());
} }
void CMakeParser::stdError(const QString &line) void CMakeParser::stdError(const QString &line)
{ {
QString trimmedLine = rightTrimmed(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) 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(); doFlush();
else m_lastTask = Task(trimmedLine.contains(QLatin1String("Error")) ? Task::Error : Task::Warning,
m_skippedFirstEmptyLine = true; QString(), Utils::FileName(), -1, Constants::TASK_CATEGORY_BUILDSYSTEM);
return;
}
IOutputParser::stdError(line);
return; return;
} case LINE_LOCATION:
if (m_skippedFirstEmptyLine) {
m_skippedFirstEmptyLine = false; QRegularExpressionMatch m = m_locationLine.match(trimmedLine);
QTC_CHECK(m.hasMatch());
if (m_commonError.indexIn(trimmedLine) != -1) { m_lastTask.file = Utils::FileName::fromUserInput(trimmedLine.mid(0, m.capturedStart()));
m_lastTask = Task(Task::Error, QString(), Utils::FileName::fromUserInput(m_commonError.cap(1)), m_lastTask.line = m.captured(1).toInt();
m_commonError.cap(2).toInt(), Constants::TASK_CATEGORY_BUILDSYSTEM); m_expectTripleLineErrorData = LINE_DESCRIPTION;
m_lines = 1; }
return; return;
} else if (m_nextSubError.indexIn(trimmedLine) != -1) { case LINE_DESCRIPTION:
m_lastTask = Task(Task::Error, QString(), Utils::FileName::fromUserInput(m_nextSubError.cap(1)), -1, m_lastTask.description = trimmedLine;
Constants::TASK_CATEGORY_BUILDSYSTEM); if (trimmedLine.endsWith(QLatin1Char('\"')))
m_lines = 1; m_expectTripleLineErrorData = LINE_DESCRIPTION2;
else {
m_expectTripleLineErrorData = NONE;
doFlush();
}
return; return;
} else if (trimmedLine.startsWith(QLatin1String(" ")) && !m_lastTask.isNull()) { case LINE_DESCRIPTION2:
if (!m_lastTask.description.isEmpty()) m_lastTask.description.append(QLatin1Char('\n'));
m_lastTask.description.append(QLatin1Char(' ')); m_lastTask.description.append(trimmedLine);
m_lastTask.description.append(trimmedLine.trimmed()); m_expectTripleLineErrorData = NONE;
++m_lines; doFlush();
return; return;
} }
IOutputParser::stdError(line);
} }
void CMakeParser::doFlush() void CMakeParser::doFlush()
...@@ -183,6 +225,33 @@ void CMakeProjectPlugin::testCMakeParser_data() ...@@ -183,6 +225,33 @@ void CMakeProjectPlugin::testCMakeParser_data()
Utils::FileName::fromUserInput(QLatin1String("src/1/CMakeLists.txt")), 8, Utils::FileName::fromUserInput(QLatin1String("src/1/CMakeLists.txt")), 8,
categoryBuild)) categoryBuild))
<< QString(); << 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() void CMakeProjectPlugin::testCMakeParser()
......
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
#include <projectexplorer/ioutputparser.h> #include <projectexplorer/ioutputparser.h>
#include <projectexplorer/task.h> #include <projectexplorer/task.h>
#include <QRegularExpression>
namespace CMakeProjectManager { namespace CMakeProjectManager {
namespace Internal { namespace Internal {
...@@ -43,13 +45,17 @@ protected: ...@@ -43,13 +45,17 @@ protected:
void doFlush() override; void doFlush() override;
private: private:
enum TripleLineError { NONE, LINE_LOCATION, LINE_DESCRIPTION, LINE_DESCRIPTION2 };
TripleLineError m_expectTripleLineErrorData = NONE;
ProjectExplorer::Task m_lastTask; ProjectExplorer::Task m_lastTask;
QRegExp m_commonError; QRegExp m_commonError;
QRegExp m_nextSubError; QRegExp m_nextSubError;
QRegularExpression m_locationLine;
bool m_skippedFirstEmptyLine = false; bool m_skippedFirstEmptyLine = false;
int m_lines = 0; int m_lines = 0;
}; };
} // namespace CMakeProjectManager } // namespace CMakeProjectManager
} // namespace Internal } // 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