Skip to content

Commit

Permalink
Construct regex to filter defines beforehand
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Sep 11, 2024
1 parent a0ebae0 commit 2f5f401
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
3 changes: 2 additions & 1 deletion include/core/parseutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class ParseUtil
};
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;
Expand Down
32 changes: 24 additions & 8 deletions src/core/parseutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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>[^}]*)}");
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 2f5f401

Please sign in to comment.