Skip to content

Commit 59d6367

Browse files
amai2012danmar
authored andcommitted
Fixed cppcheck-opensource#4594 (Analyzing errors about system headers not being found)
1 parent ee942c5 commit 59d6367

3 files changed

Lines changed: 36 additions & 37 deletions

File tree

lib/preprocessor.cpp

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -814,14 +814,11 @@ void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processe
814814

815815
fin.open(cur.c_str());
816816
if (!fin.is_open()) {
817-
if (_settings && !_settings->nomsg.isSuppressed("missingInclude", Path::fromNativeSeparators(cur), 1)) {
818-
missingIncludeFlag = true;
819-
if (_settings->checkConfiguration) {
820-
missingInclude(Path::toNativeSeparators(Path::getPathFromFilename(cur)),
821-
1,
822-
cur);
823-
}
824-
}
817+
missingInclude(cur,
818+
1,
819+
cur,
820+
UserHeader
821+
);
825822
continue;
826823
}
827824
std::string fileData = read(fin, filename);
@@ -2088,14 +2085,11 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str
20882085
filepath = path;
20892086
std::ifstream fin;
20902087
if (!openHeader(filename, includePaths, filepath, fin)) {
2091-
if (_settings && !_settings->nomsg.isSuppressed("missingInclude", Path::fromNativeSeparators(filename), linenr)) {
2092-
missingIncludeFlag = true;
2093-
2094-
if (_settings->checkConfiguration)
2095-
missingInclude(Path::toNativeSeparators(filePath),
2096-
linenr,
2097-
filename);
2098-
}
2088+
missingInclude(Path::toNativeSeparators(filePath),
2089+
linenr,
2090+
filename,
2091+
headerType
2092+
);
20992093
ostr << std::endl;
21002094
continue;
21012095
}
@@ -2216,31 +2210,35 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath
22162210
}
22172211
}
22182212

2219-
if (!_settings->nomsg.isSuppressed("missingInclude", Path::fromNativeSeparators(f), linenr)) {
2220-
missingIncludeFlag = true;
2221-
if (_errorLogger && _settings->checkConfiguration) {
2222-
missingInclude(Path::toNativeSeparators(f),
2223-
linenr,
2224-
filename);
2225-
}
2226-
}
2213+
missingInclude(Path::toNativeSeparators(f),
2214+
linenr,
2215+
filename,
2216+
headerType);
22272217
}
22282218
}
22292219
}
22302220

22312221
// Report that include is missing
2232-
void Preprocessor::missingInclude(const std::string &filename, unsigned int linenr, const std::string &header)
2222+
void Preprocessor::missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType)
22332223
{
2234-
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
2235-
if (!filename.empty()) {
2236-
ErrorLogger::ErrorMessage::FileLocation loc;
2237-
loc.line = linenr;
2238-
loc.setfile(filename);
2239-
locationList.push_back(loc);
2224+
const std::string msgtype = (headerType==SystemHeader)?"missingIncludeSystem":"missingInclude";
2225+
if (!_settings->nomsg.isSuppressed(msgtype, Path::fromNativeSeparators(filename), linenr)) {
2226+
missingIncludeFlag = true;
2227+
if (_errorLogger && _settings->checkConfiguration) {
2228+
2229+
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
2230+
if (!filename.empty()) {
2231+
ErrorLogger::ErrorMessage::FileLocation loc;
2232+
loc.line = linenr;
2233+
loc.setfile(Path::toNativeSeparators(filename));
2234+
locationList.push_back(loc);
2235+
}
2236+
ErrorLogger::ErrorMessage errmsg(locationList, Severity::information, "Include file: \"" + header + "\" not found.",
2237+
msgtype, false);
2238+
errmsg.file0 = file0;
2239+
_errorLogger->reportInfo(errmsg);
2240+
}
22402241
}
2241-
ErrorLogger::ErrorMessage errmsg(locationList, Severity::information, "Include file: \"" + header + "\" not found.", "missingInclude", false);
2242-
errmsg.file0 = file0;
2243-
_errorLogger->reportInfo(errmsg);
22442242
}
22452243

22462244
/**
@@ -3039,7 +3037,8 @@ void Preprocessor::getErrorMessages(ErrorLogger *errorLogger, const Settings *se
30393037
{
30403038
Settings settings2(*settings);
30413039
Preprocessor preprocessor(&settings2, errorLogger);
3042-
preprocessor.missingInclude("", 1, "");
3040+
preprocessor.missingInclude("", 1, "", UserHeader);
3041+
preprocessor.missingInclude("", 1, "", SystemHeader);
30433042
preprocessor.validateCfgError("X");
30443043
preprocessor.error("", 1, "#error message"); // #error ..
30453044
}

lib/preprocessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class CPPCHECKLIB Preprocessor {
244244
}
245245

246246
private:
247-
void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header);
247+
void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType);
248248

249249
void error(const std::string &filename, unsigned int linenr, const std::string &msg);
250250

test/testpreprocessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3362,7 +3362,7 @@ class TestPreprocessor : public TestFixture {
33623362
ASSERT_EQUALS("[test.c:1]: (information) Include file: \"missing-include!!.h\" not found.\n", errout.str());
33633363

33643364
errout.str("");
3365-
settings.nomsg.addSuppression("missingInclude");
3365+
settings.nomsg.addSuppression("missingIncludeSystem");
33663366
preprocessor.handleIncludes(code,"test.c",includePaths,defs);
33673367
ASSERT_EQUALS("", errout.str());
33683368
}

0 commit comments

Comments
 (0)