diff --git a/Makefile b/Makefile index ae59722df1f..ad9f84ee8ce 100644 --- a/Makefile +++ b/Makefile @@ -599,7 +599,7 @@ $(libcppdir)/infer.o: lib/infer.cpp lib/calculate.h lib/config.h lib/errortypes. $(libcppdir)/keywords.o: lib/keywords.cpp lib/config.h lib/keywords.h lib/standards.h lib/utils.h $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/keywords.cpp -$(libcppdir)/library.o: lib/library.cpp externals/tinyxml2/tinyxml2.h lib/astutils.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/vfvalue.h lib/xml.h +$(libcppdir)/library.o: lib/library.cpp externals/tinyxml2/tinyxml2.h lib/astutils.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/library.cpp $(libcppdir)/mathlib.o: lib/mathlib.cpp externals/simplecpp/simplecpp.h lib/config.h lib/errortypes.h lib/mathlib.h lib/utils.h @@ -620,7 +620,7 @@ $(libcppdir)/platform.o: lib/platform.cpp externals/tinyxml2/tinyxml2.h lib/conf $(libcppdir)/preprocessor.o: lib/preprocessor.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/preprocessor.cpp -$(libcppdir)/programmemory.o: lib/programmemory.cpp lib/addoninfo.h lib/astutils.h lib/calculate.h lib/config.h lib/errortypes.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/programmemory.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vfvalue.h +$(libcppdir)/programmemory.o: lib/programmemory.cpp lib/addoninfo.h lib/astutils.h lib/calculate.h lib/config.h lib/errortypes.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/programmemory.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vfvalue.h $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/programmemory.cpp $(libcppdir)/reverseanalyzer.o: lib/reverseanalyzer.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/config.h lib/errortypes.h lib/forwardanalyzer.h lib/library.h lib/mathlib.h lib/platform.h lib/reverseanalyzer.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueptr.h lib/vfvalue.h diff --git a/lib/library.cpp b/lib/library.cpp index d689ff4f7ac..f6be797cc8c 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -26,7 +26,6 @@ #include "token.h" #include "tokenlist.h" #include "utils.h" -#include "valueflow.h" #include "vfvalue.h" #include @@ -37,7 +36,6 @@ #include #include #include -#include #include #include @@ -1801,55 +1799,6 @@ bool Library::hasAnyTypeCheck(const std::string& typeName) const }); } -std::shared_ptr createTokenFromExpression(const std::string& returnValue, - const Settings& settings, - bool cpp, - std::unordered_map* lookupVarId) -{ - std::shared_ptr tokenList = std::make_shared(&settings); - { - const std::string code = "return " + returnValue + ";"; - std::istringstream istr(code); - if (!tokenList->createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C)) - return nullptr; - } - - // TODO: put in a helper? - // combine operators, set links, etc.. - std::stack lpar; - for (Token* tok2 = tokenList->front(); tok2; tok2 = tok2->next()) { - if (Token::Match(tok2, "[!<>=] =")) { - tok2->str(tok2->str() + "="); - tok2->deleteNext(); - } else if (tok2->str() == "(") - lpar.push(tok2); - else if (tok2->str() == ")") { - if (lpar.empty()) - return nullptr; - Token::createMutualLinks(lpar.top(), tok2); - lpar.pop(); - } - } - if (!lpar.empty()) - return nullptr; - - // set varids - for (Token* tok2 = tokenList->front(); tok2; tok2 = tok2->next()) { - if (!startsWith(tok2->str(), "arg")) - continue; - nonneg int const id = strToInt(tok2->str().c_str() + 3); - tok2->varId(id); - if (lookupVarId) - (*lookupVarId)[id] = tok2; - } - - // Evaluate expression - tokenList->createAst(); - Token* expr = tokenList->front()->astOperand1(); - ValueFlow::valueFlowConstantFoldAST(expr, settings); - return {tokenList, expr}; -} - const Library::AllocFunc* Library::getAllocFuncInfo(const char name[]) const { return getAllocDealloc(mAlloc, name); diff --git a/lib/library.h b/lib/library.h index 8909391da4a..da636ccc704 100644 --- a/lib/library.h +++ b/lib/library.h @@ -25,9 +25,9 @@ #include "mathlib.h" #include "standards.h" +#include #include #include -#include #include #include #include @@ -36,7 +36,6 @@ #include class Token; -class Settings; enum class Severity : std::uint8_t; namespace tinyxml2 { @@ -567,11 +566,6 @@ class CPPCHECKLIB Library { CPPCHECKLIB const Library::Container * getLibraryContainer(const Token * tok); -std::shared_ptr createTokenFromExpression(const std::string& returnValue, - const Settings& settings, - bool cpp, - std::unordered_map* lookupVarId = nullptr); - /// @} //--------------------------------------------------------------------------- #endif // libraryH diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index e8609cdae9c..da7770f65ce 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -24,8 +24,10 @@ #include "library.h" #include "mathlib.h" #include "settings.h" +#include "standards.h" #include "symboldatabase.h" #include "token.h" +#include "tokenlist.h" #include "utils.h" #include "valueflow.h" #include "valueptr.h" @@ -37,6 +39,8 @@ #include #include #include +#include +#include #include #include #include @@ -1760,6 +1764,54 @@ std::vector execute(const Scope* scope, ProgramMemory& pm, con return ex.execute(scope); } +static std::shared_ptr createTokenFromExpression(const std::string& returnValue, + const Settings& settings, + bool cpp, + std::unordered_map& lookupVarId) +{ + std::shared_ptr tokenList = std::make_shared(&settings); + { + const std::string code = "return " + returnValue + ";"; + std::istringstream istr(code); + if (!tokenList->createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C)) + return nullptr; + } + + // TODO: put in a helper? + // combine operators, set links, etc.. + std::stack lpar; + for (Token* tok2 = tokenList->front(); tok2; tok2 = tok2->next()) { + if (Token::Match(tok2, "[!<>=] =")) { + tok2->str(tok2->str() + "="); + tok2->deleteNext(); + } else if (tok2->str() == "(") + lpar.push(tok2); + else if (tok2->str() == ")") { + if (lpar.empty()) + return nullptr; + Token::createMutualLinks(lpar.top(), tok2); + lpar.pop(); + } + } + if (!lpar.empty()) + return nullptr; + + // set varids + for (Token* tok2 = tokenList->front(); tok2; tok2 = tok2->next()) { + if (!startsWith(tok2->str(), "arg")) + continue; + nonneg int const id = strToInt(tok2->str().c_str() + 3); + tok2->varId(id); + lookupVarId[id] = tok2; + } + + // Evaluate expression + tokenList->createAst(); + Token* expr = tokenList->front()->astOperand1(); + ValueFlow::valueFlowConstantFoldAST(expr, settings); + return {tokenList, expr}; +} + ValueFlow::Value evaluateLibraryFunction(const std::unordered_map& args, const std::string& returnValue, const Settings& settings, @@ -1771,7 +1823,7 @@ ValueFlow::Value evaluateLibraryFunction(const std::unordered_map lookupVarId; - std::shared_ptr expr = createTokenFromExpression(returnValue, settings, cpp, &lookupVarId); + std::shared_ptr expr = createTokenFromExpression(returnValue, settings, cpp, lookupVarId); functions[returnValue] = [lookupVarId, expr, settings](const std::unordered_map& xargs) { diff --git a/oss-fuzz/Makefile b/oss-fuzz/Makefile index 6262eca078a..30559784264 100644 --- a/oss-fuzz/Makefile +++ b/oss-fuzz/Makefile @@ -278,7 +278,7 @@ $(libcppdir)/infer.o: ../lib/infer.cpp ../lib/calculate.h ../lib/config.h ../lib $(libcppdir)/keywords.o: ../lib/keywords.cpp ../lib/config.h ../lib/keywords.h ../lib/standards.h ../lib/utils.h $(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/keywords.cpp -$(libcppdir)/library.o: ../lib/library.cpp ../externals/tinyxml2/tinyxml2.h ../lib/astutils.h ../lib/config.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/valueflow.h ../lib/vfvalue.h ../lib/xml.h +$(libcppdir)/library.o: ../lib/library.cpp ../externals/tinyxml2/tinyxml2.h ../lib/astutils.h ../lib/config.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h ../lib/xml.h $(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/library.cpp $(libcppdir)/mathlib.o: ../lib/mathlib.cpp ../externals/simplecpp/simplecpp.h ../lib/config.h ../lib/errortypes.h ../lib/mathlib.h ../lib/utils.h @@ -299,7 +299,7 @@ $(libcppdir)/platform.o: ../lib/platform.cpp ../externals/tinyxml2/tinyxml2.h .. $(libcppdir)/preprocessor.o: ../lib/preprocessor.cpp ../externals/simplecpp/simplecpp.h ../lib/addoninfo.h ../lib/color.h ../lib/config.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/platform.h ../lib/preprocessor.h ../lib/settings.h ../lib/standards.h ../lib/suppressions.h ../lib/utils.h $(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/preprocessor.cpp -$(libcppdir)/programmemory.o: ../lib/programmemory.cpp ../lib/addoninfo.h ../lib/astutils.h ../lib/calculate.h ../lib/config.h ../lib/errortypes.h ../lib/infer.h ../lib/library.h ../lib/mathlib.h ../lib/platform.h ../lib/programmemory.h ../lib/settings.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/suppressions.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/utils.h ../lib/valueflow.h ../lib/valueptr.h ../lib/vfvalue.h +$(libcppdir)/programmemory.o: ../lib/programmemory.cpp ../lib/addoninfo.h ../lib/astutils.h ../lib/calculate.h ../lib/config.h ../lib/errortypes.h ../lib/infer.h ../lib/library.h ../lib/mathlib.h ../lib/platform.h ../lib/programmemory.h ../lib/settings.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/suppressions.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/valueflow.h ../lib/valueptr.h ../lib/vfvalue.h $(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/programmemory.cpp $(libcppdir)/reverseanalyzer.o: ../lib/reverseanalyzer.cpp ../lib/addoninfo.h ../lib/analyzer.h ../lib/astutils.h ../lib/config.h ../lib/errortypes.h ../lib/forwardanalyzer.h ../lib/library.h ../lib/mathlib.h ../lib/platform.h ../lib/reverseanalyzer.h ../lib/settings.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/suppressions.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/utils.h ../lib/valueptr.h ../lib/vfvalue.h