tracegen: Fix handling enumerators with duplicate values

Aggregate the names of the same value enumerators. The values can also
be hexadecimal so handle them also.

Change-Id: I89693d7e3b8f6c051b298401dcbe8a9f5c0a38aa
Reviewed-by: Antti Määttä <antti.maatta@qt.io>
Reviewed-by: Tomi Korpipää <tomi.korpipaa@qt.io>
(cherry picked from commit 83effb3a3f6bf73e682f7a9ccedebf4073ade776)
This commit is contained in:
Antti Määttä 2023-01-30 09:01:54 +02:00
parent 1f23ed3c85
commit b3f77d25aa
5 changed files with 72 additions and 22 deletions

View File

@ -305,11 +305,20 @@ static void writeEnums(QTextStream &stream, const Provider &provider)
name.replace(QStringLiteral("::"), QStringLiteral("_"));
stream << "TRACEPOINT_METADATA(" << provider.name << ", " << name << ", \n";
stream << "QStringLiteral(\"typealias enum : integer { size = " << e.valueSize << "; } {\\n\\\n";
for (const auto &v : e.values) {
if (v.range)
const auto values = e.values;
QList<int> handledValues;
for (const auto &v : values) {
if (handledValues.contains(v.value))
continue;
if (v.range) {
stream << v.name << " = " << v.value << " ... " << v.range << ", \\n\\\n";
else
stream << v.name << " = " << v.value << ", \\n\\\n";
} else {
const QString names = aggregateListValues(v.value, values);
stream << names << " = " << v.value << ", \\n\\\n";
handledValues.append(v.value);
}
}
stream << "} := " << name << ";\\n\\n\"));\n\n";
}
@ -323,8 +332,16 @@ static void writeFlags(QTextStream &stream, const Provider &provider)
name.replace(QStringLiteral("::"), QStringLiteral("_"));
stream << "TRACEPOINT_METADATA(" << provider.name << ", " << name << ", \n";
stream << "QStringLiteral(\"typealias enum : integer { size = 8; } {\\n\\\n";
for (const auto &v : e.values) {
stream << v.name << " = " << v.value << ", \\n\\\n";
const auto values = e.values;
QList<int> handledValues;
for (const auto &v : values) {
if (handledValues.contains(v.value))
continue;
const QString names = aggregateListValues(v.value, values);
stream << names << " = " << v.value << ", \\n\\\n";
handledValues.append(v.value);
}
stream << "} := " << name << ";\\n\\n\"));\n\n";
}

View File

@ -215,9 +215,14 @@ static void writeEnumConverter(QTextStream &stream, const TraceEnum &enumeration
}
}
stream << "\n QString ret;\n switch (val) {\n";
QList<int> handledValues;
for (const auto &v : enumeration.values) {
if (v.range == 0)
stream << " case " << v.value << ": ret = QStringLiteral(\"" << v.name << "\"); break;\n";
if (v.range == 0 && !handledValues.contains(v.value)) {
stream << " case " << v.value << ": ret = QStringLiteral(\""
<< aggregateListValues(v.value, enumeration.values) << "\"); break;\n";
handledValues.append(v.value);
}
}
stream << " }\n return ret;\n}\n";
@ -229,13 +234,18 @@ static void writeFlagConverter(QTextStream &stream, const TraceFlags &flag)
stream << "{\n QString ret;\n";
for (const auto &v : flag.values) {
if (v.value == 0) {
stream << " if (val == 0)\n return QStringLiteral(\"" << v.name << "\");\n";
stream << " if (val == 0)\n return QStringLiteral(\""
<< aggregateListValues(v.value, flag.values) << "\");\n";
break;
}
}
QList<int> handledValues;
for (const auto &v : flag.values) {
if (v.value != 0)
stream << " if (val & " << (1 << (v.value - 1)) << ") { if (ret.length()) ret += QLatin1Char(\'|\'); ret += QStringLiteral(\"" << v.name << "\"); }\n";
if (v.value != 0 && !handledValues.contains(v.value)) {
stream << " if (val & " << (1 << (v.value - 1))
<< ") { if (ret.length()) ret += QLatin1Char(\'|\'); ret += QStringLiteral(\"" << v.name << "\"); }\n";
handledValues.append(v.value);
}
}
stream << " return ret;\n}\n";
}

View File

@ -20,4 +20,15 @@ QString includeGuard(const QString &filename);
QString formatFunctionSignature(const QList<Tracepoint::Argument> &args);
QString formatParameterList(const Provider &provider, const QList<Tracepoint::Argument> &args, const QList<Tracepoint::Field> &fields, ParamType type);
template <typename T>
static QString aggregateListValues(int value, const QList<T> &list)
{
QStringList values;
for (auto l : list) {
if (l.value == value)
values << l.name;
}
return values.join(QLatin1Char('_'));
}
#endif // HELPERS_H

View File

@ -198,11 +198,14 @@ static void writeEnums(QTextStream &stream, const Provider &provider)
<< " " << provider.name << ",\n"
<< " " << typeToTypeName(e.name) << ",\n"
<< " TP_ENUM_VALUES(\n";
QList<int> handledValues;
for (const auto &v : e.values) {
if (v.range > 0)
if (v.range > 0) {
stream << " ctf_enum_range(\"" << v.name << "\", " << v.value << ", " << v.range << ")\n";
else
stream << " ctf_enum_value(\"" << v.name << "\", " << v.value << ")\n";
} else if (!handledValues.contains(v.value)) {
stream << " ctf_enum_value(\"" << aggregateListValues(v.value, e.values) << "\", " << v.value << ")\n";
handledValues.append(v.value);
}
}
stream << " )\n)\n\n";
}
@ -215,8 +218,13 @@ static void writeFlags(QTextStream &stream, const Provider &provider)
<< " " << provider.name << ",\n"
<< " " << typeToTypeName(f.name) << ",\n"
<< " TP_ENUM_VALUES(\n";
for (const auto &v : f.values)
stream << " ctf_enum_value(\"" << v.name << "\", " << v.value << ")\n";
QList<int> handledValues;
for (const auto &v : f.values) {
if (!handledValues.contains(v.value)) {
stream << " ctf_enum_value(\"" << aggregateListValues(v.value, f.values) << "\", " << v.value << ")\n";
handledValues.append(v.value);
}
}
stream << " )\n)\n\n";
}

View File

@ -298,7 +298,7 @@ Provider parseProvider(const QString &filename)
static const QRegularExpression tracedef(QStringLiteral("^([A-Za-z][A-Za-z0-9_]*)\\((.*)\\)$"));
static const QRegularExpression enumenddef(QStringLiteral("^} ?([A-Za-z][A-Za-z0-9_:]*);"));
static const QRegularExpression enumdef(QStringLiteral("^([A-Za-z][A-Za-z0-9_]*)( ?= ?([0-9]*))?"));
static const QRegularExpression enumdef(QStringLiteral("^([A-Za-z][A-Za-z0-9_]*)( *= *([xabcdef0-9]*))?"));
static const QRegularExpression rangedef(QStringLiteral("^RANGE\\(([A-Za-z][A-Za-z0-9_]*) ?, ?([0-9]*) ?... ?([0-9]*) ?\\)"));
Provider provider;
@ -370,7 +370,7 @@ Provider parseProvider(const QString &filename)
value.name = m.captured(1);
value.value = m.captured(2).toInt();
value.range = m.captured(3).toInt();
currentEnumValue = value.range;
currentEnumValue = value.range + 1;
currentEnum.values.push_back(value);
maxEnumValue = qMax(maxEnumValue, value.range);
minEnumValue = qMin(minEnumValue, value.value);
@ -382,22 +382,26 @@ Provider parseProvider(const QString &filename)
value.name = m.captured(1);
value.value = m.captured(3).toInt();
value.range = 0;
currentEnumValue = value.value;
currentEnumValue = value.value + 1;
currentEnum.values.push_back(value);
maxEnumValue = qMax(maxEnumValue, value.value);
minEnumValue = qMin(minEnumValue, value.value);
} else {
TraceFlags::FlagValue value;
value.name = m.captured(1);
value.value = m.captured(3).toInt();
if (m.captured(3).startsWith(QStringLiteral("0x")))
value.value = m.captured(3).toInt(nullptr, 16);
else
value.value = m.captured(3).toInt();
if (!isPow2OrZero(value.value)) {
printf("Warning: '%s' line %d:\n"
" '%s' flag value is not power of two.\n",
qPrintable(filename), lineNumber,
qPrintable(line));
} else {
value.value = pow2Log2(value.value);
currentFlags.values.push_back(value);
}
value.value = pow2Log2(value.value);
currentFlags.values.push_back(value);
}
} else {
maxEnumValue = qMax(maxEnumValue, currentEnumValue);