cppcheck/lib/checkuninitvar.h

161 lines
6.5 KiB
C
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
2023-01-28 10:16:34 +01:00
* Copyright (C) 2007-2023 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
#ifndef checkuninitvarH
#define checkuninitvarH
//---------------------------------------------------------------------------
#include "check.h"
2017-05-27 04:33:47 +02:00
#include "config.h"
2018-12-25 21:11:23 +01:00
#include "ctu.h"
#include "mathlib.h"
2020-05-23 07:16:49 +02:00
#include "errortypes.h"
#include "vfvalue.h"
2017-05-27 04:33:47 +02:00
#include <list>
#include <map>
2017-05-27 04:33:47 +02:00
#include <set>
#include <string>
class Scope;
2017-05-27 04:33:47 +02:00
class Token;
class Tokenizer;
class Variable;
2020-05-23 07:16:49 +02:00
class ErrorLogger;
class Settings;
class Library;
namespace tinyxml2 {
class XMLElement;
}
struct VariableValue {
explicit VariableValue(MathLib::bigint val = 0) : value(val) {}
MathLib::bigint value;
bool notEqual{};
};
/// @addtogroup Checks
/// @{
/** @brief Checking for uninitialized variables */
class CPPCHECKLIB CheckUninitVar : public Check {
public:
/** @brief This constructor is used when registering the CheckUninitVar */
2021-08-07 20:51:18 +02:00
CheckUninitVar() : Check(myName()) {}
/** @brief This constructor is used when running checks. */
CheckUninitVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
2021-08-07 20:51:18 +02:00
: Check(myName(), tokenizer, settings, errorLogger) {}
/** @brief Run checks against the normal token list */
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) override {
CheckUninitVar checkUninitVar(tokenizer, settings, errorLogger);
checkUninitVar.valueFlowUninit();
checkUninitVar.check();
}
bool diag(const Token* tok);
/** Check for uninitialized variables */
void check();
void checkScope(const Scope* scope, const std::set<std::string> &arrayTypeDefs);
void checkStruct(const Token *tok, const Variable &structvar);
enum Alloc { NO_ALLOC, NO_CTOR_CALL, CTOR_CALL, ARRAY };
bool checkScopeForVariable(const Token *tok, const Variable& var, bool* const possibleInit, bool* const noreturn, Alloc* const alloc, const std::string &membervar, std::map<nonneg int, VariableValue> variableValue);
const Token* checkExpr(const Token* tok, const Variable& var, const Alloc alloc, bool known, bool* bailout = nullptr) const;
2015-01-23 19:38:39 +01:00
bool checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, Alloc alloc, const std::string &membervar);
bool checkLoopBody(const Token *tok, const Variable& var, const Alloc alloc, const std::string &membervar, const bool suppressErrors);
const Token* checkLoopBodyRecursive(const Token *start, const Variable& var, const Alloc alloc, const std::string &membervar, bool &bailout) const;
void checkRhs(const Token *tok, const Variable &var, Alloc alloc, nonneg int number_of_if, const std::string &membervar);
static const Token *isVariableUsage(bool cpp, const Token *vartok, const Library &library, bool pointer, Alloc alloc, int indirect = 0);
const Token *isVariableUsage(const Token *vartok, bool pointer, Alloc alloc, int indirect = 0) const;
static int isFunctionParUsage(const Token *vartok, const Library &library, bool pointer, Alloc alloc, int indirect = 0);
int isFunctionParUsage(const Token *vartok, bool pointer, Alloc alloc, int indirect = 0) const;
bool isMemberVariableAssignment(const Token *tok, const std::string &membervar) const;
2015-01-23 19:38:39 +01:00
bool isMemberVariableUsage(const Token *tok, bool isPointer, Alloc alloc, const std::string &membervar) const;
/** ValueFlow-based checking for uninitialized variables */
void valueFlowUninit();
/* data for multifile checking */
class MyFileInfo : public Check::FileInfo {
public:
/** function arguments that data are unconditionally read */
2018-12-25 21:11:23 +01:00
std::list<CTU::FileInfo::UnsafeUsage> unsafeUsage;
/** Convert MyFileInfo data into xml string */
std::string toString() const override;
};
/** @brief Parse current TU and extract file info */
Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const override;
Check::FileInfo * loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const override;
/** @brief Analyse all file infos for all TU */
bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger) override;
void uninitvarError(const Token* tok, const ValueFlow::Value& v);
void uninitdataError(const Token *tok, const std::string &varname);
void uninitvarError(const Token *tok, const std::string &varname, ErrorPath errorPath);
2019-08-15 10:46:16 +02:00
void uninitvarError(const Token *tok, const std::string &varname) {
ErrorPath errorPath;
uninitvarError(tok, varname, errorPath);
}
void uninitvarError(const Token *tok, const std::string &varname, Alloc alloc) {
if (alloc == NO_CTOR_CALL || alloc == CTOR_CALL)
uninitdataError(tok, varname);
else
uninitvarError(tok, varname);
}
void uninitStructMemberError(const Token *tok, const std::string &membername);
private:
std::set<const Token*> mUninitDiags;
Check::FileInfo* getFileInfo() const;
void getErrorMessages(ErrorLogger* errorLogger, const Settings* settings) const override
{
CheckUninitVar c(nullptr, settings, errorLogger);
ValueFlow::Value v{};
c.uninitvarError(nullptr, v);
c.uninitdataError(nullptr, "varname");
c.uninitStructMemberError(nullptr, "a.b");
}
2014-11-20 14:20:09 +01:00
static std::string myName() {
return "Uninitialized variables";
}
std::string classInfo() const override {
return "Uninitialized variables\n"
"- using uninitialized local variables\n"
"- using allocated data before it has been initialized\n";
}
};
/// @}
//---------------------------------------------------------------------------
#endif // checkuninitvarH