Index: include/llvm/Support/Timer.h =================================================================== --- include/llvm/Support/Timer.h +++ include/llvm/Support/Timer.h @@ -77,9 +77,8 @@ /// class Timer { TimeRecord Time; // The total time captured - TimeRecord StartTime; // The time startTimer() was last called std::string Name; // The name of this time variable. - bool Running; // Is the timer currently running? + unsigned RefCount; // How many times has this timer been started? bool Triggered; // Has the timer ever been triggered? TimerGroup *TG; // The TimerGroup this Timer is in. @@ -119,7 +118,10 @@ void clear(); /// Return the duration for which this timer has been running. - TimeRecord getTotalTime() const { return Time; } + TimeRecord getTotalTime() const { + assert(RefCount == 0 && "Cannot query active timer"); + return Time; + } private: friend class TimerGroup; Index: lib/Support/Timer.cpp =================================================================== --- lib/Support/Timer.cpp +++ lib/Support/Timer.cpp @@ -101,8 +101,10 @@ void Timer::init(StringRef N, TimerGroup &tg) { assert(!TG && "Timer already initialized"); + Time = TimeRecord(); Name.assign(N.begin(), N.end()); - Running = Triggered = false; + Triggered = false; + RefCount = 0; TG = &tg; TG->addTimer(*this); } @@ -136,21 +138,23 @@ } void Timer::startTimer() { - assert(!Running && "Cannot start a running timer"); - Running = Triggered = true; - StartTime = TimeRecord::getCurrentTime(true); + Triggered = true; + if (!RefCount) + Time -= TimeRecord::getCurrentTime(true); + ++RefCount; } void Timer::stopTimer() { - assert(Running && "Cannot stop a paused timer"); - Running = false; - Time += TimeRecord::getCurrentTime(false); - Time -= StartTime; + assert(RefCount > 0 && "Cannot stop a paused timer"); + --RefCount; + if (!RefCount) + Time += TimeRecord::getCurrentTime(false); } void Timer::clear() { - Running = Triggered = false; - Time = StartTime = TimeRecord(); + RefCount = 0; + Triggered = false; + Time = TimeRecord(); } static void printVal(double Val, double Total, raw_ostream &OS) { Index: unittests/Support/TimerTest.cpp =================================================================== --- unittests/Support/TimerTest.cpp +++ unittests/Support/TimerTest.cpp @@ -62,4 +62,13 @@ EXPECT_FALSE(T1.hasTriggered()); } +TEST(Timer, Nesting) { + Timer T1("T1"); + T1.startTimer(); + T1.startTimer(); + T1.stopTimer(); + T1.stopTimer(); + EXPECT_TRUE(T1.hasTriggered()); +} + } // end anon namespace