Clang: Use the clang indexer for indexing

It is not removing so much code but will makes somethings easier in the
future. We added the SymbolType::Definition too.

Change-Id: I4e106b8518e6bfed0c6a4aa6be61c4a5fe5f8bef
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
Marco Bubke
2018-03-22 13:57:03 +01:00
parent 41a12410e6
commit 576eb3370c
11 changed files with 247 additions and 190 deletions

View File

@@ -25,10 +25,13 @@
#include "symbolscollector.h"
#include <clang/Frontend/FrontendActions.h>
namespace ClangBackEnd {
SymbolsCollector::SymbolsCollector(FilePathCachingInterface &filePathCache)
: m_collectSymbolsAction(m_symbolEntries, m_sourceLocationEntries, filePathCache),
: m_indexDataConsumer(std::make_shared<IndexDataConsumer>(m_symbolEntries, m_sourceLocationEntries, filePathCache)),
m_collectSymbolsAction(m_indexDataConsumer),
m_collectMacrosSourceFileCallbacks(m_symbolEntries, m_sourceLocationEntries, filePathCache),
m_filePathCache(filePathCache)
{
@@ -54,22 +57,80 @@ void SymbolsCollector::clear()
m_clangTool = ClangTool();
}
template <typename Factory>
std::unique_ptr<clang::tooling::FrontendActionFactory>
newFrontendActionFactory(Factory *consumerFactory,
clang::tooling::SourceFileCallbacks *sourceFileCallbacks)
{
class FrontendActionFactoryAdapter : public clang::tooling::FrontendActionFactory
{
public:
explicit FrontendActionFactoryAdapter(Factory *consumerFactory,
clang::tooling::SourceFileCallbacks *sourceFileCallbacks)
: m_consumerFactory(consumerFactory),
m_sourceFileCallbacks(sourceFileCallbacks)
{}
clang::FrontendAction *create() override {
return new ConsumerFactoryAdaptor(m_consumerFactory, m_sourceFileCallbacks);
}
private:
class ConsumerFactoryAdaptor : public clang::ASTFrontendAction {
public:
ConsumerFactoryAdaptor(Factory *consumerFactory,
clang::tooling::SourceFileCallbacks *sourceFileCallbacks)
: m_consumerFactory(consumerFactory),
m_sourceFileCallbacks(sourceFileCallbacks)
{}
std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &instance, StringRef inFile) override {
return m_consumerFactory->newASTConsumer(instance, inFile);
}
protected:
bool BeginSourceFileAction(clang::CompilerInstance &CI) override {
if (!clang::ASTFrontendAction::BeginSourceFileAction(CI))
return false;
if (m_sourceFileCallbacks)
return m_sourceFileCallbacks->handleBeginSource(CI);
return true;
}
void EndSourceFileAction() override {
if (m_sourceFileCallbacks)
m_sourceFileCallbacks->handleEndSource();
clang::ASTFrontendAction::EndSourceFileAction();
}
private:
Factory *m_consumerFactory;
clang::tooling::SourceFileCallbacks *m_sourceFileCallbacks;
};
Factory *m_consumerFactory;
clang::tooling::SourceFileCallbacks *m_sourceFileCallbacks;
};
return std::unique_ptr<clang::tooling::FrontendActionFactory>(
new FrontendActionFactoryAdapter(consumerFactory, sourceFileCallbacks));
}
void SymbolsCollector::collectSymbols()
{
auto tool = m_clangTool.createTool();
tool.run(clang::tooling::newFrontendActionFactory(&m_collectSymbolsAction,
&m_collectMacrosSourceFileCallbacks).get());
tool.run(ClangBackEnd::newFrontendActionFactory(&m_collectSymbolsAction,
&m_collectMacrosSourceFileCallbacks).get());
}
const SymbolEntries &SymbolsCollector::symbols() const
{
return m_collectSymbolsAction.symbols();
return m_symbolEntries;
}
const SourceLocationEntries &SymbolsCollector::sourceLocations() const
{
return m_collectSymbolsAction.sourceLocations();
return m_sourceLocationEntries;
}
const FilePathIds &SymbolsCollector::sourceFiles() const