Skip to content

Commit

Permalink
Do not expand macros in strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Laupetin committed Dec 30, 2023
1 parent 2af58e5 commit d3519c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
38 changes: 29 additions & 9 deletions src/Parser/Parsing/Impl/DefinesStreamProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,29 +559,49 @@ bool DefinesStreamProxy::FindNextMacro(const std::string& input, unsigned& input
auto wordStart = 0u;
auto lastWordEnd = 0u;
auto inWord = false;
auto inString = false;
auto stringEscape = false;

for (; inputPos < inputSize; inputPos++)
{
const auto c = input[inputPos];
if (!inWord)
if (inString)
{
if (isalpha(c) || c == '_')
if (!stringEscape)
{
wordStart = inputPos;
inWord = true;
if (c == '"')
inString = false;
else if (c == '\\')
stringEscape = true;
}
else
stringEscape = false;
}
else
{
if (!isalnum(c) && c != '_')
if (c == '"')
inString = true;

if (!inWord)
{
if (FindMacroForIdentifier(input, wordStart, inputPos, define))
if (isalpha(c) || c == '_')
{
defineStart = wordStart;
return true;
wordStart = inputPos;
inWord = true;
}
}
else
{
if (!isalnum(c) && c != '_')
{
if (FindMacroForIdentifier(input, wordStart, inputPos, define))
{
defineStart = wordStart;
return true;
}

inWord = false;
inWord = false;
}
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion test/ParserTests/Parsing/Impl/DefinesStreamProxyTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,22 @@ namespace test::parsing::impl::defines_stream_proxy
REQUIRE(proxy.Eof());
}

TEST_CASE("DefinesStreamProxy: Ensure does not expand macros in strings", "[parsing][parsingstream]")
{
const std::vector<std::string> lines{
"#define TEST Wrong",
"System.out.println(\"TEST\")",
};

MockParserLineStream mockStream(lines);
DefinesStreamProxy proxy(&mockStream);

ExpectLine(&proxy, 1, "");
ExpectLine(&proxy, 2, "System.out.println(\"TEST\")");

REQUIRE(proxy.Eof());
}

TEST_CASE("DefinesStreamProxy: Ensure simple if is working with truthy value", "[parsing][parsingstream]")
{
const std::vector<std::string> lines{
Expand Down Expand Up @@ -911,7 +927,7 @@ namespace test::parsing::impl::defines_stream_proxy
REQUIRE(proxy.Eof());
}

TEST_CASE("DefinesStreamProxy: Can use strinizing operator inside sample code", "[parsing][parsingstream]")
TEST_CASE("DefinesStreamProxy: Can use stringizing operator inside sample code", "[parsing][parsingstream]")
{
const std::vector<std::string> lines{
"#define testMacro(a) System.out.println(#a)",
Expand Down

0 comments on commit d3519c2

Please sign in to comment.