"Fossies" - the Fresh Open Source Software Archive

Member "cppcheck-1.89/lib/checkleakautovar.h" (1 Sep 2019, 5903 Bytes) of package /windows/misc/cppcheck-1.89.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "checkleakautovar.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.88_vs_1.89.

    1 /*
    2  * Cppcheck - A tool for static C/C++ code analysis
    3  * Copyright (C) 2007-2019 Cppcheck team.
    4  *
    5  * This program is free software: you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation, either version 3 of the License, or
    8  * (at your option) any later version.
    9  *
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  *
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   17  */
   18 
   19 
   20 //---------------------------------------------------------------------------
   21 #ifndef checkleakautovarH
   22 #define checkleakautovarH
   23 //---------------------------------------------------------------------------
   24 
   25 #include "check.h"
   26 #include "config.h"
   27 #include "library.h"
   28 
   29 #include <map>
   30 #include <set>
   31 #include <string>
   32 
   33 class ErrorLogger;
   34 class Settings;
   35 class Token;
   36 class Tokenizer;
   37 
   38 
   39 class CPPCHECKLIB VarInfo {
   40 public:
   41     enum AllocStatus { OWNED = -2, DEALLOC = -1, NOALLOC = 0, ALLOC = 1 };
   42     struct AllocInfo {
   43         AllocStatus status;
   44         /** Allocation type. If it is a positive value then it corresponds to
   45          * a Library allocation id. A negative value is a builtin
   46          * checkleakautovar allocation type.
   47          */
   48         int type;
   49         AllocInfo(int type_ = 0, AllocStatus status_ = NOALLOC) : status(status_), type(type_) {}
   50 
   51         bool managed() const {
   52             return status < 0;
   53         }
   54     };
   55     std::map<int, AllocInfo> alloctype;
   56     std::map<int, std::string> possibleUsage;
   57     std::set<int> conditionalAlloc;
   58     std::set<int> referenced;
   59 
   60     void clear() {
   61         alloctype.clear();
   62         possibleUsage.clear();
   63         conditionalAlloc.clear();
   64         referenced.clear();
   65     }
   66 
   67     void erase(nonneg int varid) {
   68         alloctype.erase(varid);
   69         possibleUsage.erase(varid);
   70         conditionalAlloc.erase(varid);
   71         referenced.erase(varid);
   72     }
   73 
   74     void swap(VarInfo &other) {
   75         alloctype.swap(other.alloctype);
   76         possibleUsage.swap(other.possibleUsage);
   77         conditionalAlloc.swap(other.conditionalAlloc);
   78         referenced.swap(other.referenced);
   79     }
   80 
   81     /** set possible usage for all variables */
   82     void possibleUsageAll(const std::string &functionName);
   83 
   84     void print();
   85 };
   86 
   87 
   88 /// @addtogroup Checks
   89 /// @{
   90 
   91 /**
   92  * @brief Check for leaks
   93  */
   94 
   95 class CPPCHECKLIB CheckLeakAutoVar : public Check {
   96 public:
   97     /** This constructor is used when registering the CheckLeakAutoVar */
   98     CheckLeakAutoVar() : Check(myName()) {
   99     }
  100 
  101     /** This constructor is used when running checks. */
  102     CheckLeakAutoVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
  103         : Check(myName(), tokenizer, settings, errorLogger) {
  104     }
  105 
  106     void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) OVERRIDE {
  107         CheckLeakAutoVar checkLeakAutoVar(tokenizer, settings, errorLogger);
  108         checkLeakAutoVar.check();
  109     }
  110 
  111 private:
  112 
  113     /** check for leaks in all scopes */
  114     void check();
  115 
  116     /** check for leaks in a function scope */
  117     void checkScope(const Token * const startToken,
  118                     VarInfo *varInfo,
  119                     std::set<int> notzero,
  120                     nonneg int recursiveCount);
  121 
  122     /** Check token inside expression.
  123     * @param tok token inside expression.
  124     * @param varInfo Variable info
  125     * @return next token to process (if no other checks needed for this token). NULL if other checks could be performed.
  126     */
  127     const Token * checkTokenInsideExpression(const Token * const tok, VarInfo *varInfo);
  128 
  129     /** parse function call */
  130     void functionCall(const Token *tokName, const Token *tokOpeningPar, VarInfo *varInfo, const VarInfo::AllocInfo& allocation, const Library::AllocFunc* af);
  131 
  132     /** parse changes in allocation status */
  133     void changeAllocStatus(VarInfo *varInfo, const VarInfo::AllocInfo& allocation, const Token* tok, const Token* arg);
  134 
  135     /** update allocation status if reallocation function */
  136     void changeAllocStatusIfRealloc(std::map<int, VarInfo::AllocInfo> &alloctype, const Token *fTok, const Token *retTok);
  137 
  138     /** return. either "return" or end of variable scope is seen */
  139     void ret(const Token *tok, const VarInfo &varInfo);
  140 
  141     /** if variable is allocated then there is a leak */
  142     void leakIfAllocated(const Token *vartok, const VarInfo &varInfo);
  143 
  144     void leakError(const Token* tok, const std::string &varname, int type);
  145     void mismatchError(const Token* tok, const std::string &varname);
  146     void deallocUseError(const Token *tok, const std::string &varname);
  147     void deallocReturnError(const Token *tok, const std::string &varname);
  148     void doubleFreeError(const Token *tok, const std::string &varname, int type);
  149 
  150     /** message: user configuration is needed to complete analysis */
  151     void configurationInfo(const Token* tok, const std::string &functionName);
  152 
  153     void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const OVERRIDE {
  154         CheckLeakAutoVar c(nullptr, settings, errorLogger);
  155         c.deallocReturnError(nullptr, "p");
  156         c.configurationInfo(nullptr, "f");  // user configuration is needed to complete analysis
  157         c.doubleFreeError(nullptr, "varname", 0);
  158     }
  159 
  160     static std::string myName() {
  161         return "Leaks (auto variables)";
  162     }
  163 
  164     std::string classInfo() const OVERRIDE {
  165         return "Detect when a auto variable is allocated but not deallocated or deallocated twice.\n";
  166     }
  167 };
  168 /// @}
  169 //---------------------------------------------------------------------------
  170 #endif // checkleakautovarH