Preprocessor: Minor fixes (#772)

This commit is contained in:
Daniel Marjamäki 2009-10-04 15:41:50 +02:00
parent 6b1fae75e5
commit ab18f1bd3c
2 changed files with 55 additions and 14 deletions

View File

@ -452,16 +452,35 @@ std::string Preprocessor::removeParantheses(const std::string &str)
std::string line;
while (std::getline(istr, line))
{
if (line.substr(0, 3) == "#if")
if (line.substr(0, 3) == "#if" || line.substr(0, 5) == "#elif")
{
while (line.find(" (") != std::string::npos)
line.erase(line.find(" ("), 1);
while (line.find("( ") != std::string::npos)
line.erase(line.find("( ") + 1, 1);
while (line.find(" )") != std::string::npos)
line.erase(line.find(" )"), 1);
while (line.find(") ") != std::string::npos)
line.erase(line.find(") ") + 1, 1);
std::string::size_type pos;
pos = 0;
while ((pos = line.find(" (", pos)) != std::string::npos)
line.erase(pos, 1);
pos = 0;
while ((pos = line.find("( ", pos)) != std::string::npos)
line.erase(pos + 1, 1);
pos = 0;
while ((pos = line.find(" )", pos)) != std::string::npos)
line.erase(pos, 1);
pos = 0;
while ((pos = line.find(") ", pos)) != std::string::npos)
line.erase(pos + 1, 1);
// Remove inner paranthesis "((..))"..
pos = 0;
while ((pos = line.find("((", pos)) != std::string::npos)
{
++pos;
std::string::size_type pos2 = line.find_first_of("()", pos + 1);
if (pos2 != std::string::npos && line[pos2] == ')')
{
line.erase(pos2, 1);
line.erase(pos, 1);
}
}
if (line.substr(0, 4) == "#if(" && line.find(")") == line.length() - 1)
{
line[3] = ' ';
@ -595,6 +614,20 @@ std::string Preprocessor::replaceIfDefined(const std::string &str)
++pos;
}
pos = 0;
while ((pos = ret.find("#elif 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 + 6, 8);
}
++pos;
}
return ret;
}

View File

@ -344,13 +344,15 @@ private:
{
const char filedata[] = "#if(A)\n"
"#if ( A ) \n"
"#if A\n";
"#if A\n"
"#if defined((A))\n"
"#elif defined (A)\n";
std::istringstream istr(filedata);
const std::string actual(Preprocessor::read(istr));
// Compare results..
ASSERT_EQUALS("#if A\n#if A\n#if A\n", actual);
ASSERT_EQUALS("#if A\n#if A\n#if A\n#if defined(A)\n#elif defined(A)\n", actual);
}
void test7()
@ -778,9 +780,15 @@ private:
void if_defined()
{
const char filedata[] = "#if defined(AAA)\n"
"#endif\n";
ASSERT_EQUALS("#ifdef AAA\n#endif\n", OurPreprocessor::replaceIfDefined(filedata));
{
const char filedata[] = "#if defined(AAA)\n"
"#endif\n";
ASSERT_EQUALS("#ifdef AAA\n#endif\n", OurPreprocessor::replaceIfDefined(filedata));
}
{
ASSERT_EQUALS("#elif A\n", OurPreprocessor::replaceIfDefined("#elif defined(A)\n"));
}
}
void if_not_defined()