Clang: Filter duplicates in clang query output

Because we can visit headers many times, we get results many times too.

Change-Id: I3bbe7d7a5d01c2580a4569bfe115f14a69edc8a7
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Tim Jenssen
2017-07-03 11:53:47 +02:00
parent 07c3a37054
commit 23cae9a9e0
17 changed files with 347 additions and 23 deletions

View File

@@ -34,6 +34,7 @@ ClangQueryGatherer::ClangQueryGatherer(StringCache<Utils::PathString, std::mutex
std::vector<V2::FileContainer> &&unsaved,
Utils::SmallString &&query)
: m_filePathCache(filePathCache),
m_sourceRangeFilter(sources.size()),
m_sources(std::move(sources)),
m_unsaved(std::move(unsaved)),
m_query(std::move(query))
@@ -125,7 +126,7 @@ std::vector<SourceRangesAndDiagnosticsForQueryMessage> ClangQueryGatherer::allCu
std::vector<SourceRangesAndDiagnosticsForQueryMessage> messages;
for (Future &future : m_sourceFutures)
messages.push_back(future.get());
messages.push_back(m_sourceRangeFilter.removeDuplicates(future.get()));
return messages;
}
@@ -135,7 +136,7 @@ std::vector<SourceRangesAndDiagnosticsForQueryMessage> ClangQueryGatherer::finis
std::vector<SourceRangesAndDiagnosticsForQueryMessage> messages;
for (auto &&future : finishedFutures())
messages.push_back(future.get());
messages.push_back(m_sourceRangeFilter.removeDuplicates(future.get()));
return messages;
}

View File

@@ -25,6 +25,8 @@
#pragma once
#include "sourcerangefilter.h"
#include <sourcerangesanddiagnosticsforquerymessage.h>
#include <filecontainerv2.h>
#include <stringcache.h>
@@ -68,6 +70,7 @@ protected:
private:
StringCache<Utils::PathString, std::mutex> *m_filePathCache = nullptr;
SourceRangeFilter m_sourceRangeFilter;
std::vector<V2::FileContainer> m_sources;
std::vector<V2::FileContainer> m_unsaved;
Utils::SmallString m_query;

View File

@@ -1,7 +1,8 @@
INCLUDEPATH += $$PWD
HEADERS += \
$$PWD/clangrefactoringbackend_global.h
$$PWD/clangrefactoringbackend_global.h \
$$PWD/sourcerangefilter.h
!isEmpty(LIBTOOLING_LIBS) {
SOURCES += \
@@ -33,3 +34,6 @@ HEADERS += \
$$PWD/locationsourcefilecallbacks.h \
$$PWD/clangquerygatherer.h
}
SOURCES += \
$$PWD/sourcerangefilter.cpp

View File

@@ -115,6 +115,11 @@ bool RefactoringServer::pollTimerIsActive() const
return m_pollTimer.isActive();
}
void RefactoringServer::setGathererProcessingSlotCount(uint count)
{
m_gatherer.setProcessingSlotCount(count);
}
void RefactoringServer::gatherSourceRangesAndDiagnosticsForQueryMessages(
std::vector<V2::FileContainer> &&sources,
std::vector<V2::FileContainer> &&unsaved,

View File

@@ -64,6 +64,8 @@ public:
bool pollTimerIsActive() const;
void setGathererProcessingSlotCount(uint count);
private:
void gatherSourceRangesAndDiagnosticsForQueryMessages(std::vector<V2::FileContainer> &&sources,
std::vector<V2::FileContainer> &&unsaved,

View File

@@ -0,0 +1,59 @@
/****************************************************************************
**
** Copyright (C) 2017 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 "sourcerangefilter.h"
#include <algorithm>
namespace ClangBackEnd {
SourceRangeFilter::SourceRangeFilter(std::size_t sourcesCount)
{
m_collectedSourceRanges.reserve(sourcesCount);
}
SourceRangesAndDiagnosticsForQueryMessage SourceRangeFilter::removeDuplicates(SourceRangesAndDiagnosticsForQueryMessage &&message)
{
removeDuplicates(message.sourceRanges().sourceRangeWithTextContainers());
return std::move(message);
}
void SourceRangeFilter::removeDuplicates(SourceRangeWithTextContainers &sourceRanges)
{
auto partitionPoint = std::stable_partition(sourceRanges.begin(),
sourceRanges.end(),
[&] (const SourceRangeWithTextContainer &sourceRange) {
return m_collectedSourceRanges.find(sourceRange) == m_collectedSourceRanges.end();
});
sourceRanges.erase(partitionPoint, sourceRanges.end());
std::copy(sourceRanges.begin(),
sourceRanges.end(),
std::inserter(m_collectedSourceRanges, m_collectedSourceRanges.end()));
}
} // namespace ClangBackEnd

View File

@@ -0,0 +1,47 @@
/****************************************************************************
**
** Copyright (C) 2017 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 <unordered_set>
#include <sourcerangesanddiagnosticsforquerymessage.h>
namespace ClangBackEnd {
class SourceRangeFilter
{
public:
SourceRangeFilter(std::size_t sourcesCount = 0);
SourceRangesAndDiagnosticsForQueryMessage
removeDuplicates(SourceRangesAndDiagnosticsForQueryMessage &&message);
void removeDuplicates(SourceRangeWithTextContainers &sourceRanges);
private:
std::unordered_set<SourceRangeWithTextContainer> m_collectedSourceRanges;
};
} // namespace ClangBackEnd