diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index b126df5d6..2f571e16b 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -492,9 +492,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri if (_settings && _settings->terminated()) return ""; - if ((str.compare(i, 7, "#error ") == 0 && (!_settings || _settings->userDefines.empty())) || - str.compare(i, 9, "#warning ") == 0) { - + if (str.compare(i, 7, "#error ") == 0 || str.compare(i, 9, "#warning ") == 0) { if (str.compare(i, 6, "#error") == 0) code << "#error"; @@ -1931,10 +1929,8 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string // #error => return "" if (match && line.compare(0, 6, "#error") == 0) { - if (_settings && !_settings->userDefines.empty()) { - Settings settings2(*_settings); - Preprocessor preprocessor(&settings2, _errorLogger); - preprocessor.error(filenames.top(), lineno, line); + if (_settings && !_settings->userDefines.empty() && !_settings->_force) { + error(filenames.top(), lineno, line); } return ""; } @@ -2213,10 +2209,6 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str defs.erase(line.substr(7)); } - else if (!suppressCurrentCodePath && line.compare(0,7,"#error ") == 0) { - error(filePath, linenr, line.substr(7)); - } - else if (!suppressCurrentCodePath && line.compare(0,9,"#include ")==0) { std::string filename(line.substr(9)); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index d98e7f90b..a1d72f51e 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -97,14 +97,12 @@ private: TEST_CASE(test9); // Don't crash for invalid code TEST_CASE(test10); // Ticket #5139 - // #error => don't extract any code - TEST_CASE(error1); - - // #error with extended chars - TEST_CASE(error2); + TEST_CASE(error1); // #error => don't extract any code + TEST_CASE(error2); // #error with extended chars TEST_CASE(error3); TEST_CASE(error4); // #2919 - wrong filename is reported + TEST_CASE(error5); TEST_CASE(if0_exclude); TEST_CASE(if0_whitespace); @@ -892,6 +890,17 @@ private: } } + void error5() { + errout.str(""); + Settings settings; + settings.userDefines = "FOO"; + settings._force = true; // No message if --force is given + Preprocessor preprocessor(&settings, this); + const std::string code("#error hello world!\n"); + preprocessor.getcode(code, "X", "test.c"); + ASSERT_EQUALS("", errout.str()); + } + void if0_exclude() { Settings settings; Preprocessor preprocessor(&settings, this); @@ -3512,7 +3521,6 @@ private: const std::string code("#ifndef X\n#error abc\n#endif"); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list())); ASSERT_EQUALS("\n#error abc\n\n", actual); - ASSERT_EQUALS("[test.c:2]: (error) abc\n", errout.str()); } }