forked from qt-creator/qt-creator
Clang: Add SymbolQuery
Change-Id: I5cb81dffd6f1fda6bdcba0eedaf79f0bc91348b5 Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -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
|
||||
|
||||
54
src/plugins/clangrefactoring/querysqlitestatementfactory.h
Normal file
54
src/plugins/clangrefactoring/querysqlitestatementfactory.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
57
src/plugins/clangrefactoring/sourcelocations.h
Normal file
57
src/plugins/clangrefactoring/sourcelocations.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
30
src/plugins/clangrefactoring/symbolquery.cpp
Normal file
30
src/plugins/clangrefactoring/symbolquery.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
110
src/plugins/clangrefactoring/symbolquery.h
Normal file
110
src/plugins/clangrefactoring/symbolquery.h
Normal file
@@ -0,0 +1,110 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
Reference in New Issue
Block a user