Commit 7f757884 authored by Tim Jenssen's avatar Tim Jenssen
Browse files

Clang: Extend clang query



It's a first step to introduce clang query.

Change-Id: I4d001a8883f56066765ce6bc561fa3f49611c0a4
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 52fc4a4e
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://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 https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "cancelmessage.h"
#include <QDebug>
namespace ClangBackEnd {
QDebug operator<<(QDebug debug, const CancelMessage &)
{
debug.nospace() << "CancelMessage()";
return debug;
}
void PrintTo(const CancelMessage &, ::std::ostream* os)
{
*os << "CancelMessage()";
}
} // namespace ClangBackEnd
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://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 https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "clangbackendipc_global.h"
namespace ClangBackEnd {
class CancelMessage
{
public:
friend QDataStream &operator<<(QDataStream &out, const CancelMessage &/*message*/)
{
return out;
}
friend QDataStream &operator>>(QDataStream &in, CancelMessage &/*message*/)
{
return in;
}
friend bool operator==(const CancelMessage &/*first*/, const CancelMessage &/*second*/)
{
return true;
}
};
CMBIPC_EXPORT QDebug operator<<(QDebug debug, const CancelMessage &message);
void PrintTo(const CancelMessage &message, ::std::ostream* os);
DECLARE_MESSAGE(CancelMessage)
} // namespace ClangBackEnd
......@@ -59,6 +59,7 @@ SOURCES += $$PWD/clangcodemodelserverinterface.cpp \
$$PWD/requestsourcelocationforrenamingmessage.cpp \
$$PWD/filepath.cpp \
$$PWD/sourcerangescontainer.cpp \
$$PWD/sourcefilepathcontainerbase.cpp \
$$PWD/sourcerangecontainerv2.cpp \
$$PWD/dynamicastmatcherdiagnosticcontainer.cpp \
$$PWD/dynamicastmatcherdiagnosticcontextcontainer.cpp \
......@@ -66,7 +67,8 @@ SOURCES += $$PWD/clangcodemodelserverinterface.cpp \
$$PWD/requestsourcerangesanddiagnosticsforquerymessage.cpp \
$$PWD/sourcerangesanddiagnosticsforquerymessage.cpp \
$$PWD/sourcerangewithtextcontainer.cpp \
$$PWD/filecontainerv2.cpp
$$PWD/filecontainerv2.cpp \
$$PWD/cancelmessage.cpp
HEADERS += \
$$PWD/clangcodemodelserverinterface.h \
......@@ -130,6 +132,7 @@ HEADERS += \
$$PWD/requestsourcerangesanddiagnosticsforquerymessage.h \
$$PWD/sourcerangesanddiagnosticsforquerymessage.h \
$$PWD/sourcerangewithtextcontainer.h \
$$PWD/filecontainerv2.h
$$PWD/filecontainerv2.h \
$$PWD/cancelmessage.h
contains(QT_CONFIG, reduce_exports):CONFIG += hide_symbols
......@@ -46,7 +46,7 @@
#endif
namespace Utils {
template<uint Size>
template <uint Size>
class BasicSmallString;
using SmallString = BasicSmallString<31>;
}
......@@ -122,7 +122,9 @@ enum class MessageType : quint8 {
RequestSourceLocationsForRenamingMessage,
RequestSourceRangesAndDiagnosticsForQueryMessage,
SourceRangesAndDiagnosticsForQueryMessage
SourceRangesAndDiagnosticsForQueryMessage,
CancelMessage
};
template<MessageType messageEnumeration>
......
......@@ -28,6 +28,7 @@
#include "messageenvelop.h"
#include "requestsourcelocationforrenamingmessage.h"
#include "requestsourcerangesanddiagnosticsforquerymessage.h"
#include "cancelmessage.h"
#include <QDebug>
......@@ -45,6 +46,9 @@ void RefactoringServerInterface::dispatch(const MessageEnvelop &messageEnvelop)
case MessageType::RequestSourceRangesAndDiagnosticsForQueryMessage:
requestSourceRangesAndDiagnosticsForQueryMessage(messageEnvelop.message<RequestSourceRangesAndDiagnosticsForQueryMessage>());
break;
case MessageType::CancelMessage:
cancel();
break;
default:
qWarning() << "Unknown IpcClientMessage";
}
......
......@@ -34,6 +34,7 @@ namespace ClangBackEnd {
class RefactoringClientInterface;
class RequestSourceLocationsForRenamingMessage;
class RequestSourceRangesAndDiagnosticsForQueryMessage;
class CancelMessage;
class CMBIPC_EXPORT RefactoringServerInterface : public IpcServerInterface<RefactoringClientInterface>
{
......@@ -43,6 +44,7 @@ public:
virtual void end() = 0;
virtual void requestSourceLocationsForRenamingMessage(RequestSourceLocationsForRenamingMessage &&message) = 0;
virtual void requestSourceRangesAndDiagnosticsForQueryMessage(RequestSourceRangesAndDiagnosticsForQueryMessage &&message) = 0;
virtual void cancel() = 0;
bool isUsable() const
{
......
......@@ -25,6 +25,7 @@
#include "refactoringserverproxy.h"
#include "cancelmessage.h"
#include "cmbendmessage.h"
#include "messageenvelop.h"
#include "refactoringclientinterface.h"
......@@ -59,6 +60,11 @@ void RefactoringServerProxy::requestSourceRangesAndDiagnosticsForQueryMessage(Re
writeMessageBlock.write(message);
}
void RefactoringServerProxy::cancel()
{
writeMessageBlock.write(CancelMessage());
}
void RefactoringServerProxy::readMessages()
{
for (const auto &message : readMessageBlock.readAll())
......
......@@ -42,7 +42,7 @@ namespace ClangBackEnd {
class RefactoringClientInterface;
class CMBIPC_EXPORT RefactoringServerProxy : public RefactoringServerInterface
class CMBIPC_EXPORT RefactoringServerProxy final : public RefactoringServerInterface
{
public:
explicit RefactoringServerProxy(RefactoringClientInterface *client, QIODevice *ioDevice);
......@@ -50,8 +50,9 @@ public:
const RefactoringServerProxy &operator=(const RefactoringServerProxy&) = delete;
void end() override;
void requestSourceLocationsForRenamingMessage(ClangBackEnd::RequestSourceLocationsForRenamingMessage &&message) override;
void requestSourceRangesAndDiagnosticsForQueryMessage(RequestSourceRangesAndDiagnosticsForQueryMessage &&message);
void requestSourceLocationsForRenamingMessage(RequestSourceLocationsForRenamingMessage &&message) override;
void requestSourceRangesAndDiagnosticsForQueryMessage(RequestSourceRangesAndDiagnosticsForQueryMessage &&message) override;
void cancel() override;
void readMessages();
......
......@@ -25,13 +25,11 @@
#pragma once
#include "clangbackendipc_global.h"
#include "filecontainerv2.h"
namespace ClangBackEnd {
class CMBIPC_EXPORT RequestSourceRangesAndDiagnosticsForQueryMessage
class RequestSourceRangesAndDiagnosticsForQueryMessage
{
public:
RequestSourceRangesAndDiagnosticsForQueryMessage() = default;
......
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://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 https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "sourcefilepathcontainerbase.h"
namespace ClangBackEnd {
} // namespace ClangBackEnd
......@@ -44,9 +44,11 @@ public:
void insertFilePath(uint fileId, Utils::SmallString &&fileDirectory, Utils::SmallString &&fileName)
{
filePathHash.emplace(std::piecewise_construct,
std::forward_as_tuple(fileId),
std::forward_as_tuple(std::move(fileDirectory), std::move(fileName)));
if (filePathHash.find(fileId) == filePathHash.end()) {
filePathHash.emplace(std::piecewise_construct,
std::forward_as_tuple(fileId),
std::forward_as_tuple(std::move(fileDirectory), std::move(fileName)));
}
}
void reserve(std::size_t size)
......
......@@ -25,6 +25,10 @@
#include "sourcerangewithtextcontainer.h"
#ifdef UNIT_TESTS
#include <gtest/gtest.h>
#endif
namespace ClangBackEnd {
QDebug operator<<(QDebug debug, const SourceRangeWithTextContainer &container)
......@@ -40,12 +44,18 @@ QDebug operator<<(QDebug debug, const SourceRangeWithTextContainer &container)
void PrintTo(const SourceRangeWithTextContainer &container, ::std::ostream* os)
{
Q_UNUSED(container)
Q_UNUSED(os)
#ifdef UNIT_TESTS
*os << "(("
<< container.start().line() << ", "
<< container.start().column() << "), ("
<< container.start().column() << ", "
<< container.start().offset() << "), ("
<< container.end().line() << ", "
<< container.end().column() << ", "
<< "\"" << container.text() << "\""
<< "))";
<< container.end().offset() << "), "
<< testing::PrintToString(container.text())
<< ")";
#endif
}
} // namespace ClangBackEnd
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://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 https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "clangquerycurrentfilefindfilter.h"
#include "projectpartutilities.h"
#include "refactoringclient.h"
#include "refactoringcompileroptionsbuilder.h"
#include "searchinterface.h"
#include <refactoringserverinterface.h>
#include <requestsourcerangesanddiagnosticsforquerymessage.h>
namespace ClangRefactoring {
ClangQueryCurrentFileFindFilter::ClangQueryCurrentFileFindFilter(
ClangBackEnd::RefactoringServerInterface &server,
SearchInterface &searchInterface,
RefactoringClient &refactoringClient)
: server(server),
searchInterface(searchInterface),
refactoringClient(refactoringClient)
{
}
QString ClangQueryCurrentFileFindFilter::id() const
{
return QStringLiteral("Clang Query Current File");
}
QString ClangQueryCurrentFileFindFilter::displayName() const
{
return tr("Clang Query Current File");
}
bool ClangQueryCurrentFileFindFilter::isEnabled() const
{
return true;
}
void ClangQueryCurrentFileFindFilter::findAll(const QString &queryText, Core::FindFlags)
{
searchHandle = searchInterface.startNewSearch(tr("Clang Query"), queryText);
refactoringClient.setSearchHandle(searchHandle.get());
server.requestSourceRangesAndDiagnosticsForQueryMessage(createMessage(queryText));
}
Core::FindFlags ClangQueryCurrentFileFindFilter::supportedFindFlags() const
{
return 0;
}
void ClangQueryCurrentFileFindFilter::setCurrentDocumentFilePath(const QString &filePath)
{
currentDocumentFilePath = filePath;
}
void ClangQueryCurrentFileFindFilter::setUnsavedDocumentContent(const QString &unsavedContent)
{
unsavedDocumentContent = unsavedContent;
}
void ClangQueryCurrentFileFindFilter::setProjectPart(const CppTools::ProjectPart::Ptr &projectPart)
{
this->projectPart = projectPart;
}
void ClangQueryCurrentFileFindFilter::setUsable(bool isUsable)
{
server.setUsable(isUsable);
}
bool ClangQueryCurrentFileFindFilter::isUsable() const
{
return server.isUsable();
}
ClangBackEnd::RequestSourceRangesAndDiagnosticsForQueryMessage
ClangQueryCurrentFileFindFilter::createMessage(const QString &queryText) const
{
std::vector<ClangBackEnd::V2::FileContainer> fileContainers;
fileContainers.emplace_back(ClangBackEnd::FilePath(currentDocumentFilePath),
unsavedDocumentContent,
createCommandLine());
return ClangBackEnd::RequestSourceRangesAndDiagnosticsForQueryMessage(
Utils::SmallString(queryText),
std::move(fileContainers));
}
Utils::SmallStringVector ClangQueryCurrentFileFindFilter::createCommandLine() const
{
using ClangRefactoring::RefactoringCompilerOptionsBuilder;
auto commandLine = RefactoringCompilerOptionsBuilder::build(
projectPart.data(),
fileKindInProjectPart(projectPart.data(), currentDocumentFilePath),
RefactoringCompilerOptionsBuilder::PchUsage::None);
commandLine.push_back(currentDocumentFilePath);
return commandLine;
}
} // namespace ClangRefactoring
......@@ -25,10 +25,13 @@
#include "clangqueryprojectsfindfilter.h"
#include "projectpartutilities.h"
#include "refactoringclient.h"
#include "refactoringcompileroptionsbuilder.h"
#include "searchinterface.h"
#include <refactoringserverinterface.h>
#include <requestsourcerangesanddiagnosticsforquerymessage.h>
namespace ClangRefactoring {
......@@ -61,9 +64,15 @@ void ClangQueryProjectsFindFilter::findAll(const QString &queryText, Core::FindF
{
searchHandle = searchInterface.startNewSearch(tr("Clang Query"), queryText);
searchHandle->setRefactoringServer(&server);
refactoringClient.setSearchHandle(searchHandle.get());
//server.requestSourceRangesAndDiagnosticsForQueryMessage(createMessage(queryText));
auto message = createMessage(queryText);
refactoringClient.setExpectedResultCount(message.fileContainers().size());
server.requestSourceRangesAndDiagnosticsForQueryMessage(std::move(message));
}
Core::FindFlags ClangQueryProjectsFindFilter::supportedFindFlags() const
......@@ -71,6 +80,11 @@ Core::FindFlags ClangQueryProjectsFindFilter::supportedFindFlags() const
return 0;
}
void ClangQueryProjectsFindFilter::setProjectParts(const std::vector<CppTools::ProjectPart::Ptr> &projectParts)
{
this->projectParts = projectParts;
}
bool ClangQueryProjectsFindFilter::isUsable() const
{
return server.isUsable();
......@@ -81,4 +95,53 @@ void ClangQueryProjectsFindFilter::setUsable(bool isUsable)
server.setUsable(isUsable);
}
SearchHandle *ClangQueryProjectsFindFilter::searchHandleForTestOnly() const
{
return searchHandle.get();
}
namespace {
Utils::SmallStringVector createCommandLine(CppTools::ProjectPart *projectPart,
const QString &documentFilePath,
CppTools::ProjectFile::Kind fileKind)
{
using ClangRefactoring::RefactoringCompilerOptionsBuilder;
auto commandLine = RefactoringCompilerOptionsBuilder::build(projectPart,
fileKind,
CppTools::CompilerOptionsBuilder::PchUsage::None);
commandLine.push_back(documentFilePath);
return commandLine;
}
std::vector<ClangBackEnd::V2::FileContainer>
createFileContainers(const std::vector<CppTools::ProjectPart::Ptr> &projectParts)
{
std::vector<ClangBackEnd::V2::FileContainer> fileContainers;
for (const CppTools::ProjectPart::Ptr &projectPart : projectParts) {
for (const CppTools::ProjectFile &projectFile : projectPart->files) {
fileContainers.emplace_back(ClangBackEnd::FilePath(projectFile.path),
"",
createCommandLine(projectPart.data(),
projectFile.path,
projectFile.kind));
}
}
return fileContainers;
}
}
ClangBackEnd::RequestSourceRangesAndDiagnosticsForQueryMessage ClangQueryProjectsFindFilter::createMessage(const QString &queryText) const
{
return ClangBackEnd::RequestSourceRangesAndDiagnosticsForQueryMessage(
Utils::SmallString(queryText),
createFileContainers(projectParts));
}
} // namespace ClangRefactoring
......@@ -25,10 +25,14 @@
#pragma once
#include "searchhandleinterface.h"
#include "searchhandle.h"
#include <cpptools/projectpart.h>
#include <coreplugin/find/ifindfilter.h>
#include <utils/smallstringvector.h>
#include <memory>
namespace ClangBackEnd {
......@@ -54,11 +58,20 @@ public:
void findAll(const QString &queryText, Core::FindFlags findFlags = 0);
Core::FindFlags supportedFindFlags() const;
void setProjectParts(const std::vector<CppTools::ProjectPart::Ptr> &projectParts);
bool isUsable() const;
void setUsable(bool isUsable);
SearchHandle* searchHandleForTestOnly() const;
private:
ClangBackEnd::RequestSourceRangesAndDiagnosticsForQueryMessage createMessage(
const QString &queryText) const;