preprocessor: fixed bug with mixed macros ABC and ABCD

This commit is contained in:
Daniel Marjamäki 2009-01-23 17:14:42 +00:00
parent 9af881d548
commit 193cffdb0b
2 changed files with 22 additions and 6 deletions

View File

@ -679,15 +679,22 @@ std::string Preprocessor::expandMacros(std::string code)
// #undef => break
if (code[pos1] == '#')
{
std::string substr;
// Are we at a #undef or #define?
if (code.substr(pos1, 7) == "#undef ")
pos1 += 7;
else if (code.substr(pos1, 8) == "#define ")
pos1 += 8;
else
continue;
substr = code.substr(pos1, 7 + macro.name().length());
if (substr == "#undef " + macro.name())
// Compare the macroname with the macroname we're currently parsing (macro.name())
// If it's the same macroname.. break.
std::string::size_type pos = pos1 + macro.name().length();
if (pos < code.length()
&& code.substr(pos1, macro.name().length()) == macro.name()
&& !isalnum(code[pos]) && code[pos] != '_')
break;
substr = code.substr(pos1, 8 + macro.name().length());
if (substr == "#define " + macro.name())
break;
continue;
}

View File

@ -79,6 +79,7 @@ private:
TEST_CASE(macro_simple5);
TEST_CASE(macro_simple6);
TEST_CASE(macro_simple7);
TEST_CASE(macro_simple8);
TEST_CASE(macro_mismatch);
TEST_CASE(string1);
TEST_CASE(string2);
@ -515,6 +516,14 @@ private:
ASSERT_EQUALS("\n\"(\"", Preprocessor::expandMacros(filedata));
}
void macro_simple8()
{
const char filedata[] = "#define ABC 123\n"
"#define ABCD 1234\n"
"ABC ABCD";
ASSERT_EQUALS("\n\n123 1234", Preprocessor::expandMacros(filedata));
}
void macro_mismatch()
{
const char filedata[] = "#define AAA(aa,bb) f(aa)\n"