Fix #1534 (False positive with #elif (defined NAME))

http://sourceforge.net/apps/trac/cppcheck/ticket/1534
This commit is contained in:
Reijo Tomperi 2010-03-28 15:15:25 +03:00
parent 2dc4222c9a
commit 417f14fccf
2 changed files with 43 additions and 18 deletions

View File

@ -353,7 +353,8 @@ std::string Preprocessor::removeParantheses(const std::string &str)
}
// "#if(A) => #if A", but avoid "#if (defined A) || defined (B)"
if (line.compare(0, 4, "#if(") == 0 && line[line.length() - 1] == ')')
if ((line.compare(0, 4, "#if(") == 0 || line.compare(0, 6, "#elif(") == 0) &&
line[line.length() - 1] == ')')
{
int ind = 0;
for (std::string::size_type i = 0; i < line.length(); ++i)
@ -367,7 +368,7 @@ std::string Preprocessor::removeParantheses(const std::string &str)
{
if (i == line.length() - 1)
{
line[3] = ' ';
line[line.find('(')] = ' ';
line.erase(line.length() - 1);
}
break;
@ -378,7 +379,7 @@ std::string Preprocessor::removeParantheses(const std::string &str)
if (line.compare(0, 4, "#if(") == 0)
line.insert(3, " ");
else if (line.compare(0, 4, "#elif(") == 0)
else if (line.compare(0, 6, "#elif(") == 0)
line.insert(5, " ");
}
ret << line << "\n";

View File

@ -616,23 +616,47 @@ private:
void elif()
{
const char filedata[] = "#if DEF1\n"
"ABC\n"
"#elif DEF2\n"
"DEF\n"
"#endif\n";
{
const char filedata[] = "#if DEF1\n"
"ABC\n"
"#elif DEF2\n"
"DEF\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Preprocessor preprocessor;
preprocessor.preprocess(istr, actual, "file.c");
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Preprocessor preprocessor;
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS("\n\n\n\n\n", actual[""]);
ASSERT_EQUALS("\nABC\n\n\n\n", actual["DEF1"]);
ASSERT_EQUALS("\n\n\nDEF\n\n", actual["DEF2"]);
ASSERT_EQUALS(3, static_cast<unsigned int>(actual.size()));
// Compare results..
ASSERT_EQUALS("\n\n\n\n\n", actual[""]);
ASSERT_EQUALS("\nABC\n\n\n\n", actual["DEF1"]);
ASSERT_EQUALS("\n\n\nDEF\n\n", actual["DEF2"]);
ASSERT_EQUALS(3, static_cast<unsigned int>(actual.size()));
}
{
const char filedata[] = "#if(defined DEF1)\n"
"ABC\n"
"#elif(defined DEF2)\n"
"DEF\n"
"#else\n"
"GHI\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Preprocessor preprocessor;
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS("\n\n\n\n\nGHI\n\n", actual[""]);
ASSERT_EQUALS("\nABC\n\n\n\n\n\n", actual["DEF1"]);
ASSERT_EQUALS("\n\n\nDEF\n\n\n\n", actual["DEF2"]);
ASSERT_EQUALS(3, static_cast<unsigned int>(actual.size()));
}
}