preprocessor: handle redefinition of macro

This commit is contained in:
Daniel Marjamäki 2009-01-22 20:19:07 +00:00
parent e897637c7a
commit 67e4ea10c9
2 changed files with 25 additions and 3 deletions

View File

@ -678,11 +678,17 @@ std::string Preprocessor::expandMacros(std::string code)
// #undef => break
if (code[pos1] == '#')
{
const std::string substr(code.substr(pos1, 7 + macro.name().length()));
std::string substr;
substr = code.substr(pos1, 7 + macro.name().length());
if (substr == "#undef " + macro.name())
break;
else
continue;
substr = code.substr(pos1, 8 + macro.name().length());
if (substr == "#define " + macro.name())
break;
continue;
}
// String or char..

View File

@ -83,10 +83,12 @@ private:
TEST_CASE(string1);
TEST_CASE(string2);
TEST_CASE(preprocessor_undef);
TEST_CASE(defdef); // Defined multiple times
TEST_CASE(preprocessor_doublesharp);
TEST_CASE(preprocessor_include_in_str);
// TODO TEST_CASE(fmt);
TEST_CASE(multi_character_character);
}
@ -559,6 +561,17 @@ private:
ASSERT_EQUALS("\n\n\nchar b=0;\n", Preprocessor::expandMacros(filedata));
}
void defdef()
{
const char filedata[] = "#define AAA 123\n"
"#define AAA 456\n"
"#define AAA 789\n"
"AAA\n";
// Compare results..
ASSERT_EQUALS("\n\n\n789\n", Preprocessor::expandMacros(filedata));
}
void preprocessor_doublesharp()
{
const char filedata[] = "#define TEST(var,val) var = val\n"
@ -596,6 +609,8 @@ private:
}
void fmt()
{
const char filedata[] = "#define DBG(fmt...) printf(fmt)\n"
@ -627,6 +642,7 @@ private:
ASSERT_EQUALS("\nint main()\n{\nif( 'ABCD' == 0 );\nreturn 0;\n}\n", actual[""]);
}
};
REGISTER_TEST(TestPreprocessor)