2009-03-19 20:55:50 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 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
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "check.h"
|
|
|
|
#include "token.h"
|
2009-08-16 10:59:47 +02:00
|
|
|
#include <set>
|
2009-03-19 20:55:50 +01:00
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
2009-03-19 20:55:50 +01:00
|
|
|
class CheckAutoVariables : public Check
|
|
|
|
{
|
|
|
|
public:
|
2009-03-21 07:53:23 +01:00
|
|
|
/** This constructor is used when registering the CheckClass */
|
|
|
|
CheckAutoVariables() : Check()
|
|
|
|
{ }
|
|
|
|
|
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)
|
2010-04-02 07:30:58 +02:00
|
|
|
: Check(tokenizer, settings, errorLogger)
|
2009-03-21 07:53:23 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
2009-03-19 20:55:50 +01:00
|
|
|
{
|
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();
|
2010-01-23 20:39:12 +01:00
|
|
|
checkAutoVariables.returnReference();
|
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();
|
|
|
|
|
2010-01-26 22:11:34 +01:00
|
|
|
/** Returning c_str to local variable */
|
|
|
|
void returncstr();
|
|
|
|
|
2009-03-19 20:55:50 +01:00
|
|
|
private:
|
2009-08-16 11:16:35 +02:00
|
|
|
std::set<std::string> fp_list;
|
2009-08-16 11:10:42 +02:00
|
|
|
std::set<unsigned int> vd_list;
|
2009-08-16 10:59:47 +02:00
|
|
|
std::set<unsigned int> vda_list;
|
2009-03-22 08:01:48 +01:00
|
|
|
bool errorAv(const Token* left, const Token* right);
|
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-08-16 11:10:42 +02:00
|
|
|
void addVD(unsigned int varId);
|
2009-08-16 10:59:47 +02:00
|
|
|
void addVDA(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
|
|
|
|
* @return true if a temporary object is returned
|
|
|
|
*/
|
|
|
|
bool returnTemporary(const Token *tok) const;
|
2009-06-09 19:45:58 +02:00
|
|
|
|
|
|
|
void errorReturnPointerToLocalArray(const Token *tok);
|
2009-10-04 11:45:45 +02:00
|
|
|
void errorAutoVariableAssignment(const Token *tok);
|
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);
|
2010-01-26 22:11:34 +01:00
|
|
|
void errorReturnAutocstr(const Token *tok);
|
2010-01-27 19:16:32 +01:00
|
|
|
void errorReturnTempPointer(const Token *tok);
|
2009-06-09 19:45:58 +02:00
|
|
|
|
2009-03-22 08:20:15 +01:00
|
|
|
void getErrorMessages()
|
|
|
|
{
|
2009-10-04 11:45:45 +02:00
|
|
|
errorAutoVariableAssignment(0);
|
2009-06-09 19:45:58 +02:00
|
|
|
errorReturnPointerToLocalArray(0);
|
2010-01-23 20:39:12 +01:00
|
|
|
errorReturnReference(0);
|
2010-01-27 19:16:32 +01:00
|
|
|
errorReturnTempReference(0);
|
2010-01-26 22:11:34 +01:00
|
|
|
errorReturnAutocstr(0);
|
2010-01-27 19:16:32 +01:00
|
|
|
errorReturnTempPointer(0);
|
2009-03-22 08:20:15 +01:00
|
|
|
}
|
2009-06-12 12:19:37 +02:00
|
|
|
|
2009-06-12 15:20:08 +02:00
|
|
|
std::string name() const
|
|
|
|
{
|
|
|
|
return "Auto Variables";
|
|
|
|
}
|
|
|
|
|
2009-06-12 12:19:37 +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"
|
|
|
|
"* returning reference to local/temporary variable\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
|
|
|
|
|