2009-03-19 20:55:50 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2013-01-01 17:29:08 +01:00
|
|
|
* Copyright (C) 2007-2013 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
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#ifndef checkautovariablesH
|
|
|
|
#define checkautovariablesH
|
2009-03-19 20:55:50 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
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 */
|
2013-08-07 16:27:37 +02: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)
|
2013-08-07 16:27:37 +02: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:
|
2013-02-18 17:52:49 +01:00
|
|
|
static bool isPtrArg(const Token *tok);
|
|
|
|
static bool isRefPtrArg(const Token *tok);
|
|
|
|
static bool isNonReferenceArg(const Token *tok);
|
|
|
|
static bool isAutoVar(const Token *tok);
|
|
|
|
static bool isAutoVarArray(const Token *tok);
|
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
|
|
|
|
* @return true if a temporary object is returned
|
|
|
|
*/
|
2013-01-28 06:47:48 +01:00
|
|
|
bool returnTemporary(const Token *tok) 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);
|
2013-02-01 19:16:17 +01:00
|
|
|
void errorUselessAssignmentPtrArg(const Token *tok);
|
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");
|
2013-02-01 19:16:17 +01:00
|
|
|
c.errorUselessAssignmentPtrArg(0);
|
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"
|
2013-02-01 19:16:17 +01:00
|
|
|
"* returning address of function parameter\n"
|
|
|
|
"* useless assignment of pointer 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
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checkautovariablesH
|