2009-03-19 20:55:50 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2012-01-01 00:05:37 +01:00
|
|
|
* Copyright (C) 2007-2012 Daniel Marjamäki and Cppcheck team.
|
2009-03-19 20:55:50 +01:00
|
|
|
*
|
|
|
|
* 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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-03-19 20:55:50 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef CheckAutoVariablesH
|
|
|
|
#define CheckAutoVariablesH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
#include "config.h"
|
2009-03-19 20:55:50 +01:00
|
|
|
#include "check.h"
|
|
|
|
#include "token.h"
|
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckAutoVariables : public Check {
|
2009-03-19 20:55:50 +01:00
|
|
|
public:
|
2009-03-21 07:53:23 +01:00
|
|
|
/** This constructor is used when registering the CheckClass */
|
2011-02-02 10:29:10 +01:00
|
|
|
CheckAutoVariables() : Check(myName())
|
2009-03-21 07:53:23 +01:00
|
|
|
{ }
|
|
|
|
|
2010-03-17 22:16:18 +01:00
|
|
|
/** This constructor is used when running checks. */
|
2009-03-21 07:53:23 +01:00
|
|
|
CheckAutoVariables(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
2011-02-02 10:29:10 +01:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger)
|
2009-03-21 07:53:23 +01:00
|
|
|
{ }
|
|
|
|
|
2011-02-12 15:39:26 +01:00
|
|
|
/** @brief Run checks against the normal token list */
|
2011-10-13 20:53:06 +02:00
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
2011-02-12 15:39:26 +01:00
|
|
|
CheckAutoVariables checkAutoVariables(tokenizer, settings, errorLogger);
|
|
|
|
checkAutoVariables.returnReference();
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
2009-03-21 07:53:23 +01:00
|
|
|
CheckAutoVariables checkAutoVariables(tokenizer, settings, errorLogger);
|
|
|
|
checkAutoVariables.autoVariables();
|
2009-06-09 19:45:58 +02:00
|
|
|
checkAutoVariables.returnPointerToLocalArray();
|
2009-03-19 20:55:50 +01:00
|
|
|
}
|
|
|
|
|
2009-05-22 07:51:30 +02:00
|
|
|
/** Check auto variables */
|
2009-03-19 20:55:50 +01:00
|
|
|
void autoVariables();
|
2009-03-21 07:53:23 +01:00
|
|
|
|
2009-06-09 19:45:58 +02:00
|
|
|
/** Returning pointer to local array */
|
|
|
|
void returnPointerToLocalArray();
|
|
|
|
|
2010-01-23 20:39:12 +01:00
|
|
|
/** Returning reference to local/temporary variable */
|
|
|
|
void returnReference();
|
|
|
|
|
2009-03-19 20:55:50 +01:00
|
|
|
private:
|
2012-07-13 14:21:45 +02:00
|
|
|
bool isRefPtrArg(unsigned int varId);
|
2012-03-01 18:38:20 +01:00
|
|
|
bool isPtrArg(unsigned int varId);
|
2009-08-16 10:46:52 +02:00
|
|
|
bool isAutoVar(unsigned int varId);
|
2009-08-16 10:27:40 +02:00
|
|
|
bool isAutoVarArray(unsigned int varId);
|
2009-03-22 08:20:15 +01:00
|
|
|
|
2010-01-27 19:16:32 +01:00
|
|
|
/**
|
|
|
|
* Returning a temporary object?
|
|
|
|
* @param tok pointing at the "return" token
|
2012-10-24 01:20:14 +02:00
|
|
|
* @param startScope indicates the function scope to be checked
|
2010-01-27 19:16:32 +01:00
|
|
|
* @return true if a temporary object is returned
|
|
|
|
*/
|
2012-10-24 01:20:14 +02:00
|
|
|
bool returnTemporary(const Token *tok, const Scope *startScope) const;
|
2009-06-09 19:45:58 +02:00
|
|
|
|
2011-07-21 14:50:38 +02:00
|
|
|
void errorReturnAddressToAutoVariable(const Token *tok);
|
2009-06-09 19:45:58 +02:00
|
|
|
void errorReturnPointerToLocalArray(const Token *tok);
|
2011-08-09 18:24:39 +02:00
|
|
|
void errorAutoVariableAssignment(const Token *tok, bool inconclusive);
|
2010-01-23 20:39:12 +01:00
|
|
|
void errorReturnReference(const Token *tok);
|
2010-01-27 19:16:32 +01:00
|
|
|
void errorReturnTempReference(const Token *tok);
|
2011-07-21 14:50:38 +02:00
|
|
|
void errorInvalidDeallocation(const Token *tok);
|
2011-09-01 03:36:31 +02:00
|
|
|
void errorReturnAddressOfFunctionParameter(const Token *tok, const std::string &varname);
|
2009-06-09 19:45:58 +02:00
|
|
|
|
2012-02-18 23:43:51 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
|
2010-12-29 12:43:29 +01:00
|
|
|
CheckAutoVariables c(0,settings,errorLogger);
|
2011-08-09 18:24:39 +02:00
|
|
|
c.errorAutoVariableAssignment(0, false);
|
2011-07-21 14:50:38 +02:00
|
|
|
c.errorReturnAddressToAutoVariable(0);
|
2010-12-29 12:43:29 +01:00
|
|
|
c.errorReturnPointerToLocalArray(0);
|
|
|
|
c.errorReturnReference(0);
|
|
|
|
c.errorReturnTempReference(0);
|
2011-07-21 14:50:38 +02:00
|
|
|
c.errorInvalidDeallocation(0);
|
2011-09-01 03:36:31 +02:00
|
|
|
c.errorReturnAddressOfFunctionParameter(0, "parameter");
|
2009-03-22 08:20:15 +01:00
|
|
|
}
|
2009-06-12 12:19:37 +02:00
|
|
|
|
2012-08-02 18:50:48 +02:00
|
|
|
static std::string myName() {
|
2009-06-12 15:20:08 +02:00
|
|
|
return "Auto Variables";
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
std::string classInfo() const {
|
2009-06-21 21:03:21 +02:00
|
|
|
return "A pointer to a variable is only valid as long as the variable is in scope.\n"
|
2009-06-12 12:19:37 +02:00
|
|
|
"Check:\n"
|
2010-01-27 19:16:32 +01:00
|
|
|
"* returning a pointer to auto or temporary variable\n"
|
2010-01-23 20:39:12 +01:00
|
|
|
"* assigning address of an variable to an effective parameter of a function\n"
|
2011-09-01 03:36:31 +02:00
|
|
|
"* returning reference to local/temporary variable\n"
|
|
|
|
"* returning address of function parameter\n";
|
2009-06-12 12:19:37 +02:00
|
|
|
}
|
2009-03-19 20:55:50 +01:00
|
|
|
};
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @}
|
2009-03-19 20:55:50 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif
|