From f8591f90041c76b2f8882e8cbedb2a87aa5ed7a7 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Tue, 4 Sep 2012 02:03:38 +0200 Subject: [PATCH] CheckOther::clarifyStatement: improved message to show what's the 'bad' and the 'good' expression. --- lib/checkother.cpp | 13 ++++++++----- lib/checkother.h | 4 ++-- test/testother.cpp | 14 +++++++------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 331380c12..18d0f7e10 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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)++;'?"); } //--------------------------------------------------------------------------- diff --git a/lib/checkother.h b/lib/checkother.h index bed34b1e6..3ab32d658 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -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\""); diff --git a/test/testother.cpp b/test/testother.cpp index efe309058..44b0bad98 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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"