Preprocessor: Replace "#if !defined(A)" with "#ifndef A"
This commit is contained in:
parent
dc75761856
commit
a0ba52ccf1
|
@ -467,7 +467,9 @@ std::string Preprocessor::removeSpaceNearNL(const std::string &str)
|
||||||
std::string Preprocessor::replaceIfDefined(const std::string &str)
|
std::string Preprocessor::replaceIfDefined(const std::string &str)
|
||||||
{
|
{
|
||||||
std::string ret(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)
|
while ((pos = ret.find("#if defined(", pos)) != std::string::npos)
|
||||||
{
|
{
|
||||||
std::string::size_type pos2 = ret.find(")", pos + 9);
|
std::string::size_type pos2 = ret.find(")", pos + 9);
|
||||||
|
@ -482,6 +484,21 @@ std::string Preprocessor::replaceIfDefined(const std::string &str)
|
||||||
++pos;
|
++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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -93,6 +93,7 @@ private:
|
||||||
TEST_CASE(multiline4);
|
TEST_CASE(multiline4);
|
||||||
|
|
||||||
TEST_CASE(if_defined); // "#if defined(AAA)" => "#ifdef AAA"
|
TEST_CASE(if_defined); // "#if defined(AAA)" => "#ifdef AAA"
|
||||||
|
TEST_CASE(if_not_defined); // "#if !defined(AAA)" => "#ifndef AAA"
|
||||||
|
|
||||||
// Macros..
|
// Macros..
|
||||||
TEST_CASE(macro_simple1);
|
TEST_CASE(macro_simple1);
|
||||||
|
@ -591,13 +592,14 @@ private:
|
||||||
{
|
{
|
||||||
const char filedata[] = "#if defined(AAA)\n"
|
const char filedata[] = "#if defined(AAA)\n"
|
||||||
"#endif\n";
|
"#endif\n";
|
||||||
|
ASSERT_EQUALS("#ifdef AAA\n#endif\n", OurPreprocessor::replaceIfDefined(filedata));
|
||||||
|
}
|
||||||
|
|
||||||
|
void if_not_defined()
|
||||||
// Expected result..
|
{
|
||||||
std::string expected("#ifdef AAA\n#endif\n");
|
const char filedata[] = "#if !defined(AAA)\n"
|
||||||
|
"#endif\n";
|
||||||
// Compare result..
|
ASSERT_EQUALS("#ifndef AAA\n#endif\n", OurPreprocessor::replaceIfDefined(filedata));
|
||||||
ASSERT_EQUALS(expected, OurPreprocessor::replaceIfDefined(filedata));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue