diff options
author | Tobias Hunger <tobias.hunger@qt.io> | 2016-04-29 16:52:58 +0200 |
---|---|---|
committer | Tobias Hunger <tobias.hunger@qt.io> | 2016-05-11 10:04:38 +0000 |
commit | ddefe062c73e35def585f8fc6c90a4f18e47c0f4 (patch) | |
tree | 03c3aecc501c03b92e259fe0ae1c4d472033b7e5 /src/plugins/beautifier | |
parent | 1a248b1b932e2c7c42e25993d921e78c52aa4bcf (diff) |
Fix up QProcess::waitForFinished()
waitForFinish returns false if the process is no longer running at
the time of the call. Handle that throughout the codebase.
Change-Id: Ia7194095454e82efbd4eb88f2d55926bdd09e094
Reviewed-by: hjk <hjk@theqtcompany.com>
Diffstat (limited to 'src/plugins/beautifier')
-rw-r--r-- | src/plugins/beautifier/artisticstyle/artisticstylesettings.cpp | 27 | ||||
-rw-r--r-- | src/plugins/beautifier/beautifierplugin.cpp | 23 | ||||
-rw-r--r-- | src/plugins/beautifier/uncrustify/uncrustifysettings.cpp | 13 |
3 files changed, 32 insertions, 31 deletions
diff --git a/src/plugins/beautifier/artisticstyle/artisticstylesettings.cpp b/src/plugins/beautifier/artisticstyle/artisticstylesettings.cpp index c3293afbb8b..ea7ea114318 100644 --- a/src/plugins/beautifier/artisticstyle/artisticstylesettings.cpp +++ b/src/plugins/beautifier/artisticstyle/artisticstylesettings.cpp @@ -32,11 +32,11 @@ #include <coreplugin/icore.h> #include <utils/runextensions.h> +#include <utils/synchronousprocess.h> #include <QDateTime> #include <QFile> #include <QFileInfo> -#include <QProcess> #include <QXmlStreamWriter> namespace Beautifier { @@ -78,18 +78,17 @@ static int parseVersion(const QString &text) static int updateVersionHelper(const QString &command) { - QProcess process; - process.start(command, {"--version"}); - if (!process.waitForFinished()) { - process.kill(); + Utils::SynchronousProcess process; + Utils::SynchronousProcessResponse response + = process.run(command, QStringList() << QLatin1String("--version")); + if (response.result != Utils::SynchronousProcessResponse::Finished) return 0; - } // Astyle prints the version on stdout or stderr, depending on platform - const int version = parseVersion(QString::fromUtf8(process.readAllStandardOutput()).trimmed()); + const int version = parseVersion(response.stdOut.trimmed()); if (version != 0) return version; - return parseVersion(QString::fromUtf8(process.readAllStandardError()).trimmed()); + return parseVersion(response.stdErr.trimmed()); } void ArtisticStyleSettings::updateVersion() @@ -155,10 +154,11 @@ QString ArtisticStyleSettings::documentationFilePath() const void ArtisticStyleSettings::createDocumentationFile() const { - QProcess process; - process.start(command(), {"-h"}); - process.waitForFinished(2000); // show help should be really fast. - if (process.error() != QProcess::UnknownError) + Utils::SynchronousProcess process; + process.setTimeoutS(2); + Utils::SynchronousProcessResponse response + = process.run(command(), QStringList() << QLatin1String("-h")); + if (response.result != Utils::SynchronousProcessResponse::Finished) return; QFile file(documentationFilePath()); @@ -176,8 +176,7 @@ void ArtisticStyleSettings::createDocumentationFile() const stream.writeStartElement(Constants::DOCUMENTATION_XMLROOT); // astyle writes its output to 'error'... - const QStringList lines = QString::fromUtf8(process.readAllStandardError()) - .split('\n'); + const QStringList lines = response.stdErr.split(QLatin1Char('\n')); QStringList keys; QStringList docu; for (QString line : lines) { diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp index 508a5117422..c44c2e2b55d 100644 --- a/src/plugins/beautifier/beautifierplugin.cpp +++ b/src/plugins/beautifier/beautifierplugin.cpp @@ -45,6 +45,7 @@ #include <texteditor/texteditor.h> #include <utils/qtcassert.h> #include <utils/runextensions.h> +#include <utils/synchronousprocess.h> #include <QDir> #include <QFileInfo> @@ -84,20 +85,18 @@ FormatTask format(FormatTask task) } // Format temporary file - QProcess process; QStringList options = task.command.options(); - options.replaceInStrings("%file", sourceFile.fileName()); - process.start(executable, options); - if (!process.waitForFinished(5000)) { - process.kill(); - task.error = BeautifierPlugin::tr("Cannot call %1 or some other error occurred. Timeout " - "reached while formatting file %2.") - .arg(executable).arg(task.filePath); + options.replaceInStrings(QLatin1String("%file"), sourceFile.fileName()); + Utils::SynchronousProcess process; + process.setTimeoutS(5); + Utils::SynchronousProcessResponse response = process.run(executable, options); + if (response.result != Utils::SynchronousProcessResponse::Finished) { + task.error = QObject::tr("Failed to format: %1.").arg(response.exitMessage(executable, 5)); return task; } - const QByteArray output = process.readAllStandardError(); + const QString output = response.stdErr; if (!output.isEmpty()) - task.error = executable + ": " + QString::fromUtf8(output); + task.error = executable + QLatin1String(": ") + output; // Read text back Utils::FileReader reader; @@ -107,8 +106,8 @@ FormatTask format(FormatTask task) return task; } task.formattedData = QString::fromUtf8(reader.data()); - return task; } + return task; case Command::PipeProcessing: { QProcess process; @@ -123,7 +122,7 @@ FormatTask format(FormatTask task) } process.write(task.sourceData.toUtf8()); process.closeWriteChannel(); - if (!process.waitForFinished(5000)) { + if (!process.waitForFinished(5000) && process.state() == QProcess::Running) { process.kill(); task.error = BeautifierPlugin::tr("Cannot call %1 or some other error occurred. Timeout " "reached while formatting file %2.") diff --git a/src/plugins/beautifier/uncrustify/uncrustifysettings.cpp b/src/plugins/beautifier/uncrustify/uncrustifysettings.cpp index ed552ae7208..bdbb34c2117 100644 --- a/src/plugins/beautifier/uncrustify/uncrustifysettings.cpp +++ b/src/plugins/beautifier/uncrustify/uncrustifysettings.cpp @@ -31,6 +31,8 @@ #include <coreplugin/icore.h> +#include <utils/synchronousprocess.h> + #include <QDateTime> #include <QFile> #include <QFileInfo> @@ -124,10 +126,11 @@ QString UncrustifySettings::documentationFilePath() const void UncrustifySettings::createDocumentationFile() const { - QProcess process; - process.start(command(), {"--show-config"}); - process.waitForFinished(2000); // show config should be really fast. - if (process.error() != QProcess::UnknownError) + Utils::SynchronousProcess process; + process.setTimeoutS(2); + Utils::SynchronousProcessResponse response + = process.run(command(), QStringList() << QLatin1String("--show-config")); + if (response.result != Utils::SynchronousProcessResponse::Finished) return; QFile file(documentationFilePath()); @@ -144,7 +147,7 @@ void UncrustifySettings::createDocumentationFile() const stream.writeComment("Created " + QDateTime::currentDateTime().toString(Qt::ISODate)); stream.writeStartElement(Constants::DOCUMENTATION_XMLROOT); - const QStringList lines = QString::fromUtf8(process.readAll()).split('\n'); + const QStringList lines = response.allOutput().split(QLatin1Char('\n')); const int totalLines = lines.count(); for (int i = 0; i < totalLines; ++i) { const QString &line = lines.at(i); |