CompilationDB: Reparse only on actual project file change

Fixes: QTCREATORBUG-22574
Change-Id: I39fe58f96c1ff9118405be225f39e5348304222e
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Christian Kandeler
2019-10-22 09:43:24 +02:00
parent 70e8954402
commit 61bb28cdc0
4 changed files with 49 additions and 24 deletions

View File

@@ -31,6 +31,7 @@
#include <utils/mimetypes/mimetype.h>
#include <utils/runextensions.h>
#include <QCryptographicHash>
#include <QDir>
#include <QFileInfo>
#include <QJsonArray>
@@ -59,12 +60,27 @@ CompilationDbParser::CompilationDbParser(const QString &projectName,
connect(&m_parserWatcher, &QFutureWatcher<void>::finished, this, [this] {
m_dbContents = m_parserWatcher.result();
if (!m_treeScanner || m_treeScanner->isFinished())
finish();
finish(ParseResult::Success);
});
}
void CompilationDbParser::start()
{
// Check hash first.
QFile file(m_projectFilePath.toString());
if (!file.open(QIODevice::ReadOnly)) {
finish(ParseResult::Failure);
return;
}
m_projectFileContents = file.readAll();
const QByteArray newHash = QCryptographicHash::hash(m_projectFileContents,
QCryptographicHash::Sha1);
if (m_projectFileHash == newHash) {
finish(ParseResult::Cached);
return;
}
m_projectFileHash = newHash;
// Thread 1: Scan disk.
if (!m_rootPath.isEmpty()) {
m_treeScanner = new TreeScanner(this);
@@ -95,7 +111,7 @@ void CompilationDbParser::start()
"CompilationDatabase.Scan.Tree");
connect(m_treeScanner, &TreeScanner::finished, this, [this] {
if (m_parserWatcher.isFinished())
finish();
finish(ParseResult::Success);
});
}
@@ -126,9 +142,9 @@ QList<FileNode *> CompilationDbParser::scannedFiles() const
? m_treeScanner->release() : QList<FileNode *>();
}
void CompilationDbParser::finish()
void CompilationDbParser::finish(ParseResult result)
{
emit finished(true);
emit finished(result);
deleteLater();
}
@@ -158,24 +174,20 @@ static FilePath jsonObjectFilename(const QJsonObject &object)
return fileName;
}
static std::vector<DbEntry> readJsonObjects(const QString &filePath)
std::vector<DbEntry> CompilationDbParser::readJsonObjects() const
{
std::vector<DbEntry> result;
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly))
return result;
const QByteArray contents = file.readAll();
int objectStart = contents.indexOf('{');
int objectEnd = contents.indexOf('}', objectStart + 1);
int objectStart = m_projectFileContents.indexOf('{');
int objectEnd = m_projectFileContents.indexOf('}', objectStart + 1);
QSet<QString> flagsCache;
while (objectStart >= 0 && objectEnd >= 0) {
const QJsonDocument document = QJsonDocument::fromJson(
contents.mid(objectStart, objectEnd - objectStart + 1));
m_projectFileContents.mid(objectStart, objectEnd - objectStart + 1));
if (document.isNull()) {
// The end was found incorrectly, search for the next one.
objectEnd = contents.indexOf('}', objectEnd + 1);
objectEnd = m_projectFileContents.indexOf('}', objectEnd + 1);
continue;
}
@@ -185,8 +197,8 @@ static std::vector<DbEntry> readJsonObjects(const QString &filePath)
fileName.toFileInfo().baseName());
result.push_back({flags, fileName, object["directory"].toString()});
objectStart = contents.indexOf('{', objectEnd + 1);
objectEnd = contents.indexOf('}', objectStart + 1);
objectStart = m_projectFileContents.indexOf('{', objectEnd + 1);
objectEnd = m_projectFileContents.indexOf('}', objectStart + 1);
}
return result;
@@ -217,7 +229,7 @@ QStringList readExtraFiles(const QString &filePath)
DbContents CompilationDbParser::parseProject()
{
DbContents dbContents;
dbContents.entries = readJsonObjects(m_projectFilePath.toString());
dbContents.entries = readJsonObjects();
dbContents.extraFileName = m_projectFilePath.toString() +
Constants::COMPILATIONDATABASEPROJECT_FILES_SUFFIX;
dbContents.extras = readExtraFiles(dbContents.extraFileName);