Skip to content

Commit d3a2cdc

Browse files
authored
converted (undocumented) define MAXTIME into (undocumented) command-line options --checks-max-time=, --template-max-time= and --typedef-max-time= (danmar#4661)
1 parent 71cdacd commit d3a2cdc

8 files changed

+94
-41
lines changed

cli/cmdlineparser.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
250250
mSettings->addEnabled("information");
251251
}
252252

253+
else if (std::strncmp(argv[i], "--checks-max-time=", 18) == 0) {
254+
mSettings->checksMaxTime = std::atoi(argv[i] + 18);
255+
}
256+
253257
else if (std::strcmp(argv[i], "--clang") == 0) {
254258
mSettings->clang = true;
255259
}
@@ -915,6 +919,14 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
915919
}
916920
}
917921

922+
else if (std::strncmp(argv[i], "--template-max-time=", 20) == 0) {
923+
mSettings->templateMaxTime = std::atoi(argv[i] + 20);
924+
}
925+
926+
else if (std::strncmp(argv[i], "--typedef-max-time=", 19) == 0) {
927+
mSettings->typedefMaxTime = std::atoi(argv[i] + 19);
928+
}
929+
918930
else if (std::strncmp(argv[i], "--valueflow-max-iterations=", 27) == 0) {
919931
long tmp;
920932
try {

lib/config.h

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#ifndef configH
2020
#define configH
2121

22+
#ifdef MAXTIME
23+
#error "MAXTIME is no longer supported - please use command-line options --checks-max-time=, --template-max-time= and --typedef-max-time= instead"
24+
#endif
25+
2226
#ifdef _WIN32
2327
# ifdef CPPCHECKLIB_EXPORT
2428
# define CPPCHECKLIB __declspec(dllexport)

lib/cppcheck.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -1055,13 +1055,27 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
10551055
const char* unusedFunctionOnly = std::getenv("UNUSEDFUNCTION_ONLY");
10561056
const bool doUnusedFunctionOnly = unusedFunctionOnly && (std::strcmp(unusedFunctionOnly, "1") == 0);
10571057

1058+
const std::time_t maxTime = mSettings.checksMaxTime > 0 ? std::time(nullptr) + mSettings.checksMaxTime : 0;
1059+
10581060
// call all "runChecks" in all registered Check classes
10591061
for (Check *check : Check::instances()) {
10601062
if (Settings::terminated())
10611063
return;
10621064

1063-
if (Tokenizer::isMaxTime())
1065+
if (maxTime > 0 && std::time(nullptr) > maxTime) {
1066+
if (mSettings.debugwarnings) {
1067+
ErrorMessage::FileLocation loc;
1068+
loc.setfile(tokenizer.list.getFiles()[0]);
1069+
ErrorMessage errmsg({std::move(loc)},
1070+
emptyString,
1071+
Severity::debug,
1072+
"Checks maximum time exceeded",
1073+
"checksMaxTime",
1074+
Certainty::normal);
1075+
reportErr(errmsg);
1076+
}
10641077
return;
1078+
}
10651079

10661080
if (doUnusedFunctionOnly && dynamic_cast<CheckUnusedFunctions*>(check) == nullptr)
10671081
continue;

lib/settings.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Settings::Settings()
3939
checkConfiguration(false),
4040
checkHeaders(true),
4141
checkLibrary(false),
42+
checksMaxTime(0),
4243
checkUnusedTemplates(true),
4344
clang(false),
4445
clangExecutable("clang"),
@@ -66,6 +67,8 @@ Settings::Settings()
6667
relativePaths(false),
6768
reportProgress(false),
6869
showtime(SHOWTIME_MODES::SHOWTIME_NONE),
70+
templateMaxTime(0),
71+
typedefMaxTime(0),
6972
valueFlowMaxIterations(4),
7073
verbose(false),
7174
xml(false),

lib/settings.h

+9
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ class CPPCHECKLIB Settings : public cppcheck::Platform {
125125
/** Check for incomplete info in library files? */
126126
bool checkLibrary;
127127

128+
/** @brief The maximum time in seconds for the checks of a single file */
129+
std::size_t checksMaxTime;
130+
128131
/** @brief check unknown function return values */
129132
std::set<std::string> checkUnknownFunctionReturn;
130133

@@ -342,6 +345,12 @@ class CPPCHECKLIB Settings : public cppcheck::Platform {
342345
* text mode, e.g. "{file}:{line} {info}" */
343346
std::string templateLocation;
344347

348+
/** @brief The maximum time in seconds for the template instantation */
349+
std::size_t templateMaxTime;
350+
351+
/** @brief The maximum time in seconds for the typedef simplification */
352+
std::size_t typedefMaxTime;
353+
345354
/** @brief defines given by the user */
346355
std::string userDefines;
347356

lib/templatesimplifier.cpp

+30-10
Original file line numberDiff line numberDiff line change
@@ -3112,12 +3112,22 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
31123112
Token * const tok2 = instantiation.token();
31133113
if (mErrorLogger && !mTokenList.getFiles().empty())
31143114
mErrorLogger->reportProgress(mTokenList.getFiles()[0], "TemplateSimplifier::simplifyTemplateInstantiations()", tok2->progressValue());
3115-
#ifdef MAXTIME
3116-
if (std::time(0) > maxtime)
3115+
3116+
if (maxtime > 0 && std::time(nullptr) > maxtime) {
3117+
if (mSettings->debugwarnings) {
3118+
ErrorMessage::FileLocation loc;
3119+
loc.setfile(mTokenList.getFiles()[0]);
3120+
ErrorMessage errmsg({std::move(loc)},
3121+
emptyString,
3122+
Severity::debug,
3123+
"Template instantation maximum time exceeded",
3124+
"templateMaxTime",
3125+
Certainty::normal);
3126+
mErrorLogger->reportErr(errmsg);
3127+
}
31173128
return false;
3118-
#else
3119-
(void)maxtime;
3120-
#endif
3129+
}
3130+
31213131
assert(mTokenList.validateToken(tok2)); // that assertion fails on examples from #6021
31223132

31233133
const Token *startToken = tok2;
@@ -3173,12 +3183,22 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
31733183
Token * tok2 = const_cast<Token *>(templateDeclaration.nameToken());
31743184
if (mErrorLogger && !mTokenList.getFiles().empty())
31753185
mErrorLogger->reportProgress(mTokenList.getFiles()[0], "TemplateSimplifier::simplifyTemplateInstantiations()", tok2->progressValue());
3176-
#ifdef MAXTIME
3177-
if (std::time(0) > maxtime)
3186+
3187+
if (maxtime > 0 && std::time(nullptr) > maxtime) {
3188+
if (mSettings->debugwarnings) {
3189+
ErrorMessage::FileLocation loc;
3190+
loc.setfile(mTokenList.getFiles()[0]);
3191+
ErrorMessage errmsg({std::move(loc)},
3192+
emptyString,
3193+
Severity::debug,
3194+
"Template instantation maximum time exceeded",
3195+
"templateMaxTime",
3196+
Certainty::normal);
3197+
mErrorLogger->reportErr(errmsg);
3198+
}
31783199
return false;
3179-
#else
3180-
(void)maxtime;
3181-
#endif
3200+
}
3201+
31823202
assert(mTokenList.validateToken(tok2)); // that assertion fails on examples from #6021
31833203

31843204
Token *startToken = tok2;

lib/tokenize.cpp

+21-16
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,8 @@ Tokenizer::Tokenizer() :
164164
mVarId(0),
165165
mUnnamedCount(0),
166166
mCodeWithTemplates(false), //is there any templates?
167-
mTimerResults(nullptr)
168-
#ifdef MAXTIME
169-
, mMaxTime(std::time(0) + MAXTIME)
170-
#endif
171-
, mPreprocessor(nullptr)
167+
mTimerResults(nullptr),
168+
mPreprocessor(nullptr)
172169
{}
173170

174171
Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger) :
@@ -180,11 +177,8 @@ Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger) :
180177
mVarId(0),
181178
mUnnamedCount(0),
182179
mCodeWithTemplates(false), //is there any templates?
183-
mTimerResults(nullptr)
184-
#ifdef MAXTIME
185-
,mMaxTime(std::time(0) + MAXTIME)
186-
#endif
187-
, mPreprocessor(nullptr)
180+
mTimerResults(nullptr),
181+
mPreprocessor(nullptr)
188182
{
189183
// make sure settings are specified
190184
assert(mSettings);
@@ -614,15 +608,29 @@ void Tokenizer::simplifyTypedef()
614608
// Convert "using a::b;" to corresponding typedef statements
615609
simplifyUsingToTypedef();
616610

611+
const std::time_t maxTime = mSettings->typedefMaxTime > 0 ? std::time(nullptr) + mSettings->typedefMaxTime: 0;
612+
617613
for (Token *tok = list.front(); tok; tok = tok->next()) {
618614
if (mErrorLogger && !list.getFiles().empty())
619615
mErrorLogger->reportProgress(list.getFiles()[0], "Tokenize (typedef)", tok->progressValue());
620616

621617
if (Settings::terminated())
622618
return;
623619

624-
if (isMaxTime())
620+
if (maxTime > 0 && std::time(nullptr) > maxTime) {
621+
if (mSettings->debugwarnings) {
622+
ErrorMessage::FileLocation loc;
623+
loc.setfile(list.getFiles()[0]);
624+
ErrorMessage errmsg({std::move(loc)},
625+
emptyString,
626+
Severity::debug,
627+
"Typedef simplification instantation maximum time exceeded",
628+
"typedefMaxTime",
629+
Certainty::normal);
630+
mErrorLogger->reportErr(errmsg);
631+
}
625632
return;
633+
}
626634

627635
if (goback) {
628636
//jump back once, see the comment at the end of the function
@@ -3461,12 +3469,9 @@ void Tokenizer::simplifyTemplates()
34613469
if (isC())
34623470
return;
34633471

3472+
const std::time_t maxTime = mSettings->templateMaxTime > 0 ? std::time(nullptr) + mSettings->templateMaxTime : 0;
34643473
mTemplateSimplifier->simplifyTemplates(
3465-
#ifdef MAXTIME
3466-
mMaxTime,
3467-
#else
3468-
0, // ignored
3469-
#endif
3474+
maxTime,
34703475
mCodeWithTemplates);
34713476
}
34723477
//---------------------------------------------------------------------------

lib/tokenize.h

-14
Original file line numberDiff line numberDiff line change
@@ -680,15 +680,6 @@ class CPPCHECKLIB Tokenizer {
680680
*/
681681
static const Token * startOfExecutableScope(const Token * tok);
682682

683-
#ifdef MAXTIME
684-
bool isMaxTime() const {
685-
return (std::time(0) > mMaxTime);
686-
#else
687-
static bool isMaxTime() {
688-
return false;
689-
#endif
690-
}
691-
692683
const Settings *getSettings() const {
693684
return mSettings;
694685
}
@@ -759,11 +750,6 @@ class CPPCHECKLIB Tokenizer {
759750
*/
760751
TimerResults *mTimerResults;
761752

762-
#ifdef MAXTIME
763-
/** Tokenizer maxtime */
764-
const std::time_t mMaxTime;
765-
#endif
766-
767753
const Preprocessor *mPreprocessor;
768754
};
769755

0 commit comments

Comments
 (0)