fix: Keep string literals whole in macro arguments (#1065)

This commit is contained in:
wroy authored and GitHub committed 2026-11-18 19:46:14 +01:00
1 parent 9ec01498ac
commit 0beced197e
3 files changed
+41 -2

No files matched your search

+21 -2
View File
@@ -69,7 +69,9 @@ DefinesStreamProxy::Define::Define(std::string name, std::string value, const bo
}
DefinesStreamProxy::MacroParameterState::MacroParameterState()
: m_parameter_state(ParameterState::NOT_IN_PARAMETERS)
: m_parameter_state(ParameterState::NOT_IN_PARAMETERS),
m_in_string(false),
m_after_escape(false)
{
}
@@ -929,7 +931,24 @@ void DefinesStreamProxy::ContinueMacroParameters(
{
const auto c = input[inputPos];
if (c == ',')
if (state.m_in_string)
{
if (state.m_after_escape)
state.m_after_escape = false;
else if (c == '\\')
state.m_after_escape = true;
else if (c == '"')
state.m_in_string = false;
state.m_current_parameter << c;
}
else if (c == '"')
{
state.m_in_string = true;
state.m_parameter_state = ParameterState::AFTER_PARAM;
state.m_current_parameter << c;
}
else if (c == ',')
{
if (!state.m_bracket_depth.empty())
{
@@ -60,6 +60,8 @@ public:
std::ostringstream m_current_parameter;
std::vector<std::string> m_parameters;
std::stack<char> m_bracket_depth;
bool m_in_string;
bool m_after_escape;
};
explicit DefinesStreamProxy(IParserLineStream* stream, bool skipDirectiveLines = false);
@@ -915,6 +915,24 @@ namespace test::parsing::impl::defines_stream_proxy
REQUIRE(proxy.Eof());
}
TEST_CASE("DefinesStreamProxy: Macro arguments can contain strings with commas and brackets", "[parsing][parsingstream]")
{
const std::vector<std::string> lines{
"#define testMacro(a, b) [a|b]",
R"(testMacro("Hello, world", "a ) b"))",
R"(testMacro("with \"quotes, and\" more", x))",
};
MockParserLineStream mockStream(lines);
DefinesStreamProxy proxy(&mockStream);
ExpectLine(&proxy, 1, "");
ExpectLine(&proxy, 2, R"(["Hello, world"|"a ) b"])");
ExpectLine(&proxy, 3, R"(["with \"quotes, and\" more"|x])");
REQUIRE(proxy.Eof());
}
TEST_CASE("DefinesStreamProxy: Can use second macro after multi-line macro", "[parsing][parsingstream]")
{
const std::vector<std::string> lines{