CheckOther::clarifyStatement: improved message to show what's the 'bad' and the 'good' expression.

This commit is contained in:
Edoardo Prezioso 2012-09-04 02:03:38 +02:00
parent 6fd60eebb0
commit f8591f9004
3 changed files with 17 additions and 14 deletions

View File

@ -259,17 +259,20 @@ void CheckOther::clarifyStatement()
break;
}
if (Token::Match(tok, "++|-- [;,]"))
clarifyStatementError(tok);
//TODO: change the string in order to remove the excessive spaces between the tokens.
clarifyStatementError(tok,
tok2->next()->stringifyList(tok->tokAt(2)),
"("+tok2->next()->stringifyList(tok)+")"+tok->stringifyList(tok->tokAt(2)));
}
}
}
}
void CheckOther::clarifyStatementError(const Token *tok)
void CheckOther::clarifyStatementError(const Token *tok, const std::string &expr, const std::string &suggested)
{
reportError(tok, Severity::warning, "clarifyStatement", "Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'?\n"
"A statement like '*A++;' might not do what you intended. 'operator*' is executed before postfix 'operator++'. "
"Thus, the dereference is meaningless. Did you intend to write '(*A)++;'?");
reportError(tok, Severity::warning, "clarifyStatement", "Ineffective statement: '" + expr + "'. Did you intend to write '" + suggested + "'?\n"
"A statement like '*expr++;' might not do what you intended. 'operator*' is executed before postfix 'operator++'. "
"Thus, the dereference is meaningless. Did you intend to write '(*expr)++;'?");
}
//---------------------------------------------------------------------------

View File

@ -264,7 +264,7 @@ private:
// Error messages..
void clarifyCalculationError(const Token *tok, const std::string &op);
void clarifyConditionError(const Token *tok, bool assign, bool boolop);
void clarifyStatementError(const Token* tok);
void clarifyStatementError(const Token* tok, const std::string &expr, const std::string &suggested);
void sizeofsizeofError(const Token *tok);
void sizeofCalculationError(const Token *tok, bool inconclusive);
void cstyleCastError(const Token *tok);
@ -366,7 +366,7 @@ private:
c.memsetZeroBytesError(0, "varname");
c.clarifyCalculationError(0, "+");
c.clarifyConditionError(0, true, false);
c.clarifyStatementError(0);
c.clarifyStatementError(0,"* A ++ ;","(* A)++ ;");
c.incorrectStringCompareError(0, "substr", "\"Hello World\"", "12");
c.suspiciousStringCompareError(0, "foo");
c.incorrectStringBooleanError(0, "\"Hello World\"");

View File

@ -4007,28 +4007,28 @@ private:
" *c++;\n"
" return c;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'?\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement: '* c ++ ;'. Did you intend to write '(* c)++ ;'?\n", errout.str());
check("char* f(char** c) {\n"
" *c[5]--;\n"
" return *c;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'?\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement: '* c [ 5 ] -- ;'. Did you intend to write '(* c [ 5 ])-- ;'?\n", errout.str());
check("void f(Foo f) {\n"
" *f.a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'?\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement: '* f . a ++ ;'. Did you intend to write '(* f . a)++ ;'?\n", errout.str());
check("void f(Foo f) {\n"
" *f.a[5].v[3]++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'?\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement: '* f . a [ 5 ] . v [ 3 ] ++ ;'. Did you intend to write '(* f . a [ 5 ] . v [ 3 ])++ ;'?\n", errout.str());
check("void f(Foo f) {\n"
" *f.a(1, 5).v[x + y]++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'?\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement: '* f . a ( 1 , 5 ) . v [ x + y ] ++ ;'. Did you intend to write '(* f . a ( 1 , 5 ) . v [ x + y ])++ ;'?\n", errout.str());
check("char* f(char* c) {\n"
" (*c)++;\n"
@ -4045,13 +4045,13 @@ private:
" ***c++;\n"
" return c;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'?\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement: '* * * c ++ ;'. Did you intend to write '(* * * c)++ ;'?\n", errout.str());
check("char** f(char*** c) {\n"
" **c[5]--;\n"
" return **c;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'?\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (warning) Ineffective statement: '* * c [ 5 ] -- ;'. Did you intend to write '(* * c [ 5 ])-- ;'?\n", errout.str());
check("char*** f(char*** c) {\n"
" (***c)++;\n"