Fixed #1872 (Confused -v switch)

This commit is contained in:
Daniel Marjamäki 2010-09-03 13:30:49 +02:00
parent 1938b8a423
commit f490ebcf88
5 changed files with 57 additions and 8 deletions

View File

@ -830,5 +830,7 @@ void CppCheck::getErrorMessages()
Tokenizer tokenizer(&_settings, 0);
tokenizer.getErrorMessages();
Preprocessor::getErrorMessages(std::cout);
std::cout << ErrorLogger::ErrorMessage::getXMLFooter() << std::endl;
}

View File

@ -602,6 +602,9 @@ void Preprocessor::preprocessWhitespaces(std::string &processedFile)
void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processedFile, std::list<std::string> &resultConfigurations, const std::string &filename, const std::list<std::string> &includePaths)
{
if (file0.empty())
file0 = filename;
processedFile = read(srcCodeStream, filename, _settings);
// normalize the whitespaces of the file
@ -1439,10 +1442,10 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath
if (headerType == UserHeader && !fileOpened)
{
filename = paths.back() + filename;
fin.open(filename.c_str());
fin.open((paths.back() + filename).c_str());
if (fin.is_open())
{
filename = paths.back() + filename;
fileOpened = true;
}
}
@ -1464,7 +1467,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath
fin.close();
}
if (processedFile.length() > 0)
if (!processedFile.empty())
{
// Replace all tabs with spaces..
std::replace(processedFile.begin(), processedFile.end(), '\t', ' ');
@ -1484,10 +1487,39 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath
}
else if (!fileOpened)
{
if (headerType == UserHeader && _errorLogger && _settings && _settings->_verbose)
if (headerType == UserHeader && _errorLogger && _settings && _settings->isEnabled("missingInclude"))
{
std::string fixedpath = Path::toNativeSeparators(filename);
_errorLogger->reportOut("Include file: \"" + fixedpath + "\" not found.");
// Determine line number of include
unsigned int linenr = 1;
unsigned int level = 0;
for (std::string::size_type p = 0; p < pos; ++p)
{
if (level == 0 && code[pos-p] == '\n')
++linenr;
else if (code.compare(pos-p, 9, "#endfile\n") == 0)
{
++level;
}
else if (code.compare(pos-p, 6, "#file ") == 0)
{
if (level == 0)
{
--linenr;
break;
}
--level;
}
}
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = linenr; /** @todo set correct line */
loc.setfile(Path::toNativeSeparators(filePath));
locationList.push_back(loc);
ErrorLogger::ErrorMessage errmsg(locationList, Severity::style, "Include file: \"" + filename + "\" not found.", "missingInclude");
errmsg.file0 = file0;
_errorLogger->reportErr(errmsg);
}
}
}
@ -2259,3 +2291,12 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
return ostr.str();
}
void Preprocessor::getErrorMessages(std::ostream &ostr)
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::style,
"Include file: \"\" not found.",
"missingInclude");
ostr << errmsg.toXML() << std::endl;
}

View File

@ -195,6 +195,8 @@ public:
*/
static bool match_cfg_def(const std::map<std::string, std::string> &cfg, std::string def);
static void getErrorMessages(std::ostream &ostr);
private:
/**
* Search includes from code and append code from the included
@ -212,6 +214,9 @@ private:
Settings *_settings;
ErrorLogger *_errorLogger;
/** filename for cpp/c file - useful when reporting errors */
std::string file0;
};
/// @}

View File

@ -188,7 +188,8 @@ std::string Settings::addEnabled(const std::string &str)
handled = _checkCodingStyle = true;
std::set<std::string> id;
id.insert("unusedFunctions");
id.insert("missingInclude");
id.insert("unusedFunction");
if (str == "all")
{

View File

@ -1100,7 +1100,7 @@ private:
std::map<std::string, std::string> actual;
Settings settings;
settings.debug = settings.debugwarnings = true;
settings._verbose = true;
settings.addEnabled("missingInclude");;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");