Skip to content

Commit 07ba92f

Browse files
committed
added lazy execution of ValueFlow via DISABLE_VALUEFLOW=2 environment variable
1 parent ebdcf37 commit 07ba92f

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

lib/token.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,8 @@ class CPPCHECKLIB Token {
11621162
}
11631163

11641164
const std::list<ValueFlow::Value>& values() const {
1165+
if (mTokensFrontBack && !mTokensFrontBack->list->isValuesSet())
1166+
mTokensFrontBack->list->setValues();
11651167
return mImpl->mValues ? *mImpl->mValues : TokenImpl::mEmptyValueList;
11661168
}
11671169

lib/tokenize.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,6 +2796,7 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration)
27962796
createSymbolDatabase();
27972797
}
27982798

2799+
// TODO: this accesses ValueFlow information before it has been generated
27992800
if (mTimerResults) {
28002801
Timer t("Tokenizer::simplifyTokens1::setValueType", mSettings->showtime, mTimerResults);
28012802
mSymbolDatabase->setValueTypeInTokenList(true);
@@ -2819,11 +2820,7 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration)
28192820
}
28202821
}
28212822

2822-
// TODO: do not run valueflow if no checks are being performed at all - e.g. unusedFunctions only
2823-
const char* disableValueflowEnv = std::getenv("DISABLE_VALUEFLOW");
2824-
const bool doValueFlow = !disableValueflowEnv || (std::strcmp(disableValueflowEnv, "1") != 0);
2825-
2826-
if (doValueFlow) {
2823+
auto setValues = [&](){
28272824
if (mTimerResults) {
28282825
Timer t("Tokenizer::simplifyTokens1::ValueFlow", mSettings->showtime, mTimerResults);
28292826
ValueFlow::setValues(&list, mSymbolDatabase, mErrorLogger, mSettings);
@@ -2832,6 +2829,15 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration)
28322829
}
28332830

28342831
mSymbolDatabase->setArrayDimensionsUsingValueFlow();
2832+
};
2833+
2834+
const char* disableValueflowEnv = std::getenv("DISABLE_VALUEFLOW");
2835+
const bool doLazyValueFlow = disableValueflowEnv && (std::strcmp(disableValueflowEnv, "2") == 0);
2836+
const bool doValueFlow = !disableValueflowEnv || (std::strcmp(disableValueflowEnv, "1") != 0);
2837+
if (doLazyValueFlow) {
2838+
list.setValuesFunc(std::move(setValues));
2839+
} else if (doValueFlow) {
2840+
setValues();
28352841
}
28362842

28372843
printDebugOutput(1);

lib/tokenlist.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ TokenList::TokenList(const Settings* settings) :
4949
mTokensFrontBack(),
5050
mSettings(settings),
5151
mIsC(false),
52-
mIsCpp(false)
52+
mIsCpp(false),
53+
mValuesSet(false),
54+
mInSetValues(false)
5355
{
5456
mTokensFrontBack.list = this;
5557
mKeywords.insert("asm");

lib/tokenlist.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <cstddef>
2727
#include <iosfwd>
28+
#include <functional>
2829
#include <string>
2930
#include <unordered_set>
3031
#include <vector>
@@ -207,6 +208,23 @@ class CPPCHECKLIB TokenList {
207208

208209
bool isKeyword(const std::string &str) const;
209210

211+
void setValuesFunc(std::function<void()> f) {
212+
mValuesFunc = std::move(f);
213+
}
214+
215+
void setValues() const {
216+
if (!mInSetValues && !mValuesSet && mValuesFunc) {
217+
mInSetValues = true;
218+
mValuesFunc();
219+
mValuesSet = true;
220+
mInSetValues = false;
221+
}
222+
}
223+
224+
bool isValuesSet() const {
225+
return mValuesSet;
226+
}
227+
210228
private:
211229
void determineCppC();
212230

@@ -227,6 +245,10 @@ class CPPCHECKLIB TokenList {
227245
/** File is known to be C/C++ code */
228246
bool mIsC;
229247
bool mIsCpp;
248+
249+
std::function<void()> mValuesFunc;
250+
mutable bool mValuesSet;
251+
mutable bool mInSetValues;
230252
};
231253

232254
/// @}

0 commit comments

Comments
 (0)