From 2eb3bda5a85b6919bdfe7719a77d4b0877fdbec8 Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Thu, 18 Jun 2015 18:08:48 +0200 Subject: [PATCH] Clang: Indicate lines from ClangBackEnd ...with a prefix. Change-Id: I6e23e00254d7b7316057514d1234cde66ab44a0d Reviewed-by: Nikolai Kosjar --- .../clangbackendipc/clangbackendipc-lib.pri | 7 +- src/libs/clangbackendipc/connectionclient.cpp | 8 +- src/libs/clangbackendipc/connectionclient.h | 4 + src/libs/clangbackendipc/lineprefixer.cpp | 61 +++++++++ src/libs/clangbackendipc/lineprefixer.h | 55 ++++++++ tests/unit/unittest/lineprefixertest.cpp | 120 ++++++++++++++++++ tests/unit/unittest/unittest.pro | 3 +- 7 files changed, 251 insertions(+), 7 deletions(-) create mode 100644 src/libs/clangbackendipc/lineprefixer.cpp create mode 100644 src/libs/clangbackendipc/lineprefixer.h create mode 100644 tests/unit/unittest/lineprefixertest.cpp diff --git a/src/libs/clangbackendipc/clangbackendipc-lib.pri b/src/libs/clangbackendipc/clangbackendipc-lib.pri index f49e3ab2197..50c5974d4e8 100644 --- a/src/libs/clangbackendipc/clangbackendipc-lib.pri +++ b/src/libs/clangbackendipc/clangbackendipc-lib.pri @@ -35,8 +35,8 @@ SOURCES += $$PWD/ipcserverinterface.cpp \ $$PWD/translationunitdoesnotexistcommand.cpp \ $$PWD/codecompletionchunk.cpp \ $$PWD/projectpartcontainer.cpp \ - $$PWD/projectpartsdonotexistcommand.cpp - + $$PWD/projectpartsdonotexistcommand.cpp \ + $$PWD/lineprefixer.cpp HEADERS += \ $$PWD/ipcserverinterface.h \ @@ -66,6 +66,7 @@ HEADERS += \ $$PWD/projectpartcontainer.h \ $$PWD/projectpartsdonotexistcommand.h \ $$PWD/container_common.h \ - $$PWD/clangbackendipc_global.h + $$PWD/clangbackendipc_global.h \ + $$PWD/lineprefixer.h contains(QT_CONFIG, reduce_exports):CONFIG += hide_symbols diff --git a/src/libs/clangbackendipc/connectionclient.cpp b/src/libs/clangbackendipc/connectionclient.cpp index 0b803b8a407..90a6d4a4718 100644 --- a/src/libs/clangbackendipc/connectionclient.cpp +++ b/src/libs/clangbackendipc/connectionclient.cpp @@ -54,7 +54,9 @@ QString connectionName() ConnectionClient::ConnectionClient(IpcClientInterface *client) : serverProxy_(client, &localSocket), - isAliveTimerResetted(false) + isAliveTimerResetted(false), + stdErrPrefixer("ClangBackEnd-StdErr: "), + stdOutPrefixer("ClangBackEnd: ") { processAliveTimer.setInterval(10000); @@ -204,12 +206,12 @@ void ConnectionClient::printLocalSocketError(QLocalSocket::LocalSocketError sock void ConnectionClient::printStandardOutput() { - qWarning() << "ClangBackEnd:" << process_->readAllStandardOutput(); + QTextStream(stdout) << stdOutPrefixer.prefix(process_->readAllStandardOutput()); } void ConnectionClient::printStandardError() { - qWarning() << "ClangBackEnd Error:" << process_->readAllStandardError(); + QTextStream(stderr) << stdErrPrefixer.prefix(process_->readAllStandardError()); } void ConnectionClient::finishProcess() diff --git a/src/libs/clangbackendipc/connectionclient.h b/src/libs/clangbackendipc/connectionclient.h index a3dd243a844..eaf4b227bf8 100644 --- a/src/libs/clangbackendipc/connectionclient.h +++ b/src/libs/clangbackendipc/connectionclient.h @@ -32,6 +32,7 @@ #define CLANGBACKEND_CONNECTIONCLIENT_H #include "ipcserverproxy.h" +#include "lineprefixer.h" #include @@ -106,6 +107,9 @@ private: QTimer processAliveTimer; QString processPath_; bool isAliveTimerResetted; + + LinePrefixer stdErrPrefixer; + LinePrefixer stdOutPrefixer; }; } // namespace ClangBackEnd diff --git a/src/libs/clangbackendipc/lineprefixer.cpp b/src/libs/clangbackendipc/lineprefixer.cpp new file mode 100644 index 00000000000..0c8c9faa5b4 --- /dev/null +++ b/src/libs/clangbackendipc/lineprefixer.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing +** +** 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 The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/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 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#include "lineprefixer.h" + +namespace ClangBackEnd { + +LinePrefixer::LinePrefixer(const QByteArray &prefix) + : m_prefix(prefix) + , m_previousIsEndingWithNewLine(true) +{} + +QByteArray LinePrefixer::prefix(const QByteArray &text) +{ + QByteArray output = text; + + if (m_previousIsEndingWithNewLine) + output.prepend(m_prefix); + + if (output.endsWith('\n')) { + m_previousIsEndingWithNewLine = true; + output.chop(1); + } else { + m_previousIsEndingWithNewLine = false; + } + + output.replace("\n", "\n" + m_prefix); + if (m_previousIsEndingWithNewLine) + output.append('\n'); + + return output; +} + +} // namespace ClangBackEnd diff --git a/src/libs/clangbackendipc/lineprefixer.h b/src/libs/clangbackendipc/lineprefixer.h new file mode 100644 index 00000000000..ae8183a0e14 --- /dev/null +++ b/src/libs/clangbackendipc/lineprefixer.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing +** +** 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 The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/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 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + + +#ifndef PRINTLINESWITHPREFIX_H +#define PRINTLINESWITHPREFIX_H + +#include +#include +#include + +namespace ClangBackEnd { + +class LinePrefixer +{ +public: + LinePrefixer() = delete; + LinePrefixer(const QByteArray &m_prefix); + QByteArray prefix(const QByteArray &text); + +private: + QByteArray m_prefix; + bool m_previousIsEndingWithNewLine; +}; + +} // namespace ClangBackEnd + +#endif // PRINTLINESWITHPREFIX_H diff --git a/tests/unit/unittest/lineprefixertest.cpp b/tests/unit/unittest/lineprefixertest.cpp new file mode 100644 index 00000000000..6f205e924de --- /dev/null +++ b/tests/unit/unittest/lineprefixertest.cpp @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt.io/licensing +** +** 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://www.qt.io/licensing. For further information +** use the contact form at http://www.qt.io/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 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** 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 +#include + +#include +#include +#include + +namespace { + +QByteArray runPrefixer(QList inputChunks) +{ + QByteArray actualOutput; + ClangBackEnd::LinePrefixer prefixer("PREFIX "); + foreach (const QByteArray &chunk, inputChunks) + actualOutput += prefixer.prefix(chunk); + return actualOutput; +} + +TEST(LinePrefixer, OneChunkEndsWithNewline) +{ + const QList inputChunks { "hello\n" }; + ASSERT_THAT(Utf8String::fromByteArray(runPrefixer(inputChunks)), + Utf8String::fromUtf8("PREFIX hello\n")); +} + +TEST(LinePrefixer, OneChunkEndsWithoutNewline) +{ + const QList inputChunks { "hello" }; + ASSERT_THAT(Utf8String::fromByteArray(runPrefixer(inputChunks)), + Utf8String::fromUtf8("PREFIX hello")); +} + +TEST(LinePrefixer, OneChunkStartsWithNewline) +{ + const QList inputChunks { "\nhello" }; + ASSERT_THAT(Utf8String::fromByteArray(runPrefixer(inputChunks)), + Utf8String::fromUtf8("PREFIX \n" + "PREFIX hello")); +} + +TEST(LinePrefixer, OneChunkStartsAndEndsWithNewline) +{ + const QList inputChunks { "\nhello\n" }; + ASSERT_THAT(Utf8String::fromByteArray(runPrefixer(inputChunks)), + Utf8String::fromUtf8("PREFIX \n" + "PREFIX hello\n")); +} + +TEST(LinePrefixer, OneChunkEndsWithExtraNewline) +{ + const QList inputChunks { "hello\n\n" }; + ASSERT_THAT(Utf8String::fromByteArray(runPrefixer(inputChunks)), + Utf8String::fromUtf8("PREFIX hello\n" + "PREFIX \n")); +} + +TEST(LinePrefixer, OneChunkEndsWithTwoExtraNewlines) +{ + const QList inputChunks { "hello\n\n\n" }; + ASSERT_THAT(Utf8String::fromByteArray(runPrefixer(inputChunks)), + Utf8String::fromUtf8("PREFIX hello\n" + "PREFIX \n" + "PREFIX \n")); +} + +TEST(LinePrefixer, ChunkWithoutNewlineAndChunkWithNewline) +{ + const QList inputChunks { "hello", "\n" }; + ASSERT_THAT(Utf8String::fromByteArray(runPrefixer(inputChunks)), + Utf8String::fromUtf8("PREFIX hello\n")); +} + +TEST(LinePrefixer, ChunkWithoutNewlineAndChunkWithTwoNewlines) +{ + const QList inputChunks { "hello", "\n\n" }; + ASSERT_THAT(Utf8String::fromByteArray(runPrefixer(inputChunks)), + Utf8String::fromUtf8("PREFIX hello\n" + "PREFIX \n")); +} + +TEST(LinePrefixer, ChunkWithTwoNewlinesAndChunkWithoutNewline) +{ + const QList inputChunks { "\n\n", "hello" }; + ASSERT_THAT(Utf8String::fromByteArray(runPrefixer(inputChunks)), + Utf8String::fromUtf8("PREFIX \n" + "PREFIX \n" + "PREFIX hello")); +} + +} // anonymous namespace diff --git a/tests/unit/unittest/unittest.pro b/tests/unit/unittest/unittest.pro index a7cab75de44..8bdec64110a 100644 --- a/tests/unit/unittest/unittest.pro +++ b/tests/unit/unittest/unittest.pro @@ -56,7 +56,8 @@ SOURCES += main.cpp \ projecttest.cpp \ clangipcservertest.cpp \ translationunitstest.cpp \ - completionchunkstotextconvertertest.cpp + completionchunkstotextconvertertest.cpp \ + lineprefixertest.cpp HEADERS += \ gtest-qt-printing.h \