Clang: Split symbol storage

We need UsedMacros and source related statements in the PCH manager too,
so we have to split that class.

Task-number: QTCREATORBUG-21289
Change-Id: Ie27d4b518b3d6d9174e93fcb243fdb55a09ddf51
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
Marco Bubke
2018-10-11 18:09:55 +02:00
parent 468dcc67cb
commit 2c885fa3d4
17 changed files with 773 additions and 789 deletions

View File

@@ -27,9 +27,10 @@
#include "symbolstorageinterface.h"
#include <filepathcachingfwd.h>
#include <sqliteexception.h>
#include <sqlitetransaction.h>
#include <filepathcachingfwd.h>
#include <sqlitetable.h>
#include <QJsonArray>
#include <QJsonDocument>
@@ -37,17 +38,19 @@
namespace ClangBackEnd {
template <typename StatementFactory>
template <typename DatabaseType>
class SymbolStorage final : public SymbolStorageInterface
{
using ReadStatement = typename StatementFactory::ReadStatement;
using WriteStatement = typename StatementFactory::WriteStatement;
using Database = typename StatementFactory::Database;
using Database = DatabaseType;
using ReadStatement = typename Database::ReadStatement;
using WriteStatement = typename Database::WriteStatement;
public:
SymbolStorage(StatementFactory &statementFactory)
: m_statementFactory(statementFactory)
SymbolStorage(Database &database)
: m_transaction(database),
m_database(database)
{
m_transaction.commit();
}
void addSymbolsAndSourceLocations(const SymbolEntries &symbolEntries,
@@ -69,95 +72,54 @@ public:
const CompilerMacros &compilerMacros,
const Utils::SmallStringVector &includeSearchPaths) override
{
m_statementFactory.database.setLastInsertedRowId(-1);
m_database.setLastInsertedRowId(-1);
Utils::SmallString compilerArguementsAsJson = toJson(commandLineArguments);
Utils::SmallString compilerMacrosAsJson = toJson(compilerMacros);
Utils::SmallString includeSearchPathsAsJason = toJson(includeSearchPaths);
WriteStatement &insertStatement = m_statementFactory.insertProjectPartStatement;
WriteStatement &insertStatement = m_insertProjectPartStatement;
insertStatement.write(projectPartName,
compilerArguementsAsJson,
compilerMacrosAsJson,
includeSearchPathsAsJason);
if (m_statementFactory.database.lastInsertedRowId() == -1) {
WriteStatement &updateStatement = m_statementFactory.updateProjectPartStatement;
if (m_database.lastInsertedRowId() == -1) {
WriteStatement &updateStatement = m_updateProjectPartStatement;
updateStatement.write(compilerArguementsAsJson,
compilerMacrosAsJson,
includeSearchPathsAsJason,
projectPartName);
}
return int(m_statementFactory.database.lastInsertedRowId());
return int(m_database.lastInsertedRowId());
}
Utils::optional<ProjectPartArtefact> fetchProjectPartArtefact(FilePathId sourceId) const override
{
ReadStatement &statement = m_statementFactory.getProjectPartArtefactsBySourceId;
ReadStatement &statement = m_getProjectPartArtefactsBySourceId;
return statement.template value<ProjectPartArtefact, 4>(sourceId.filePathId);
}
Utils::optional<ProjectPartArtefact> fetchProjectPartArtefact(Utils::SmallStringView projectPartName) const override
{
ReadStatement &statement = m_statementFactory.getProjectPartArtefactsByProjectPartName;
ReadStatement &statement = m_getProjectPartArtefactsByProjectPartName;
return statement.template value<ProjectPartArtefact, 4>(projectPartName);
}
long long fetchLowestLastModifiedTime(FilePathId sourceId) const override
{
ReadStatement &statement = m_statementFactory.getLowestLastModifiedTimeOfDependencies;
return statement.template value<long long>(sourceId.filePathId).value_or(0);
}
void insertOrUpdateUsedMacros(const UsedMacros &usedMacros) override
{
WriteStatement &insertStatement = m_statementFactory.insertIntoNewUsedMacrosStatement;
for (const UsedMacro &usedMacro : usedMacros)
insertStatement.write(usedMacro.filePathId.filePathId, usedMacro.macroName);
m_statementFactory.syncNewUsedMacrosStatement.execute();
m_statementFactory.deleteOutdatedUsedMacrosStatement.execute();
m_statementFactory.deleteNewUsedMacrosTableStatement.execute();
}
void insertOrUpdateSourceDependencies(const SourceDependencies &sourceDependencies) override
{
WriteStatement &insertStatement = m_statementFactory.insertIntoNewSourceDependenciesStatement;
for (SourceDependency sourceDependency : sourceDependencies)
insertStatement.write(sourceDependency.filePathId.filePathId,
sourceDependency.dependencyFilePathId.filePathId);
m_statementFactory.syncNewSourceDependenciesStatement.execute();
m_statementFactory.deleteOutdatedSourceDependenciesStatement.execute();
m_statementFactory.deleteNewSourceDependenciesStatement.execute();
}
void updateProjectPartSources(int projectPartId,
const FilePathIds &sourceFilePathIds) override
{
WriteStatement &deleteStatement = m_statementFactory.deleteAllProjectPartsSourcesWithProjectPartIdStatement;
WriteStatement &deleteStatement = m_deleteAllProjectPartsSourcesWithProjectPartIdStatement;
deleteStatement.write(projectPartId);
WriteStatement &insertStatement = m_statementFactory.insertProjectPartSourcesStatement;
WriteStatement &insertStatement = m_insertProjectPartSourcesStatement;
for (const FilePathId &sourceFilePathId : sourceFilePathIds)
insertStatement.write(projectPartId, sourceFilePathId.filePathId);
}
void insertFileStatuses(const FileStatuses &fileStatuses)
{
WriteStatement &statement = m_statementFactory.insertFileStatuses;
for (const FileStatus &fileStatus : fileStatuses)
statement.write(fileStatus.filePathId.filePathId,
fileStatus.size,
fileStatus.lastModified,
fileStatus.isInPrecompiledHeader);
}
static Utils::SmallString toJson(const Utils::SmallStringVector &strings)
{
QJsonDocument document;
@@ -187,7 +149,7 @@ public:
void fillTemporarySymbolsTable(const SymbolEntries &symbolEntries)
{
WriteStatement &statement = m_statementFactory.insertSymbolsToNewSymbolsStatement;
WriteStatement &statement = m_insertSymbolsToNewSymbolsStatement;
for (const auto &symbolEntry : symbolEntries) {
statement.write(symbolEntry.first,
@@ -199,7 +161,7 @@ public:
void fillTemporaryLocationsTable(const SourceLocationEntries &sourceLocations)
{
WriteStatement &statement = m_statementFactory.insertLocationsToNewLocationsStatement;
WriteStatement &statement = m_insertLocationsToNewLocationsStatement;
for (const auto &locationEntry : sourceLocations) {
statement.write(locationEntry.symbolId,
@@ -212,42 +174,42 @@ public:
void addNewSymbolsToSymbols()
{
m_statementFactory.addNewSymbolsToSymbolsStatement.execute();
m_addNewSymbolsToSymbolsStatement.execute();
}
void syncNewSymbolsFromSymbols()
{
m_statementFactory.syncNewSymbolsFromSymbolsStatement.execute();
m_syncNewSymbolsFromSymbolsStatement.execute();
}
void syncSymbolsIntoNewLocations()
{
m_statementFactory.syncSymbolsIntoNewLocationsStatement.execute();
m_syncSymbolsIntoNewLocationsStatement.execute();
}
void deleteAllLocationsFromUpdatedFiles()
{
m_statementFactory.deleteAllLocationsFromUpdatedFilesStatement.execute();
m_deleteAllLocationsFromUpdatedFilesStatement.execute();
}
void insertNewLocationsInLocations()
{
m_statementFactory.insertNewLocationsInLocationsStatement.execute();
m_insertNewLocationsInLocationsStatement.execute();
}
void deleteNewSymbolsTable()
{
m_statementFactory.deleteNewSymbolsTableStatement.execute();
m_deleteNewSymbolsTableStatement.execute();
}
void deleteNewLocationsTable()
{
m_statementFactory.deleteNewLocationsTableStatement.execute();
m_deleteNewLocationsTableStatement.execute();
}
Utils::optional<ProjectPartPch> fetchPrecompiledHeader(int projectPartId) const
{
return m_statementFactory.getPrecompiledHeader.template value<ProjectPartPch, 2>(projectPartId);
return m_getPrecompiledHeader.template value<ProjectPartPch, 2>(projectPartId);
}
SourceLocationEntries sourceLocations() const
@@ -255,8 +217,124 @@ public:
return SourceLocationEntries();
}
private:
StatementFactory &m_statementFactory;
Sqlite::Table createNewSymbolsTable() const
{
Sqlite::Table table;
table.setName("newSymbols");
table.setUseTemporaryTable(true);
table.addColumn("temporarySymbolId", Sqlite::ColumnType::Integer, Sqlite::Contraint::PrimaryKey);
const Sqlite::Column &symbolIdColumn = table.addColumn("symbolId", Sqlite::ColumnType::Integer);
const Sqlite::Column &usrColumn = table.addColumn("usr", Sqlite::ColumnType::Text);
const Sqlite::Column &symbolNameColumn = table.addColumn("symbolName", Sqlite::ColumnType::Text);
table.addColumn("symbolKind", Sqlite::ColumnType::Integer);
table.addIndex({usrColumn, symbolNameColumn});
table.addIndex({symbolIdColumn});
table.initialize(m_database);
return table;
}
Sqlite::Table createNewLocationsTable() const
{
Sqlite::Table table;
table.setName("newLocations");
table.setUseTemporaryTable(true);
table.addColumn("temporarySymbolId", Sqlite::ColumnType::Integer);
table.addColumn("symbolId", Sqlite::ColumnType::Integer);
const Sqlite::Column &sourceIdColumn = table.addColumn("sourceId", Sqlite::ColumnType::Integer);
const Sqlite::Column &lineColumn = table.addColumn("line", Sqlite::ColumnType::Integer);
const Sqlite::Column &columnColumn = table.addColumn("column", Sqlite::ColumnType::Integer);
table.addColumn("locationKind", Sqlite::ColumnType::Integer);
table.addUniqueIndex({sourceIdColumn, lineColumn, columnColumn});
table.initialize(m_database);
return table;
}
public:
Sqlite::ImmediateNonThrowingDestructorTransaction m_transaction;
Database &m_database;
Sqlite::Table newSymbolsTablet{createNewSymbolsTable()};
Sqlite::Table newLocationsTable{createNewLocationsTable()};
WriteStatement m_insertSymbolsToNewSymbolsStatement{
"INSERT INTO newSymbols(temporarySymbolId, usr, symbolName, symbolKind) VALUES(?,?,?,?)",
m_database};
WriteStatement m_insertLocationsToNewLocationsStatement{
"INSERT OR IGNORE INTO newLocations(temporarySymbolId, line, column, sourceId, locationKind) VALUES(?,?,?,?,?)",
m_database
};
ReadStatement m_selectNewSourceIdsStatement{
"SELECT DISTINCT sourceId FROM newLocations WHERE NOT EXISTS (SELECT sourceId FROM sources WHERE newLocations.sourceId == sources.sourceId)",
m_database
};
WriteStatement m_addNewSymbolsToSymbolsStatement{
"INSERT INTO symbols(usr, symbolName, symbolKind) "
"SELECT usr, symbolName, symbolKind FROM newSymbols WHERE NOT EXISTS "
"(SELECT usr FROM symbols WHERE symbols.usr == newSymbols.usr)",
m_database
};
WriteStatement m_syncNewSymbolsFromSymbolsStatement{
"UPDATE newSymbols SET symbolId = (SELECT symbolId FROM symbols WHERE newSymbols.usr = symbols.usr)",
m_database
};
WriteStatement m_syncSymbolsIntoNewLocationsStatement{
"UPDATE newLocations SET symbolId = (SELECT symbolId FROM newSymbols WHERE newSymbols.temporarySymbolId = newLocations.temporarySymbolId)",
m_database
};
WriteStatement m_deleteAllLocationsFromUpdatedFilesStatement{
"DELETE FROM locations WHERE sourceId IN (SELECT DISTINCT sourceId FROM newLocations)",
m_database
};
WriteStatement m_insertNewLocationsInLocationsStatement{
"INSERT INTO locations(symbolId, line, column, sourceId, locationKind) SELECT symbolId, line, column, sourceId, locationKind FROM newLocations",
m_database
};
WriteStatement m_deleteNewSymbolsTableStatement{
"DELETE FROM newSymbols",
m_database
};
WriteStatement m_deleteNewLocationsTableStatement{
"DELETE FROM newLocations",
m_database
};
WriteStatement m_insertProjectPartStatement{
"INSERT OR IGNORE INTO projectParts(projectPartName, compilerArguments, compilerMacros, includeSearchPaths) VALUES (?,?,?,?)",
m_database
};
WriteStatement m_updateProjectPartStatement{
"UPDATE projectParts SET compilerArguments = ?, compilerMacros = ?, includeSearchPaths = ? WHERE projectPartName = ?",
m_database
};
mutable ReadStatement m_getProjectPartIdStatement{
"SELECT projectPartId FROM projectParts WHERE projectPartName = ?",
m_database
};
WriteStatement m_deleteAllProjectPartsSourcesWithProjectPartIdStatement{
"DELETE FROM projectPartsSources WHERE projectPartId = ?",
m_database
};
WriteStatement m_insertProjectPartSourcesStatement{
"INSERT INTO projectPartsSources(projectPartId, sourceId) VALUES (?,?)",
m_database
};
mutable ReadStatement m_getCompileArgumentsForFileIdStatement{
"SELECT compilerArguments FROM projectParts WHERE projectPartId = (SELECT projectPartId FROM projectPartsSources WHERE sourceId = ?)",
m_database
};
mutable ReadStatement m_getProjectPartArtefactsBySourceId{
"SELECT compilerArguments, compilerMacros, includeSearchPaths, projectPartId FROM projectParts WHERE projectPartId = (SELECT projectPartId FROM projectPartsSources WHERE sourceId = ?)",
m_database
};
mutable ReadStatement m_getProjectPartArtefactsByProjectPartName{
"SELECT compilerArguments, compilerMacros, includeSearchPaths, projectPartId FROM projectParts WHERE projectPartName = ?",
m_database
};
mutable ReadStatement m_getPrecompiledHeader{
"SELECT pchPath, pchBuildTime FROM precompiledHeaders WHERE projectPartId = ?",
m_database
};
};
} // namespace ClangBackEnd