From c564805095e4f2cebb263dba28ce07ea6f1f5350 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Thu, 9 Apr 2020 14:30:44 +0200 Subject: [PATCH] Output parsers: Make file look-up self-contained Change-Id: Iffe104b6b52f0902f1977adeaa0fee3dc1e374a4 Reviewed-by: hjk --- .../projectexplorer/abstractprocessstep.cpp | 42 ++++++------------- .../projectexplorer/abstractprocessstep.h | 2 - src/plugins/projectexplorer/ioutputparser.cpp | 28 ++++++++++++- src/plugins/projectexplorer/ioutputparser.h | 8 +++- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/src/plugins/projectexplorer/abstractprocessstep.cpp b/src/plugins/projectexplorer/abstractprocessstep.cpp index f45ecec4903..29a13663119 100644 --- a/src/plugins/projectexplorer/abstractprocessstep.cpp +++ b/src/plugins/projectexplorer/abstractprocessstep.cpp @@ -108,7 +108,6 @@ public: std::unique_ptr m_process; IOutputParser m_outputParser; ProcessParameters m_param; - Utils::FileInProjectFinder m_fileFinder; bool m_ignoreReturnValue = false; bool m_lowPriority = false; std::unique_ptr stdoutStream; @@ -119,8 +118,15 @@ AbstractProcessStep::AbstractProcessStep(BuildStepList *bsl, Core::Id id) : BuildStep(bsl, id), d(new Private(this)) { - connect(&d->m_outputParser, &IOutputParser::addTask, - this, &AbstractProcessStep::taskAdded); + connect(&d->m_outputParser, &IOutputParser::addTask, this, + [this](const Task &task, int linkedLines, int skipLines) { + // Do not bother to report issues if we do not care about the results of + // the buildstep anyway: + // TODO: Does that make sense? The user might still want to know that + // something failed, even if it wasn't fatal... + if (!d->m_ignoreReturnValue) + emit addTask(task, linkedLines, skipLines); + }); } AbstractProcessStep::~AbstractProcessStep() @@ -188,9 +194,11 @@ void AbstractProcessStep::setIgnoreReturnValue(bool b) bool AbstractProcessStep::init() { + Utils::FileInProjectFinder fileFinder; + fileFinder.setProjectDirectory(project()->projectDirectory()); + fileFinder.setProjectFiles(project()->files(Project::AllFiles)); d->m_outputParser.addFilter(&Internal::filterAnsiEscapeCodes); - d->m_fileFinder.setProjectDirectory(project()->projectDirectory()); - d->m_fileFinder.setProjectFiles(project()->files(Project::AllFiles)); + d->m_outputParser.setFileFinder(fileFinder); return !d->m_process; } @@ -387,30 +395,6 @@ void AbstractProcessStep::finish(bool success) emit finished(success); } -void AbstractProcessStep::taskAdded(const Task &task, int linkedOutputLines, int skipLines) -{ - // Do not bother to report issues if we do not care about the results of - // the buildstep anyway: - if (d->m_ignoreReturnValue) - return; - - Task editable(task); - QString filePath = task.file.toString(); - if (!filePath.isEmpty() && !filePath.startsWith('<') && !QDir::isAbsolutePath(filePath)) { - while (filePath.startsWith("../")) - filePath.remove(0, 3); - bool found = false; - const Utils::FilePaths candidates - = d->m_fileFinder.findFile(QUrl::fromLocalFile(filePath), &found); - if (found && candidates.size() == 1) - editable.file = candidates.first(); - else - qWarning() << "Could not find absolute location of file " << filePath; - } - - emit addTask(editable, linkedOutputLines, skipLines); -} - void AbstractProcessStep::outputAdded(const QString &string, BuildStep::OutputFormat format) { emit addOutput(string, format, BuildStep::DontAppendNewline); diff --git a/src/plugins/projectexplorer/abstractprocessstep.h b/src/plugins/projectexplorer/abstractprocessstep.h index b575a780f5f..f6b720eb138 100644 --- a/src/plugins/projectexplorer/abstractprocessstep.h +++ b/src/plugins/projectexplorer/abstractprocessstep.h @@ -79,8 +79,6 @@ private: void cleanUp(QProcess *process); - void taskAdded(const Task &task, int linkedOutputLines = 0, int skipLines = 0); - void outputAdded(const QString &string, BuildStep::OutputFormat format); class Private; diff --git a/src/plugins/projectexplorer/ioutputparser.cpp b/src/plugins/projectexplorer/ioutputparser.cpp index 8acd8c57c14..98f39fb4db1 100644 --- a/src/plugins/projectexplorer/ioutputparser.cpp +++ b/src/plugins/projectexplorer/ioutputparser.cpp @@ -27,6 +27,7 @@ #include "task.h" #include +#include #include #include @@ -76,6 +77,7 @@ class OutputTaskParser::Private { public: Utils::FilePaths searchDirs; + Utils::FileInProjectFinder *fileFinder = nullptr; QPointer redirectionDetector; bool skipFileExistsCheck = false; }; @@ -122,6 +124,11 @@ bool OutputTaskParser::needsRedirection() const || d->redirectionDetector->needsRedirection()); } +void OutputTaskParser::setFileFinder(Utils::FileInProjectFinder *finder) +{ + d->fileFinder = finder; +} + Utils::FilePath OutputTaskParser::absoluteFilePath(const Utils::FilePath &filePath) { if (filePath.isEmpty() || filePath.toFileInfo().isAbsolute()) @@ -134,6 +141,15 @@ Utils::FilePath OutputTaskParser::absoluteFilePath(const Utils::FilePath &filePa } if (candidates.count() == 1) return Utils::FilePath::fromString(QDir::cleanPath(candidates.first().toString())); + + QString fp = filePath.toString(); + while (fp.startsWith("../")) + fp.remove(0, 3); + bool found = false; + candidates = d->fileFinder->findFile(QUrl::fromLocalFile(fp), &found); + if (found && candidates.size() == 1) + return candidates.first(); + return filePath; } @@ -200,6 +216,7 @@ public: QList lineParsers; OutputTaskParser *nextParser = nullptr; QList filters; + Utils::FileInProjectFinder fileFinder; OutputChannelState stdoutState; OutputChannelState stderrState; }; @@ -261,8 +278,9 @@ QString IOutputParser::filteredLine(const QString &line) const return l; } -void IOutputParser::connectLineParser(OutputTaskParser *parser) +void IOutputParser::setupLineParser(OutputTaskParser *parser) { + parser->setFileFinder(&d->fileFinder); connect(parser, &OutputTaskParser::addTask, this, &IOutputParser::addTask); connect(parser, &OutputTaskParser::newSearchDir, this, &IOutputParser::addSearchDir); connect(parser, &OutputTaskParser::searchDirExpired, this, &IOutputParser::dropSearchDir); @@ -291,11 +309,12 @@ void IOutputParser::clear() d->lineParsers.clear(); d->stdoutState.pendingData.clear(); d->stderrState.pendingData.clear(); + d->fileFinder = Utils::FileInProjectFinder(); } void IOutputParser::addLineParser(OutputTaskParser *parser) { - connectLineParser(parser); + setupLineParser(parser); d->lineParsers << parser; } @@ -312,6 +331,11 @@ void IOutputParser::setLineParsers(const QList &parsers) addLineParsers(parsers); } +void IOutputParser::setFileFinder(const Utils::FileInProjectFinder &finder) +{ + d->fileFinder = finder; +} + #ifdef WITH_TESTS QList IOutputParser::lineParsers() const { diff --git a/src/plugins/projectexplorer/ioutputparser.h b/src/plugins/projectexplorer/ioutputparser.h index 21e153b305c..efe717f1f0e 100644 --- a/src/plugins/projectexplorer/ioutputparser.h +++ b/src/plugins/projectexplorer/ioutputparser.h @@ -33,6 +33,8 @@ #include +namespace Utils { class FileInProjectFinder; } + namespace ProjectExplorer { class Task; @@ -57,6 +59,8 @@ public: bool needsRedirection() const; virtual bool hasDetectedRedirection() const { return false; } + void setFileFinder(Utils::FileInProjectFinder *finder); + #ifdef WITH_TESTS void skipFileExistsCheck(); #endif @@ -102,6 +106,8 @@ public: void addLineParsers(const QList &parsers); void setLineParsers(const QList &parsers); + void setFileFinder(const Utils::FileInProjectFinder &finder); + #ifdef WITH_TESTS QList lineParsers() const; #endif @@ -112,7 +118,7 @@ signals: private: void handleLine(const QString &line, Utils::OutputFormat type); QString filteredLine(const QString &line) const; - void connectLineParser(OutputTaskParser *parser); + void setupLineParser(OutputTaskParser *parser); Utils::OutputFormat outputTypeForParser(const OutputTaskParser *parser, Utils::OutputFormat type) const;