From 06162c1aa53ee9440f8b377db2ec86a9e7fd949c Mon Sep 17 00:00:00 2001 From: Ivan Donchevskii Date: Thu, 1 Nov 2018 13:05:34 +0100 Subject: [PATCH] CompilationDatabase: Use QJsonDocument for separate objects It is pretty easy to split objects without QJsonDocument help. Due to the QJsonObject data size restriction use it only for the already split objects inside the database. Change-Id: Ice7c7407ad00aaac151a767f4d943fdcecf6a6b8 Reviewed-by: Orgad Shaneh --- .../compilationdatabaseproject.cpp | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/plugins/compilationdatabaseprojectmanager/compilationdatabaseproject.cpp b/src/plugins/compilationdatabaseprojectmanager/compilationdatabaseproject.cpp index 9e724cb8252..155ed5185aa 100644 --- a/src/plugins/compilationdatabaseprojectmanager/compilationdatabaseproject.cpp +++ b/src/plugins/compilationdatabaseprojectmanager/compilationdatabaseproject.cpp @@ -302,26 +302,51 @@ void createFolders(FolderNode *root, const Utils::FileName &rootPath) } } +std::vector readJsonObjects(const QString &filePath) +{ + std::vector 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); + + while (objectStart >= 0 && objectEnd >= 0) { + const QJsonDocument document = QJsonDocument::fromJson( + contents.mid(objectStart, objectEnd - objectStart + 1)); + if (document.isNull()) { + // The end was found incorrectly, search for the next one. + objectEnd = contents.indexOf('}', objectEnd + 1); + continue; + } + + result.push_back(document.object()); + objectStart = contents.indexOf('{', objectEnd + 1); + objectEnd = contents.indexOf('}', objectStart + 1); + } + + return result; +} + } // anonymous namespace void CompilationDatabaseProject::buildTreeAndProjectParts(const Utils::FileName &projectFile) { - QFile file(projectFilePath().toString()); - if (!file.open(QIODevice::ReadOnly)) { + std::vector array = readJsonObjects(projectFilePath().toString()); + if (array.empty()) { emitParsingFinished(false); return; } - const QJsonArray array = QJsonDocument::fromJson(file.readAll()).array(); - auto root = std::make_unique(projectDirectory()); CppTools::RawProjectParts rpps; Utils::FileName commonPath; ToolChain *cToolchain = nullptr; ToolChain *cxxToolchain = nullptr; - for (const QJsonValue &element : array) { - const QJsonObject object = element.toObject(); + for (const QJsonObject &object : array) { Utils::FileName fileName = jsonObjectFilename(object); const QStringList flags = jsonObjectFlags(object);