preprocessor: stringify macros

This commit is contained in:
Daniel Marjamäki 2009-01-25 13:30:15 +00:00
parent ed2758b082
commit 9c51729be6
2 changed files with 24 additions and 2 deletions

View File

@ -637,13 +637,22 @@ public:
std::string str = tok->str(); std::string str = tok->str();
if (str == "##") if (str == "##")
continue; continue;
if (tok->isName()) if (str[0] == '#' || tok->isName())
{ {
bool stringify = false;
if (str[0] == '#')
{
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)
{ {
if (str == _params[i]) if (str == _params[i])
{ {
str = params2[i]; if (stringify)
str = "\"" + params2[i] + "\"";
else
str = params2[i];
break; break;
} }
} }

View File

@ -91,6 +91,8 @@ private:
// TODO TEST_CASE(fmt); // TODO TEST_CASE(fmt);
TEST_CASE(multi_character_character); TEST_CASE(multi_character_character);
// TODO TEST_CASE(preprocessor_and_operation); // TODO TEST_CASE(preprocessor_and_operation);
TEST_CASE(stringify);
} }
@ -682,6 +684,17 @@ private:
} }
void stringify()
{
const char filedata[] = "#define STRINGIFY(x) #x\n"
"STRINGIFY(abc)";
// expand macros..
std::string actual = Preprocessor::expandMacros(filedata);
ASSERT_EQUALS("\n\"abc\"", actual);
}
}; };
REGISTER_TEST(TestPreprocessor) REGISTER_TEST(TestPreprocessor)