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

View File

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

View File

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

View File

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