Commit a307ee74 authored by Marco Bubke's avatar Marco Bubke

Clang: Add SymbolQuery

Change-Id: I5cb81dffd6f1fda6bdcba0eedaf79f0bc91348b5
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent f49a1d72
......@@ -15,7 +15,10 @@ HEADERS += \
clangqueryexampletexteditorwidget.h \
clangquerytexteditorwidget.h \
baseclangquerytexteditorwidget.h \
clangqueryhoverhandler.h
clangqueryhoverhandler.h \
symbolquery.h \
querysqlitestatementfactory.h \
sourcelocations.h
SOURCES += \
clangrefactoringplugin.cpp \
......@@ -26,7 +29,8 @@ SOURCES += \
clangqueryexampletexteditorwidget.cpp \
clangquerytexteditorwidget.cpp \
baseclangquerytexteditorwidget.cpp \
clangqueryhoverhandler.cpp
clangqueryhoverhandler.cpp \
symbolquery.cpp
FORMS += \
clangqueryprojectsfindfilter.ui
/****************************************************************************
**
** 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
namespace ClangRefactoring {
template<typename Database,
typename ReadStatement>
class QuerySqliteStatementFactory
{
public:
using DatabaseType = Database;
using ReadStatementType = ReadStatement;
QuerySqliteStatementFactory(Database &database)
: database(database)
{}
Database &database;
ReadStatement selectLocationsForSymbolLocation{
"SELECT sourceId, line, column FROM locations WHERE symbolId = "
" (SELECT symbolId FROM locations WHERE sourceId="
" (SELECT sourceId FROM sources WHERE sourcePath =?)"
" AND line=? AND column=?) "
"ORDER BY sourceId, line, column",
database}; // alternatively SELECT l2.symbolid, l2.sourceId, l2.line, l2.column FROM locations AS l2, sources, locations AS l1 ON sources.sourceId = l1.sourceId AND l2.symbolId=l1.symbolId WHERE sourcePath = ? AND l1.line=? AND l1.column=? ORDER BY l2.sourceId, l2.line, l2.column
ReadStatement selectSourcePathForId{
"SELECT sourceId, sourcePath FROM sources WHERE sourceId = ?",
database};
};
} // namespace ClangRefactoring
/****************************************************************************
**
** 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 <utils/smallstring.h>
#include <cstdint>
#include <vector>
#include <tuple>
#include <unordered_map>
namespace ClangRefactoring {
class SourceLocations
{
public:
struct Location
{
qint64 sourceId;
qint64 line;
qint64 column;
};
struct Source
{
qint64 sourceId;
Utils::PathString sourcePath;
};
std::vector<Location> locations;
std::unordered_map<qint64, Utils::PathString> sources;
};
} // namespace ClangRefactoring
/****************************************************************************
**
** 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 "symbolquery.h"
namespace ClangRefactoring {
} // namespace ClangRefactoring
/****************************************************************************
**
** 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 <utils/smallstring.h>
#include <sourcelocations.h>
#include <algorithm>
namespace ClangRefactoring {
template <typename StatementFactory>
class SymbolQuery
{
using ReadStatement = typename StatementFactory::ReadStatementType;
public:
using Location = SourceLocations::Location;
using Source = SourceLocations::Source;
SymbolQuery(StatementFactory &statementFactory)
: m_statementFactory(statementFactory)
{}
SourceLocations locationsAt(const Utils::PathString &filePath, uint line, uint utf8Column)
{
ReadStatement &locationsStatement = m_statementFactory.selectLocationsForSymbolLocation;
const std::size_t reserveSize = 128;
auto locations = locationsStatement.template structValues<Location, qint64, qint64, qint64>(
reserveSize,
filePath,
line,
utf8Column);
const std::vector<qint64> sourceIds = uniqueSourceIds(locations);
ReadStatement &sourcesStatement = m_statementFactory.selectSourcePathForId;
auto sources = sourcesStatement.template structValues<Source, qint64, Utils::PathString>(
reserveSize,
sourceIds);
return {locations, sourcesToHashMap(sources)};
}
static
qint64 sourceId(const Location &location)
{
return location.sourceId;
}
static
std::vector<qint64> uniqueSourceIds(const std::vector<Location> &locations)
{
std::vector<qint64> ids;
ids.reserve(locations.size());
std::transform(locations.begin(),
locations.end(),
std::back_inserter(ids),
sourceId);
auto newEnd = std::unique(ids.begin(), ids.end());
ids.erase(newEnd, ids.end());
return ids;
}
static
std::unordered_map<qint64, Utils::PathString> sourcesToHashMap(const std::vector<Source> &sources)
{
std::unordered_map<qint64, Utils::PathString> dictonary;
for (const Source &source : sources)
dictonary.emplace(source.sourceId, std::move(source.sourcePath));
return dictonary;
}
private:
StatementFactory &m_statementFactory;
};
} // namespace ClangRefactoring
......@@ -57,20 +57,21 @@ std::vector<FilePathIndex> MockSqliteReadStatement::values<FilePathIndex>(std::s
}
template <>
std::vector<std::tuple<int64_t, int64_t, int64_t>>
MockSqliteReadStatement::tupleValues<int64_t, int64_t, int64_t>(
std::vector<Location>
MockSqliteReadStatement::structValues<Location, qint64, qint64, qint64>(
std::size_t reserveSize,
const Utils::PathString &sourcePath,
const uint &line,
const uint &column)
{
return valuesReturnStdVectorTupleInt64Int64Int64(reserveSize, sourcePath, line, column);
return structValuesReturnStdVectorLocation(reserveSize, sourcePath, line, column);
}
template <>
std::vector<std::tuple<int64_t, Utils::PathString>>
MockSqliteReadStatement::tupleValues<int64_t, Utils::PathString>(std::size_t reserveSize,
const std::vector<int64_t> &sourceIds)
std::vector<Source>
MockSqliteReadStatement::structValues<Source, qint64, Utils::PathString>(
std::size_t reserveSize,
const std::vector<qint64> &sourceIds)
{
return valuesReturnStdVectorTupleInt64PathString(reserveSize, sourceIds);
return structValuesReturnStdVectorSource(reserveSize, sourceIds);
}
......@@ -27,6 +27,8 @@
#include <stringcachefwd.h>
#include <sourcelocations.h>
#include "mocksqlitedatabase.h"
#include <utils/smallstring.h>
......@@ -37,6 +39,8 @@
using std::int64_t;
using ClangBackEnd::FilePathIndex;
using Location = ClangRefactoring::SourceLocations::Location;
using Source = ClangRefactoring::SourceLocations::Source;
class MockSqliteReadStatement
{
......@@ -49,24 +53,27 @@ public:
MOCK_CONST_METHOD1(valuesReturnStdVectorInt,
std::vector<FilePathIndex>(std::size_t));
MOCK_CONST_METHOD4(valuesReturnStdVectorTupleInt64Int64Int64,
std::vector<std::tuple<int64_t, int64_t, int64_t>>(std::size_t, Utils::SmallStringView, int64_t, int64_t));
MOCK_CONST_METHOD4(structValuesReturnStdVectorLocation,
std::vector<Location>(std::size_t, Utils::SmallStringView, qint64, qint64));
MOCK_CONST_METHOD2(valuesReturnStdVectorTupleInt64PathString,
std::vector<std::tuple<int64_t, Utils::PathString>>(std::size_t, const std::vector<int64_t> &));
MOCK_CONST_METHOD2(structValuesReturnStdVectorSource,
std::vector<Source>(std::size_t, const std::vector<qint64> &));
template <typename ResultType,
typename... QueryType>
std::vector<ResultType> values(std::size_t, QueryType...);
template <typename... ResultType,
template <typename ResultType,
typename... ResultEntryType,
typename... QueryType>
std::vector<std::tuple<ResultType...>> tupleValues(std::size_t reserveSize, const QueryType&... queryValues);
std::vector<ResultType> structValues(std::size_t reserveSize, const QueryType&... queryValues);
template <typename... ResultType,
template <typename...> class ContainerType,
typename ElementType>
std::vector<std::tuple<ResultType...>> tupleValues(std::size_t reserveSize, const ContainerType<ElementType> &queryValues);
template <typename ResultType,
typename... ResultEntryType,
template <typename...> class QueryContainerType,
typename QueryElementType>
std::vector<ResultType> structValues(std::size_t reserveSize,
const QueryContainerType<QueryElementType> &queryValues);
public:
......@@ -77,16 +84,17 @@ template <>
std::vector<int> MockSqliteReadStatement::values<int>(std::size_t reserveSize);
template <>
std::vector<std::tuple<int64_t, int64_t, int64_t>>
MockSqliteReadStatement::tupleValues<int64_t, int64_t, int64_t>(
std::vector<Location>
MockSqliteReadStatement::structValues<Location, qint64, qint64, qint64>(
std::size_t reserveSize,
const Utils::PathString &sourcePath,
const uint &line,
const uint &column);
template <>
std::vector<std::tuple<int64_t, Utils::PathString>>
MockSqliteReadStatement::tupleValues<int64_t, Utils::PathString>(std::size_t reserveSize,
const std::vector<int64_t> &);
std::vector<Source>
MockSqliteReadStatement::structValues<Source, qint64, Utils::PathString>(
std::size_t reserveSize,
const std::vector<qint64> &);
/****************************************************************************
**
** 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 "googletest.h"
#include "mocksqlitedatabase.h"
#include "mocksqlitereadstatement.h"
#include "mocksqlitewritestatement.h"
#include <querysqlitestatementfactory.h>
namespace {
using StatementFactory = ClangRefactoring::QuerySqliteStatementFactory<MockSqliteDatabase,
MockSqliteReadStatement>;
class QuerySqliteStatementFactory : public testing::Test
{
protected:
MockSqliteDatabase mockDatabase;
StatementFactory factory{mockDatabase};
};
TEST_F(QuerySqliteStatementFactory, SelectLocationsForSymbolLocation)
{
ASSERT_THAT(factory.selectLocationsForSymbolLocation.sqlStatement,
"SELECT sourceId, line, column FROM locations WHERE symbolId = "
" (SELECT symbolId FROM locations WHERE sourceId="
" (SELECT sourceId FROM sources WHERE sourcePath =?)"
" AND line=? AND column=?) "
"ORDER BY sourceId, line, column");
}
TEST_F(QuerySqliteStatementFactory, SelectSourcePathForId)
{
ASSERT_THAT(factory.selectSourcePathForId.sqlStatement,
"SELECT sourceId, sourcePath FROM sources WHERE sourceId = ?");
}
}
/****************************************************************************
**
** 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 "googletest.h"
#include "mocksqlitedatabase.h"
#include "mocksqlitereadstatement.h"
#include <symbolquery.h>
#include <querysqlitestatementfactory.h>
#include <sqlitedatabase.h>
namespace {
using ClangRefactoring::QuerySqliteStatementFactory;
using Sqlite::SqliteDatabase;
using StatementFactory = QuerySqliteStatementFactory<MockSqliteDatabase,
MockSqliteReadStatement>;
using Query = ClangRefactoring::SymbolQuery<StatementFactory>;
using Location = Query::Location;
using Source = Query::Source;
class SymbolQuery : public testing::Test
{
protected:
NiceMock<MockSqliteDatabase> mockDatabase;
StatementFactory statementFactory{mockDatabase};
MockSqliteReadStatement &selectLocationsForSymbolLocation = statementFactory.selectLocationsForSymbolLocation;
MockSqliteReadStatement &selectSourcePathForId = statementFactory.selectSourcePathForId;
std::vector<Location> locations{{1, 1, 1},
{1, 2, 3},
{2, 1, 1},
{2, 3, 1},
{4, 1, 1},
{4, 1, 3}};
Query query{statementFactory};
};
TEST_F(SymbolQuery, LocationsAt)
{
EXPECT_CALL(selectLocationsForSymbolLocation, structValuesReturnStdVectorLocation(_, Eq("/path/to/file.cpp"), 14, 7))
.WillRepeatedly(Return(locations));
EXPECT_CALL(selectSourcePathForId, structValuesReturnStdVectorSource(_, ElementsAre(1, 2, 4)));
query.locationsAt("/path/to/file.cpp", 14, 7);
}
TEST_F(SymbolQuery, UniqueSourceIds)
{
auto uniqueSourceIds = query.uniqueSourceIds(locations);
ASSERT_THAT(uniqueSourceIds, ElementsAre(1, 2, 4));
}
TEST_F(SymbolQuery, SourcesToHashMap)
{
std::vector<Source> sources{{1, "/path/first"},
{2, "/path/second"},
{4, "/path/third"}};
auto hashMap = query.sourcesToHashMap(sources);
ASSERT_THAT(hashMap, UnorderedElementsAre(Pair(1, Eq("/path/first")),
Pair(2, Eq("/path/second")),
Pair(4, Eq("/path/third"))));
}
}
......@@ -71,8 +71,10 @@ SOURCES += \
unittests-main.cpp \
utf8-test.cpp \
symbolstorage-test.cpp \
mocksqlitereadstatement.cpp \
symbolquery-test.cpp \
storagesqlitestatementfactory-test.cpp \
mocksqlitereadstatement.cpp
querysqlitestatementfactory-test.cpp
!isEmpty(LIBCLANG_LIBS) {
SOURCES += \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment