CompilationDatabase: Force QString implicit sharing

Most of the flags in compilation database are the same,
let's share them by inserting them all into the temporary
QSet of QString-s.

Change-Id: I7d9b410b4b0bee40247434b49371bd37214d4c59
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Ivan Donchevskii
2019-04-15 13:44:01 +02:00
parent e76efa69d5
commit a5ee354b19
3 changed files with 24 additions and 13 deletions

View File

@@ -64,16 +64,18 @@ namespace Internal {
namespace {
QStringList jsonObjectFlags(const QJsonObject &object)
QStringList jsonObjectFlags(const QJsonObject &object, QSet<QString> &flagsCache)
{
QStringList flags;
const QJsonArray arguments = object["arguments"].toArray();
if (arguments.isEmpty()) {
flags = splitCommandLine(object["command"].toString());
flags = splitCommandLine(object["command"].toString(), flagsCache);
} else {
flags.reserve(arguments.size());
for (const QJsonValue &arg : arguments)
flags.append(arg.toString());
for (const QJsonValue &arg : arguments) {
auto flagIt = flagsCache.insert(arg.toString());
flags.append(*flagIt);
}
}
return flags;
@@ -339,6 +341,7 @@ std::vector<Entry> readJsonObjects(const QString &filePath)
int objectStart = contents.indexOf('{');
int objectEnd = contents.indexOf('}', objectStart + 1);
QSet<QString> flagsCache;
while (objectStart >= 0 && objectEnd >= 0) {
const QJsonDocument document = QJsonDocument::fromJson(
contents.mid(objectStart, objectEnd - objectStart + 1));
@@ -350,8 +353,8 @@ std::vector<Entry> readJsonObjects(const QString &filePath)
const QJsonObject object = document.object();
const Utils::FileName fileName = jsonObjectFilename(object);
const QStringList flags
= filterFromFileName(jsonObjectFlags(object), fileName.toFileInfo().baseName());
const QStringList flags = filterFromFileName(jsonObjectFlags(object, flagsCache),
fileName.toFileInfo().baseName());
result.push_back({flags, fileName, object["directory"].toString()});
objectStart = contents.indexOf('{', objectEnd + 1);

View File

@@ -33,6 +33,7 @@
#include <QDir>
#include <QRegularExpression>
#include <QSet>
using namespace ProjectExplorer;
@@ -205,7 +206,7 @@ void filteredFlags(const QString &fileName,
flags = filtered;
}
QStringList splitCommandLine(QString commandLine)
QStringList splitCommandLine(QString commandLine, QSet<QString> &flagsCache)
{
QStringList result;
bool insideQuotes = false;
@@ -215,12 +216,19 @@ QStringList splitCommandLine(QString commandLine)
for (const QString &part : commandLine.split(QRegularExpression("\""))) {
if (insideQuotes) {
const QString quotedPart = "\"" + part + "\"";
if (result.last().endsWith("="))
result.last().append(quotedPart);
else
result.append(quotedPart);
if (result.last().endsWith("=")) {
auto flagIt = flagsCache.insert(result.last() + quotedPart);
result.last() = *flagIt;
} else {
auto flagIt = flagsCache.insert(quotedPart);
result.append(*flagIt);
}
} else { // If 's' is outside quotes ...
result.append(part.split(QRegularExpression("\\s+"), QString::SkipEmptyParts));
for (const QString &flag :
part.split(QRegularExpression("\\s+"), QString::SkipEmptyParts)) {
auto flagIt = flagsCache.insert(flag);
result.append(*flagIt);
}
}
insideQuotes = !insideQuotes;
}

View File

@@ -47,6 +47,6 @@ void filteredFlags(const QString &fileName,
QVector<ProjectExplorer::Macro> &macros,
CppTools::ProjectFile::Kind &fileKind);
QStringList splitCommandLine(QString commandLine);
QStringList splitCommandLine(QString commandLine, QSet<QString> &flagsCache);
} // namespace CompilationDatabaseProjectManager