Cppcheck
checkunusedvar.h
Go to the documentation of this file.
00001 /*
00002  * Cppcheck - A tool for static C/C++ code analysis
00003  * Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 
00020 //---------------------------------------------------------------------------
00021 #ifndef CheckUnusedVarH
00022 #define CheckUnusedVarH
00023 //---------------------------------------------------------------------------
00024 
00025 #include <map>
00026 
00027 #include "config.h"
00028 #include "check.h"
00029 #include "settings.h"
00030 
00031 class Type;
00032 class Token;
00033 class Scope;
00034 class Variables;
00035 
00036 /// @addtogroup Checks
00037 /// @{
00038 
00039 
00040 /** @brief Various small checks */
00041 
00042 class CPPCHECKLIB CheckUnusedVar : public Check {
00043 public:
00044     /** @brief This constructor is used when registering the CheckClass */
00045     CheckUnusedVar() : Check(myName())
00046     { }
00047 
00048     /** @brief This constructor is used when running checks. */
00049     CheckUnusedVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
00050         : Check(myName(), tokenizer, settings, errorLogger)
00051     { }
00052 
00053     /** @brief Run checks against the normal token list */
00054     void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
00055         CheckUnusedVar checkUnusedVar(tokenizer, settings, errorLogger);
00056 
00057         // Coding style checks
00058         checkUnusedVar.checkStructMemberUsage();
00059         checkUnusedVar.checkFunctionVariableUsage();
00060     }
00061 
00062     /** @brief Run checks against the simplified token list */
00063     void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
00064         (void)tokenizer;
00065         (void)settings;
00066         (void)errorLogger;
00067     }
00068 
00069     /** @brief %Check for unused function variables */
00070     void checkFunctionVariableUsage_iterateScopes(const Scope* const scope, Variables& variables, bool insideLoop);
00071     void checkVariableUsage(const Scope* const scope, const Token* start, Variables& variables);
00072     void checkFunctionVariableUsage();
00073 
00074     /** @brief %Check that all struct members are used */
00075     void checkStructMemberUsage();
00076 
00077 private:
00078     bool isRecordTypeWithoutSideEffects(const Type* type);
00079 
00080     // Error messages..
00081     void unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname);
00082     void unusedVariableError(const Token *tok, const std::string &varname);
00083     void allocatedButUnusedVariableError(const Token *tok, const std::string &varname);
00084     void unreadVariableError(const Token *tok, const std::string &varname);
00085     void unassignedVariableError(const Token *tok, const std::string &varname);
00086 
00087     void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
00088         CheckUnusedVar c(0, settings, errorLogger);
00089 
00090         // style/warning
00091         c.unusedVariableError(0, "varname");
00092         c.allocatedButUnusedVariableError(0, "varname");
00093         c.unreadVariableError(0, "varname");
00094         c.unassignedVariableError(0, "varname");
00095         c.unusedStructMemberError(0, "structname", "variable");
00096     }
00097 
00098     static std::string myName() {
00099         return "UnusedVar";
00100     }
00101 
00102     std::string classInfo() const {
00103         return "UnusedVar checks\n"
00104 
00105                // style
00106                "* unused variable\n"
00107                "* allocated but unused variable\n"
00108                "* unred variable\n"
00109                "* unassigned variable\n"
00110                "* unused struct member\n";
00111     }
00112 
00113     std::map<const Type *,bool> isRecordTypeWithoutSideEffectsMap;
00114 };
00115 /// @}
00116 //---------------------------------------------------------------------------
00117 #endif