Preprocessor: Replace "#if !defined(A)" with "#ifndef A"

This commit is contained in:
Daniel Marjamäki 2009-06-21 08:03:42 +02:00
parent dc75761856
commit a0ba52ccf1
2 changed files with 26 additions and 7 deletions

View File

@ -467,7 +467,9 @@ std::string Preprocessor::removeSpaceNearNL(const std::string &str)
std::string Preprocessor::replaceIfDefined(const std::string &str)
{
std::string ret(str);
std::string::size_type pos = 0;
std::string::size_type pos;
pos = 0;
while ((pos = ret.find("#if defined(", pos)) != std::string::npos)
{
std::string::size_type pos2 = ret.find(")", pos + 9);
@ -482,6 +484,21 @@ std::string Preprocessor::replaceIfDefined(const std::string &str)
++pos;
}
pos = 0;
while ((pos = ret.find("#if !defined(", pos)) != std::string::npos)
{
std::string::size_type pos2 = ret.find(")", pos + 9);
if (pos2 > ret.length() - 1)
break;
if (ret[pos2+1] == '\n')
{
ret.erase(pos2, 1);
ret.erase(pos + 3, 10);
ret.insert(pos + 3, "ndef ");
}
++pos;
}
return ret;
}

View File

@ -93,6 +93,7 @@ private:
TEST_CASE(multiline4);
TEST_CASE(if_defined); // "#if defined(AAA)" => "#ifdef AAA"
TEST_CASE(if_not_defined); // "#if !defined(AAA)" => "#ifndef AAA"
// Macros..
TEST_CASE(macro_simple1);
@ -591,13 +592,14 @@ private:
{
const char filedata[] = "#if defined(AAA)\n"
"#endif\n";
ASSERT_EQUALS("#ifdef AAA\n#endif\n", OurPreprocessor::replaceIfDefined(filedata));
}
// Expected result..
std::string expected("#ifdef AAA\n#endif\n");
// Compare result..
ASSERT_EQUALS(expected, OurPreprocessor::replaceIfDefined(filedata));
void if_not_defined()
{
const char filedata[] = "#if !defined(AAA)\n"
"#endif\n";
ASSERT_EQUALS("#ifndef AAA\n#endif\n", OurPreprocessor::replaceIfDefined(filedata));
}