Skip to content

Commit

Permalink
added lazy execution of ValueFlow via DISABLE_VALUEFLOW=2 environme…
Browse files Browse the repository at this point in the history
…nt variable
  • Loading branch information
firewave committed Sep 29, 2022
1 parent 3c6c383 commit 2b93752
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,8 @@ class CPPCHECKLIB Token {
}

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

Expand Down
16 changes: 11 additions & 5 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2796,6 +2796,7 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration)
createSymbolDatabase();
}

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

// TODO: do not run valueflow if no checks are being performed at all - e.g. unusedFunctions only
const char* disableValueflowEnv = std::getenv("DISABLE_VALUEFLOW");
const bool doValueFlow = !disableValueflowEnv || (std::strcmp(disableValueflowEnv, "1") != 0);

if (doValueFlow) {
auto setValues = [&](){
if (mTimerResults) {
Timer t("Tokenizer::simplifyTokens1::ValueFlow", mSettings->showtime, mTimerResults);
ValueFlow::setValues(&list, mSymbolDatabase, mErrorLogger, mSettings);
Expand All @@ -2832,6 +2829,15 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration)
}

mSymbolDatabase->setArrayDimensionsUsingValueFlow();
};

const char* disableValueflowEnv = std::getenv("DISABLE_VALUEFLOW");
const bool doLazyValueFlow = !disableValueflowEnv && (std::strcmp(disableValueflowEnv, "2") == 0);
const bool doValueFlow = !disableValueflowEnv || (std::strcmp(disableValueflowEnv, "1") != 0);
if (doLazyValueFlow) {
list.setValuesFunc(std::move(setValues));
} else if (doValueFlow) {
setValues();
}

printDebugOutput(1);
Expand Down
4 changes: 3 additions & 1 deletion lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ TokenList::TokenList(const Settings* settings) :
mTokensFrontBack(),
mSettings(settings),
mIsC(false),
mIsCpp(false)
mIsCpp(false),
mValuesSet(false),
mInSetValues(false)
{
mTokensFrontBack.list = this;
mKeywords.insert("asm");
Expand Down
22 changes: 22 additions & 0 deletions lib/tokenlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <cstddef>
#include <iosfwd>
#include <functional>
#include <string>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -207,6 +208,23 @@ class CPPCHECKLIB TokenList {

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

void setValuesFunc(std::function<void()> f) {
mValuesFunc = std::move(f);
}

void setValues() const {
if (!mInSetValues && !mValuesSet && mValuesFunc) {
mInSetValues = true;
mValuesFunc();
mValuesSet = true;
mInSetValues = false;
}
}

bool isValuesSet() const {
return mValuesSet;
}

private:
void determineCppC();

Expand All @@ -227,6 +245,10 @@ class CPPCHECKLIB TokenList {
/** File is known to be C/C++ code */
bool mIsC;
bool mIsCpp;

std::function<void()> mValuesFunc;
mutable bool mValuesSet;
mutable bool mInSetValues;
};

/// @}
Expand Down

0 comments on commit 2b93752

Please sign in to comment.