Merge pull request #610 from GriffinRichards/parse-speed

Improve #define parsing speed
This commit is contained in:
GriffinR 2024-09-16 12:24:24 -04:00 committed by GitHub
commit 9feafd64b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 9 deletions

View file

@ -104,7 +104,8 @@ private:
};
ParsedDefines readCDefines(const QString &filename, const QStringList &filterList, bool useRegex);
QMap<QString, int> evaluateCDefines(const QString &filename, const QStringList &filterList, bool useRegex);
bool defineNameMatchesFilter(const QString &name, const QStringList &filterList, bool useRegex);
bool defineNameMatchesFilter(const QString &name, const QStringList &filterList) const;
bool defineNameMatchesFilter(const QString &name, const QList<QRegularExpression> &filterList) const;
static const QRegularExpression re_incScriptLabel;
static const QRegularExpression re_globalIncScriptLabel;

View file

@ -370,13 +370,14 @@ QStringList ParseUtil::readCIncbinArray(const QString &filename, const QString &
return paths;
}
bool ParseUtil::defineNameMatchesFilter(const QString &name, const QStringList &filterList, bool useRegex) {
bool ParseUtil::defineNameMatchesFilter(const QString &name, const QStringList &filterList) const {
return filterList.contains(name);
}
bool ParseUtil::defineNameMatchesFilter(const QString &name, const QList<QRegularExpression> &filterList) const {
for (auto filter : filterList) {
if (useRegex) {
// TODO: These QRegularExpression should probably be constructed beforehand,
// otherwise we recreate them for every define we check.
if (QRegularExpression(filter).match(name).hasMatch()) return true;
} else if (name == filter) return true;
if (filter.match(name).hasMatch())
return true;
}
return false;
}
@ -405,6 +406,21 @@ ParseUtil::ParsedDefines ParseUtil::readCDefines(const QString &filename, const
if (this->text.isEmpty())
return result;
// If necessary, construct regular expressions from filter list
QList<QRegularExpression> filterList_Regex;
if (useRegex) {
for (auto filter : filterList) {
filterList_Regex.append(QRegularExpression(filter));
}
}
// Create lambda function to match the define name to the filter, depending on the filter type
auto matchesFilter = [this, &filterList, &filterList_Regex, useRegex](const QString &name) {
if (useRegex)
return defineNameMatchesFilter(name, filterList_Regex);
return defineNameMatchesFilter(name, filterList);
};
// Capture either the name and value of a #define, or everything between the braces of 'enum { }'
static const QRegularExpression re("#define\\s+(?<defineName>\\w+)[\\s\\n][^\\S\\n]*(?<defineValue>.+)?"
"|\\benum\\b[^{]*{(?<enumBody>[^}]*)}");
@ -437,14 +453,14 @@ ParseUtil::ParsedDefines ParseUtil::readCDefines(const QString &filename, const
baseNum = 1;
}
result.expressions.insert(name, expression);
if (defineNameMatchesFilter(name, filterList, useRegex))
if (matchesFilter(name))
result.filteredNames.append(name);
}
} else {
// Encountered a #define
const QString name = match.captured("defineName");
result.expressions.insert(name, match.captured("defineValue"));
if (defineNameMatchesFilter(name, filterList, useRegex))
if (matchesFilter(name))
result.filteredNames.append(name);
}
}