2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2019 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2019-05-16 15:36:55 +02:00
|
|
|
|
|
|
|
|
#include "compilationdbparser.h"
|
|
|
|
|
|
2022-06-30 09:08:47 +02:00
|
|
|
#include "compilationdatabaseconstants.h"
|
2023-02-08 14:30:01 +01:00
|
|
|
#include "compilationdatabaseprojectmanagertr.h"
|
2022-06-30 09:08:47 +02:00
|
|
|
|
2019-05-16 15:36:55 +02:00
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
|
|
|
|
#include <projectexplorer/treescanner.h>
|
2023-03-07 15:48:57 +01:00
|
|
|
#include <utils/asynctask.h>
|
2022-02-23 17:11:20 +01:00
|
|
|
#include <utils/mimeutils.h>
|
2019-05-16 15:36:55 +02:00
|
|
|
|
2019-10-22 09:43:24 +02:00
|
|
|
#include <QCryptographicHash>
|
2019-05-16 15:36:55 +02:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
|
|
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
|
|
|
|
namespace CompilationDatabaseProjectManager {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2019-08-06 14:46:37 +02:00
|
|
|
CompilationDbParser::CompilationDbParser(const QString &projectName,
|
|
|
|
|
const FilePath &projectPath,
|
|
|
|
|
const FilePath &rootPath,
|
|
|
|
|
MimeBinaryCache &mimeBinaryCache,
|
2019-10-25 09:55:32 +02:00
|
|
|
BuildSystem::ParseGuard &&guard,
|
2019-05-16 15:36:55 +02:00
|
|
|
QObject *parent)
|
2019-08-06 14:46:37 +02:00
|
|
|
: QObject(parent)
|
|
|
|
|
, m_projectName(projectName)
|
|
|
|
|
, m_projectFilePath(projectPath)
|
|
|
|
|
, m_rootPath(rootPath)
|
|
|
|
|
, m_mimeBinaryCache(mimeBinaryCache)
|
|
|
|
|
, m_guard(std::move(guard))
|
2019-05-16 15:36:55 +02:00
|
|
|
{
|
|
|
|
|
connect(&m_parserWatcher, &QFutureWatcher<void>::finished, this, [this] {
|
|
|
|
|
m_dbContents = m_parserWatcher.result();
|
2021-09-15 16:32:11 +02:00
|
|
|
parserJobFinished();
|
2019-05-16 15:36:55 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CompilationDbParser::start()
|
|
|
|
|
{
|
2019-10-22 09:43:24 +02:00
|
|
|
// 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;
|
2021-09-15 16:32:11 +02:00
|
|
|
m_runningParserJobs = 0;
|
2019-10-22 09:43:24 +02:00
|
|
|
|
2019-05-16 15:36:55 +02:00
|
|
|
// Thread 1: Scan disk.
|
|
|
|
|
if (!m_rootPath.isEmpty()) {
|
|
|
|
|
m_treeScanner = new TreeScanner(this);
|
2019-05-28 13:49:26 +02:00
|
|
|
m_treeScanner->setFilter([this](const MimeType &mimeType, const FilePath &fn) {
|
2019-05-16 15:36:55 +02:00
|
|
|
// Mime checks requires more resources, so keep it last in check list
|
|
|
|
|
bool isIgnored = fn.toString().startsWith(m_projectFilePath.toString() + ".user")
|
|
|
|
|
|| TreeScanner::isWellKnownBinary(mimeType, fn);
|
|
|
|
|
|
|
|
|
|
// Cache mime check result for speed up
|
|
|
|
|
if (!isIgnored) {
|
|
|
|
|
auto it = m_mimeBinaryCache.find(mimeType.name());
|
|
|
|
|
if (it != m_mimeBinaryCache.end()) {
|
|
|
|
|
isIgnored = *it;
|
|
|
|
|
} else {
|
|
|
|
|
isIgnored = TreeScanner::isMimeBinary(mimeType, fn);
|
|
|
|
|
m_mimeBinaryCache[mimeType.name()] = isIgnored;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isIgnored;
|
|
|
|
|
});
|
2019-05-28 13:49:26 +02:00
|
|
|
m_treeScanner->setTypeFactory([](const Utils::MimeType &mimeType, const Utils::FilePath &fn) {
|
2019-05-16 15:36:55 +02:00
|
|
|
return TreeScanner::genericFileType(mimeType, fn);
|
|
|
|
|
});
|
|
|
|
|
m_treeScanner->asyncScanForFiles(m_rootPath);
|
|
|
|
|
Core::ProgressManager::addTask(m_treeScanner->future(),
|
2023-02-08 14:30:01 +01:00
|
|
|
Tr::tr("Scan \"%1\" project tree").arg(m_projectName),
|
2019-05-16 15:36:55 +02:00
|
|
|
"CompilationDatabase.Scan.Tree");
|
2021-09-15 16:32:11 +02:00
|
|
|
++m_runningParserJobs;
|
|
|
|
|
connect(m_treeScanner, &TreeScanner::finished,
|
|
|
|
|
this, &CompilationDbParser::parserJobFinished);
|
2019-05-16 15:36:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Thread 2: Parse the project file.
|
2023-03-07 15:48:57 +01:00
|
|
|
const QFuture<DbContents> future = Utils::asyncRun(&CompilationDbParser::parseProject, this);
|
2019-05-16 15:36:55 +02:00
|
|
|
Core::ProgressManager::addTask(future,
|
2023-02-08 14:30:01 +01:00
|
|
|
Tr::tr("Parse \"%1\" project").arg(m_projectName),
|
2019-05-16 15:36:55 +02:00
|
|
|
"CompilationDatabase.Parse");
|
2021-09-15 16:32:11 +02:00
|
|
|
++m_runningParserJobs;
|
2019-05-16 15:36:55 +02:00
|
|
|
m_parserWatcher.setFuture(future);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CompilationDbParser::stop()
|
|
|
|
|
{
|
|
|
|
|
disconnect();
|
|
|
|
|
m_parserWatcher.disconnect();
|
|
|
|
|
m_parserWatcher.cancel();
|
|
|
|
|
if (m_treeScanner) {
|
|
|
|
|
m_treeScanner->disconnect();
|
|
|
|
|
m_treeScanner->future().cancel();
|
|
|
|
|
}
|
2019-08-06 14:46:37 +02:00
|
|
|
m_guard = {};
|
2019-05-16 15:36:55 +02:00
|
|
|
deleteLater();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<FileNode *> CompilationDbParser::scannedFiles() const
|
|
|
|
|
{
|
2021-10-21 14:05:40 +02:00
|
|
|
const bool canceled = m_treeScanner->future().isCanceled();
|
2021-06-16 11:33:35 +02:00
|
|
|
const TreeScanner::Result result = m_treeScanner->release();
|
2021-10-21 14:05:40 +02:00
|
|
|
return !canceled ? result.allFiles : QList<FileNode *>();
|
2019-05-16 15:36:55 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-15 16:32:11 +02:00
|
|
|
void CompilationDbParser::parserJobFinished()
|
|
|
|
|
{
|
|
|
|
|
if (--m_runningParserJobs == 0)
|
|
|
|
|
finish(ParseResult::Success);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 09:43:24 +02:00
|
|
|
void CompilationDbParser::finish(ParseResult result)
|
2019-05-16 15:36:55 +02:00
|
|
|
{
|
2022-07-18 08:13:38 +03:00
|
|
|
if (result != ParseResult::Failure)
|
|
|
|
|
m_guard.markAsSuccess();
|
|
|
|
|
|
2019-10-22 09:43:24 +02:00
|
|
|
emit finished(result);
|
2019-05-16 15:36:55 +02:00
|
|
|
deleteLater();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QStringList jsonObjectFlags(const QJsonObject &object, QSet<QString> &flagsCache)
|
|
|
|
|
{
|
|
|
|
|
QStringList flags;
|
|
|
|
|
const QJsonArray arguments = object["arguments"].toArray();
|
|
|
|
|
if (arguments.isEmpty()) {
|
|
|
|
|
flags = splitCommandLine(object["command"].toString(), flagsCache);
|
|
|
|
|
} else {
|
|
|
|
|
flags.reserve(arguments.size());
|
|
|
|
|
for (const QJsonValue &arg : arguments) {
|
|
|
|
|
auto flagIt = flagsCache.insert(arg.toString());
|
|
|
|
|
flags.append(*flagIt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-01 14:37:30 +02:00
|
|
|
static FilePath jsonObjectFilePath(const QJsonObject &object)
|
2019-05-16 15:36:55 +02:00
|
|
|
{
|
2023-01-26 13:52:11 +01:00
|
|
|
const FilePath workingDir = FilePath::fromUserInput(object["directory"].toString());
|
|
|
|
|
return workingDir.resolvePath(object["file"].toString());
|
2019-05-16 15:36:55 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-22 09:43:24 +02:00
|
|
|
std::vector<DbEntry> CompilationDbParser::readJsonObjects() const
|
2019-05-16 15:36:55 +02:00
|
|
|
{
|
|
|
|
|
std::vector<DbEntry> result;
|
|
|
|
|
|
2019-10-22 09:43:24 +02:00
|
|
|
int objectStart = m_projectFileContents.indexOf('{');
|
|
|
|
|
int objectEnd = m_projectFileContents.indexOf('}', objectStart + 1);
|
2019-05-16 15:36:55 +02:00
|
|
|
|
|
|
|
|
QSet<QString> flagsCache;
|
|
|
|
|
while (objectStart >= 0 && objectEnd >= 0) {
|
|
|
|
|
const QJsonDocument document = QJsonDocument::fromJson(
|
2019-10-22 09:43:24 +02:00
|
|
|
m_projectFileContents.mid(objectStart, objectEnd - objectStart + 1));
|
2019-05-16 15:36:55 +02:00
|
|
|
if (document.isNull()) {
|
|
|
|
|
// The end was found incorrectly, search for the next one.
|
2019-10-22 09:43:24 +02:00
|
|
|
objectEnd = m_projectFileContents.indexOf('}', objectEnd + 1);
|
2019-05-16 15:36:55 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QJsonObject object = document.object();
|
2022-08-01 14:37:30 +02:00
|
|
|
const Utils::FilePath filePath = jsonObjectFilePath(object);
|
2019-05-16 15:36:55 +02:00
|
|
|
const QStringList flags = filterFromFileName(jsonObjectFlags(object, flagsCache),
|
2022-08-01 14:37:30 +02:00
|
|
|
filePath.fileName());
|
|
|
|
|
result.push_back({flags, filePath, object["directory"].toString()});
|
2019-05-16 15:36:55 +02:00
|
|
|
|
2019-10-22 09:43:24 +02:00
|
|
|
objectStart = m_projectFileContents.indexOf('{', objectEnd + 1);
|
|
|
|
|
objectEnd = m_projectFileContents.indexOf('}', objectStart + 1);
|
2019-05-16 15:36:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList readExtraFiles(const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
QStringList result;
|
|
|
|
|
|
|
|
|
|
QFile file(filePath);
|
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
|
QTextStream stream(&file);
|
|
|
|
|
|
|
|
|
|
while (!stream.atEnd()) {
|
|
|
|
|
QString line = stream.readLine();
|
|
|
|
|
line = line.trimmed();
|
|
|
|
|
|
|
|
|
|
if (line.isEmpty() || line.startsWith('#'))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
result.push_back(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DbContents CompilationDbParser::parseProject()
|
|
|
|
|
{
|
|
|
|
|
DbContents dbContents;
|
2019-10-22 09:43:24 +02:00
|
|
|
dbContents.entries = readJsonObjects();
|
2019-05-16 15:36:55 +02:00
|
|
|
dbContents.extraFileName = m_projectFilePath.toString() +
|
|
|
|
|
Constants::COMPILATIONDATABASEPROJECT_FILES_SUFFIX;
|
|
|
|
|
dbContents.extras = readExtraFiles(dbContents.extraFileName);
|
|
|
|
|
std::sort(dbContents.entries.begin(), dbContents.entries.end(),
|
|
|
|
|
[](const DbEntry &lhs, const DbEntry &rhs) {
|
|
|
|
|
return std::lexicographical_compare(lhs.flags.begin(), lhs.flags.end(),
|
|
|
|
|
rhs.flags.begin(), rhs.flags.end());
|
|
|
|
|
});
|
|
|
|
|
return dbContents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace CompilationDatabaseProjectManager
|