From 1c58628223c489de8c48841344d2b58010780bcb Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 28 Mar 2014 16:10:27 +0100 Subject: [PATCH] Handle pointer/reference to array in setVarId (#2645) --- lib/tokenize.cpp | 5 ++++- test/testbufferoverrun.cpp | 2 +- test/testtokenize.cpp | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 097f67475..5fb190f00 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2178,6 +2178,7 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::mapisName()) { if (tok2->str() == "struct" || tok2->str() == "union" || (cpp && (tok2->str() == "class" || tok2->str() == "typename"))) { @@ -2199,7 +2200,9 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map|>>")) break; } else if (tok2->str() == "&" || tok2->str() == "&&") { - ref = true; + ref = !bracket; + } else if (typeCount == 1 && tok2->str() == "(" && Token::Match(tok2->link()->next(), "(|[")) { + bracket = true; // Skip: Seems to be valid pointer to array or function pointer } else if (tok2->str() != "*" && tok2->str() != "::") { break; } diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 6769e8f8e..3175e99ec 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -3684,7 +3684,7 @@ private: } void crash5() { - check("static f() { int i; int source[1] = { 1 }; for (i = 0; i < 4; i++) (u, if (y u.x e)) }"); // Garbage code + check("static f() { int i; int source[1] = { 1 }; for (i = 0; i < 4; i++) (u, if (y u.x e)) }", true, "test.cpp", false); // Garbage code } void garbage1() { // Ticket #5203 diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 35101ce46..cb841f739 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -304,6 +304,7 @@ private: TEST_CASE(varid_arrayFuncPar); // #5294 TEST_CASE(varid_sizeofPassed); // #5295 TEST_CASE(varid_classInFunction); // #5293 + TEST_CASE(varid_pointerToArray); // #2645 TEST_CASE(varidclass1); TEST_CASE(varidclass2); @@ -4737,6 +4738,32 @@ private: "}")); } + void varid_pointerToArray() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: int ( * a1@1 ) [ 10 ] ;\n" + "2: void f1 ( ) {\n" + "3: int ( * a2@2 ) [ 10 ] ;\n" + "4: int ( & a3@3 ) [ 10 ] ;\n" + "5: }\n" + "6: struct A {\n" + "7: int ( & a4@4 ) [ 10 ] ;\n" + "8: int f2 ( int i@5 ) { return a4@4 [ i@5 ] ; }\n" + "9: int f3 ( int ( & a5@6 ) [ 10 ] , int i@7 ) { return a5@6 [ i@7 ] ; }\n" + "10: } ;\n" + "11: int f4 ( int ( & a6@8 ) [ 10 ] , int i@9 ) { return a6@8 [ i@9 ] ; }\n", + tokenizeDebugListing("int (*a1)[10];\n" // pointer to array of 10 ints + "void f1() {\n" + " int(*a2)[10];\n" + " int(&a3)[10];\n" + "}\n" + "struct A {\n" + " int(&a4)[10];\n" + " int f2(int i) { return a4[i]; }\n" + " int f3(int(&a5)[10], int i) { return a5[i]; }\n" + "};\n" + "int f4(int(&a6)[10], int i) { return a6[i]; }")); + } + void varidclass1() { const std::string actual = tokenizeDebugListing( "class Fred\n" @@ -4975,11 +5002,11 @@ private: "##file 0\n" "1: class A {\n" "2: public:\n" - "3: void f ( char ( & cl ) [ 10 ] ) ;\n" - "4: void g ( char cl@1 [ 10 ] ) ;\n" + "3: void f ( char ( & cl@1 ) [ 10 ] ) ;\n" + "4: void g ( char cl@2 [ 10 ] ) ;\n" "5: }\n" - "6: void Fred :: f ( char ( & cl ) [ 10 ] ) {\n" - "7: sizeof ( cl ) ;\n" + "6: void Fred :: f ( char ( & cl@3 ) [ 10 ] ) {\n" + "7: sizeof ( cl@3 ) ;\n" "8: }\n"); ASSERT_EQUALS(expected, tokenizeDebugListing(code));