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

@@ -406,10 +406,11 @@ CompilationDatabaseProject::CompilationDatabaseProject(const Utils::FilePath &pr
m_kit.reset(KitManager::defaultKit()->clone());
addTargetForKit(m_kit.get());
connect(this,
&CompilationDatabaseProject::rootProjectDirectoryChanged,
m_parseDelay,
QOverload<>::of(&QTimer::start));
connect(this, &CompilationDatabaseProject::rootProjectDirectoryChanged,
this, [this] {
m_projectFileHash.clear();
m_parseDelay->start();
});
setExtraProjectFiles(
{projectFile.stringAppended(Constants::COMPILATIONDATABASEPROJECT_FILES_SUFFIX)});
@@ -458,11 +459,13 @@ void CompilationDatabaseProject::reparseProject()
m_mimeBinaryCache,
guardParsingRun(),
this);
connect(m_parser, &CompilationDbParser::finished, this, [this](bool success) {
if (success)
connect(m_parser, &CompilationDbParser::finished, this, [this](ParseResult result) {
m_projectFileHash = m_parser->projectFileHash();
if (result == ParseResult::Success)
buildTreeAndProjectParts();
m_parser = nullptr;
});
m_parser->setPreviousProjectFileHash(m_projectFileHash);
m_parser->start();
}

View File

@@ -70,6 +70,7 @@ private:
std::unique_ptr<CppTools::CppProjectUpdater> m_cppCodeModelUpdater;
std::unique_ptr<ProjectExplorer::Kit> m_kit;
MimeBinaryCache m_mimeBinaryCache;
QByteArray m_projectFileHash;
QTimer * const m_parseDelay;
CompilationDbParser *m_parser = nullptr;
};

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);

View File

@@ -46,6 +46,8 @@ class TreeScanner;
namespace CompilationDatabaseProjectManager {
namespace Internal {
enum class ParseResult { Success, Failure, Cached };
class CompilationDbParser : public QObject
{
Q_OBJECT
@@ -57,6 +59,10 @@ public:
ProjectExplorer::Project::ParseGuard &&guard,
QObject *parent = nullptr);
void setPreviousProjectFileHash(const QByteArray &fileHash) { m_projectFileHash = fileHash; }
QByteArray projectFileHash() const { return m_projectFileHash; }
void start();
void stop();
@@ -68,11 +74,12 @@ public:
}
signals:
void finished(bool success);
void finished(ParseResult result);
private:
void finish();
void finish(ParseResult result);
DbContents parseProject();
std::vector<DbEntry> readJsonObjects() const;
const QString m_projectName;
const Utils::FilePath m_projectFilePath;
@@ -81,6 +88,8 @@ private:
ProjectExplorer::TreeScanner *m_treeScanner = nullptr;
QFutureWatcher<DbContents> m_parserWatcher;
DbContents m_dbContents;
QByteArray m_projectFileContents;
QByteArray m_projectFileHash;
ProjectExplorer::Project::ParseGuard m_guard;
};