Skip to content

Commit 295ba9c

Browse files
committed
Fixed cppcheck-opensource#2709 (Negative times in --showtime summary)
1 parent a5854ac commit 295ba9c

4 files changed

Lines changed: 25 additions & 15 deletions

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ TESTOBJ = test/options.o \
158158
test/testsuppressions.o \
159159
test/testsymboldatabase.o \
160160
test/testthreadexecutor.o \
161+
test/testtimer.o \
161162
test/testtoken.o \
162163
test/testtokenize.o \
163164
test/testuninitvar.o \
@@ -393,7 +394,7 @@ test/testleakautovar.o: test/testleakautovar.cpp lib/tokenize.h lib/errorlogger.
393394
test/testmathlib.o: test/testmathlib.cpp lib/mathlib.h lib/config.h test/testsuite.h lib/errorlogger.h lib/suppressions.h test/redirect.h
394395
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_TEST} -c -o test/testmathlib.o test/testmathlib.cpp
395396

396-
test/testmemleak.o: test/testmemleak.cpp lib/tokenize.h lib/errorlogger.h lib/config.h lib/suppressions.h lib/tokenlist.h lib/checkmemoryleak.h lib/check.h lib/token.h lib/settings.h lib/standards.h test/testsuite.h test/redirect.h
397+
test/testmemleak.o: test/testmemleak.cpp lib/tokenize.h lib/errorlogger.h lib/config.h lib/suppressions.h lib/tokenlist.h lib/checkmemoryleak.h lib/check.h lib/token.h lib/settings.h lib/standards.h test/testsuite.h test/redirect.h lib/symboldatabase.h lib/mathlib.h
397398
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_TEST} -c -o test/testmemleak.o test/testmemleak.cpp
398399

399400
test/testnonreentrantfunctions.o: test/testnonreentrantfunctions.cpp lib/tokenize.h lib/errorlogger.h lib/config.h lib/suppressions.h lib/tokenlist.h lib/checknonreentrantfunctions.h lib/check.h lib/token.h lib/settings.h lib/standards.h test/testsuite.h test/redirect.h
@@ -444,6 +445,9 @@ test/testsymboldatabase.o: test/testsymboldatabase.cpp test/testsuite.h lib/erro
444445
test/testthreadexecutor.o: test/testthreadexecutor.cpp lib/cppcheck.h lib/config.h lib/settings.h lib/suppressions.h lib/standards.h lib/errorlogger.h lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/tokenlist.h test/testsuite.h test/redirect.h
445446
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_TEST} -c -o test/testthreadexecutor.o test/testthreadexecutor.cpp
446447

448+
test/testtimer.o: test/testtimer.cpp lib/timer.h lib/config.h test/testsuite.h lib/errorlogger.h lib/suppressions.h test/redirect.h
449+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_TEST} -c -o test/testtimer.o test/testtimer.cpp
450+
447451
test/testtoken.o: test/testtoken.cpp test/testsuite.h lib/errorlogger.h lib/config.h lib/suppressions.h test/redirect.h test/testutils.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/token.h
448452
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_TEST} -c -o test/testtoken.o test/testtoken.cpp
449453

lib/timer.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,36 @@
1919
#include "timer.h"
2020

2121
/*
22-
TODO:
23-
- handle SHOWTIME_TOP5 in TimerResults
24-
- sort list by time
25-
- do not sort the results alphabetically
26-
- rename "file" to "single"
27-
- synchronise map access in multithreaded mode or disable timing
28-
- add unit tests
29-
- for --showtime (needs input file)
30-
- for Timer* classes
22+
TODO:
23+
- handle SHOWTIME_TOP5 in TimerResults
24+
- sort list by time
25+
- do not sort the results alphabetically
26+
- rename "file" to "single"
27+
- synchronise map access in multithreaded mode or disable timing
28+
- add unit tests
29+
- for --showtime (needs input file)
30+
- for Timer* classes
3131
*/
3232

3333

3434
void TimerResults::ShowResults() const
3535
{
36-
std::clock_t overallClocks = 0;
36+
TimerResultsData overallData;
3737

3838
std::map<std::string, struct TimerResultsData>::const_iterator I = _results.begin();
3939
const std::map<std::string, struct TimerResultsData>::const_iterator E = _results.end();
4040

4141
while (I != E) {
42-
const double sec = (double)I->second._clocks / CLOCKS_PER_SEC;
43-
const double secAverage = (double)(I->second._clocks / I->second._numberOfResults) / CLOCKS_PER_SEC;
42+
const double sec = I->second.seconds();
43+
const double secAverage = sec / (double)(I->second._numberOfResults);
4444
std::cout << I->first << ": " << sec << "s (avg. " << secAverage << "s - " << I->second._numberOfResults << " result(s))" << std::endl;
4545

46-
overallClocks += I->second._clocks;
46+
overallData._clocks += I->second._clocks;
4747

4848
++I;
4949
}
5050

51-
const double secOverall = (double)overallClocks / CLOCKS_PER_SEC;
51+
const double secOverall = overallData.seconds();
5252
std::cout << "Overall time: " << secOverall << "s" << std::endl;
5353
}
5454

lib/timer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ struct TimerResultsData {
4646
: _clocks(0)
4747
, _numberOfResults(0) {
4848
}
49+
50+
double seconds() const {
51+
double ret = (double)((unsigned long)_clocks) / (double)CLOCKS_PER_SEC;
52+
return ret;
53+
}
4954
};
5055

5156
class CPPCHECKLIB TimerResults : public TimerResultsIntf {

test/testfiles.pri

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ SOURCES += $${BASEPATH}/test64bit.cpp \
3939
$${BASEPATH}/testsuppressions.cpp \
4040
$${BASEPATH}/testsymboldatabase.cpp \
4141
$${BASEPATH}/testthreadexecutor.cpp \
42+
$${BASEPATH}/testtimer.cpp \
4243
$${BASEPATH}/testtoken.cpp \
4344
$${BASEPATH}/testtokenize.cpp \
4445
$${BASEPATH}/testuninitvar.cpp \

0 commit comments

Comments
 (0)