Clang: Add symbol indexer

Change-Id: I8ff879631ae022bc870431628be002903360369c
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2017-08-02 16:00:55 +02:00
parent f0e00a8c25
commit 81d43b8a11
15 changed files with 469 additions and 10 deletions

View File

@@ -4,12 +4,15 @@ HEADERS += \
$$PWD/clangrefactoringbackend_global.h \
$$PWD/sourcerangefilter.h \
$$PWD/symbolscollector.h \
$$PWD/symbolindexer.h \
$$PWD/symbolentry.h \
$$PWD/collectsymbolsconsumer.h \
$$PWD/collectsymbolsaction.h \
$$PWD/collectmacrossourcefilecallbacks.h \
$$PWD/collectsymbolsastvisitor.h \
$$PWD/sourcelocationentry.h
$$PWD/sourcelocationentry.h \
$$PWD/symbolscollectorinterface.h \
$$PWD/symbolstorageinterface.h
!isEmpty(LIBTOOLING_LIBS) {
SOURCES += \
@@ -45,6 +48,7 @@ HEADERS += \
SOURCES += \
$$PWD/sourcerangefilter.cpp \
$$PWD/symbolscollector.cpp \
$$PWD/symbolindexer.cpp \
$$PWD/collectsymbolsaction.cpp \
$$PWD/collectmacrossourcefilecallbacks.cpp \
$$PWD/symbolentry.cpp \

View File

@@ -43,9 +43,9 @@ String toNativePath(String &&path)
}
void ClangTool::addFile(std::string &&directory,
std::string &&fileName,
std::string &&content,
std::vector<std::string> &&commandLine)
std::string &&fileName,
std::string &&content,
std::vector<std::string> &&commandLine)
{
m_fileContents.emplace_back(toNativePath(std::move(directory)),
std::move(fileName),

View File

@@ -54,8 +54,8 @@ public:
std::vector<clang::tooling::CompileCommand> getAllCompileCommands() const override;
void addFile(const std::string &directory,
const std::string &fileName,
const std::vector<std::string> &commandLine);
const std::string &fileName,
const std::vector<std::string> &commandLine);
private:
std::vector<clang::tooling::CompileCommand> compileCommands;

View File

@@ -70,6 +70,15 @@ public:
uint line = 0;
uint column = 0;
SymbolType symbolType;
friend bool operator==(const SourceLocationEntry &first, const SourceLocationEntry &second)
{
return first.symbolId == second.symbolId
&& first.fileId == second.fileId
&& first.line == second.line
&& first.column == second.column
&& first.symbolType == second.symbolType;
}
};
using SourceLocationEntries = std::vector<SourceLocationEntry>;

View File

@@ -45,8 +45,19 @@ public:
symbolName(name.data(), name.size())
{}
SymbolEntry(Utils::PathString &&usr,
Utils::SmallString &&symbolName)
: usr(std::move(usr)),
symbolName(std::move(symbolName))
{}
Utils::PathString usr;
Utils::SmallString symbolName;
friend bool operator==(const SymbolEntry &first, const SymbolEntry &second)
{
return first.usr == second.usr && first.symbolName == second.symbolName;
}
};
using SymbolEntries = std::unordered_map<uint, SymbolEntry>;

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.
**
****************************************************************************/
#include "symbolindexer.h"
namespace ClangBackEnd {
SymbolIndexer::SymbolIndexer(SymbolsCollectorInterface &symbolsCollector, SymbolStorageInterface &symbolStorage)
: m_symbolsCollector(symbolsCollector),
m_symbolStorage(symbolStorage)
{
}
void SymbolIndexer::updateProjectParts(V2::ProjectPartContainers &&projectParts)
{
for (const V2::ProjectPartContainer &projectPart : projectParts)
m_symbolsCollector.addFiles(projectPart.sourcePaths(), projectPart.arguments());
m_symbolsCollector.collectSymbols();
m_symbolStorage.addSymbolsAndSourceLocations(m_symbolsCollector.symbols(),
m_symbolsCollector.sourceLocations());
}
} // namespace ClangBackEnd

View File

@@ -0,0 +1,48 @@
/****************************************************************************
**
** 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 "symbolscollectorinterface.h"
#include "symbolstorageinterface.h"
#include <projectpartcontainerv2.h>
namespace ClangBackEnd {
class SymbolIndexer
{
public:
SymbolIndexer(SymbolsCollectorInterface &symbolsCollector,
SymbolStorageInterface &symbolStorage);
void updateProjectParts(V2::ProjectPartContainers &&projectParts);
private:
SymbolsCollectorInterface &m_symbolsCollector;
SymbolStorageInterface &m_symbolStorage;
};
} // namespace ClangBackEnd

View File

@@ -32,6 +32,11 @@ SymbolsCollector::SymbolsCollector(FilePathCache<> &filePathCache)
{
}
void SymbolsCollector::addFiles(const Utils::PathStringVector &filePaths, const Utils::SmallStringVector &arguments)
{
ClangTool::addFiles(filePaths, arguments);
}
void SymbolsCollector::collectSymbols()
{
auto tool = createTool();

View File

@@ -28,20 +28,24 @@
#include "clangtool.h"
#include "collectmacrossourcefilecallbacks.h"
#include "collectsymbolsaction.h"
#include "symbolscollectorinterface.h"
#include "symbolentry.h"
#include "stringcache.h"
namespace ClangBackEnd {
class SymbolsCollector: public ClangTool
class SymbolsCollector : public ClangTool, public SymbolsCollectorInterface
{
public:
SymbolsCollector(FilePathCache<> &filePathCache);
void collectSymbols();
void addFiles(const Utils::PathStringVector &filePaths,
const Utils::SmallStringVector &arguments) override;
const SymbolEntries &symbols() const;
const SourceLocationEntries &sourceLocations() const;
void collectSymbols() override;
const SymbolEntries &symbols() const override;
const SourceLocationEntries &sourceLocations() const override;
private:
CollectSymbolsAction m_collectSymbolsAction;

View File

@@ -0,0 +1,50 @@
/****************************************************************************
**
** 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 "symbolentry.h"
#include "sourcelocationentry.h"
#include <utils/smallstringvector.h>
#include <string>
#include <vector>
namespace ClangBackEnd {
class SymbolsCollectorInterface
{
public:
virtual void addFiles(const Utils::PathStringVector &filePaths,
const Utils::SmallStringVector &arguments) = 0;
virtual void collectSymbols() = 0;
virtual const SymbolEntries &symbols() const = 0;
virtual const SourceLocationEntries &sourceLocations() const = 0;
};
} // namespace ClangBackEnd

View File

@@ -0,0 +1,40 @@
/****************************************************************************
**
** 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 "sourcelocationentry.h"
#include "symbolentry.h"
namespace ClangBackEnd {
class SymbolStorageInterface
{
public:
virtual void addSymbolsAndSourceLocations(const SymbolEntries &symbolEentries,
const SourceLocationEntries &sourceLocations) = 0;
};
} // namespace ClangBackEnd