1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-22 07:25:02 +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 <algorithm>
#include <cassert> #include <cassert>
#include <memory> #include <memory>
#include <stack>
#include <utility> #include <utility>
#include <cm/string_view> #include <cm/string_view>
@@ -156,27 +157,40 @@ std::string cmGeneratorExpression::StripEmptyListElements(
return result; 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 result;
std::string::size_type pos = 0; std::string::size_type pos = 0;
std::string::size_type lastPos = pos; 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) { while ((pos = input.find("$<", lastPos)) != std::string::npos) {
result += input.substr(lastPos, pos - lastPos); result += input.substr(lastPos, pos - lastPos);
pos += 2; pos += 2;
nestingLevel = 1;
char const* c = input.c_str() + pos; char const* c = input.c_str() + pos;
char const* cName = c;
char const* const cStart = c; char const* const cStart = c;
for (; *c; ++c) { for (; *c; ++c) {
if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) { if (cmGeneratorExpression::StartsWithGeneratorExpression(c)) {
++nestingLevel;
++c; ++c;
cName = c + 1;
continue; continue;
} }
if (c[0] == '>') { if (c[0] == ':' && cName) {
--nestingLevel; genexps.push({ input.substr(pos + (cName - cStart), c - cName),
if (nestingLevel == 0) { 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; break;
} }
} }
@@ -188,12 +202,17 @@ static std::string stripAllGeneratorExpressions(std::string const& input)
pos += traversed; pos += traversed;
lastPos = pos; lastPos = pos;
} }
if (nestingLevel == 0) { if (genexps.empty()) {
result += input.substr(lastPos); result += input.substr(lastPos);
} }
return cmGeneratorExpression::StripEmptyListElements(result); 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, static void prefixItems(std::string const& content, std::string& result,
cm::string_view const& prefix) cm::string_view const& prefix)
{ {
@@ -375,6 +394,13 @@ std::string cmGeneratorExpression::Preprocess(std::string const& input,
return std::string(); 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::size_type cmGeneratorExpression::Find(
cm::string_view const& input) cm::string_view const& input)
{ {

View File

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