Fixed #error handling:

- Reporting them once is enough
- Don't report them if --force is used - since we silently drop these configurations when we check multiple configurations. Without the fix, -f combined with -D resulted in #error being shown erroneously.
- No redundant preprocessor instance to report them
This commit is contained in:
PKEuS 2014-06-18 17:26:51 +02:00
parent dcc646735d
commit 188f9b4509
2 changed files with 17 additions and 17 deletions

View File

@ -492,9 +492,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
if (_settings && _settings->terminated()) if (_settings && _settings->terminated())
return ""; return "";
if ((str.compare(i, 7, "#error ") == 0 && (!_settings || _settings->userDefines.empty())) || if (str.compare(i, 7, "#error ") == 0 || str.compare(i, 9, "#warning ") == 0) {
str.compare(i, 9, "#warning ") == 0) {
if (str.compare(i, 6, "#error") == 0) if (str.compare(i, 6, "#error") == 0)
code << "#error"; code << "#error";
@ -1931,10 +1929,8 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string
// #error => return "" // #error => return ""
if (match && line.compare(0, 6, "#error") == 0) { if (match && line.compare(0, 6, "#error") == 0) {
if (_settings && !_settings->userDefines.empty()) { if (_settings && !_settings->userDefines.empty() && !_settings->_force) {
Settings settings2(*_settings); error(filenames.top(), lineno, line);
Preprocessor preprocessor(&settings2, _errorLogger);
preprocessor.error(filenames.top(), lineno, line);
} }
return ""; return "";
} }
@ -2213,10 +2209,6 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str
defs.erase(line.substr(7)); 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) { else if (!suppressCurrentCodePath && line.compare(0,9,"#include ")==0) {
std::string filename(line.substr(9)); std::string filename(line.substr(9));

View File

@ -97,14 +97,12 @@ private:
TEST_CASE(test9); // Don't crash for invalid code TEST_CASE(test9); // Don't crash for invalid code
TEST_CASE(test10); // Ticket #5139 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(error3);
TEST_CASE(error4); // #2919 - wrong filename is reported TEST_CASE(error4); // #2919 - wrong filename is reported
TEST_CASE(error5);
TEST_CASE(if0_exclude); TEST_CASE(if0_exclude);
TEST_CASE(if0_whitespace); 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() { void if0_exclude() {
Settings settings; Settings settings;
Preprocessor preprocessor(&settings, this); Preprocessor preprocessor(&settings, this);
@ -3512,7 +3521,6 @@ private:
const std::string code("#ifndef X\n#error abc\n#endif"); const std::string code("#ifndef X\n#error abc\n#endif");
const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>())); const std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs,pragmaOnce,std::list<std::string>()));
ASSERT_EQUALS("\n#error abc\n\n", actual); ASSERT_EQUALS("\n#error abc\n\n", actual);
ASSERT_EQUALS("[test.c:2]: (error) abc\n", errout.str());
} }
} }