1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-18 08:51:52 +08:00

cmGeneratorExpression: Update strip function to collect parsed expressions

This commit is contained in:
Martin Duffy
2025-04-08 09:32:46 -04:00
parent 902b0b46c8
commit 13c7bb5b0c
2 changed files with 38 additions and 8 deletions

View File

@@ -5,6 +5,7 @@
#include <algorithm>
#include <cassert>
#include <memory>
#include <stack>
#include <utility>
#include <cm/string_view>
@@ -156,27 +157,40 @@ std::string cmGeneratorExpression::StripEmptyListElements(
return result;
}
static std::string stripAllGeneratorExpressions(std::string const& input)
static std::string extractAllGeneratorExpressions(
std::string const& input,
std::map<std::string, std::vector<std::string>>* collected)
{
std::string result;
std::string::size_type pos = 0;
std::string::size_type lastPos = pos;
int nestingLevel = 0;
// stack of { Generator Expression Name, Start Position of Value }
std::stack<std::pair<std::string, std::string::size_type>> genexps;
while ((pos = input.find("$<", lastPos)) != std::string::npos) {
result += input.substr(lastPos, pos - lastPos);
pos += 2;
nestingLevel = 1;
char const* c = input.c_str() + pos;
char const* cName = c;
char const* const cStart = c;
for (; *c; ++c) {
if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
++nestingLevel;
++c;
cName = c + 1;
continue;
}
if (c[0] == '>') {
--nestingLevel;
if (nestingLevel == 0) {
if (c[0] == ':' && cName) {
genexps.push({ input.substr(pos + (cName - cStart), c - cName),
pos + (c + 1 - cStart) });
cName = nullptr;
} else if (c[0] == '>') {
if (!cName && !genexps.empty()) {
if (collected) {
(*collected)[genexps.top().first].push_back(input.substr(
genexps.top().second, pos + c - cStart - genexps.top().second));
}
genexps.pop();
}
if (genexps.empty()) {
break;
}
}
@@ -188,12 +202,17 @@ static std::string stripAllGeneratorExpressions(std::string const& input)
pos += traversed;
lastPos = pos;
}
if (nestingLevel == 0) {
if (genexps.empty()) {
result += input.substr(lastPos);
}
return cmGeneratorExpression::StripEmptyListElements(result);
}
static std::string stripAllGeneratorExpressions(std::string const& input)
{
return extractAllGeneratorExpressions(input, nullptr);
}
static void prefixItems(std::string const& content, std::string& result,
cm::string_view const& prefix)
{
@@ -375,6 +394,13 @@ std::string cmGeneratorExpression::Preprocess(std::string const& input,
return std::string();
}
std::string cmGeneratorExpression::Collect(
std::string const& input,
std::map<std::string, std::vector<std::string>>& collected)
{
return extractAllGeneratorExpressions(input, &collected);
}
cm::string_view::size_type cmGeneratorExpression::Find(
cm::string_view const& input)
{

View File

@@ -63,6 +63,10 @@ public:
PreprocessContext context,
cm::string_view importPrefix = {});
static std::string Collect(
std::string const& input,
std::map<std::string, std::vector<std::string>>& collected);
static void Split(std::string const& input,
std::vector<std::string>& output);