CheckAutoVariables: added braces.

No functional change.
This commit is contained in:
Slava Semushin 2009-08-16 16:36:07 +07:00
parent 743d5331ee
commit fc5f4d366e
1 changed files with 30 additions and 0 deletions

View File

@ -77,7 +77,9 @@ bool isTypeName(const Token *tok)
std::string _str(tok->str()); std::string _str(tok->str());
static const char * const type[] = {"case", "return", "delete", 0}; static const char * const type[] = {"case", "return", "delete", 0};
for (int i = 0; type[i]; i++) for (int i = 0; type[i]; i++)
{
ret |= (_str == type[i]); ret |= (_str == type[i]);
}
return !ret; return !ret;
} }
@ -86,11 +88,17 @@ bool isExternOrStatic(const Token *tok)
bool res = false; bool res = false;
if (Token::Match(tok->tokAt(-1), "extern|static")) if (Token::Match(tok->tokAt(-1), "extern|static"))
{
res = true; res = true;
}
else if (Token::Match(tok->tokAt(-2), "extern|static")) else if (Token::Match(tok->tokAt(-2), "extern|static"))
{
res = true; res = true;
}
else if (Token::Match(tok->tokAt(-3), "extern|static")) else if (Token::Match(tok->tokAt(-3), "extern|static"))
{
res = true; res = true;
}
//std::cout << __PRETTY_FUNCTION__ << " " << tok->str() << " " << res << std::endl; //std::cout << __PRETTY_FUNCTION__ << " " << tok->str() << " " << res << std::endl;
return res; return res;
@ -132,13 +140,17 @@ void CheckAutoVariables::autoVariables()
fp_list.insert(tok->tokAt(2)->str()); fp_list.insert(tok->tokAt(2)->str());
} }
else if (begin_function && tok->str() == "(") else if (begin_function && tok->str() == "(")
{
begin_function_decl = true; begin_function_decl = true;
}
else if (begin_function && tok->str() == ")") else if (begin_function && tok->str() == ")")
{ {
begin_function_decl = false; begin_function_decl = false;
} }
else if (begin_function && tok->str() == "{") else if (begin_function && tok->str() == "{")
{
bindent++; bindent++;
}
else if (begin_function && tok->str() == "}") else if (begin_function && tok->str() == "}")
{ {
bindent--; bindent--;
@ -155,47 +167,59 @@ void CheckAutoVariables::autoVariables()
else if (bindent > 0 && Token::Match(tok, "%var% %var% ;") && !isExternOrStatic(tok)) //Inside a function else if (bindent > 0 && Token::Match(tok, "%var% %var% ;") && !isExternOrStatic(tok)) //Inside a function
{ {
if (!isTypeName(tok)) if (!isTypeName(tok))
{
continue; continue;
}
addVD(tok->next()->varId()); addVD(tok->next()->varId());
} }
else if (bindent > 0 && Token::Match(tok, "const %var% %var% ;") && !isExternOrStatic(tok)) //Inside a function else if (bindent > 0 && Token::Match(tok, "const %var% %var% ;") && !isExternOrStatic(tok)) //Inside a function
{ {
if (!isTypeName(tok->tokAt(1))) if (!isTypeName(tok->tokAt(1)))
{
continue; continue;
}
addVD(tok->tokAt(2)->varId()); addVD(tok->tokAt(2)->varId());
} }
else if (bindent > 0 && Token::Match(tok, "[;{}] %var% = & %var%")) //Critical assignement else if (bindent > 0 && Token::Match(tok, "[;{}] %var% = & %var%")) //Critical assignement
{ {
if (errorAv(tok->tokAt(1), tok->tokAt(4))) if (errorAv(tok->tokAt(1), tok->tokAt(4)))
{
reportError(tok, reportError(tok,
Severity::error, Severity::error,
"autoVariables", "autoVariables",
"Wrong assignement of an auto-variable to an effective parameter of a function"); "Wrong assignement of an auto-variable to an effective parameter of a function");
}
} }
else if (bindent > 0 && Token::Match(tok, "[;{}] %var% [ %any% ] = & %var%")) //Critical assignement else if (bindent > 0 && Token::Match(tok, "[;{}] %var% [ %any% ] = & %var%")) //Critical assignement
{ {
if (errorAv(tok->tokAt(1), tok->tokAt(7))) if (errorAv(tok->tokAt(1), tok->tokAt(7)))
{
reportError(tok, reportError(tok,
Severity::error, Severity::error,
"autoVariables", "autoVariables",
"Wrong assignement of an auto-variable to an effective parameter of a function"); "Wrong assignement of an auto-variable to an effective parameter of a function");
}
} }
else if (bindent > 0 && Token::Match(tok, "return & %var% ;")) //Critical return else if (bindent > 0 && Token::Match(tok, "return & %var% ;")) //Critical return
{ {
if (isAutoVar(tok->tokAt(2)->varId())) if (isAutoVar(tok->tokAt(2)->varId()))
{
reportError(tok, reportError(tok,
Severity::error, Severity::error,
"autoVariables", "autoVariables",
"Return of the address of an auto-variable"); "Return of the address of an auto-variable");
}
} }
// Invalid pointer deallocation // Invalid pointer deallocation
else if (bindent > 0 && Token::Match(tok, "free ( %var% ) ;")) else if (bindent > 0 && Token::Match(tok, "free ( %var% ) ;"))
{ {
if (isAutoVarArray(tok->tokAt(2)->varId())) if (isAutoVarArray(tok->tokAt(2)->varId()))
{
reportError(tok, reportError(tok,
Severity::error, Severity::error,
"autoVariables", "autoVariables",
"Invalid deallocation"); "Invalid deallocation");
}
} }
} }
vd_list.clear(); vd_list.clear();
@ -236,12 +260,16 @@ void CheckAutoVariables::returnPointerToLocalArray()
if (infunc) if (infunc)
{ {
if (tok->str() == "{") if (tok->str() == "{")
{
++indentlevel; ++indentlevel;
}
else if (tok->str() == "}") else if (tok->str() == "}")
{ {
--indentlevel; --indentlevel;
if (indentlevel <= 0) if (indentlevel <= 0)
{
infunc = false; infunc = false;
}
continue; continue;
} }
@ -256,7 +284,9 @@ void CheckAutoVariables::returnPointerToLocalArray()
{ {
unsigned int varid = tok->next()->varId(); unsigned int varid = tok->next()->varId();
if (varid > 0 && arrayVar.find(varid) != arrayVar.end()) if (varid > 0 && arrayVar.find(varid) != arrayVar.end())
{
errorReturnPointerToLocalArray(tok); errorReturnPointerToLocalArray(tok);
}
} }
} }