From c8394909c007addadf7605f975f8789307736b33 Mon Sep 17 00:00:00 2001 From: Greg Hewgill Date: Sun, 6 Mar 2011 12:14:10 +1300 Subject: [PATCH] Relax detection of 'fall through' comment so it only adds a suppression if it immediately precedes 'case' or 'default' --- lib/preprocessor.cpp | 17 +++++++++++++++-- test/testother.cpp | 6 ++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index f98578a8c..25a43d806 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -331,6 +331,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri unsigned char previous = 0; bool inPreprocessorLine = false; std::vector suppressionIDs; + bool fallThroughComment = false; for (std::string::size_type i = hasbom(str) ? 3U : 0U; i < str.length(); ++i) { @@ -412,7 +413,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri if (_settings->_checkCodingStyle && isFallThroughComment(comment)) { - suppressionIDs.push_back("switchCaseFallThrough"); + fallThroughComment = true; } code << "\n"; @@ -439,7 +440,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri if (_settings->_checkCodingStyle && isFallThroughComment(comment)) { - suppressionIDs.push_back("switchCaseFallThrough"); + fallThroughComment = true; } if (settings && settings->_inlineSuppressions) @@ -467,6 +468,18 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri { // Not whitespace, not a comment, and not preprocessor. // Must be code here! + + // First check for a "fall through" comment match, but only + // add a suppression if the next token is 'case' or 'default' + if (fallThroughComment) + { + std::string::size_type j = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz", i); + std::string tok = str.substr(i, j - i); + if (tok == "case" || tok == "default") + suppressionIDs.push_back("switchCaseFallThrough"); + fallThroughComment = false; + } + // Add any pending inline suppressions that have accumulated. if (!suppressionIDs.empty()) { diff --git a/test/testother.cpp b/test/testother.cpp index e5dd1481b..00759ffac 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1482,6 +1482,12 @@ private: " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check_preprocess_suppress( + "void foo() {\n" + " // unrelated comment saying 'fall through'\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void selfAssignment()