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; continue;
if (str[0] == '#' || tok->isName()) if (str[0] == '#' || tok->isName())
{ {
bool stringify = false; const bool stringify(str[0] == '#');
if (str[0] == '#') if (stringify)
{ {
str = str.erase(0, 1); str = str.erase(0, 1);
stringify = true;
} }
for (unsigned int i = 0; i < _params.size(); ++i) for (unsigned int i = 0; i < _params.size(); ++i)
{ {
@ -1557,7 +1556,18 @@ public:
return false; return false;
} }
else if (stringify) 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 else
str = params2[i]; str = params2[i];

View File

@ -136,6 +136,7 @@ private:
TEST_CASE(stringify2); TEST_CASE(stringify2);
TEST_CASE(stringify3); TEST_CASE(stringify3);
TEST_CASE(stringify4); TEST_CASE(stringify4);
TEST_CASE(stringify5);
TEST_CASE(ifdefwithfile); TEST_CASE(ifdefwithfile);
TEST_CASE(pragma); TEST_CASE(pragma);
TEST_CASE(pragma_asm); TEST_CASE(pragma_asm);
@ -1211,6 +1212,13 @@ private:
ASSERT_EQUALS("\n1 \n\n\"abc\" 2", actual); 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() void pragma()
{ {
const char filedata[] = "#pragma once\n" const char filedata[] = "#pragma once\n"