21 #ifndef checkleakautovarH
22 #define checkleakautovarH
41 enum AllocStatus { REALLOC = -3, OWNED = -2, DEALLOC = -1, NOALLOC = 0, ALLOC = 1 };
49 int reallocedFromType = -1;
51 explicit AllocInfo(
int type_ = 0,
AllocStatus status_ = NOALLOC,
const Token* allocTok_ =
nullptr) : status(status_), type(type_), allocTok(allocTok_) {}
65 possibleUsage.clear();
66 conditionalAlloc.clear();
71 alloctype.erase(varid);
72 possibleUsage.erase(varid);
73 conditionalAlloc.erase(varid);
74 referenced.erase(varid);
77 void swap(VarInfo &other) {
78 alloctype.swap(other.alloctype);
79 possibleUsage.swap(other.possibleUsage);
80 conditionalAlloc.swap(other.conditionalAlloc);
81 referenced.swap(other.referenced);
85 const AllocInfo& alloc = alloctype[varid];
87 const std::map<int, VarInfo::AllocInfo>::iterator it = alloctype.find(alloc.
reallocedFromType);
88 if (it != alloctype.end() && it->second.status == REALLOC) {
95 void possibleUsageAll(
const std::pair<const Token*, Usage>& functionUsage);
116 :
Check(myName(), tokenizer, settings, errorLogger) {}
120 checkLeakAutoVar.
check();
127 bool checkScope(
const Token *
const startToken,
129 std::set<int> notzero,
130 nonneg int recursiveCount);
137 const Token * checkTokenInsideExpression(
const Token *
const tok, VarInfo &varInfo,
bool inFuncCall =
false);
146 void changeAllocStatusIfRealloc(std::map<int, VarInfo::AllocInfo> &alloctype,
const Token *fTok,
const Token *retTok)
const;
149 void ret(
const Token *tok, VarInfo &varInfo,
const bool isEndOfScope =
false);
152 void leakIfAllocated(
const Token *vartok,
const VarInfo &varInfo);
154 void leakError(
const Token* tok,
const std::string &varname,
int type)
const;
155 void mismatchError(
const Token* deallocTok,
const Token* allocTok,
const std::string &varname)
const;
156 void deallocUseError(
const Token *tok,
const std::string &varname)
const;
157 void deallocReturnError(
const Token *tok,
const Token *deallocTok,
const std::string &varname);
158 void doubleFreeError(
const Token *tok,
const Token *prevFreeTok,
const std::string &varname,
int type);
161 void configurationInfo(
const Token* tok,
const std::pair<const Token*, VarInfo::Usage>& functionUsage);
171 return "Leaks (auto variables)";
175 return "Detect when a auto variable is allocated but not deallocated or deallocated twice.\n";
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
void configurationInfo(const Token *tok, const std::pair< const Token *, VarInfo::Usage > &functionUsage)
message: user configuration is needed to complete analysis
std::string classInfo() const override
get information about this class, used to generate documentation
static std::string myName()
CheckLeakAutoVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
void check()
check for leaks in all scopes
CheckLeakAutoVar()
This constructor is used when registering the CheckLeakAutoVar.
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
run checks, the token list is not simplified
void doubleFreeError(const Token *tok, const Token *prevFreeTok, const std::string &varname, int type)
void deallocReturnError(const Token *tok, const Token *deallocTok, const std::string &varname)
Interface class that cppcheck uses to communicate with the checks.
This is an interface, which the class responsible of error logging should implement.
This is just a container for general settings so that we don't need to pass individual values to func...
The token list that the TokenList generates is a linked-list of this class.
The main purpose is to tokenize the source code.
const Settings & getSettings() const
std::map< int, AllocInfo > alloctype
void erase(nonneg int varid)
std::set< int > conditionalAlloc
void swap(VarInfo &other)
std::set< int > referenced
void reallocToAlloc(nonneg int varid)
std::map< int, std::pair< const Token *, Usage > > possibleUsage
AllocInfo(int type_=0, AllocStatus status_=NOALLOC, const Token *allocTok_=nullptr)