Fixed ticket #405 (#ifdef A and #if defined A should be handled as same configuration)

http://sourceforge.net/apps/trac/cppcheck/ticket/405
This commit is contained in:
Reijo Tomperi 2009-06-14 23:37:18 +03:00
parent 0966a2fbd4
commit 7c4423889c
2 changed files with 27 additions and 2 deletions

View File

@ -513,7 +513,7 @@ void Preprocessor::preprocess(std::istream &istr, std::string &processedFile, st
std::string Preprocessor::getdef(std::string line, bool def)
{
// If def is true, the line must start with "#ifdef"
if (def && line.find("#ifdef ") != 0 && line.find("#if ") != 0 && line.find("#elif ") != 0)
if (def && line.find("#ifdef ") != 0 && line.find("#if ") != 0 && line.find("#elif ") != 0 && line.find("#if defined ") != 0)
{
return "";
}
@ -525,7 +525,10 @@ std::string Preprocessor::getdef(std::string line, bool def)
}
// Remove the "#ifdef" or "#ifndef"
line.erase(0, line.find(" "));
if (line.find("#if defined ") == 0)
line.erase(0, 11);
else
line.erase(0, line.find(" "));
// Remove all spaces.
while (line.find(" ") != std::string::npos)

View File

@ -132,6 +132,7 @@ private:
TEST_CASE(macro_parameters);
TEST_CASE(newline_in_macro);
TEST_CASE(includes);
TEST_CASE(ifdef_ifdefined);
}
@ -1122,6 +1123,27 @@ private:
ASSERT_EQUALS("c.h", src);
}
}
void ifdef_ifdefined()
{
const char filedata[] = "#ifdef ABC\n"
"A\n"
"#endif\t\n"
"#if defined ABC\n"
"A\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\n\n", actual[""]);
ASSERT_EQUALS("\nA\n\n\nA\n\n", actual["ABC"]);
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
};
REGISTER_TEST(TestPreprocessor)