preprocessor: handled problem with parsing strings when expanding macros

This commit is contained in:
Daniel Marjamäki 2009-01-21 17:11:24 +00:00
parent 820df7fdbd
commit 76cb2310af
2 changed files with 31 additions and 1 deletions

View File

@ -729,6 +729,26 @@ std::string Preprocessor::expandMacros(std::string code)
break;
}
}
else if (code[pos2] == '\"' || code[pos2] == '\'')
{
par += code[pos2];
char ch = code[pos2];
++pos2;
while (pos2 < code.length() && code[pos2] != ch)
{
par += code[pos2];
if (code[pos2] == '\\')
{
par += code[pos2];
++pos2;
}
++pos2;
}
if (pos2 == code.length())
break;
par += code[pos2];
continue;
}
if (parlevel == 1 && code[pos2] == ',')
{

View File

@ -74,6 +74,7 @@ private:
TEST_CASE(macro_simple4);
TEST_CASE(macro_simple5);
TEST_CASE(macro_simple6);
TEST_CASE(macro_simple7);
TEST_CASE(macro_mismatch);
TEST_CASE(string1);
TEST_CASE(string2);
@ -479,6 +480,13 @@ private:
ASSERT_EQUALS("\n(a+b+c)", Preprocessor::expandMacros(filedata));
}
void macro_simple7()
{
const char filedata[] = "#define ABC(str) str\n"
"ABC(\"(\")";
ASSERT_EQUALS("\n\"(\"", Preprocessor::expandMacros(filedata));
}
void macro_mismatch()
{
const char filedata[] = "#define AAA(aa,bb) f(aa)\n"
@ -564,11 +572,13 @@ private:
void fmt()
{
const char filedata[] = "#define DBG(fmt...) printf(fmt);\n"
const char filedata[] = "#define DBG(fmt...) printf(fmt)\n"
"DBG(\"[0x%lx-0x%lx)\", pstart, pend);";
// Preprocess..
std::string actual = Preprocessor::expandMacros(filedata);
ASSERT_EQUALS("\nprintf(\"[0x%lx-0x%lx)\", pstart, pend);", actual);
}
};