From 7fd04cd8d0bdda9e682f8fb03ca975a66f8a9e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 30 Apr 2017 14:22:18 +0200 Subject: [PATCH] Updated Token::expressionString(), write '->' instead of '.' --- lib/token.cpp | 12 ++++++++---- test/testbufferoverrun.cpp | 20 +++++++++++++++----- test/testcondition.cpp | 4 ++-- test/testnullpointer.cpp | 2 +- test/testother.cpp | 4 ++-- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index c05021c61..5f5f00ab0 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1200,13 +1200,17 @@ static const Token* goToRightParenthesis(const Token* start, const Token* end) static std::string stringFromTokenRange(const Token* start, const Token* end) { - std::string ret; + std::ostringstream ret; for (const Token *tok = start; tok && tok != end; tok = tok->next()) { - ret += tok->str(); + if (tok->originalName() == "->") + ret << "->"; + else + ret << tok->str(); if (Token::Match(tok, "%name%|%num% %name%|%num%")) - ret += " "; + ret << ' '; } - return ret + end->str(); + ret << end->str(); + return ret.str(); } std::string Token::expressionString() const diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 10009d820..d6bd32bae 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -460,7 +460,8 @@ private: " struct ABC* x = malloc(sizeof(struct ABC) + 10);\n" " x->str[1] = 0;" "}"); - ASSERT_EQUALS("[test.cpp:10]: (error) Array 'x.str[1]' accessed at index 1, which is out of bounds.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:10]: (error) Array 'x->str[1]' accessed at index 1, which is out of bounds.\n" + "[test.cpp:10]: (error) Array 'x.str[1]' accessed at index 1, which is out of bounds.\n", errout.str()); // This is not out of bounds because it is a variable length array // and the index is within the memory allocated. @@ -582,7 +583,8 @@ private: "{\n" " abc->str[10] = 0;\n" "}"); - ASSERT_EQUALS("[test.cpp:8]: (error) Array 'abc.str[10]' accessed at index 10, which is out of bounds.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:8]: (error) Array 'abc->str[10]' accessed at index 10, which is out of bounds.\n" + "[test.cpp:8]: (error) Array 'abc.str[10]' accessed at index 10, which is out of bounds.\n", errout.str()); } void array_index_9() { @@ -669,7 +671,8 @@ private: " abc->str[10] = 0;\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:13]: (error) Array 'abc.str[10]' accessed at index 10, which is out of bounds.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:13]: (error) Array 'abc->str[10]' accessed at index 10, which is out of bounds.\n" + "[test.cpp:13]: (error) Array 'abc.str[10]' accessed at index 10, which is out of bounds.\n", errout.str()); } void array_index_12() { @@ -1131,6 +1134,9 @@ private: ASSERT_EQUALS("[test.cpp:9]: (error) Array 'test.a[10]' accessed at index 10, which is out of bounds.\n" "[test.cpp:10]: (error) Array 'test.b[10][5]' index test.b[10][2] out of bounds.\n" "[test.cpp:11]: (error) Array 'test.b[10][5]' index test.b[0][19] out of bounds.\n" + "[test.cpp:14]: (error) Array 'ptest->a[10]' accessed at index 10, which is out of bounds.\n" + "[test.cpp:15]: (error) Array 'ptest->b[10][5]' index ptest->b[10][2] out of bounds.\n" + "[test.cpp:16]: (error) Array 'ptest->b[10][5]' index ptest->b[0][19] out of bounds.\n" "[test.cpp:14]: (error) Array 'ptest.a[10]' accessed at index 10, which is out of bounds.\n" "[test.cpp:15]: (error) Array 'ptest.b[10][5]' index ptest.b[10][2] out of bounds.\n" "[test.cpp:16]: (error) Array 'ptest.b[10][5]' index ptest.b[0][19] out of bounds.\n", errout.str()); @@ -1151,6 +1157,8 @@ private: "}"); ASSERT_EQUALS("[test.cpp:8]: (error) Array 'test.a[10][5]' index test.a[9][5] out of bounds.\n" "[test.cpp:9]: (error) Array 'test.a[10][5]' index test.a[0][50] out of bounds.\n" + "[test.cpp:12]: (error) Array 'ptest->a[10][5]' index ptest->a[9][5] out of bounds.\n" + "[test.cpp:13]: (error) Array 'ptest->a[10][5]' index ptest->a[0][50] out of bounds.\n" "[test.cpp:12]: (error) Array 'ptest.a[10][5]' index ptest.a[9][5] out of bounds.\n" "[test.cpp:13]: (error) Array 'ptest.a[10][5]' index ptest.a[0][50] out of bounds.\n", errout.str()); } @@ -2056,7 +2064,8 @@ private: " struct tt *tt=x;\n" " tt->name[22] = 123;\n" "}"); - ASSERT_EQUALS("[test.cpp:7]: (error) Array 'tt.name[21]' accessed at index 22, which is out of bounds.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:7]: (error) Array 'tt->name[21]' accessed at index 22, which is out of bounds.\n" + "[test.cpp:7]: (error) Array 'tt.name[21]' accessed at index 22, which is out of bounds.\n", errout.str()); } void array_index_valueflow() { @@ -2908,7 +2917,8 @@ private: " Fred *f; f = new Fred;\n" " return f->c[10];\n" "}"); - ASSERT_EQUALS("[test.cpp:5]: (error) Array 'f.c[10]' accessed at index 10, which is out of bounds.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (error) Array 'f->c[10]' accessed at index 10, which is out of bounds.\n" + "[test.cpp:5]: (error) Array 'f.c[10]' accessed at index 10, which is out of bounds.\n", errout.str()); check("static const size_t MAX_SIZE = UNAVAILABLE_TO_CPPCHECK;\n" "struct Thing { char data[MAX_SIZE]; };\n" diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 04746785e..82c524dfc 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -1583,7 +1583,7 @@ private: " if (!tok->next()->function() || \n" " (tok->next()->function() && tok->next()->function()->isConstructor()));\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: tok.next().function(). '!A || (A && B)' is equivalent to '!A || B'\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: tok->next()->function(). '!A || (A && B)' is equivalent to '!A || B'\n", errout.str()); check("void f() {\n" " if (!tok->next()->function() || \n" @@ -1601,7 +1601,7 @@ private: " if (!tok->next(1)->function(1) || \n" " (tok->next(1)->function(1) && tok->next(1)->function(1)->isConstructor()));\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: tok.next(1).function(1). '!A || (A && B)' is equivalent to '!A || B'\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: tok->next(1)->function(1). '!A || (A && B)' is equivalent to '!A || B'\n", errout.str()); check("void f() {\n" " if (!tok->next()->function(1) || \n" diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 2d11c340c..1c1c7e0dd 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -323,7 +323,7 @@ private: " if (abc && abc->b == 0)\n" " ;\n" "}"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition 'if(abc&&abc.b==0)' is redundant or there is possible null pointer dereference: abc.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition 'if(abc&&abc->b==0)' is redundant or there is possible null pointer dereference: abc.\n", errout.str()); // ok dereferencing in a condition check("void foo(struct ABC *abc)\n" diff --git a/test/testother.cpp b/test/testother.cpp index 57edb6528..e3b305a1b 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2764,7 +2764,7 @@ private: "void foo(A* a1, A* a2) {\n" " a1->b = a1->b;\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of 'a1.b' to itself.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of 'a1->b' to itself.\n", errout.str()); // #4073 (segmentation fault) check("void Foo::myFunc( int a )\n" @@ -2834,7 +2834,7 @@ private: "void Foo::func() {\n" " this->var = var;\n" "}"); - ASSERT_EQUALS("[test.cpp:6]: (warning) Redundant assignment of 'this.var' to itself.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:6]: (warning) Redundant assignment of 'this->var' to itself.\n", errout.str()); check("class Foo {\n" " int var;\n"