Skip to content

Commit 9a6569f

Browse files
committed
Always set ErrorMessage::file0 to ensure that the source file that cppcheck is checking when an error occurs can be identified
Removed unused function and unused includes from preprocessor.h/cpp
1 parent 560e7d6 commit 9a6569f

11 files changed

Lines changed: 46 additions & 65 deletions

cli/cppcheckexecutor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
790790
"std.cfg should be available in " + cfgfolder + " or the CFGDIR "
791791
"should be configured.");
792792
#endif
793-
ErrorLogger::ErrorMessage errmsg(callstack, Severity::information, msg+" "+details, "failedToLoadCfg", false);
793+
ErrorLogger::ErrorMessage errmsg(callstack, emptyString, Severity::information, msg+" "+details, "failedToLoadCfg", false);
794794
reportErr(errmsg);
795795
return EXIT_FAILURE;
796796
}
@@ -863,6 +863,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
863863
if (settings.isEnabled("missingInclude") && (Preprocessor::missingIncludeFlag || Preprocessor::missingSystemIncludeFlag)) {
864864
const std::list<ErrorLogger::ErrorMessage::FileLocation> callStack;
865865
ErrorLogger::ErrorMessage msg(callStack,
866+
emptyString,
866867
Severity::information,
867868
"Cppcheck cannot find all the include files (use --check-config for details)\n"
868869
"Cppcheck cannot find all the include files. Cppcheck can check the code without the "

lib/checkbufferoverrun.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,7 @@ void CheckBufferOverrun::analyseWholeProgram(const std::list<Check::FileInfo*> &
19811981
ostr << "Array " << it->first << '[' << sz->second << "] accessed at index " << it->second.index << " which is out of bounds";
19821982

19831983
const ErrorLogger::ErrorMessage errmsg(locationList,
1984+
emptyString,
19841985
Severity::error,
19851986
ostr.str(),
19861987
"arrayIndexOutOfBounds",

lib/checkunusedfunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger,
252252
locationList.push_back(fileLoc);
253253
}
254254

255-
const ErrorLogger::ErrorMessage errmsg(locationList, Severity::style, "The function '" + funcname + "' is never used.", "unusedFunction", CWE561, false);
255+
const ErrorLogger::ErrorMessage errmsg(locationList, emptyString, Severity::style, "The function '" + funcname + "' is never used.", "unusedFunction", CWE561, false);
256256
if (errorLogger)
257257
errorLogger->reportErr(errmsg);
258258
else

lib/cppcheck.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,16 +308,17 @@ unsigned int CppCheck::processFile(const std::string& filename, std::istream& fi
308308
loc.setfile(fixedpath);
309309
} else {
310310
ErrorLogger::ErrorMessage::FileLocation loc2;
311-
loc2.setfile(Path::toNativeSeparators(filename.c_str()));
311+
loc2.setfile(Path::toNativeSeparators(filename));
312312
locationList.push_back(loc2);
313313
loc.setfile(_tokenizer.list.getSourceFilePath());
314314
}
315315
locationList.push_back(loc);
316-
const ErrorLogger::ErrorMessage errmsg(locationList,
317-
Severity::error,
318-
e.errorMessage,
319-
e.id,
320-
false);
316+
ErrorLogger::ErrorMessage errmsg(locationList,
317+
_tokenizer.list.getSourceFilePath(),
318+
Severity::error,
319+
e.errorMessage,
320+
e.id,
321+
false);
321322

322323
reportErr(errmsg);
323324
}
@@ -360,6 +361,7 @@ void CppCheck::internalError(const std::string &filename, const std::string &msg
360361
callstack.push_back(loc1);
361362

362363
ErrorLogger::ErrorMessage errmsg(callstack,
364+
emptyString,
363365
Severity::information,
364366
fullmsg,
365367
"internalError",
@@ -557,6 +559,7 @@ void CppCheck::tooManyConfigsError(const std::string &file, const std::size_t nu
557559

558560

559561
ErrorLogger::ErrorMessage errmsg(loclist,
562+
emptyString,
560563
Severity::information,
561564
msg.str(),
562565
"toomanyconfigs", CWE398,
@@ -567,7 +570,6 @@ void CppCheck::tooManyConfigsError(const std::string &file, const std::size_t nu
567570

568571
void CppCheck::purgedConfigurationMessage(const std::string &file, const std::string& configuration)
569572
{
570-
571573
tooManyConfigs = false;
572574

573575
if (_settings.isEnabled("information") && file.empty())
@@ -581,6 +583,7 @@ void CppCheck::purgedConfigurationMessage(const std::string &file, const std::st
581583
}
582584

583585
ErrorLogger::ErrorMessage errmsg(loclist,
586+
emptyString,
584587
Severity::information,
585588
"The configuration '" + configuration + "' was not checked because its code equals another one.",
586589
"purgedConfiguration",

lib/errorlogger.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ ErrorLogger::ErrorMessage::ErrorMessage()
4848
{
4949
}
5050

51-
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive) :
51+
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, const std::string& file0_, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive) :
5252
_callStack(callStack), // locations for this error message
5353
_id(id), // set the message id
54+
file0(file0_),
5455
_severity(severity), // severity for this error message
5556
_cwe(0U),
5657
_inconclusive(inconclusive)
@@ -61,9 +62,10 @@ ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack
6162

6263

6364

64-
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, const CWE &cwe, bool inconclusive) :
65+
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, const std::string& file0_, Severity::SeverityType severity, const std::string &msg, const std::string &id, const CWE &cwe, bool inconclusive) :
6566
_callStack(callStack), // locations for this error message
6667
_id(id), // set the message id
68+
file0(file0_),
6769
_severity(severity), // severity for this error message
6870
_cwe(cwe.id),
6971
_inconclusive(inconclusive)
@@ -413,7 +415,7 @@ void ErrorLogger::reportUnmatchedSuppressions(const std::list<Suppressions::Supp
413415

414416
const std::list<ErrorLogger::ErrorMessage::FileLocation> callStack = make_container< std::list<ErrorLogger::ErrorMessage::FileLocation> > ()
415417
<< ErrorLogger::ErrorMessage::FileLocation(i->file, i->line);
416-
reportErr(ErrorLogger::ErrorMessage(callStack, Severity::information, "Unmatched suppression: " + i->id, "unmatchedSuppression", false));
418+
reportErr(ErrorLogger::ErrorMessage(callStack, emptyString, Severity::information, "Unmatched suppression: " + i->id, "unmatchedSuppression", false));
417419
}
418420
}
419421

lib/errorlogger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ class CPPCHECKLIB ErrorLogger {
208208
std::string _file;
209209
};
210210

211-
ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive);
212-
ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, const CWE &cwe, bool inconclusive);
211+
ErrorMessage(const std::list<FileLocation> &callStack, const std::string& file0, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive);
212+
ErrorMessage(const std::list<FileLocation> &callStack, const std::string& file0, Severity::SeverityType severity, const std::string &msg, const std::string &id, const CWE &cwe, bool inconclusive);
213213
ErrorMessage(const std::list<const Token*>& callstack, const TokenList* list, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive);
214214
ErrorMessage(const std::list<const Token*>& callstack, const TokenList* list, Severity::SeverityType severity, const std::string& id, const std::string& msg, const CWE &cwe, bool inconclusive);
215215
ErrorMessage();

lib/preprocessor.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@
2525

2626
#include <algorithm>
2727
#include <sstream>
28-
#include <fstream>
2928
#include <cstdlib>
3029
#include <cctype>
3130
#include <vector>
3231
#include <set>
33-
#include <stack>
3432

3533
/**
3634
* Remove heading and trailing whitespaces from the input parameter.
@@ -69,21 +67,6 @@ Preprocessor::~Preprocessor()
6967
delete it->second;
7068
}
7169

72-
void Preprocessor::writeError(const std::string &fileName, const unsigned int linenr, ErrorLogger *errorLogger, const std::string &errorType, const std::string &errorText)
73-
{
74-
if (!errorLogger)
75-
return;
76-
77-
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
78-
ErrorLogger::ErrorMessage::FileLocation loc(fileName, linenr);
79-
locationList.push_back(loc);
80-
errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList,
81-
Severity::error,
82-
errorText,
83-
errorType,
84-
false));
85-
}
86-
8770

8871
static void inlineSuppressions(const simplecpp::TokenList &tokens, Settings &_settings)
8972
{
@@ -657,6 +640,7 @@ void Preprocessor::error(const std::string &filename, unsigned int linenr, const
657640
locationList.push_back(loc);
658641
}
659642
_errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList,
643+
file0,
660644
Severity::error,
661645
msg,
662646
"preprocessorErrorDirective",
@@ -685,13 +669,12 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line
685669
loc.setfile(Path::toNativeSeparators(filename));
686670
locationList.push_back(loc);
687671
}
688-
ErrorLogger::ErrorMessage errmsg(locationList, Severity::information,
672+
ErrorLogger::ErrorMessage errmsg(locationList, file0, Severity::information,
689673
(headerType==SystemHeader) ?
690674
"Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results." :
691675
"Include file: \"" + header + "\" not found.",
692676
(headerType==SystemHeader) ? "missingIncludeSystem" : "missingInclude",
693677
false);
694-
errmsg.file0 = file0;
695678
_errorLogger->reportInfo(errmsg);
696679
}
697680
}
@@ -763,7 +746,7 @@ void Preprocessor::validateCfgError(const std::string &cfg, const std::string &m
763746
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
764747
ErrorLogger::ErrorMessage::FileLocation loc(file0, 1);
765748
locationList.push_back(loc);
766-
ErrorLogger::ErrorMessage errmsg(locationList, Severity::information, "Skipping configuration '" + cfg + "' since the value of '" + macro + "' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.", id, false);
749+
ErrorLogger::ErrorMessage errmsg(locationList, file0, Severity::information, "Skipping configuration '" + cfg + "' since the value of '" + macro + "' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.", id, false);
767750
_errorLogger->reportInfo(errmsg);
768751
}
769752

lib/preprocessor.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,6 @@ class CPPCHECKLIB Preprocessor {
155155
bool validateCfg(const std::string &code, const std::string &cfg);
156156
void validateCfgError(const std::string &cfg, const std::string &macro);
157157

158-
/**
159-
* report error
160-
* @param fileName name of file that the error was found in
161-
* @param linenr linenr in file
162-
* @param errorLogger Error logger to write error to
163-
* @param errorType id string for error
164-
* @param errorText Plain text
165-
*/
166-
static void writeError(const std::string &fileName, const unsigned int linenr, ErrorLogger *errorLogger, const std::string &errorType, const std::string &errorText);
167-
168158
private:
169159

170160
/**

lib/valueflow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ static void execute(const Token *expr,
8282
static void bailout(TokenList *tokenlist, ErrorLogger *errorLogger, const Token *tok, const std::string &what)
8383
{
8484
std::list<ErrorLogger::ErrorMessage::FileLocation> callstack;
85-
callstack.push_back(ErrorLogger::ErrorMessage::FileLocation(tok,tokenlist));
86-
ErrorLogger::ErrorMessage errmsg(callstack, Severity::debug, "ValueFlow bailout: " + what, "valueFlowBailout", false);
85+
callstack.push_back(ErrorLogger::ErrorMessage::FileLocation(tok, tokenlist));
86+
ErrorLogger::ErrorMessage errmsg(callstack, tokenlist->getSourceFilePath(), Severity::debug, "ValueFlow bailout: " + what, "valueFlowBailout", false);
8787
errorLogger->reportErr(errmsg);
8888
}
8989

0 commit comments

Comments
 (0)