Fixed #2042 (#error messages should be displayed when user defines are used)

This commit is contained in:
Robert Reif 2010-09-12 21:30:47 +02:00 committed by Daniel Marjamäki
parent 5a95303405
commit aae2986361
4 changed files with 38 additions and 7 deletions

View File

@ -160,7 +160,7 @@ unsigned int CppCheck::check()
cfg = *it;
Timer t("Preprocessor::getcode", _settings._showtime, &S_timerResults);
const std::string codeWithoutCfg = Preprocessor::getcode(filedata, *it, fname, &_errorLogger);
const std::string codeWithoutCfg = Preprocessor::getcode(filedata, *it, fname, &_settings, &_errorLogger);
t.Stop();
// If only errors are printed, print filename after the check
@ -213,7 +213,7 @@ void CppCheck::analyseFile(std::istream &fin, const std::string &filename)
std::list<std::string> configurations;
std::string filedata = "";
preprocessor.preprocess(fin, filedata, configurations, filename, _settings._includePaths);
const std::string code = Preprocessor::getcode(filedata, "", filename, &_errorLogger);
const std::string code = Preprocessor::getcode(filedata, "", filename, &_settings, &_errorLogger);
// Tokenize..
Tokenizer tokenizer(&_settings, this);

View File

@ -508,7 +508,7 @@ void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::str
std::string data;
preprocess(istr, data, configs, filename, includePaths);
for (std::list<std::string>::const_iterator it = configs.begin(); it != configs.end(); ++it)
result[ *it ] = Preprocessor::getcode(data, *it, filename, _errorLogger);
result[ *it ] = Preprocessor::getcode(data, *it, filename, _settings, _errorLogger);
}
std::string Preprocessor::removeSpaceNearNL(const std::string &str)
@ -1165,8 +1165,11 @@ bool Preprocessor::match_cfg_def(const std::map<std::string, std::string> &cfg,
}
std::string Preprocessor::getcode(const std::string &filedata, std::string cfg, const std::string &filename, ErrorLogger *errorLogger)
std::string Preprocessor::getcode(const std::string &filedata, std::string cfg, const std::string &filename, const Settings *settings, ErrorLogger *errorLogger)
{
// For the error report
unsigned int lineno = 0;
std::ostringstream ret;
bool match = true;
@ -1211,6 +1214,8 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg,
std::string line;
while (getline(istr, line))
{
++lineno;
if (line.compare(0, 11, "#pragma asm") == 0)
{
ret << "\n";
@ -1310,7 +1315,15 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg,
// #error => return ""
if (match && line.compare(0, 6, "#error") == 0)
{
if (settings && !settings->userDefines.empty())
{
std::ostringstream errmsg;
errmsg << line;
writeError(filename, lineno, errorLogger, "preprocessorErrorDirective", errmsg.str());
}
return "";
}
if (!match && line.compare(0, 8, "#define ") == 0)
{
@ -2299,4 +2312,10 @@ void Preprocessor::getErrorMessages(std::ostream &ostr)
"Include file: \"\" not found.",
"missingInclude");
ostr << errmsg.toXML() << std::endl;
const ErrorLogger::ErrorMessage errmsg2(locationList,
Severity::error,
"#error ...",
"preprocessorErrorDirective");
ostr << errmsg2.toXML() << std::endl;
}

View File

@ -91,7 +91,7 @@ public:
/**
* Get preprocessed code for a given configuration
*/
static std::string getcode(const std::string &filedata, std::string cfg, const std::string &filename, ErrorLogger *errorLogger);
static std::string getcode(const std::string &filedata, std::string cfg, const std::string &filename, const Settings *settings, ErrorLogger *errorLogger);
/**
* simplify condition

View File

@ -84,6 +84,8 @@ private:
// #error with extended chars
TEST_CASE(error2);
TEST_CASE(error3);
// Handling include guards (don't create extra configuration for it)
TEST_CASE(includeguard1);
TEST_CASE(includeguard2);
@ -450,6 +452,16 @@ private:
}
void error3()
{
Settings settings;
settings.userDefines = "__cplusplus";
const std::string code("#error hello world!\n");
Preprocessor::getcode(code, "X", "test.c", &settings, this);
ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str());
}
void includeguard1()
{
// Handling include guards..
@ -1120,7 +1132,7 @@ private:
const std::string code("#if X || Y\n"
"a1;\n"
"#endif\n");
const std::string actual = Preprocessor::getcode(code, "X", "test.c", NULL);
const std::string actual = Preprocessor::getcode(code, "X", "test.c", NULL, NULL);
ASSERT_EQUALS("\na1;\n\n", actual);
}
@ -1575,7 +1587,7 @@ private:
"int z;\n"
"z = 0;\n";
ASSERT_EQUALS("\n\nint z;\nz = 0;\n", OurPreprocessor::getcode(filedata, "", "", NULL));
ASSERT_EQUALS("\n\nint z;\nz = 0;\n", OurPreprocessor::getcode(filedata, "", "", NULL, NULL));
}
}