diff --git a/include/core/parseutil.h b/include/core/parseutil.h index bc167a30..e3df8375 100644 --- a/include/core/parseutil.h +++ b/include/core/parseutil.h @@ -104,7 +104,8 @@ class ParseUtil }; ParsedDefines readCDefines(const QString &filename, const QStringList &filterList, bool useRegex); QMap 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 &filterList) const; static const QRegularExpression re_incScriptLabel; static const QRegularExpression re_globalIncScriptLabel; diff --git a/src/core/parseutil.cpp b/src/core/parseutil.cpp index 76d58af8..9664fdc7 100644 --- a/src/core/parseutil.cpp +++ b/src/core/parseutil.cpp @@ -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 &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 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+(?\\w+)[\\s\\n][^\\S\\n]*(?.+)?" "|\\benum\\b[^{]*{(?[^}]*)}"); @@ -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); } }