diff --git a/src/Parser/Parsing/Impl/DefinesStreamProxy.cpp b/src/Parser/Parsing/Impl/DefinesStreamProxy.cpp index 00b5d264..d59483f1 100644 --- a/src/Parser/Parsing/Impl/DefinesStreamProxy.cpp +++ b/src/Parser/Parsing/Impl/DefinesStreamProxy.cpp @@ -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()) { diff --git a/src/Parser/Parsing/Impl/DefinesStreamProxy.h b/src/Parser/Parsing/Impl/DefinesStreamProxy.h index 0daeafbb..becdb45d 100644 --- a/src/Parser/Parsing/Impl/DefinesStreamProxy.h +++ b/src/Parser/Parsing/Impl/DefinesStreamProxy.h @@ -60,6 +60,8 @@ public: std::ostringstream m_current_parameter; std::vector m_parameters; std::stack m_bracket_depth; + bool m_in_string; + bool m_after_escape; }; explicit DefinesStreamProxy(IParserLineStream* stream, bool skipDirectiveLines = false); diff --git a/test/ParserTests/Parsing/Impl/DefinesStreamProxyTests.cpp b/test/ParserTests/Parsing/Impl/DefinesStreamProxyTests.cpp index aef72382..bc3c3423 100644 --- a/test/ParserTests/Parsing/Impl/DefinesStreamProxyTests.cpp +++ b/test/ParserTests/Parsing/Impl/DefinesStreamProxyTests.cpp @@ -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 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 lines{