Skip to content

Commit f490ebc

Browse files
author
Daniel Marjamäki
committed
Fixed cppcheck-opensource#1872 (Confused -v switch)
1 parent 1938b8a commit f490ebc

5 files changed

Lines changed: 57 additions & 8 deletions

File tree

lib/cppcheck.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,5 +830,7 @@ void CppCheck::getErrorMessages()
830830
Tokenizer tokenizer(&_settings, 0);
831831
tokenizer.getErrorMessages();
832832

833+
Preprocessor::getErrorMessages(std::cout);
834+
833835
std::cout << ErrorLogger::ErrorMessage::getXMLFooter() << std::endl;
834836
}

lib/preprocessor.cpp

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,9 @@ void Preprocessor::preprocessWhitespaces(std::string &processedFile)
602602

603603
void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processedFile, std::list<std::string> &resultConfigurations, const std::string &filename, const std::list<std::string> &includePaths)
604604
{
605+
if (file0.empty())
606+
file0 = filename;
607+
605608
processedFile = read(srcCodeStream, filename, _settings);
606609

607610
// normalize the whitespaces of the file
@@ -1439,10 +1442,10 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath
14391442

14401443
if (headerType == UserHeader && !fileOpened)
14411444
{
1442-
filename = paths.back() + filename;
1443-
fin.open(filename.c_str());
1445+
fin.open((paths.back() + filename).c_str());
14441446
if (fin.is_open())
14451447
{
1448+
filename = paths.back() + filename;
14461449
fileOpened = true;
14471450
}
14481451
}
@@ -1464,7 +1467,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath
14641467
fin.close();
14651468
}
14661469

1467-
if (processedFile.length() > 0)
1470+
if (!processedFile.empty())
14681471
{
14691472
// Replace all tabs with spaces..
14701473
std::replace(processedFile.begin(), processedFile.end(), '\t', ' ');
@@ -1484,10 +1487,39 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath
14841487
}
14851488
else if (!fileOpened)
14861489
{
1487-
if (headerType == UserHeader && _errorLogger && _settings && _settings->_verbose)
1490+
if (headerType == UserHeader && _errorLogger && _settings && _settings->isEnabled("missingInclude"))
14881491
{
1489-
std::string fixedpath = Path::toNativeSeparators(filename);
1490-
_errorLogger->reportOut("Include file: \"" + fixedpath + "\" not found.");
1492+
// Determine line number of include
1493+
unsigned int linenr = 1;
1494+
unsigned int level = 0;
1495+
for (std::string::size_type p = 0; p < pos; ++p)
1496+
{
1497+
if (level == 0 && code[pos-p] == '\n')
1498+
++linenr;
1499+
else if (code.compare(pos-p, 9, "#endfile\n") == 0)
1500+
{
1501+
++level;
1502+
}
1503+
else if (code.compare(pos-p, 6, "#file ") == 0)
1504+
{
1505+
if (level == 0)
1506+
{
1507+
--linenr;
1508+
break;
1509+
}
1510+
--level;
1511+
}
1512+
}
1513+
1514+
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
1515+
ErrorLogger::ErrorMessage::FileLocation loc;
1516+
loc.line = linenr; /** @todo set correct line */
1517+
loc.setfile(Path::toNativeSeparators(filePath));
1518+
locationList.push_back(loc);
1519+
1520+
ErrorLogger::ErrorMessage errmsg(locationList, Severity::style, "Include file: \"" + filename + "\" not found.", "missingInclude");
1521+
errmsg.file0 = file0;
1522+
_errorLogger->reportErr(errmsg);
14911523
}
14921524
}
14931525
}
@@ -2259,3 +2291,12 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
22592291
return ostr.str();
22602292
}
22612293

2294+
void Preprocessor::getErrorMessages(std::ostream &ostr)
2295+
{
2296+
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
2297+
const ErrorLogger::ErrorMessage errmsg(locationList,
2298+
Severity::style,
2299+
"Include file: \"\" not found.",
2300+
"missingInclude");
2301+
ostr << errmsg.toXML() << std::endl;
2302+
}

lib/preprocessor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ class Preprocessor
195195
*/
196196
static bool match_cfg_def(const std::map<std::string, std::string> &cfg, std::string def);
197197

198+
static void getErrorMessages(std::ostream &ostr);
199+
198200
private:
199201
/**
200202
* Search includes from code and append code from the included
@@ -212,6 +214,9 @@ class Preprocessor
212214

213215
Settings *_settings;
214216
ErrorLogger *_errorLogger;
217+
218+
/** filename for cpp/c file - useful when reporting errors */
219+
std::string file0;
215220
};
216221

217222
/// @}

lib/settings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ std::string Settings::addEnabled(const std::string &str)
188188
handled = _checkCodingStyle = true;
189189

190190
std::set<std::string> id;
191-
id.insert("unusedFunctions");
191+
id.insert("missingInclude");
192+
id.insert("unusedFunction");
192193

193194
if (str == "all")
194195
{

test/testpreprocessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ class TestPreprocessor : public TestFixture
11001100
std::map<std::string, std::string> actual;
11011101
Settings settings;
11021102
settings.debug = settings.debugwarnings = true;
1103-
settings._verbose = true;
1103+
settings.addEnabled("missingInclude");;
11041104
Preprocessor preprocessor(&settings, this);
11051105
preprocessor.preprocess(istr, actual, "file.c");
11061106

0 commit comments

Comments
 (0)