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); ParsedDefines readCDefines(const QString &filename, const QStringList &filterList, bool useRegex);
QMap<QString, int> evaluateCDefines(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_incScriptLabel;
static const QRegularExpression re_globalIncScriptLabel; static const QRegularExpression re_globalIncScriptLabel;

View file

@ -370,13 +370,14 @@ QStringList ParseUtil::readCIncbinArray(const QString &filename, const QString &
return paths; 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) { for (auto filter : filterList) {
if (useRegex) { if (filter.match(name).hasMatch())
// TODO: These QRegularExpression should probably be constructed beforehand, return true;
// otherwise we recreate them for every define we check.
if (QRegularExpression(filter).match(name).hasMatch()) return true;
} else if (name == filter) return true;
} }
return false; return false;
} }
@ -405,6 +406,21 @@ ParseUtil::ParsedDefines ParseUtil::readCDefines(const QString &filename, const
if (this->text.isEmpty()) if (this->text.isEmpty())
return result; 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 { }' // 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>.+)?" static const QRegularExpression re("#define\\s+(?<defineName>\\w+)[\\s\\n][^\\S\\n]*(?<defineValue>.+)?"
"|\\benum\\b[^{]*{(?<enumBody>[^}]*)}"); "|\\benum\\b[^{]*{(?<enumBody>[^}]*)}");
@ -437,14 +453,14 @@ ParseUtil::ParsedDefines ParseUtil::readCDefines(const QString &filename, const
baseNum = 1; baseNum = 1;
} }
result.expressions.insert(name, expression); result.expressions.insert(name, expression);
if (defineNameMatchesFilter(name, filterList, useRegex)) if (matchesFilter(name))
result.filteredNames.append(name); result.filteredNames.append(name);
} }
} else { } else {
// Encountered a #define // Encountered a #define
const QString name = match.captured("defineName"); const QString name = match.captured("defineName");
result.expressions.insert(name, match.captured("defineValue")); result.expressions.insert(name, match.captured("defineValue"));
if (defineNameMatchesFilter(name, filterList, useRegex)) if (matchesFilter(name))
result.filteredNames.append(name); result.filteredNames.append(name);
} }
} }