Fixed #821 (Preprocessor: Wrong stringification in macros)

This commit is contained in:
Daniel Marjamäki 2009-10-14 20:40:17 +02:00
parent dd20f26c83
commit 91de8f399b
2 changed files with 22 additions and 4 deletions

View File

@ -1529,11 +1529,10 @@ public:
continue;
if (str[0] == '#' || tok->isName())
{
bool stringify = false;
if (str[0] == '#')
const bool stringify(str[0] == '#');
if (stringify)
{
str = str.erase(0, 1);
stringify = true;
}
for (unsigned int i = 0; i < _params.size(); ++i)
{
@ -1557,7 +1556,18 @@ public:
return false;
}
else if (stringify)
str = "\"" + params2[i] + "\"";
{
const std::string &s(params2[i]);
std::ostringstream ostr;
ostr << "\"";
for (std::string::size_type i = 0; i < s.size(); ++i)
{
if (s[i] == '\\' || s[i] == '\"')
ostr << '\\';
ostr << s[i];
}
str = ostr.str() + "\"";
}
else
str = params2[i];

View File

@ -136,6 +136,7 @@ private:
TEST_CASE(stringify2);
TEST_CASE(stringify3);
TEST_CASE(stringify4);
TEST_CASE(stringify5);
TEST_CASE(ifdefwithfile);
TEST_CASE(pragma);
TEST_CASE(pragma_asm);
@ -1211,6 +1212,13 @@ private:
ASSERT_EQUALS("\n1 \n\n\"abc\" 2", actual);
}
void stringify5()
{
const char filedata[] = "#define A(x) a(#x,x)\n"
"A(foo(\"\\\"\"))\n";
ASSERT_EQUALS("\na(\"foo(\\\"\\\\\\\"\\\")\",foo(\"\\\"\"))\n", OurPreprocessor::expandMacros(filedata));
}
void pragma()
{
const char filedata[] = "#pragma once\n"