From ad3ef65816e1d524ff83c631df90bd3febebac36 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 11 Dec 2021 15:14:32 +0100 Subject: [PATCH 1/6] fixed some unusedFunction warnings --- lib/astutils.cpp | 8 ------ lib/astutils.h | 2 -- lib/checkmemoryleak.cpp | 51 -------------------------------------- lib/checkmemoryleak.h | 3 --- lib/checkstl.cpp | 25 ------------------- lib/checkstl.h | 3 --- lib/forwardanalyzer.cpp | 4 --- lib/importproject.cpp | 10 -------- lib/importproject.h | 2 +- lib/library.cpp | 22 ---------------- lib/library.h | 1 - lib/preprocessor.cpp | 9 ------- lib/preprocessor.h | 6 ----- lib/programmemory.cpp | 10 -------- lib/programmemory.h | 1 - lib/symboldatabase.cpp | 12 +-------- lib/symboldatabase.h | 6 ----- lib/token.cpp | 48 ----------------------------------- lib/token.h | 7 ------ lib/valueflow.cpp | 7 ------ test/testcmdlineparser.cpp | 16 ++++++------ 21 files changed, 11 insertions(+), 242 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index eb11d28a1b5..9d3920b9f00 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -3270,14 +3270,6 @@ bool FwdAnalysis::unusedValue(const Token *expr, const Token *startToken, const return (result.type == FwdAnalysis::Result::Type::NONE || result.type == FwdAnalysis::Result::Type::RETURN) && !possiblyAliased(expr, startToken); } -std::vector FwdAnalysis::valueFlow(const Token *expr, const Token *startToken, const Token *endToken) -{ - mWhat = What::ValueFlow; - mValueFlowKnown = true; - check(expr, startToken, endToken); - return mValueFlow; -} - bool FwdAnalysis::possiblyAliased(const Token *expr, const Token *startToken) const { if (expr->isUnaryOp("*")) diff --git a/lib/astutils.h b/lib/astutils.h index 284eed92804..2a13a7386bb 100644 --- a/lib/astutils.h +++ b/lib/astutils.h @@ -363,8 +363,6 @@ class FwdAnalysis { const Token *token; }; - std::vector valueFlow(const Token *expr, const Token *startToken, const Token *endToken); - /** Is there some possible alias for given expression */ bool possiblyAliased(const Token *expr, const Token *startToken) const; diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 2245dee9955..158bcac0412 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -408,57 +408,6 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Function* f } -const char *CheckMemoryLeak::functionArgAlloc(const Function *func, nonneg int targetpar, AllocType &allocType) const -{ - allocType = No; - - if (!func || !func->functionScope) - return ""; - - if (!Token::simpleMatch(func->retDef, "void")) - return ""; - - std::list::const_iterator arg = func->argumentList.begin(); - for (; arg != func->argumentList.end(); ++arg) { - if (arg->index() == targetpar-1) - break; - } - if (arg == func->argumentList.end()) - return ""; - - // Is ** - if (!arg->isPointer()) - return ""; - const Token* tok = arg->typeEndToken(); - tok = tok->previous(); - if (tok->str() != "*") - return ""; - - // Check if pointer is allocated. - bool realloc = false; - for (tok = func->functionScope->bodyStart; tok && tok != func->functionScope->bodyEnd; tok = tok->next()) { - if (tok->varId() == arg->declarationId()) { - if (Token::Match(tok->tokAt(-3), "free ( * %name% )")) { - realloc = true; - allocType = No; - } else if (Token::Match(tok->previous(), "* %name% =")) { - allocType = getAllocationType(tok->tokAt(2), arg->declarationId()); - if (allocType != No) { - if (realloc) - return "realloc"; - return "alloc"; - } - } else { - // unhandled variable usage: bailout - return ""; - } - } - } - - return ""; -} - - static bool notvar(const Token *tok, nonneg int varid) { if (!tok) diff --git a/lib/checkmemoryleak.h b/lib/checkmemoryleak.h index a61f3a59851..6b3795ec80f 100644 --- a/lib/checkmemoryleak.h +++ b/lib/checkmemoryleak.h @@ -145,9 +145,6 @@ class CPPCHECKLIB CheckMemoryLeak { /** What type of allocated memory does the given function return? */ AllocType functionReturnType(const Function* func, std::list *callstack = nullptr) const; - - /** Function allocates pointed-to argument (a la asprintf)? */ - const char *functionArgAlloc(const Function *func, nonneg int targetpar, AllocType &allocType) const; }; /// @} diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 46ad2057a34..313f15c385f 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -2381,31 +2381,6 @@ void CheckStl::dereferenceInvalidIteratorError(const Token* deref, const std::st "Possible dereference of an invalid iterator: $symbol. Make sure to check that the iterator is valid before dereferencing it - not after.", CWE825, Certainty::normal); } - -void CheckStl::readingEmptyStlContainer2() -{ - for (const Scope *function : mTokenizer->getSymbolDatabase()->functionScopes) { - for (const Token *tok = function->bodyStart; tok != function->bodyEnd; tok = tok->next()) { - if (!tok->isName() || !tok->valueType()) - continue; - const Library::Container *container = tok->valueType()->container; - if (!container) - continue; - const ValueFlow::Value *value = tok->getContainerSizeValue(0); - if (!value) - continue; - if (value->isInconclusive() && !mSettings->certainty.isEnabled(Certainty::inconclusive)) - continue; - if (!value->errorSeverity() && !mSettings->severity.isEnabled(Severity::warning)) - continue; - if (Token::Match(tok, "%name% . %name% (")) { - if (container->getYield(tok->strAt(2)) == Library::Container::Yield::ITEM) - readingEmptyStlContainerError(tok,value); - } - } - } -} - void CheckStl::readingEmptyStlContainerError(const Token *tok, const ValueFlow::Value *value) { const std::string varname = tok ? tok->str() : std::string("var"); diff --git a/lib/checkstl.h b/lib/checkstl.h index c26c74f2b9f..33b40e3b8d8 100644 --- a/lib/checkstl.h +++ b/lib/checkstl.h @@ -178,9 +178,6 @@ class CPPCHECKLIB CheckStl : public Check { */ void dereferenceErasedError(const Token* erased, const Token* deref, const std::string& itername, bool inconclusive); - /** @brief Reading from empty stl container (using valueflow) */ - void readingEmptyStlContainer2(); - /** @brief Look for loops that can replaced with std algorithms */ void useStlAlgorithm(); diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index 67c48347d2e..1d8058f163b 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -89,10 +89,6 @@ struct ForwardTraversal { return evalCond(tok, ctx).first; } - bool isConditionFalse(const Token* tok, const Token* ctx = nullptr) const { - return evalCond(tok, ctx).second; - } - template )> Progress traverseTok(T* tok, F f, bool traverseUnknown, T** out = nullptr) { if (Token::Match(tok, "asm|goto|setjmp|longjmp")) diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 79e3ced69be..68036e197f2 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -76,16 +76,6 @@ void ImportProject::ignoreOtherConfigs(const std::string &cfg) } } -void ImportProject::ignoreOtherPlatforms(cppcheck::Platform::PlatformType platformType) -{ - for (std::list::iterator it = fileSettings.begin(); it != fileSettings.end();) { - if (it->platformType != cppcheck::Platform::Unspecified && it->platformType != platformType) - fileSettings.erase(it++); - else - ++it; - } -} - void ImportProject::FileSettings::setDefines(std::string defs) { while (defs.find(";%(") != std::string::npos) { diff --git a/lib/importproject.h b/lib/importproject.h index 6a6fa4173f7..e02f77effd1 100644 --- a/lib/importproject.h +++ b/lib/importproject.h @@ -103,7 +103,7 @@ class CPPCHECKLIB ImportProject { void ignorePaths(const std::vector &ipaths); void ignoreOtherConfigs(const std::string &cfg); - void ignoreOtherPlatforms(cppcheck::Platform::PlatformType platformType); + Type import(const std::string &filename, Settings *settings=nullptr); protected: diff --git a/lib/library.cpp b/lib/library.cpp index 80694185388..36fb2a5ddcc 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -1577,28 +1577,6 @@ const Token* Library::getContainerFromYield(const Token* tok, Library::Container } return nullptr; } -const Token* Library::getContainerFromAction(const Token* tok, Library::Container::Action action) const -{ - if (!tok) - return nullptr; - if (Token::Match(tok->tokAt(-2), ". %name% (")) { - const Token* containerTok = tok->tokAt(-2)->astOperand1(); - if (!astIsContainer(containerTok)) - return nullptr; - if (containerTok->valueType()->container && - containerTok->valueType()->container->getAction(tok->strAt(-1)) == action) - return containerTok; - if (Token::simpleMatch(tok->tokAt(-1), "empty ( )")) - return containerTok; - } else if (Token::Match(tok->previous(), "%name% (")) { - if (const Library::Function* f = this->getFunction(tok->previous())) { - if (f->containerAction == action) { - return tok->astOperand2(); - } - } - } - return nullptr; -} bool Library::isSmartPointer(const Token* tok) const { diff --git a/lib/library.h b/lib/library.h index 5aed161c847..f1555ed1f3f 100644 --- a/lib/library.h +++ b/lib/library.h @@ -459,7 +459,6 @@ class CPPCHECKLIB Library { bool isimporter(const std::string& file, const std::string &importer) const; const Token* getContainerFromYield(const Token* tok, Container::Yield yield) const; - const Token* getContainerFromAction(const Token* tok, Container::Action action) const; bool isreflection(const std::string &token) const { return mReflection.find(token) != mReflection.end(); diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 79b5e3e6b4d..3954ba4af46 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -580,15 +580,6 @@ std::string Preprocessor::removeSpaceNearNL(const std::string &str) return tmp; } -void Preprocessor::preprocessWhitespaces(std::string &processedFile) -{ - // Replace all tabs with spaces.. - std::replace(processedFile.begin(), processedFile.end(), '\t', ' '); - - // Remove space characters that are after or before new line character - processedFile = removeSpaceNearNL(processedFile); -} - void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processedFile, std::list &resultConfigurations, const std::string &filename, const std::list &includePaths) { (void)includePaths; diff --git a/lib/preprocessor.h b/lib/preprocessor.h index 59f759b4cbf..cae01c57c6a 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -154,12 +154,6 @@ class CPPCHECKLIB Preprocessor { */ std::string getcode(const std::string &filedata, const std::string &cfg, const std::string &filename); - /** - * preprocess all whitespaces - * @param processedFile The data to be processed - */ - static void preprocessWhitespaces(std::string &processedFile); - /** * make sure empty configuration macros are not used in code. the given code must be a single configuration * @param cfg configuration diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 1dd612fb0ef..a2b6e9cdaf6 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -28,16 +28,6 @@ const ValueFlow::Value* ProgramMemory::getValue(nonneg int exprid, bool impossib return nullptr; } -bool ProgramMemory::getIntValue(nonneg int exprid, MathLib::bigint* result) const -{ - const ValueFlow::Value* value = getValue(exprid); - if (value && value->isIntValue()) { - *result = value->intvalue; - return true; - } - return false; -} - void ProgramMemory::setIntValue(nonneg int exprid, MathLib::bigint value, bool impossible) { ValueFlow::Value v(value); diff --git a/lib/programmemory.h b/lib/programmemory.h index 5dfe5b15647..ab217273b71 100644 --- a/lib/programmemory.h +++ b/lib/programmemory.h @@ -17,7 +17,6 @@ struct ProgramMemory { void setValue(nonneg int exprid, const ValueFlow::Value& value); const ValueFlow::Value* getValue(nonneg int exprid, bool impossible = false) const; - bool getIntValue(nonneg int exprid, MathLib::bigint* result) const; void setIntValue(nonneg int exprid, MathLib::bigint value, bool impossible = false); bool getContainerSizeValue(nonneg int exprid, MathLib::bigint* result) const; diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index cb6d9ecdbd4..deb7af0a40d 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1840,6 +1840,7 @@ void SymbolDatabase::validate() const if (mSettings->debugwarnings) { validateExecutableScopes(); } + // TODO //validateVariables(); } @@ -5329,17 +5330,6 @@ const Scope *SymbolDatabase::findScopeByName(const std::string& name) const //--------------------------------------------------------------------------- -Scope *Scope::findInNestedList(const std::string & name) -{ - for (Scope *scope: nestedList) { - if (scope->className == name) - return scope; - } - return nullptr; -} - -//--------------------------------------------------------------------------- - const Scope *Scope::findRecordInNestedList(const std::string & name) const { for (const Scope* scope: nestedList) { diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index e46b38c8ced..e7762a41e57 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -1128,12 +1128,6 @@ class CPPCHECKLIB Scope { */ const Function *findFunction(const Token *tok, bool requireConst=false) const; - /** - * @brief find if name is in nested list - * @param name name of nested scope - */ - Scope *findInNestedList(const std::string & name); - const Scope *findRecordInNestedList(const std::string & name) const; Scope *findRecordInNestedList(const std::string & name) { return const_cast(const_cast(this)->findRecordInNestedList(name)); diff --git a/lib/token.cpp b/lib/token.cpp index a36a4a64f81..bffcd1acd0c 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1185,14 +1185,6 @@ void Token::printOut(const char *title, const std::vector &fileName std::cout << stringifyList(stringifyOptions::forPrintOut(), &fileNames, nullptr) << std::endl; } -void Token::printLines(int lines) const -{ - const Token *end = this; - while (end && end->linenr() < lines + linenr()) - end = end->next(); - std::cout << stringifyList(stringifyOptions::forDebugExprId(), nullptr, end) << std::endl; -} - std::string Token::stringify(const stringifyOptions& options) const { std::string ret; @@ -1915,46 +1907,6 @@ const Token *Token::getValueTokenMaxStrLength() const return ret; } -static const Scope *getfunctionscope(const Scope *s) -{ - while (s && s->type != Scope::eFunction) - s = s->nestedIn; - return s; -} - -const Token *Token::getValueTokenDeadPointer() const -{ - const Scope * const functionscope = getfunctionscope(this->scope()); - - std::list::const_iterator it; - for (it = values().begin(); it != values().end(); ++it) { - // Is this a pointer alias? - if (!it->isTokValue() || (it->tokvalue && it->tokvalue->str() != "&")) - continue; - // Get variable - const Token *vartok = it->tokvalue->astOperand1(); - if (!vartok || !vartok->isName() || !vartok->variable()) - continue; - const Variable * const var = vartok->variable(); - if (var->isStatic() || var->isReference()) - continue; - if (!var->scope()) - return nullptr; // #6804 - if (var->scope()->type == Scope::eUnion && var->scope()->nestedIn == this->scope()) - continue; - // variable must be in same function (not in subfunction) - if (functionscope != getfunctionscope(var->scope())) - continue; - // Is variable defined in this scope or upper scope? - const Scope *s = this->scope(); - while ((s != nullptr) && (s != var->scope())) - s = s->nestedIn; - if (!s) - return it->tokvalue; - } - return nullptr; -} - static bool isAdjacent(const ValueFlow::Value& x, const ValueFlow::Value& y) { if (x.bound != ValueFlow::Value::Bound::Point && x.bound == y.bound) diff --git a/lib/token.h b/lib/token.h index 23b16c87165..f7275a79406 100644 --- a/lib/token.h +++ b/lib/token.h @@ -862,11 +862,6 @@ class CPPCHECKLIB Token { */ void printOut(const char *title, const std::vector &fileNames) const; - /** - * print out tokens - */ - void printLines(int lines=5) const; - /** * Replace token replaceThis with tokens between start and end, * including start and end. The replaceThis token is deleted. @@ -1171,8 +1166,6 @@ class CPPCHECKLIB Token { const Token *getValueTokenMaxStrLength() const; const Token *getValueTokenMinStrSize(const Settings *settings) const; - const Token *getValueTokenDeadPointer() const; - /** Add token value. Return true if value is added. */ bool addValue(const ValueFlow::Value &value); diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index f0f5a2409b7..d2ed087bbbc 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1966,9 +1966,6 @@ struct ValueFlowAnalyzer : Analyzer { virtual ProgramState getProgramState() const = 0; - virtual const ValueType* getValueType(const Token*) const { - return nullptr; - } virtual int getIndirect(const Token* tok) const { const ValueFlow::Value* value = getValue(tok); if (value) @@ -2640,10 +2637,6 @@ struct ExpressionAnalyzer : SingleValueFlowAnalyzer { setupExprVarIds(val.tokvalue); } - virtual const ValueType* getValueType(const Token*) const OVERRIDE { - return expr->valueType(); - } - static bool nonLocal(const Variable* var, bool deref) { return !var || (!var->isLocal() && !var->isArgument()) || (deref && var->isArgument() && var->isPointer()) || var->isStatic() || var->isReference() || var->isExtern(); diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 546c9dd0e8f..eb56b123dec 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -130,6 +130,7 @@ class TestCmdlineParser : public TestFixture { TEST_CASE(errorlistverbose2); TEST_CASE(ignorepathsnopath); + // TODO // Disabling these tests since they use relative paths to the // testrunner executable. //TEST_CASE(ignorepaths1); @@ -974,8 +975,8 @@ class TestCmdlineParser : public TestFixture { ASSERT_EQUALS("src/", parser.getIgnoredPaths()[0]); ASSERT_EQUALS("module/", parser.getIgnoredPaths()[1]); } - */ - void ignorepaths4() { + + void ignorepaths4() { REDIRECT; const char * const argv[] = {"cppcheck", "-i", "src", "-i", "module", "file.cpp"}; CmdLineParser parser(&settings); @@ -983,8 +984,8 @@ class TestCmdlineParser : public TestFixture { ASSERT_EQUALS(2, parser.getIgnoredPaths().size()); ASSERT_EQUALS("src/", parser.getIgnoredPaths()[0]); ASSERT_EQUALS("module/", parser.getIgnoredPaths()[1]); - } - /* + } + void ignorefilepaths1() { REDIRECT; const char * const argv[] = {"cppcheck", "-ifoo.cpp", "file.cpp"}; @@ -993,15 +994,16 @@ class TestCmdlineParser : public TestFixture { ASSERT_EQUALS(1, parser.getIgnoredPaths().size()); ASSERT_EQUALS("foo.cpp", parser.getIgnoredPaths()[0]); } - */ - void ignorefilepaths2() { + + void ignorefilepaths2() { REDIRECT; const char * const argv[] = {"cppcheck", "-isrc/foo.cpp", "file.cpp"}; CmdLineParser parser(&settings); ASSERT(parser.parseFromArgs(3, argv)); ASSERT_EQUALS(1, parser.getIgnoredPaths().size()); ASSERT_EQUALS("src/foo.cpp", parser.getIgnoredPaths()[0]); - } + } + */ void checkconfig() { REDIRECT; From 07d897060fe4cb3740c914565c874168061683d9 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 11 Dec 2021 19:36:08 +0100 Subject: [PATCH 2/6] fixed unusedPrivateFunction warning --- lib/preprocessor.cpp | 21 --------------------- lib/preprocessor.h | 8 -------- 2 files changed, 29 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 3954ba4af46..a8223bf917e 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -559,27 +559,6 @@ void Preprocessor::preprocess(std::istream &istr, std::map= str.size() || // treat end of file as newline - str[i+1] == '\n' - ) - ) { - // Ignore space that has new line in either side of it - } else { - tmp.append(1, str[i]); - prev = str[i]; - } - } - - return tmp; -} - void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processedFile, std::list &resultConfigurations, const std::string &filename, const std::list &includePaths) { (void)includePaths; diff --git a/lib/preprocessor.h b/lib/preprocessor.h index cae01c57c6a..3a8d8e33097 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -178,14 +178,6 @@ class CPPCHECKLIB Preprocessor { static void simplifyPragmaAsmPrivate(simplecpp::TokenList *tokenList); - /** - * Remove space that has new line character on left or right side of it. - * - * @param str The string to be converted - * @return The string where space characters have been removed. - */ - static std::string removeSpaceNearNL(const std::string &str); - public: From 0477731c7fa8f1255676a65b95baf6d4ab9e9006 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 18 Dec 2021 12:17:13 +0100 Subject: [PATCH 3/6] fixed some more unusedFunction warnings --- gui/applicationlist.cpp | 2 ++ gui/applicationlist.h | 2 ++ lib/programmemory.cpp | 9 --------- lib/programmemory.h | 1 - lib/token.cpp | 10 ---------- lib/token.h | 2 -- 6 files changed, 4 insertions(+), 22 deletions(-) diff --git a/gui/applicationlist.cpp b/gui/applicationlist.cpp index 022efa0ef65..38414fdf6f4 100644 --- a/gui/applicationlist.cpp +++ b/gui/applicationlist.cpp @@ -198,6 +198,7 @@ bool ApplicationList::checkAndAddApplication(const QString& appPath, const QStri return false; } +#ifdef _WIN32 bool ApplicationList::findDefaultWindowsEditor() { bool foundOne = false; @@ -264,3 +265,4 @@ bool ApplicationList::findDefaultWindowsEditor() return foundOne; } +#endif diff --git a/gui/applicationlist.h b/gui/applicationlist.h index d2249e2abc6..189ea62aafd 100644 --- a/gui/applicationlist.h +++ b/gui/applicationlist.h @@ -108,11 +108,13 @@ class ApplicationList : public QObject { */ void clear(); +#ifdef _WIN32 /** * @brief Find editor used by default in Windows. * Check if Notepad++ is installed and use it. If not, use Notepad. */ bool findDefaultWindowsEditor(); +#endif private: diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index a2b6e9cdaf6..ce48342aa05 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -46,15 +46,6 @@ bool ProgramMemory::getTokValue(nonneg int exprid, const Token** result) const return false; } -bool ProgramMemory::getContainerSizeValue(nonneg int exprid, MathLib::bigint* result) const -{ - const ValueFlow::Value* value = getValue(exprid); - if (value && value->isContainerSizeValue()) { - *result = value->intvalue; - return true; - } - return false; -} bool ProgramMemory::getContainerEmptyValue(nonneg int exprid, MathLib::bigint* result) const { const ValueFlow::Value* value = getValue(exprid, true); diff --git a/lib/programmemory.h b/lib/programmemory.h index ab217273b71..1964e4caa46 100644 --- a/lib/programmemory.h +++ b/lib/programmemory.h @@ -19,7 +19,6 @@ struct ProgramMemory { void setIntValue(nonneg int exprid, MathLib::bigint value, bool impossible = false); - bool getContainerSizeValue(nonneg int exprid, MathLib::bigint* result) const; bool getContainerEmptyValue(nonneg int exprid, MathLib::bigint* result) const; void setContainerSizeValue(nonneg int exprid, MathLib::bigint value, bool isEqual = true); diff --git a/lib/token.cpp b/lib/token.cpp index bffcd1acd0c..99b115d2244 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -2435,16 +2435,6 @@ const ValueFlow::Value* Token::getMovedValue() const return it == mImpl->mValues->end() ? nullptr : &*it; } -const ValueFlow::Value* Token::getContainerSizeValue(const MathLib::bigint val) const -{ - if (!mImpl->mValues) - return nullptr; - const auto it = std::find_if(mImpl->mValues->begin(), mImpl->mValues->end(), [=](const ValueFlow::Value& value) { - return value.isContainerSizeValue() && !value.isImpossible() && value.intvalue == val; - }); - return it == mImpl->mValues->end() ? nullptr : &*it; -} - TokenImpl::~TokenImpl() { delete mOriginalName; diff --git a/lib/token.h b/lib/token.h index f7275a79406..e5cf5ef7110 100644 --- a/lib/token.h +++ b/lib/token.h @@ -1161,8 +1161,6 @@ class CPPCHECKLIB Token { const ValueFlow::Value * getInvalidValue(const Token *ftok, nonneg int argnr, const Settings *settings) const; - const ValueFlow::Value* getContainerSizeValue(const MathLib::bigint val) const; - const Token *getValueTokenMaxStrLength() const; const Token *getValueTokenMinStrSize(const Settings *settings) const; From 92cc469bf8419398254b766413c22f0c7353dc1e Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 18 Dec 2021 12:17:41 +0100 Subject: [PATCH 4/6] added missing override keywords --- gui/mainwindow.h | 2 +- gui/resultstree.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 4710de8166e..d10836c0de8 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -268,7 +268,7 @@ protected slots: void setLanguage(const QString &code); /** @brief Event coming when application is about to close. */ - virtual void closeEvent(QCloseEvent *event); + void closeEvent(QCloseEvent *event) override; /** * @brief Helper function to toggle all show error menu items diff --git a/gui/resultstree.h b/gui/resultstree.h index 0e32f4944eb..12114485d1e 100644 --- a/gui/resultstree.h +++ b/gui/resultstree.h @@ -177,7 +177,7 @@ class ResultsTree : public QTreeView { */ ShowTypes mShowSeverities; - virtual void keyPressEvent(QKeyEvent *event); + void keyPressEvent(QKeyEvent *event) override; signals: /** @@ -294,7 +294,7 @@ protected slots: * @param current Model index to specify new selected item. * @param previous Model index to specify previous selected item. */ - virtual void currentChanged(const QModelIndex ¤t, const QModelIndex &previous); + void currentChanged(const QModelIndex ¤t, const QModelIndex &previous) override; protected: @@ -365,7 +365,7 @@ protected slots: * * @param e Event */ - void contextMenuEvent(QContextMenuEvent * e); + void contextMenuEvent(QContextMenuEvent * e) override; /** * @brief Add a new error item beneath a file or a backtrace item beneath an error From 76935795d302af6a803beb48d77fe366fd49a35b Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 27 Dec 2021 15:40:25 +0100 Subject: [PATCH 5/6] restored some deleted code --- lib/forwardanalyzer.cpp | 4 ++++ lib/importproject.h | 1 - lib/library.cpp | 22 ++++++++++++++++++++++ lib/library.h | 1 + lib/programmemory.cpp | 19 +++++++++++++++++++ lib/programmemory.h | 2 ++ lib/token.cpp | 8 ++++++++ lib/token.h | 5 +++++ 8 files changed, 61 insertions(+), 1 deletion(-) diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index 1d8058f163b..67c48347d2e 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -89,6 +89,10 @@ struct ForwardTraversal { return evalCond(tok, ctx).first; } + bool isConditionFalse(const Token* tok, const Token* ctx = nullptr) const { + return evalCond(tok, ctx).second; + } + template )> Progress traverseTok(T* tok, F f, bool traverseUnknown, T** out = nullptr) { if (Token::Match(tok, "asm|goto|setjmp|longjmp")) diff --git a/lib/importproject.h b/lib/importproject.h index e02f77effd1..faf63fa4099 100644 --- a/lib/importproject.h +++ b/lib/importproject.h @@ -104,7 +104,6 @@ class CPPCHECKLIB ImportProject { void ignorePaths(const std::vector &ipaths); void ignoreOtherConfigs(const std::string &cfg); - Type import(const std::string &filename, Settings *settings=nullptr); protected: bool importCompileCommands(std::istream &istr); diff --git a/lib/library.cpp b/lib/library.cpp index 36fb2a5ddcc..80694185388 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -1577,6 +1577,28 @@ const Token* Library::getContainerFromYield(const Token* tok, Library::Container } return nullptr; } +const Token* Library::getContainerFromAction(const Token* tok, Library::Container::Action action) const +{ + if (!tok) + return nullptr; + if (Token::Match(tok->tokAt(-2), ". %name% (")) { + const Token* containerTok = tok->tokAt(-2)->astOperand1(); + if (!astIsContainer(containerTok)) + return nullptr; + if (containerTok->valueType()->container && + containerTok->valueType()->container->getAction(tok->strAt(-1)) == action) + return containerTok; + if (Token::simpleMatch(tok->tokAt(-1), "empty ( )")) + return containerTok; + } else if (Token::Match(tok->previous(), "%name% (")) { + if (const Library::Function* f = this->getFunction(tok->previous())) { + if (f->containerAction == action) { + return tok->astOperand2(); + } + } + } + return nullptr; +} bool Library::isSmartPointer(const Token* tok) const { diff --git a/lib/library.h b/lib/library.h index f1555ed1f3f..5aed161c847 100644 --- a/lib/library.h +++ b/lib/library.h @@ -459,6 +459,7 @@ class CPPCHECKLIB Library { bool isimporter(const std::string& file, const std::string &importer) const; const Token* getContainerFromYield(const Token* tok, Container::Yield yield) const; + const Token* getContainerFromAction(const Token* tok, Container::Action action) const; bool isreflection(const std::string &token) const { return mReflection.find(token) != mReflection.end(); diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index ce48342aa05..1dd612fb0ef 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -28,6 +28,16 @@ const ValueFlow::Value* ProgramMemory::getValue(nonneg int exprid, bool impossib return nullptr; } +bool ProgramMemory::getIntValue(nonneg int exprid, MathLib::bigint* result) const +{ + const ValueFlow::Value* value = getValue(exprid); + if (value && value->isIntValue()) { + *result = value->intvalue; + return true; + } + return false; +} + void ProgramMemory::setIntValue(nonneg int exprid, MathLib::bigint value, bool impossible) { ValueFlow::Value v(value); @@ -46,6 +56,15 @@ bool ProgramMemory::getTokValue(nonneg int exprid, const Token** result) const return false; } +bool ProgramMemory::getContainerSizeValue(nonneg int exprid, MathLib::bigint* result) const +{ + const ValueFlow::Value* value = getValue(exprid); + if (value && value->isContainerSizeValue()) { + *result = value->intvalue; + return true; + } + return false; +} bool ProgramMemory::getContainerEmptyValue(nonneg int exprid, MathLib::bigint* result) const { const ValueFlow::Value* value = getValue(exprid, true); diff --git a/lib/programmemory.h b/lib/programmemory.h index 1964e4caa46..5dfe5b15647 100644 --- a/lib/programmemory.h +++ b/lib/programmemory.h @@ -17,8 +17,10 @@ struct ProgramMemory { void setValue(nonneg int exprid, const ValueFlow::Value& value); const ValueFlow::Value* getValue(nonneg int exprid, bool impossible = false) const; + bool getIntValue(nonneg int exprid, MathLib::bigint* result) const; void setIntValue(nonneg int exprid, MathLib::bigint value, bool impossible = false); + bool getContainerSizeValue(nonneg int exprid, MathLib::bigint* result) const; bool getContainerEmptyValue(nonneg int exprid, MathLib::bigint* result) const; void setContainerSizeValue(nonneg int exprid, MathLib::bigint value, bool isEqual = true); diff --git a/lib/token.cpp b/lib/token.cpp index 99b115d2244..c33907391c7 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1185,6 +1185,14 @@ void Token::printOut(const char *title, const std::vector &fileName std::cout << stringifyList(stringifyOptions::forPrintOut(), &fileNames, nullptr) << std::endl; } +void Token::printLines(int lines) const +{ + const Token *end = this; + while (end && end->linenr() < lines + linenr()) + end = end->next(); + std::cout << stringifyList(stringifyOptions::forDebugExprId(), nullptr, end) << std::endl; +} + std::string Token::stringify(const stringifyOptions& options) const { std::string ret; diff --git a/lib/token.h b/lib/token.h index e5cf5ef7110..487814b140c 100644 --- a/lib/token.h +++ b/lib/token.h @@ -862,6 +862,11 @@ class CPPCHECKLIB Token { */ void printOut(const char *title, const std::vector &fileNames) const; + /** + * print out tokens - used for debugging + */ + void printLines(int lines=5) const; + /** * Replace token replaceThis with tokens between start and end, * including start and end. The replaceThis token is deleted. From 038f657045da6973d4c03adfbe55200aa3e37799 Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 27 Dec 2021 16:03:31 +0100 Subject: [PATCH 6/6] restored some deleted code --- lib/token.cpp | 10 ++++++++++ lib/token.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/lib/token.cpp b/lib/token.cpp index c33907391c7..ea072b08bd4 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -2443,6 +2443,16 @@ const ValueFlow::Value* Token::getMovedValue() const return it == mImpl->mValues->end() ? nullptr : &*it; } +const ValueFlow::Value* Token::getContainerSizeValue(const MathLib::bigint val) const +{ + if (!mImpl->mValues) + return nullptr; + const auto it = std::find_if(mImpl->mValues->begin(), mImpl->mValues->end(), [=](const ValueFlow::Value& value) { + return value.isContainerSizeValue() && !value.isImpossible() && value.intvalue == val; + }); + return it == mImpl->mValues->end() ? nullptr : &*it; +} + TokenImpl::~TokenImpl() { delete mOriginalName; diff --git a/lib/token.h b/lib/token.h index 487814b140c..ba23cba17ec 100644 --- a/lib/token.h +++ b/lib/token.h @@ -1166,6 +1166,8 @@ class CPPCHECKLIB Token { const ValueFlow::Value * getInvalidValue(const Token *ftok, nonneg int argnr, const Settings *settings) const; + const ValueFlow::Value* getContainerSizeValue(const MathLib::bigint val) const; + const Token *getValueTokenMaxStrLength() const; const Token *getValueTokenMinStrSize(const Settings *settings) const;