From cc7e05a5b0ff92021f49331e6e50ed98e12c0883 Mon Sep 17 00:00:00 2001 From: Greg Hewgill Date: Fri, 4 Mar 2011 19:26:48 +1300 Subject: [PATCH] fix case where fall through comment precedes preprocessor line --- lib/preprocessor.cpp | 33 +++++++++++++++++++++++---------- test/testother.cpp | 14 ++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 2e8cd86c5..d93ddd6ab 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -326,6 +326,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri unsigned int newlines = 0; std::ostringstream code; unsigned char previous = 0; + bool inPreprocessorLine = false; std::vector suppressionIDs; for (std::string::size_type i = hasbom(str) ? 3U : 0U; i < str.length(); ++i) @@ -370,6 +371,8 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri // if there has been sequences, add extra newlines.. if (ch == '\n') { + if (previous != '\\') + inPreprocessorLine = false; ++lineno; if (newlines > 0) { @@ -449,25 +452,35 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri } } } + else if (ch == '#' && previous == '\n') + { + code << ch; + previous = ch; + inPreprocessorLine = true; + } else { - // Not whitespace and not a comment. Must be code here! - // Add any pending inline suppressions that have accumulated. - if (!suppressionIDs.empty()) + if (!inPreprocessorLine) { - if (settings != NULL) + // Not whitespace, not a comment, and not preprocessor. + // Must be code here! + // Add any pending inline suppressions that have accumulated. + if (!suppressionIDs.empty()) { - // Add the suppressions. - for (size_t j(0); j < suppressionIDs.size(); ++j) + if (settings != NULL) { - const std::string errmsg(settings->nomsg.addSuppression(suppressionIDs[j], filename, lineno)); - if (!errmsg.empty()) + // Add the suppressions. + for (size_t j(0); j < suppressionIDs.size(); ++j) { - writeError(filename, lineno, _errorLogger, "cppcheckError", errmsg); + const std::string errmsg(settings->nomsg.addSuppression(suppressionIDs[j], filename, lineno)); + if (!errmsg.empty()) + { + writeError(filename, lineno, _errorLogger, "cppcheckError", errmsg); + } } } + suppressionIDs.clear(); } - suppressionIDs.clear(); } // String or char constants.. diff --git a/test/testother.cpp b/test/testother.cpp index a95d8f391..16aa5afc1 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1414,6 +1414,20 @@ private: // (no pun intended). TODO_ASSERT_EQUALS("", "[test.cpp:11]: (warning) Switch falls through case without comment\n", errout.str()); + + check_preprocess_suppress( + "void foo() {\n" + " switch (a) {\n" + " case 1:\n" + "#ifndef A\n" + " g();\n" + " // fall through\n" + "#endif\n" + " case 2:\n" + " break;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void selfAssignment()