diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 3707b7e5e..949b0691d 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -468,6 +468,24 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri code << ch; previous = ch; inPreprocessorLine = true; + + // Add any pending inline suppressions that have accumulated. + if (!suppressionIDs.empty()) + { + if (settings != NULL) + { + // Add the suppressions. + for (size_t j(0); j < suppressionIDs.size(); ++j) + { + const std::string errmsg(settings->nomsg.addSuppression(suppressionIDs[j], filename, lineno)); + if (!errmsg.empty()) + { + writeError(filename, lineno, _errorLogger, "cppcheckError", errmsg); + } + } + } + suppressionIDs.clear(); + } } else { @@ -1917,7 +1935,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath std::string f = filePath; // Determine line number of include - unsigned int linenr = 0; + unsigned int linenr = 1; unsigned int level = 0; for (std::string::size_type p = 1; p <= pos; ++p) { @@ -1931,6 +1949,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath { if (level == 0) { + linenr--; const std::string::size_type pos1 = pos - p + 7; const std::string::size_type pos2 = code.find_first_of("\"\n", pos1); f = code.substr(pos1, (pos2 == std::string::npos) ? pos2 : (pos2 - pos1)); @@ -1940,10 +1959,13 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath } } - missingInclude(Path::toNativeSeparators(f), - linenr, - filename, - headerType == UserHeader); + if (!_settings->nomsg.isSuppressed("missingInclude", f, linenr)) + { + missingInclude(Path::toNativeSeparators(f), + linenr, + filename, + headerType == UserHeader); + } } } } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index aa341688f..ee880180e 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -218,6 +218,9 @@ private: TEST_CASE(testPreprocessorRead4); TEST_CASE(invalid_define); // #2605 - hang for: '#define =' + + // inline suppression, missingInclude + TEST_CASE(inline_suppression_for_missing_include); } @@ -2822,6 +2825,26 @@ private: std::list paths; preprocessor.preprocess(src, processedFile, cfg, "", paths); // don't hang } + + void inline_suppression_for_missing_include() + { + Settings settings; + settings._inlineSuppressions = true; + settings.addEnabled("all"); + Preprocessor preprocessor(&settings, this); + + std::istringstream src("// cppcheck-suppress missingInclude\n" + "#include \"missing.h\"\n" + "int x;"); + std::string processedFile; + std::list cfg; + std::list paths; + + // Don't report that the include is missing + errout.str(""); + preprocessor.preprocess(src, processedFile, cfg, "test.c", paths); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestPreprocessor)