Refactorizations in CheckOther:
- Make CheckOther::checkCastIntToCharAndBack() work for multiple variables at once - Improved messages of CheckOther::checkSuspiciousSemicolon() and CheckOther::checkPipeParameterSize()
This commit is contained in:
parent
d78c06dc3f
commit
5c1a05dcbe
|
@ -52,17 +52,13 @@ void CheckOther::checkCastIntToCharAndBack()
|
||||||
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
|
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
|
||||||
const std::size_t functions = symbolDatabase->functionScopes.size();
|
const std::size_t functions = symbolDatabase->functionScopes.size();
|
||||||
for (std::size_t i = 0; i < functions; ++i) {
|
for (std::size_t i = 0; i < functions; ++i) {
|
||||||
const Scope * scope = symbolDatabase->functionScopes[i];
|
const Scope * scope = symbolDatabase->functionScopes[i];
|
||||||
unsigned int uiVarId = 0;
|
std::map<unsigned int, std::string> vars;
|
||||||
std::string strFunctionName;
|
|
||||||
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
|
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
|
||||||
if (Token::Match(tok, "%var% = fclose|fflush|fputc|fputs|fscanf|getchar|getc|fgetc|putchar|putc|puts|scanf|sscanf|ungetc (")) {
|
if (Token::Match(tok, "%var% = fclose|fflush|fputc|fputs|fscanf|getchar|getc|fgetc|putchar|putc|puts|scanf|sscanf|ungetc (")) {
|
||||||
if (tok->varId()) {
|
const Variable *var = tok->variable();
|
||||||
const Variable *var = tok->variable();
|
if (var && var->typeEndToken()->str() == "char" && !var->typeEndToken()->isSigned()) {
|
||||||
if (var && var->typeEndToken()->str() == "char" && !var->typeEndToken()->isSigned()) {
|
vars[tok->varId()] = tok->strAt(2);
|
||||||
uiVarId = tok->varId();
|
|
||||||
strFunctionName = tok->strAt(2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (Token::Match(tok, "EOF %comp% ( %var% = fclose|fflush|fputc|fputs|fscanf|getchar|getc|fgetc|putchar|putc|puts|scanf|sscanf|ungetc (")) {
|
} else if (Token::Match(tok, "EOF %comp% ( %var% = fclose|fflush|fputc|fputs|fscanf|getchar|getc|fgetc|putchar|putc|puts|scanf|sscanf|ungetc (")) {
|
||||||
tok = tok->tokAt(3);
|
tok = tok->tokAt(3);
|
||||||
|
@ -74,13 +70,13 @@ void CheckOther::checkCastIntToCharAndBack()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Token::Match(tok, "%var% %comp% EOF")) {
|
if (Token::Match(tok, "%var% %comp% EOF")) {
|
||||||
if (uiVarId && tok->varId() == uiVarId) {
|
if (vars.find(tok->varId()) != vars.end()) {
|
||||||
checkCastIntToCharAndBackError(tok, strFunctionName);
|
checkCastIntToCharAndBackError(tok, vars[tok->varId()]);
|
||||||
}
|
}
|
||||||
} else if (Token::Match(tok, "EOF %comp% %var%")) {
|
} else if (Token::Match(tok, "EOF %comp% %var%")) {
|
||||||
tok = tok->tokAt(2);
|
tok = tok->tokAt(2);
|
||||||
if (uiVarId && tok->varId() == uiVarId) {
|
if (vars.find(tok->varId()) != vars.end()) {
|
||||||
checkCastIntToCharAndBackError(tok, strFunctionName);
|
checkCastIntToCharAndBackError(tok, vars[tok->varId()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -420,7 +416,7 @@ void CheckOther::checkSuspiciousSemicolon()
|
||||||
if (Token::simpleMatch(i->classStart, "{ ; } {") &&
|
if (Token::simpleMatch(i->classStart, "{ ; } {") &&
|
||||||
i->classStart->previous()->linenr() == i->classStart->tokAt(2)->linenr()
|
i->classStart->previous()->linenr() == i->classStart->tokAt(2)->linenr()
|
||||||
&& i->classStart->linenr()+1 >= i->classStart->tokAt(3)->linenr()) {
|
&& i->classStart->linenr()+1 >= i->classStart->tokAt(3)->linenr()) {
|
||||||
SuspiciousSemicolonError(i->classStart);
|
SuspiciousSemicolonError(i->classDef);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -429,7 +425,7 @@ void CheckOther::checkSuspiciousSemicolon()
|
||||||
void CheckOther::SuspiciousSemicolonError(const Token* tok)
|
void CheckOther::SuspiciousSemicolonError(const Token* tok)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::warning, "suspiciousSemicolon",
|
reportError(tok, Severity::warning, "suspiciousSemicolon",
|
||||||
"Suspicious use of ; at the end of 'if/for/while' statement.", true);
|
"Suspicious use of ; at the end of '" + (tok ? tok->str() : std::string()) + "' statement.", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -630,11 +626,9 @@ void CheckOther::checkPipeParameterSize()
|
||||||
void CheckOther::checkPipeParameterSizeError(const Token *tok, const std::string &strVarName, const std::string &strDim)
|
void CheckOther::checkPipeParameterSizeError(const Token *tok, const std::string &strVarName, const std::string &strDim)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error,
|
reportError(tok, Severity::error,
|
||||||
"wrongPipeParameterSize", "Variable " + strVarName + " must have size 2 when it is used as parameter of pipe() command.\n"
|
"wrongPipeParameterSize", "Buffer '" + strVarName + "' must have size of 2 integers if used as parameter of pipe().\n"
|
||||||
"The pipe()/pipe2() system command takes an argument, which is an array of exactly two integers."
|
"The pipe()/pipe2() system command takes an argument, which is an array of exactly two integers.\n"
|
||||||
"\nThe variable " + strVarName + " is an array of size "
|
"The variable '" + strVarName + "' is an array of size " + strDim + ", which does not match.");
|
||||||
+ strDim + ", which does not match."
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
|
@ -763,7 +763,6 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
void varScope10() {
|
void varScope10() {
|
||||||
// classes may have extra side effects
|
|
||||||
varScope("int f()\n"
|
varScope("int f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int x = 0;\n"
|
" int x = 0;\n"
|
||||||
|
@ -6024,7 +6023,7 @@ private:
|
||||||
" for(int i = 0; i < 10; ++i); {\n"
|
" for(int i = 0; i < 10; ++i); {\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'if/for/while' statement.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'for' statement.\n", errout.str());
|
||||||
|
|
||||||
// Block with some tokens to make sure the tokenizer output
|
// Block with some tokens to make sure the tokenizer output
|
||||||
// stays the same for "for(); {}"
|
// stays the same for "for(); {}"
|
||||||
|
@ -6034,7 +6033,7 @@ private:
|
||||||
" int j = 123;\n"
|
" int j = 123;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'if/for/while' statement.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'for' statement.\n", errout.str());
|
||||||
|
|
||||||
check(
|
check(
|
||||||
"void foo() {\n"
|
"void foo() {\n"
|
||||||
|
@ -6042,7 +6041,7 @@ private:
|
||||||
" do_something();\n"
|
" do_something();\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'if/for/while' statement.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'while' statement.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkForSuspiciousSemicolon2() {
|
void checkForSuspiciousSemicolon2() {
|
||||||
|
@ -6052,7 +6051,7 @@ private:
|
||||||
" do_something();\n"
|
" do_something();\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'if/for/while' statement.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'if' statement.\n", errout.str());
|
||||||
|
|
||||||
// Seen this in the wild
|
// Seen this in the wild
|
||||||
check(
|
check(
|
||||||
|
@ -7063,7 +7062,7 @@ private:
|
||||||
" return;\n"
|
" return;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}",NULL,false,false,true);
|
"}",NULL,false,false,true);
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Variable pipefd must have size 2 when it is used as parameter of pipe() command.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Buffer 'pipefd' must have size of 2 integers if used as parameter of pipe().\n", errout.str());
|
||||||
|
|
||||||
check("void f(){\n"
|
check("void f(){\n"
|
||||||
"int pipefd[2];\n"
|
"int pipefd[2];\n"
|
||||||
|
@ -7087,7 +7086,7 @@ private:
|
||||||
" return;\n"
|
" return;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}",NULL,false,false,true);
|
"}",NULL,false,false,true);
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Variable pipefd must have size 2 when it is used as parameter of pipe() command.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Buffer 'pipefd' must have size of 2 integers if used as parameter of pipe().\n", errout.str());
|
||||||
|
|
||||||
check("void f(){\n"
|
check("void f(){\n"
|
||||||
"int pipefd[2];\n"
|
"int pipefd[2];\n"
|
||||||
|
|
Loading…
Reference in New Issue