Check for cpp conditionals where a define is already guaranteed

This commit is contained in:
Raphael Geissert 2011-01-30 12:47:17 -06:00
parent 098f0bf3e6
commit b944168bdc
2 changed files with 122 additions and 2 deletions

View File

@ -888,6 +888,21 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
def += ";"; def += ";";
def += *it; def += *it;
} }
else
{
std::ostringstream lineStream;
lineStream << __LINE__;
ErrorLogger::ErrorMessage errmsg;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile(filename);
loc.line = linenr;
errmsg._callStack.push_back(loc);
errmsg._severity = Severity::fromString("error");
errmsg.setmsg(*it+" is already guaranteed to be defined");
errmsg._id = "preprocessor" + lineStream.str();
_errorLogger->reportErr(errmsg);
}
} }
if (from_negation) if (from_negation)
{ {

View File

@ -429,13 +429,118 @@ private:
preprocessor.preprocess(istr, actual, "file.c"); preprocessor.preprocess(istr, actual, "file.c");
// Make sure an error message is written.. // Make sure an error message is written..
TODO_ASSERT_EQUALS("[test.cpp:3]: this preprocessor condition is always true", ASSERT_EQUALS("[file.c:3]: (error) ABC is already guaranteed to be defined\n",
"", errout.str()); errout.str());
// Compare results.. // Compare results..
ASSERT_EQUALS("\n\n\n\n\n\n", actual[""]); ASSERT_EQUALS("\n\n\n\n\n\n", actual[""]);
ASSERT_EQUALS("\nA\n\nB\n\n\n", actual["ABC"]); ASSERT_EQUALS("\nA\n\nB\n\n\n", actual["ABC"]);
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size())); ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
test7a();
test7b();
test7c();
test7d();
}
void test7a()
{
const char filedata[] = "#ifndef ABC\n"
"A\n"
"#ifndef ABC\n"
"B\n"
"#endif\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
errout.str("");
preprocessor.preprocess(istr, actual, "file.c");
// Make sure an error message is written..
TODO_ASSERT_EQUALS("[file.c:3]: (error) ABC is already guaranteed NOT to be defined\n",
"", errout.str());
// Compare results..
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void test7b()
{
const char filedata[] = "#ifndef ABC\n"
"A\n"
"#ifdef ABC\n"
"B\n"
"#endif\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
errout.str("");
preprocessor.preprocess(istr, actual, "file.c");
// Make sure an error message is written..
TODO_ASSERT_EQUALS("[file.c:3]: (error) ABC is already guaranteed NOT to be defined\n",
"", errout.str());
// Compare results..
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void test7c()
{
const char filedata[] = "#ifdef ABC\n"
"A\n"
"#ifndef ABC\n"
"B\n"
"#endif\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
errout.str("");
preprocessor.preprocess(istr, actual, "file.c");
// Make sure an error message is written..
ASSERT_EQUALS("[file.c:3]: (error) ABC is already guaranteed to be defined\n",
errout.str());
// Compare results..
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void test7d()
{
const char filedata[] = "#if defined(ABC)\n"
"A\n"
"#if defined(ABC)\n"
"B\n"
"#endif\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
errout.str("");
preprocessor.preprocess(istr, actual, "file.c");
// Make sure an error message is written..
ASSERT_EQUALS("[file.c:3]: (error) ABC is already guaranteed to be defined\n",
errout.str());
// Compare results..
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
} }