Clang: Add file cache

The database is using file path integer ids to handle file paths because
otherwise we would save many redundant data. This patch is improving it
further with the introduction of a database based file path cache. The
entries are now divided in a directory path and file name. This is quite
handy for directory based file watching.

Change-Id: I03f2e388e43f3d521d6bf8e39dfb95eb2309dc73
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2017-09-21 11:43:59 +02:00
committed by Tim Jenssen
parent 0be8240002
commit d2e15e5f1e
135 changed files with 3458 additions and 1517 deletions

View File

@@ -54,16 +54,16 @@ struct CollectBoundNodes : MatchFinder::MatchCallback {
}
};
ClangQuery::ClangQuery(FilePathCache<std::mutex> &filePathCache,
ClangQuery::ClangQuery(FilePathCachingInterface &filePathCache,
Utils::SmallString &&query)
: query(std::move(query)),
filePathCache(filePathCache)
: m_query(std::move(query)),
m_filePathCache(filePathCache)
{
}
void ClangQuery::setQuery(Utils::SmallString &&query)
{
this->query = std::move(query);
this->m_query = std::move(query);
}
void ClangQuery::findLocations()
@@ -78,7 +78,7 @@ void ClangQuery::findLocations()
std::make_move_iterator(asts.end()),
[&] (std::unique_ptr<clang::ASTUnit> &&ast) {
Diagnostics diagnostics;
auto optionalMatcher = Parser::parseMatcherExpression({query.data(), query.size()},
auto optionalMatcher = Parser::parseMatcherExpression({m_query.data(), m_query.size()},
nullptr,
&diagnostics);
parseDiagnostics(diagnostics);
@@ -89,19 +89,19 @@ void ClangQuery::findLocations()
SourceRangesContainer ClangQuery::takeSourceRanges()
{
return std::move(sourceRangesContainer);
return std::move(m_sourceRangesContainer);
}
DynamicASTMatcherDiagnosticContainers ClangQuery::takeDiagnosticContainers()
{
return std::move(diagnosticContainers_);
return std::move(m_diagnosticContainers_);
}
namespace {
V2::SourceRangeContainer convertToContainer(const clang::ast_matchers::dynamic::SourceRange sourceRange)
{
return V2::SourceRangeContainer(0,
return V2::SourceRangeContainer({1, 0},
sourceRange.Start.Line,
sourceRange.Start.Column,
0,
@@ -159,8 +159,8 @@ void ClangQuery::parseDiagnostics(const clang::ast_matchers::dynamic::Diagnostic
auto errors = diagnostics.errors();
for (const auto &errorContent : errors) {
diagnosticContainers_.emplace_back();
DynamicASTMatcherDiagnosticContainer &diagnosticContainer = diagnosticContainers_.back();
m_diagnosticContainers_.emplace_back();
DynamicASTMatcherDiagnosticContainer &diagnosticContainer = m_diagnosticContainers_.back();
for (const auto &message : errorContent.Messages) {
diagnosticContainer.insertMessage(convertToContainer(message.Range),
@@ -216,8 +216,8 @@ void ClangQuery::matchLocation(
SourceRangeExtractor extractor(ast->getSourceManager(),
ast->getLangOpts(),
filePathCache,
sourceRangesContainer);
m_filePathCache,
m_sourceRangesContainer);
extractor.addSourceRanges(sourceRanges);
}

View File

@@ -30,7 +30,7 @@
#include <sourcerangescontainer.h>
#include <dynamicastmatcherdiagnosticcontainer.h>
#include <stringcache.h>
#include <filepathcachingfwd.h>
namespace clang {
namespace ast_matchers {
@@ -51,9 +51,9 @@ namespace ClangBackEnd {
class ClangQuery : public ClangTool
{
public:
ClangQuery(FilePathCache<std::mutex> &filePathCache, Utils::SmallString &&query={});
ClangQuery(FilePathCachingInterface &m_filePathCache, Utils::SmallString &&m_query={});
void setQuery(Utils::SmallString &&query);
void setQuery(Utils::SmallString &&m_query);
void findLocations();
@@ -66,10 +66,10 @@ private:
std::unique_ptr<clang::ASTUnit> ast);
private:
SourceRangesContainer sourceRangesContainer;
Utils::SmallString query;
std::vector<DynamicASTMatcherDiagnosticContainer> diagnosticContainers_;
FilePathCache<std::mutex> &filePathCache;
SourceRangesContainer m_sourceRangesContainer;
Utils::SmallString m_query;
std::vector<DynamicASTMatcherDiagnosticContainer> m_diagnosticContainers_;
ClangBackEnd::FilePathCachingInterface &m_filePathCache;
};
} // namespace ClangBackEnd

View File

@@ -29,7 +29,7 @@
namespace ClangBackEnd {
ClangQueryGatherer::ClangQueryGatherer(FilePathCache<std::mutex> *filePathCache,
ClangQueryGatherer::ClangQueryGatherer(FilePathCachingInterface *filePathCache,
std::vector<V2::FileContainer> &&sources,
std::vector<V2::FileContainer> &&unsaved,
Utils::SmallString &&query)
@@ -43,7 +43,7 @@ ClangQueryGatherer::ClangQueryGatherer(FilePathCache<std::mutex> *filePathCache,
SourceRangesForQueryMessage
ClangQueryGatherer::createSourceRangesForSource(
FilePathCache<std::mutex> *filePathCache,
FilePathCachingInterface *filePathCache,
V2::FileContainer &&source,
const std::vector<V2::FileContainer> &unsaved,
Utils::SmallString &&query)

View File

@@ -29,7 +29,7 @@
#include <sourcerangesforquerymessage.h>
#include <filecontainerv2.h>
#include <stringcache.h>
#include <filepathcachingfwd.h>
#include <future>
@@ -41,13 +41,13 @@ public:
using Future = std::future<SourceRangesForQueryMessage>;
ClangQueryGatherer() = default;
ClangQueryGatherer(FilePathCache<std::mutex> *filePathCache,
ClangQueryGatherer(FilePathCachingInterface *filePathCache,
std::vector<V2::FileContainer> &&sources,
std::vector<V2::FileContainer> &&unsaved,
Utils::SmallString &&query);
static SourceRangesForQueryMessage createSourceRangesForSource(
FilePathCache<std::mutex> *filePathCache,
FilePathCachingInterface *filePathCache,
V2::FileContainer &&source,
const std::vector<V2::FileContainer> &unsaved,
Utils::SmallString &&query);
@@ -69,7 +69,7 @@ protected:
std::vector<Future> finishedFutures();
private:
FilePathCache<std::mutex> *m_filePathCache = nullptr;
FilePathCachingInterface *m_filePathCache = nullptr;
SourceRangeFilter m_sourceRangeFilter;
std::vector<V2::FileContainer> m_sources;
std::vector<V2::FileContainer> m_unsaved;

View File

@@ -57,5 +57,4 @@ HEADERS += \
SOURCES += \
$$PWD/sourcerangefilter.cpp \
$$PWD/symbolindexer.cpp \
$$PWD/symbolentry.cpp \
$$PWD/sourcelocationentry.cpp
$$PWD/symbolentry.cpp

View File

@@ -31,7 +31,7 @@
#include <utils/smallstring.h>
#include <stringcachefwd.h>
#include <filepathcachingfwd.h>
#include <clang/Frontend/FrontendAction.h>
@@ -42,7 +42,7 @@ namespace ClangBackEnd {
class CollectSymbolsAction
{
public:
CollectSymbolsAction(FilePathCache<std::mutex> &filePathCache)
CollectSymbolsAction(FilePathCachingInterface &filePathCache)
: m_filePathCache(filePathCache)
{}
@@ -66,7 +66,7 @@ public:
private:
SymbolEntries m_symbolEntries;
SourceLocationEntries m_sourceLocationEntries;
FilePathCache<std::mutex> &m_filePathCache;
FilePathCachingInterface &m_filePathCache;
};

View File

@@ -28,7 +28,7 @@
#include "symbolentry.h"
#include "sourcelocationentry.h"
#include <stringcache.h>
#include <filepathcachinginterface.h>
#include <clang/AST/AST.h>
#include <clang/AST/ASTContext.h>
@@ -50,7 +50,7 @@ class CollectSymbolsASTVisitor : public clang::RecursiveASTVisitor<CollectSymbol
public:
CollectSymbolsASTVisitor(SymbolEntries &symbolEntries,
SourceLocationEntries &sourceLocationEntries,
FilePathCache<std::mutex> &filePathCache,
FilePathCachingInterface &filePathCache,
const clang::SourceManager &sourceManager)
: m_symbolEntries(symbolEntries),
m_sourceLocationEntries(sourceLocationEntries),
@@ -100,7 +100,7 @@ public:
return true;
}
FilePathIndex filePathId(clang::SourceLocation sourceLocation)
FilePathId filePathId(clang::SourceLocation sourceLocation)
{
uint clangFileId = m_sourceManager.getFileID(sourceLocation).getHashValue();
@@ -111,11 +111,11 @@ public:
auto filePath = m_sourceManager.getFilename(sourceLocation);
FilePathIndex index = m_filePathCache.stringId(toStringView(filePath));
FilePathId filePathId = m_filePathCache.filePathId(toStringView(filePath));
m_filePathIndices.emplace(clangFileId, index);
m_filePathIndices.emplace(clangFileId, filePathId);
return index;
return filePathId;
}
LineColumn lineColum(clang::SourceLocation sourceLocation)
@@ -147,9 +147,9 @@ public:
private:
SymbolEntries &m_symbolEntries;
std::unordered_map<uint, FilePathIndex> m_filePathIndices;
std::unordered_map<uint, FilePathId> m_filePathIndices;
SourceLocationEntries &m_sourceLocationEntries;
FilePathCache<std::mutex> &m_filePathCache;
FilePathCachingInterface &m_filePathCache;
const clang::SourceManager &m_sourceManager;
};

View File

@@ -31,7 +31,7 @@
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/ASTContext.h>
#include <stringcachefwd.h>
#include <filepathcachingfwd.h>
namespace ClangBackEnd {
@@ -40,7 +40,7 @@ class CollectSymbolsConsumer : public clang::ASTConsumer
public:
CollectSymbolsConsumer(SymbolEntries &symbolEntries,
SourceLocationEntries &sourceLocationEntries,
FilePathCache<std::mutex> &filePathCache)
FilePathCachingInterface &filePathCache)
: m_symbolEntries(symbolEntries),
m_sourceLocationEntries(sourceLocationEntries),
m_filePathCache(filePathCache)
@@ -69,6 +69,6 @@ public:
private:
SymbolEntries &m_symbolEntries;
SourceLocationEntries &m_sourceLocationEntries;
FilePathCache<std::mutex> &m_filePathCache;
FilePathCachingInterface &m_filePathCache;
};
}

View File

@@ -48,9 +48,12 @@
namespace ClangBackEnd {
LocationSourceFileCallbacks::LocationSourceFileCallbacks(uint line, uint column)
: line(line),
column(column)
LocationSourceFileCallbacks::LocationSourceFileCallbacks(uint line,
uint column,
FilePathCachingInterface &filePathCache)
: m_filePathCache(filePathCache),
m_line(line),
m_column(column)
{
}
@@ -58,30 +61,31 @@ bool LocationSourceFileCallbacks::handleBeginSource(clang::CompilerInstance &com
{
auto &preprocessor = compilerInstance.getPreprocessor();
macroPreprocessorCallbacks = new MacroPreprocessorCallbacks(sourceLocationsContainer,
symbolName,
preprocessor,
line,
column);
m_macroPreprocessorCallbacks = new MacroPreprocessorCallbacks(m_sourceLocationsContainer,
m_symbolName,
preprocessor,
m_filePathCache,
m_line,
m_column);
preprocessor.addPPCallbacks(std::unique_ptr<clang::PPCallbacks>(macroPreprocessorCallbacks));
preprocessor.addPPCallbacks(std::unique_ptr<clang::PPCallbacks>(m_macroPreprocessorCallbacks));
return true;
}
SourceLocationsContainer LocationSourceFileCallbacks::takeSourceLocations()
{
return std::move(sourceLocationsContainer);
return std::move(m_sourceLocationsContainer);
}
Utils::SmallString LocationSourceFileCallbacks::takeSymbolName()
{
return std::move(symbolName);
return std::move(m_symbolName);
}
bool LocationSourceFileCallbacks::hasSourceLocations() const
{
return sourceLocationsContainer.hasContent();
return m_sourceLocationsContainer.hasContent();
}

View File

@@ -25,6 +25,7 @@
#pragma once
#include <filepathcachingfwd.h>
#include <sourcelocationscontainer.h>
#include <clang/Tooling/Tooling.h>
@@ -45,7 +46,7 @@ class SourceLocationsContainer;
class LocationSourceFileCallbacks : public clang::tooling::SourceFileCallbacks
{
public:
LocationSourceFileCallbacks(uint line, uint column);
LocationSourceFileCallbacks(uint line, uint column, FilePathCachingInterface &filePathCache);
bool handleBeginSource(clang::CompilerInstance &compilerInstance,
llvm::StringRef fileName) override;
@@ -56,11 +57,12 @@ public:
bool hasSourceLocations() const;
private:
SourceLocationsContainer sourceLocationsContainer;
Utils::SmallString symbolName;
MacroPreprocessorCallbacks *macroPreprocessorCallbacks;
uint line;
uint column;
SourceLocationsContainer m_sourceLocationsContainer;
Utils::SmallString m_symbolName;
MacroPreprocessorCallbacks *m_macroPreprocessorCallbacks;
FilePathCachingInterface &m_filePathCache;
uint m_line;
uint m_column;
};
} // namespace ClangBackEnd

View File

@@ -30,13 +30,15 @@ namespace ClangBackEnd {
MacroPreprocessorCallbacks::MacroPreprocessorCallbacks(SourceLocationsContainer &sourceLocationsContainer,
Utils::SmallString &symbolName,
clang::Preprocessor &preprocessor,
FilePathCachingInterface &filePathCache,
uint line,
uint column)
: sourceLocationsContainer(sourceLocationsContainer),
symbolName(symbolName),
preprocessor(preprocessor),
line(line),
column(column)
: m_sourceLocationsContainer(sourceLocationsContainer),
m_symbolName(symbolName),
m_preprocessor(preprocessor),
m_filePathCache(filePathCache),
m_line(line),
m_column(column)
{
}

View File

@@ -27,27 +27,14 @@
#include "sourcelocationsutils.h"
#include <filepathcachingfwd.h>
#include <sourcelocationscontainer.h>
#if defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-parameter"
#elif defined(_MSC_VER)
# pragma warning(push)
# pragma warning( disable : 4100 )
#endif
#include <clang/Basic/SourceManager.h>
#include <clang/Lex/PPCallbacks.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Lex/MacroInfo.h>
#if defined(__GNUC__)
# pragma GCC diagnostic pop
#elif defined(_MSC_VER)
# pragma warning(pop)
#endif
#include <QDebug>
namespace ClangBackEnd {
@@ -67,18 +54,19 @@ struct MacroDirectiveToken
class MacroPreprocessorCallbacks : public clang::PPCallbacks
{
public:
MacroPreprocessorCallbacks(SourceLocationsContainer &sourceLocationsContainer,
Utils::SmallString &symbolName,
clang::Preprocessor &preprocessor,
uint line,
uint column);
MacroPreprocessorCallbacks(SourceLocationsContainer &m_sourceLocationsContainer,
Utils::SmallString &m_symbolName,
clang::Preprocessor &m_preprocessor,
FilePathCachingInterface &filePathCache,
uint m_line,
uint m_column);
void FileChanged(clang::SourceLocation location,
FileChangeReason reason,
clang::SrcMgr::CharacteristicKind /*fileType*/,
clang::FileID /*previousFileIdentifier*/) final
{
if (!isMainFileEntered) {
if (!m_isMainFileEntered) {
updateLocations();
updateIsMainFileEntered(location, reason);
}
@@ -88,9 +76,9 @@ public:
{
if (isInMainFile(token)) {
if (includesCursorPosition(token)) {
sourceLocations.push_back(token.getLocation());
cursorMacroDirective = macroDirective;
symbolName = Utils::SmallString(token.getIdentifierInfo()->getNameStart(),
m_sourceLocations.push_back(token.getLocation());
m_cursorMacroDirective = macroDirective;
m_symbolName = Utils::SmallString(token.getIdentifierInfo()->getNameStart(),
token.getIdentifierInfo()->getLength());
}
}
@@ -103,59 +91,60 @@ public:
{
if (includesCursorPosition(token)) {
appendSourceLocations(token, macroDefinition);
cursorMacroDirective = macroDefinition.getLocalDirective();
symbolName = Utils::SmallString(token.getIdentifierInfo()->getNameStart(),
m_cursorMacroDirective = macroDefinition.getLocalDirective();
m_symbolName = Utils::SmallString(token.getIdentifierInfo()->getNameStart(),
token.getIdentifierInfo()->getLength());
} else if (isCurrentTokenExpansion(macroDefinition)) {
sourceLocations.push_back(token.getLocation());
m_sourceLocations.push_back(token.getLocation());
} else if (isBeforeCursorSourceLocation()) {
preCursorMacroDirectiveTokens.emplace_back(macroDefinition.getLocalDirective(), token);
m_preCursorMacroDirectiveTokens.emplace_back(macroDefinition.getLocalDirective(), token);
}
}
void EndOfMainFile() final
{
appendSourceLocationsToSourceLocationsContainer(sourceLocationsContainer,
sourceLocations,
sourceManager());
appendSourceLocationsToSourceLocationsContainer(m_sourceLocationsContainer,
m_sourceLocations,
sourceManager(),
m_filePathCache);
}
private:
void appendSourceLocations(const clang::Token &token,
const clang::MacroDefinition &macroDefinition)
{
sourceLocations.push_back(macroDefinition.getLocalDirective()->getLocation());
for (const auto &macroDirectiveToken : preCursorMacroDirectiveTokens) {
m_sourceLocations.push_back(macroDefinition.getLocalDirective()->getLocation());
for (const auto &macroDirectiveToken : m_preCursorMacroDirectiveTokens) {
if (macroDirectiveToken.macroDirective == macroDefinition.getLocalDirective())
sourceLocations.push_back(macroDirectiveToken.token.getLocation());
m_sourceLocations.push_back(macroDirectiveToken.token.getLocation());
}
sourceLocations.push_back(token.getLocation());
m_sourceLocations.push_back(token.getLocation());
}
void updateLocations()
{
if (mainFileSourceLocation.isInvalid()) {
mainFileSourceLocation = sourceManager().getLocForStartOfFile(sourceManager().getMainFileID());
cursorSourceLocation = sourceManager().translateLineCol(sourceManager().getMainFileID(),
line,
column);
if (m_mainFileSourceLocation.isInvalid()) {
m_mainFileSourceLocation = sourceManager().getLocForStartOfFile(sourceManager().getMainFileID());
m_cursorSourceLocation = sourceManager().translateLineCol(sourceManager().getMainFileID(),
m_line,
m_column);
}
}
void updateIsMainFileEntered(clang::SourceLocation location, FileChangeReason reason)
{
if (location == mainFileSourceLocation && reason == PPCallbacks::EnterFile)
isMainFileEntered = true;
if (location == m_mainFileSourceLocation && reason == PPCallbacks::EnterFile)
m_isMainFileEntered = true;
}
const clang::SourceManager &sourceManager() const
{
return preprocessor.getSourceManager();
return m_preprocessor.getSourceManager();
}
bool isInMainFile(const clang::Token &token)
{
return isMainFileEntered && sourceManager().isWrittenInMainFile(token.getLocation());
return m_isMainFileEntered && sourceManager().isWrittenInMainFile(token.getLocation());
}
bool includesCursorPosition(const clang::Token &token)
@@ -163,35 +152,36 @@ private:
auto start = token.getLocation();
auto end = token.getEndLoc();
return cursorSourceLocation == start
|| cursorSourceLocation == end
|| (sourceManager().isBeforeInTranslationUnit(start, cursorSourceLocation) &&
sourceManager().isBeforeInTranslationUnit(cursorSourceLocation, end));
return m_cursorSourceLocation == start
|| m_cursorSourceLocation == end
|| (sourceManager().isBeforeInTranslationUnit(start, m_cursorSourceLocation) &&
sourceManager().isBeforeInTranslationUnit(m_cursorSourceLocation, end));
}
bool isCurrentTokenExpansion(const clang::MacroDefinition &macroDefinition)
{
return cursorMacroDirective
&& cursorMacroDirective == macroDefinition.getLocalDirective();
return m_cursorMacroDirective
&& m_cursorMacroDirective == macroDefinition.getLocalDirective();
}
bool isBeforeCursorSourceLocation() const
{
return !cursorMacroDirective;
return !m_cursorMacroDirective;
}
private:
std::vector<clang::SourceLocation> sourceLocations;
std::vector<MacroDirectiveToken> preCursorMacroDirectiveTokens;
SourceLocationsContainer &sourceLocationsContainer;
Utils::SmallString &symbolName;
clang::Preprocessor &preprocessor;
const clang::MacroDirective *cursorMacroDirective = nullptr;
clang::SourceLocation mainFileSourceLocation;
clang::SourceLocation cursorSourceLocation;
uint line;
uint column;
bool isMainFileEntered = false;
std::vector<clang::SourceLocation> m_sourceLocations;
std::vector<MacroDirectiveToken> m_preCursorMacroDirectiveTokens;
SourceLocationsContainer &m_sourceLocationsContainer;
Utils::SmallString &m_symbolName;
clang::Preprocessor &m_preprocessor;
const clang::MacroDirective *m_cursorMacroDirective = nullptr;
clang::SourceLocation m_mainFileSourceLocation;
clang::SourceLocation m_cursorSourceLocation;
FilePathCachingInterface &m_filePathCache;
uint m_line;
uint m_column;
bool m_isMainFileEntered = false;
};
} // namespace ClangBackEnd

View File

@@ -40,7 +40,7 @@
namespace ClangBackEnd {
RefactoringServer::RefactoringServer(SymbolIndexingInterface &symbolIndexing,
FilePathCache<std::mutex> &filePathCache)
FilePathCachingInterface &filePathCache)
: m_symbolIndexing(symbolIndexing),
m_filePathCache(filePathCache)
{
@@ -58,7 +58,7 @@ void RefactoringServer::end()
void RefactoringServer::requestSourceLocationsForRenamingMessage(RequestSourceLocationsForRenamingMessage &&message)
{
SymbolFinder symbolFinder(message.line(), message.column());
SymbolFinder symbolFinder(message.line(), message.column(), m_filePathCache);
symbolFinder.addFile(std::string(message.filePath().directory()),
std::string(message.filePath().name()),

View File

@@ -29,12 +29,13 @@
#include <refactoringserverinterface.h>
#include <QTimer>
#include <ipcclientprovider.h>
#include <stringcache.h>
#include <filepathcachinginterface.h>
#include <utils/smallstring.h>
#include <QTimer>
#include <future>
#include <mutex>
#include <vector>
@@ -54,7 +55,7 @@ class RefactoringServer : public RefactoringServerInterface,
using Future = std::future<SourceRangesForQueryMessage>;
public:
RefactoringServer(SymbolIndexingInterface &symbolIndexing,
FilePathCache<std::mutex> &filePathCache);
FilePathCachingInterface &filePathCache);
void end() override;
void requestSourceLocationsForRenamingMessage(RequestSourceLocationsForRenamingMessage &&message) override;
@@ -82,7 +83,7 @@ private:
ClangQueryGatherer m_gatherer;
QTimer m_pollTimer;
SymbolIndexingInterface &m_symbolIndexing;
FilePathCache<std::mutex> &m_filePathCache;
FilePathCachingInterface &m_filePathCache;
};
} // namespace ClangBackEnd

View File

@@ -1,42 +0,0 @@
/****************************************************************************
**
** 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 "sourcelocationentry.h"
#include <utils/smallstringio.h>
namespace ClangBackEnd {
std::ostream &operator<<(std::ostream &out, const SourceLocationEntry &entry)
{
out << "("
<< entry.fileId << ", "
<< entry.line << ", "
<< entry.column << ")";
return out;
}
} // namespace ClangBackEnd

View File

@@ -25,11 +25,10 @@
#pragma once
#include <stringcachefwd.h>
#include <filepathid.h>
#include <limits>
#include <vector>
#include <iosfwd>
using uint = unsigned int;
@@ -59,18 +58,18 @@ class SourceLocationEntry
{
public:
SourceLocationEntry(SymbolIndex symbolId,
FilePathIndex fileId,
FilePathId filePathId,
LineColumn lineColumn,
SymbolType symbolType)
: symbolId(symbolId),
fileId(fileId),
filePathId(filePathId),
line(lineColumn.line),
column(lineColumn.column),
symbolType(symbolType)
{}
SymbolIndex symbolId = 0;
FilePathIndex fileId = std::numeric_limits<uint>::max();
FilePathId filePathId;
uint line = 0;
uint column = 0;
SymbolType symbolType;
@@ -78,7 +77,7 @@ public:
friend bool operator==(const SourceLocationEntry &first, const SourceLocationEntry &second)
{
return first.symbolId == second.symbolId
&& first.fileId == second.fileId
&& first.filePathId == second.filePathId
&& first.line == second.line
&& first.column == second.column
&& first.symbolType == second.symbolType;
@@ -87,6 +86,4 @@ public:
using SourceLocationEntries = std::vector<SourceLocationEntry>;
std::ostream &operator<<(std::ostream &out, const SourceLocationEntry &entry);
} // namespace ClangBackEnd

View File

@@ -25,28 +25,15 @@
#pragma once
#include <filepathcachinginterface.h>
#include <sourcelocationscontainer.h>
#include <sourcerangescontainer.h>
#if defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-parameter"
#elif defined(_MSC_VER)
# pragma warning(push)
# pragma warning( disable : 4100 )
#endif
#include <clang/Basic/SourceManager.h>
#include <clang/Lex/Lexer.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/FileUtilities.h>
#if defined(__GNUC__)
# pragma GCC diagnostic pop
#elif defined(_MSC_VER)
# pragma warning(pop)
#endif
#include <iterator>
#include <cctype>
@@ -79,7 +66,8 @@ inline
void appendSourceLocationsToSourceLocationsContainer(
ClangBackEnd::SourceLocationsContainer &sourceLocationsContainer,
const std::vector<clang::SourceLocation> &sourceLocations,
const clang::SourceManager &sourceManager)
const clang::SourceManager &sourceManager,
FilePathCachingInterface &filePathCache)
{
sourceLocationsContainer.reserve(sourceLocations.size());
@@ -89,10 +77,9 @@ void appendSourceLocationsToSourceLocationsContainer(
const auto fileId = decomposedLoction.first;
const auto offset = decomposedLoction.second;
const auto fileEntry = sourceManager.getFileEntryForID(fileId);
auto filePath = fromNativePath(absolutePath(fileEntry->getName()));
sourceLocationsContainer.insertFilePath(fileId.getHashValue(),
fromNativePath(absolutePath(fileEntry->getName())));
sourceLocationsContainer.insertSourceLocation(fileId.getHashValue(),
sourceLocationsContainer.insertSourceLocation(filePathCache.filePathId(filePath),
fullSourceLocation.getSpellingLineNumber(),
fullSourceLocation.getSpellingColumnNumber(),
offset);

View File

@@ -28,14 +28,7 @@
#include "sourcelocationsutils.h"
#include <sourcerangescontainer.h>
#if defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-parameter"
#elif defined(_MSC_VER)
# pragma warning(push)
# pragma warning( disable : 4100 )
#endif
#include <filepathcachinginterface.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Lex/Lexer.h>
@@ -43,18 +36,12 @@
#include <llvm/Support/FileUtilities.h>
#include <llvm/ADT/SmallVector.h>
#if defined(__GNUC__)
# pragma GCC diagnostic pop
#elif defined(_MSC_VER)
# pragma warning(pop)
#endif
namespace ClangBackEnd {
SourceRangeExtractor::SourceRangeExtractor(
const clang::SourceManager &sourceManager,
const clang::LangOptions &languageOptions,
ClangBackEnd::FilePathCache<std::mutex> &filePathCache,
FilePathCachingInterface &filePathCache,
SourceRangesContainer &sourceRangesContainer)
: sourceManager(sourceManager),
languageOptions(languageOptions),
@@ -127,16 +114,14 @@ const clang::SourceRange SourceRangeExtractor::extendSourceRangeToLastTokenEnd(c
return {sourceRange.getBegin(), endLocation};
}
void SourceRangeExtractor::insertSourceRange(uint fileId,
Utils::PathString &&filePath,
void SourceRangeExtractor::insertSourceRange(FilePathId filePathId,
const clang::FullSourceLoc &startLocation,
uint startOffset,
const clang::FullSourceLoc &endLocation,
uint endOffset,
Utils::SmallString &&lineSnippet)
{
sourceRangesContainer.insertFilePath(fileId, std::move(filePath));
sourceRangesContainer.insertSourceRange(fileId,
sourceRangesContainer.insertSourceRange(filePathId,
startLocation.getSpellingLineNumber(),
startLocation.getSpellingColumnNumber(),
startOffset,
@@ -146,7 +131,7 @@ void SourceRangeExtractor::insertSourceRange(uint fileId,
std::move(lineSnippet));
}
FilePathIndex SourceRangeExtractor::findFileId(clang::FileID fileId, const clang::FileEntry *fileEntry) const
FilePathId SourceRangeExtractor::findFileId(clang::FileID fileId, const clang::FileEntry *fileEntry) const
{
auto found = m_fileIdMapping.find(fileId.getHashValue());
if (found != m_fileIdMapping.end()) {
@@ -154,7 +139,7 @@ FilePathIndex SourceRangeExtractor::findFileId(clang::FileID fileId, const clang
}
auto filePath = absolutePath(fileEntry->getName());
return filePathCache.stringId(fromNativePath(filePath));
return filePathCache.filePathId(fromNativePath(filePath));
}
void SourceRangeExtractor::addSourceRange(const clang::SourceRange &sourceRange)
@@ -176,13 +161,11 @@ void SourceRangeExtractor::addSourceRange(const clang::SourceRange &sourceRange)
endOffset);
insertSourceRange(findFileId(fileId, fileEntry),
fromNativePath(absolutePath(fileEntry->getName())),
startSourceLocation,
startOffset,
endSourceLocation,
endOffset,
std::move(lineSnippet));
}
}

View File

@@ -25,9 +25,10 @@
#pragma once
#include <stringcache.h>
#include <filepathcachingfwd.h>
#include <filepath.h>
#include <filepathid.h>
#include <utils/smallstringfwd.h>
@@ -59,7 +60,7 @@ class SourceRangeExtractor
public:
SourceRangeExtractor(const clang::SourceManager &sourceManager,
const clang::LangOptions &languageOptions,
ClangBackEnd::FilePathCache<std::mutex> &filePathCache,
FilePathCachingInterface &filePathCache,
SourceRangesContainer &sourceRangesContainer);
void addSourceRange(const clang::SourceRange &sourceRange);
@@ -74,21 +75,20 @@ public:
const clang::SourceRange extendSourceRangeToLastTokenEnd(const clang::SourceRange sourceRange);
private:
void insertSourceRange(uint fileId,
Utils::PathString &&filePath,
void insertSourceRange(FilePathId filePathId,
const clang::FullSourceLoc &startLocation,
uint startOffset,
const clang::FullSourceLoc &endLocation,
uint endOffset,
Utils::SmallString &&lineSnippet);
FilePathIndex findFileId(clang::FileID fileId, const clang::FileEntry *fileEntry) const;
FilePathId findFileId(clang::FileID fileId, const clang::FileEntry *fileEntry) const;
private:
mutable std::unordered_map<uint, uint> m_fileIdMapping;
mutable std::unordered_map<uint, FilePathId> m_fileIdMapping;
const clang::SourceManager &sourceManager;
const clang::LangOptions &languageOptions;
ClangBackEnd::FilePathCache<std::mutex> &filePathCache;
FilePathCachingInterface &filePathCache;
SourceRangesContainer &sourceRangesContainer;
};

View File

@@ -25,8 +25,6 @@
#pragma once
#include <createtablesqlstatementbuilder.h>
#include <sqlitetransaction.h>
#include <sqlitetable.h>
@@ -96,9 +94,6 @@ public:
"INSERT INTO newLocations(temporarySymbolId, line, column, sourceId) VALUES(?,?,?,?)",
database
};
// WriteStatement syncNewLocationsToLocationsStatement{
// "INSERT INTO locations(symbolId, line, column, sourceId) SELECT symbolId, line, column, sourceId FROM newLocations",
// database};
ReadStatement selectNewSourceIdsStatement{
"SELECT DISTINCT sourceId FROM newLocations WHERE NOT EXISTS (SELECT sourceId FROM sources WHERE newLocations.sourceId == sources.sourceId)",
database
@@ -109,10 +104,6 @@ public:
"(SELECT usr FROM symbols WHERE symbols.usr == newSymbols.usr)",
database
};
WriteStatement insertSourcesStatement{
"INSERT INTO sources(sourceId, sourcePath) VALUES(?,?)",
database
};
WriteStatement syncNewSymbolsFromSymbolsStatement{
"UPDATE newSymbols SET symbolId = (SELECT symbolId FROM symbols WHERE newSymbols.usr = symbols.usr)",
database

View File

@@ -30,9 +30,10 @@
namespace ClangBackEnd {
SymbolFinder::SymbolFinder(uint line, uint column)
: usrFindingAction(line, column),
sourceFileCallbacks(line, column)
SymbolFinder::SymbolFinder(uint line, uint column, FilePathCachingInterface &filePathCache)
: m_usrFindingAction(line, column),
m_symbolLocationFinderAction(filePathCache),
m_sourceFileCallbacks(line, column, filePathCache)
{
}
@@ -40,39 +41,39 @@ void SymbolFinder::findSymbol()
{
clang::tooling::ClangTool tool = createTool();
tool.run(clang::tooling::newFrontendActionFactory(&usrFindingAction, &sourceFileCallbacks).get());
tool.run(clang::tooling::newFrontendActionFactory(&m_usrFindingAction, &m_sourceFileCallbacks).get());
if (sourceFileCallbacks.hasSourceLocations()) {
sourceLocations_ = sourceFileCallbacks.takeSourceLocations();
symbolName = sourceFileCallbacks.takeSymbolName();
if (m_sourceFileCallbacks.hasSourceLocations()) {
m_sourceLocations_ = m_sourceFileCallbacks.takeSourceLocations();
m_symbolName = m_sourceFileCallbacks.takeSymbolName();
} else {
symbolLocationFinderAction.setUnifiedSymbolResolutions(usrFindingAction.takeUnifiedSymbolResolutions());
m_symbolLocationFinderAction.setUnifiedSymbolResolutions(m_usrFindingAction.takeUnifiedSymbolResolutions());
tool.run(clang::tooling::newFrontendActionFactory(&symbolLocationFinderAction).get());
tool.run(clang::tooling::newFrontendActionFactory(&m_symbolLocationFinderAction).get());
sourceLocations_ = symbolLocationFinderAction.takeSourceLocations();
symbolName = usrFindingAction.takeSymbolName();
m_sourceLocations_ = m_symbolLocationFinderAction.takeSourceLocations();
m_symbolName = m_usrFindingAction.takeSymbolName();
}
}
Utils::SmallString SymbolFinder::takeSymbolName()
{
return std::move(symbolName);
return std::move(m_symbolName);
}
const std::vector<USRName> &SymbolFinder::unifiedSymbolResolutions()
{
return symbolLocationFinderAction.unifiedSymbolResolutions();
return m_symbolLocationFinderAction.unifiedSymbolResolutions();
}
const SourceLocationsContainer &SymbolFinder::sourceLocations() const
{
return sourceLocations_;
return m_sourceLocations_;
}
SourceLocationsContainer SymbolFinder::takeSourceLocations()
{
return std::move(sourceLocations_);
return std::move(m_sourceLocations_);
}
} // namespace ClangBackEnd

View File

@@ -30,6 +30,7 @@
#include "symbollocationfinderaction.h"
#include "locationsourcefilecallbacks.h"
#include <filepathcachingfwd.h>
#include <sourcelocationscontainer.h>
namespace ClangBackEnd {
@@ -37,7 +38,7 @@ namespace ClangBackEnd {
class SymbolFinder : public ClangTool
{
public:
SymbolFinder(uint line, uint column);
SymbolFinder(uint line, uint column, FilePathCachingInterface &filePathCache);
void findSymbol();
@@ -47,12 +48,11 @@ public:
SourceLocationsContainer takeSourceLocations();
private:
Utils::SmallString symbolName;
USRFindingAction usrFindingAction;
SymbolLocationFinderAction symbolLocationFinderAction;
LocationSourceFileCallbacks sourceFileCallbacks;
ClangBackEnd::SourceLocationsContainer sourceLocations_;
Utils::SmallString m_symbolName;
USRFindingAction m_usrFindingAction;
SymbolLocationFinderAction m_symbolLocationFinderAction;
LocationSourceFileCallbacks m_sourceFileCallbacks;
ClangBackEnd::SourceLocationsContainer m_sourceLocations_;
};
} // namespace ClangBackEnd

View File

@@ -33,7 +33,7 @@
#include "symbolstorage.h"
#include <refactoringdatabaseinitializer.h>
#include <stringcache.h>
#include <filepathcachingfwd.h>
#include <sqlitedatabase.h>
#include <sqlitereadstatement.h>
@@ -48,13 +48,11 @@ public:
Sqlite::ReadStatement,
Sqlite::WriteStatement>;
using Storage = ClangBackEnd::SymbolStorage<StatementFactory>;
using DatabaseInitializer = RefactoringDatabaseInitializer<Sqlite::Database>;
SymbolIndexing(FilePathCache<std::mutex> &filePathCache,
Utils::PathString &&databaseFilePath)
SymbolIndexing(Sqlite::Database &database,
FilePathCachingInterface &filePathCache)
: m_filePathCache(filePathCache),
m_database(std::move(databaseFilePath)),
m_databaseInitializer(m_database)
m_statementFactory(database)
{
}
@@ -63,11 +61,6 @@ public:
return m_indexer;
}
Sqlite::Database &database()
{
return m_database;
}
void updateProjectParts(V2::ProjectPartContainers &&projectParts,
V2::FileContainers &&generatedFiles)
{
@@ -75,11 +68,9 @@ public:
}
private:
FilePathCache<std::mutex> &m_filePathCache;
Sqlite::Database m_database;
DatabaseInitializer m_databaseInitializer;
FilePathCachingInterface &m_filePathCache;
SymbolsCollector m_collector{m_filePathCache};
StatementFactory m_statementFactory{m_database};
StatementFactory m_statementFactory;
Storage m_symbolStorage{m_statementFactory, m_filePathCache};
SymbolIndexer m_indexer{m_collector, m_symbolStorage};
};

View File

@@ -28,6 +28,8 @@
#include "sourcelocationsutils.h"
#include "findlocationsofusrs.h"
#include <filepathcachingfwd.h>
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/ASTContext.h>
@@ -38,8 +40,10 @@ namespace ClangBackEnd {
class FindingSymbolsASTConsumer : public clang::ASTConsumer
{
public:
FindingSymbolsASTConsumer(std::vector<USRName> &unifiedSymbolResolutions)
: m_unifiedSymbolResolutions(unifiedSymbolResolutions)
FindingSymbolsASTConsumer(std::vector<USRName> &unifiedSymbolResolutions,
FilePathCachingInterface &filePathCache)
: m_unifiedSymbolResolutions(unifiedSymbolResolutions),
m_filePathCache(filePathCache)
{
}
@@ -65,7 +69,10 @@ public:
void updateSourceLocations(const std::vector<clang::SourceLocation> &sourceLocations,
const clang::SourceManager &sourceManager)
{
appendSourceLocationsToSourceLocationsContainer(*m_sourceLocationsContainer, sourceLocations, sourceManager);
appendSourceLocationsToSourceLocationsContainer(*m_sourceLocationsContainer,
sourceLocations,
sourceManager,
m_filePathCache);
}
void setSourceLocations(ClangBackEnd::SourceLocationsContainer *sourceLocations)
@@ -76,11 +83,13 @@ public:
private:
ClangBackEnd::SourceLocationsContainer *m_sourceLocationsContainer = nullptr;
std::vector<USRName> &m_unifiedSymbolResolutions;
FilePathCachingInterface &m_filePathCache;
};
std::unique_ptr<clang::ASTConsumer> SymbolLocationFinderAction::newASTConsumer()
{
auto consumer = std::unique_ptr<FindingSymbolsASTConsumer>(new FindingSymbolsASTConsumer(m_unifiedSymbolResolutions_));
auto consumer = std::make_unique<FindingSymbolsASTConsumer>(m_unifiedSymbolResolutions_,
m_filePathCache);
consumer->setSourceLocations(&m_sourceLocations);

View File

@@ -27,6 +27,7 @@
#include "clangrefactoringbackend_global.h"
#include <filepathcachingfwd.h>
#include <sourcelocationscontainer.h>
#include <clang/Tooling/Refactoring.h>
@@ -40,27 +41,31 @@ namespace ClangBackEnd {
class SymbolLocationFinderAction
{
public:
SymbolLocationFinderAction(FilePathCachingInterface &filePathCache)
: m_filePathCache(filePathCache)
{}
std::unique_ptr<clang::ASTConsumer> newASTConsumer();
std::unique_ptr<clang::ASTConsumer> newASTConsumer();
SourceLocationsContainer takeSourceLocations()
{
return std::move(m_sourceLocations);
}
SourceLocationsContainer takeSourceLocations()
{
return std::move(m_sourceLocations);
}
void setUnifiedSymbolResolutions(std::vector<USRName> &&unifiedSymbolResolutions)
{
m_unifiedSymbolResolutions_ = std::move(unifiedSymbolResolutions);
}
void setUnifiedSymbolResolutions(std::vector<USRName> &&unifiedSymbolResolutions)
{
m_unifiedSymbolResolutions_ = std::move(unifiedSymbolResolutions);
}
const std::vector<USRName> &unifiedSymbolResolutions() const
{
return m_unifiedSymbolResolutions_;
}
const std::vector<USRName> &unifiedSymbolResolutions() const
{
return m_unifiedSymbolResolutions_;
}
private:
ClangBackEnd::SourceLocationsContainer m_sourceLocations;
std::vector<USRName> m_unifiedSymbolResolutions_;
ClangBackEnd::SourceLocationsContainer m_sourceLocations;
std::vector<USRName> m_unifiedSymbolResolutions_;
FilePathCachingInterface &m_filePathCache;
};
} // namespace ClangBackEnd

View File

@@ -27,7 +27,7 @@
namespace ClangBackEnd {
SymbolsCollector::SymbolsCollector(FilePathCache<std::mutex> &filePathCache)
SymbolsCollector::SymbolsCollector(FilePathCachingInterface &filePathCache)
: m_collectSymbolsAction(filePathCache)
{
}

View File

@@ -30,14 +30,15 @@
#include "collectsymbolsaction.h"
#include "symbolscollectorinterface.h"
#include "symbolentry.h"
#include "stringcache.h"
#include <filepathcachingfwd.h>
namespace ClangBackEnd {
class SymbolsCollector : public ClangTool, public SymbolsCollectorInterface
{
public:
SymbolsCollector(FilePathCache<std::mutex> &filePathCache);
SymbolsCollector(FilePathCachingInterface &filePathCache);
void addFiles(const Utils::PathStringVector &filePaths,
const Utils::SmallStringVector &arguments) override;

View File

@@ -29,9 +29,7 @@
#include <sqliteexception.h>
#include <sqlitetransaction.h>
#include <stringcache.h>
#include <mutex>
#include <filepathcachingfwd.h>
namespace ClangBackEnd {
@@ -45,7 +43,7 @@ class SymbolStorage : public SymbolStorageInterface
public:
SymbolStorage(StatementFactory &statementFactory,
FilePathCache<std::mutex> &filePathCache)
FilePathCachingInterface &filePathCache)
: m_statementFactory(statementFactory),
m_filePathCache(filePathCache)
{
@@ -61,7 +59,6 @@ public:
addNewSymbolsToSymbols();
syncNewSymbolsFromSymbols();
syncSymbolsIntoNewLocations();
insertNewSources();
deleteAllLocationsFromUpdatedFiles();
insertNewLocationsInLocations();
deleteNewSymbolsTable();
@@ -89,7 +86,7 @@ public:
statement.write(locationsEntry.symbolId,
locationsEntry.line,
locationsEntry.column,
locationsEntry.fileId);
locationsEntry.filePathId.fileNameId);
}
}
@@ -118,23 +115,6 @@ public:
m_statementFactory.insertNewLocationsInLocationsStatement.execute();
}
FilePathIndices selectNewSourceIds() const
{
ReadStatement &statement = m_statementFactory.selectNewSourceIdsStatement;
return statement.template values<FilePathIndex>(16);
}
void insertNewSources()
{
WriteStatement &statement = m_statementFactory.insertSourcesStatement;
FilePathIndices newSourceIds = selectNewSourceIds();
for (FilePathIndex sourceId : newSourceIds)
statement.write(sourceId, m_filePathCache.string(sourceId));
}
void deleteNewSymbolsTable()
{
m_statementFactory.deleteNewSymbolsTableStatement.execute();
@@ -152,7 +132,7 @@ public:
private:
StatementFactory &m_statementFactory;
FilePathCache<std::mutex> &m_filePathCache;
FilePathCachingInterface &m_filePathCache;
};
} // namespace ClangBackEnd