Fixed #3972: Don't issue auto variable error when the address is assigned to a reference to a non-pointer.

This commit is contained in:
PKEuS 2012-07-13 05:21:45 -07:00
parent 4ed15d87b6
commit 247d820027
3 changed files with 10 additions and 4 deletions

View File

@ -35,11 +35,11 @@ namespace {
} }
bool CheckAutoVariables::isRefArg(unsigned int varId) bool CheckAutoVariables::isRefPtrArg(unsigned int varId)
{ {
const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(varId); const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(varId);
return(var && var->isArgument() && var->isReference()); return(var && var->isArgument() && var->isReference() && var->isPointer());
} }
bool CheckAutoVariables::isPtrArg(unsigned int varId) bool CheckAutoVariables::isPtrArg(unsigned int varId)
@ -92,7 +92,7 @@ void CheckAutoVariables::autoVariables()
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) { for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
// Critical assignment // Critical assignment
if (Token::Match(tok, "[;{}] %var% = & %var%") && isRefArg(tok->next()->varId()) && isAutoVar(tok->tokAt(4)->varId())) { if (Token::Match(tok, "[;{}] %var% = & %var%") && isRefPtrArg(tok->next()->varId()) && isAutoVar(tok->tokAt(4)->varId())) {
const Variable * var = symbolDatabase->getVariableFromVarId(tok->tokAt(4)->varId()); const Variable * var = symbolDatabase->getVariableFromVarId(tok->tokAt(4)->varId());
if (checkRvalueExpression(var, tok->tokAt(5))) if (checkRvalueExpression(var, tok->tokAt(5)))
errorAutoVariableAssignment(tok->next(), false); errorAutoVariableAssignment(tok->next(), false);

View File

@ -67,7 +67,7 @@ public:
void returncstr(); void returncstr();
private: private:
bool isRefArg(unsigned int varId); bool isRefPtrArg(unsigned int varId);
bool isPtrArg(unsigned int varId); bool isPtrArg(unsigned int varId);
bool isAutoVar(unsigned int varId); bool isAutoVar(unsigned int varId);
bool isAutoVarArray(unsigned int varId); bool isAutoVarArray(unsigned int varId);

View File

@ -221,6 +221,12 @@ private:
" p = &i;\n" " p = &i;\n"
"}", false); "}", false);
ASSERT_EQUALS("[test.cpp:3]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
check("void foo(std::string& s) {\n"
" char* p = foo;\n"
" s = &p[0];\n"
"}", false);
ASSERT_EQUALS("", errout.str());
} }
void testautovar9() { void testautovar9() {