From b944168bdc3e413b0482a5444b8bd645e4dad012 Mon Sep 17 00:00:00 2001 From: Raphael Geissert Date: Sun, 30 Jan 2011 12:47:17 -0600 Subject: [PATCH] Check for cpp conditionals where a define is already guaranteed --- lib/preprocessor.cpp | 15 ++++++ test/testpreprocessor.cpp | 109 +++++++++++++++++++++++++++++++++++++- 2 files changed, 122 insertions(+), 2 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index ddf0b6128..6a29c9e13 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -888,6 +888,21 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const def += ";"; 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) { diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 0cd5fb4ab..76823ad26 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -429,13 +429,118 @@ private: preprocessor.preprocess(istr, actual, "file.c"); // Make sure an error message is written.. - TODO_ASSERT_EQUALS("[test.cpp:3]: this preprocessor condition is always true", - "", errout.str()); + ASSERT_EQUALS("[file.c:3]: (error) ABC is already guaranteed to be defined\n", + errout.str()); // Compare results.. ASSERT_EQUALS("\n\n\n\n\n\n", actual[""]); ASSERT_EQUALS("\nA\n\nB\n\n\n", actual["ABC"]); ASSERT_EQUALS(2, static_cast(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 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(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 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(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 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(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 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(actual.size())); }