Skip to content

Commit ac867b4

Browse files
committed
Handle pointers to const member functions in Tokenizer::simplifyFunctionPointers() (cppcheck-opensource#6603)
1 parent c4ceb97 commit ac867b4

3 files changed

Lines changed: 42 additions & 30 deletions

File tree

lib/tokenize.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5270,10 +5270,13 @@ void Tokenizer:: simplifyFunctionPointers()
52705270
tok = tok->next();
52715271

52725272
// check that the declaration ends
5273-
const Token *endTok = tok->link()->next()->link();
5274-
if (!Token::Match(endTok, ") ;|,|)|=|[|{"))
5273+
Token *endTok = tok->link()->next()->link();
5274+
if (!Token::Match(endTok, ") const| ;|,|)|=|[|{"))
52755275
continue;
52765276

5277+
if (endTok->strAt(1) == "const")
5278+
endTok->deleteNext();
5279+
52775280
// ok simplify this function pointer to an ordinary pointer
52785281
Token::eraseTokens(tok->link(), endTok->next());
52795282
tok->link()->deleteThis();

test/testsimplifytypedef.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,10 +2610,10 @@ class TestSimplifyTypedef : public TestFixture {
26102610
const char expected[] = "C f1 ( ) ; "
26112611
"C * f2 ; " // this gets simplified to a regular pointer
26122612
"C ( & f3 ) ( ) ; "
2613-
"C * f4 ; "
2614-
"C ( C :: * f5 ) ( ) const ; "
2615-
"C * f6 ; " // volatile is removed
2616-
"C ( C :: * f7 ) ( ) const ;"; // volatile is removed
2613+
"C * f4 ; " // this gets simplified to a regular pointer
2614+
"C * f5 ; " // this gets simplified to a regular pointer
2615+
"C * f6 ; " // this gets simplified to a regular pointer
2616+
"C * f7 ;"; // this gets simplified to a regular pointer
26172617
ASSERT_EQUALS(expected, tok(code, true, Settings::Unspecified, false));
26182618
ASSERT_EQUALS("", errout.str());
26192619
}
@@ -2639,10 +2639,10 @@ class TestSimplifyTypedef : public TestFixture {
26392639
const char expected[] = "const C f1 ( ) ; "
26402640
"const C * f2 ; " // this gets simplified to a regular pointer
26412641
"const C ( & f3 ) ( ) ; "
2642-
"const C * f4 ; "
2643-
"const C ( C :: * f5 ) ( ) const ; "
2644-
"const C * f6 ; " // volatile is removed
2645-
"const C ( C :: * f7 ) ( ) const ;"; // volatile is removed
2642+
"const C * f4 ; " // this gets simplified to a regular pointer
2643+
"const C * f5 ; " // this gets simplified to a regular pointer
2644+
"const C * f6 ; " // this gets simplified to a regular pointer
2645+
"const C * f7 ;"; // this gets simplified to a regular pointer
26462646
ASSERT_EQUALS(expected, tok(code, true, Settings::Unspecified, false));
26472647
ASSERT_EQUALS("", errout.str());
26482648
}
@@ -2667,10 +2667,10 @@ class TestSimplifyTypedef : public TestFixture {
26672667
const char expected[] = "const C f1 ( ) ; "
26682668
"const C * f2 ; " // this gets simplified to a regular pointer
26692669
"const C ( & f3 ) ( ) ; "
2670-
"const C * f4 ; "
2671-
"const C ( C :: * f5 ) ( ) const ; "
2672-
"const C * f6 ; " // volatile is removed
2673-
"const C ( C :: * f7 ) ( ) const ;"; // volatile is removed
2670+
"const C * f4 ; " // this gets simplified to a regular pointer
2671+
"const C * f5 ; " // this gets simplified to a regular pointer
2672+
"const C * f6 ; " // this gets simplified to a regular pointer
2673+
"const C * f7 ;"; // this gets simplified to a regular pointer
26742674
ASSERT_EQUALS(expected, tok(code, true, Settings::Unspecified, false));
26752675
ASSERT_EQUALS("", errout.str());
26762676
}
@@ -2695,10 +2695,10 @@ class TestSimplifyTypedef : public TestFixture {
26952695
const char expected[] = "C * f1 ( ) ; "
26962696
"C * * f2 ; " // this gets simplified to a regular pointer
26972697
"C * ( & f3 ) ( ) ; "
2698-
"C * * f4 ; "
2699-
"C * ( C :: * f5 ) ( ) const ; "
2700-
"C * * f6 ; " // volatile is removed
2701-
"C * ( C :: * f7 ) ( ) const ;"; // volatile is removed
2698+
"C * * f4 ; " // this gets simplified to a regular pointer
2699+
"C * * f5 ; " // this gets simplified to a regular pointer
2700+
"C * * f6 ; " // this gets simplified to a regular pointer
2701+
"C * * f7 ;"; // this gets simplified to a regular pointer
27022702
ASSERT_EQUALS(expected, tok(code, true, Settings::Unspecified, false));
27032703
ASSERT_EQUALS("", errout.str());
27042704
}
@@ -2723,10 +2723,10 @@ class TestSimplifyTypedef : public TestFixture {
27232723
const char expected[] = "const C * f1 ( ) ; "
27242724
"const C * * f2 ; " // this gets simplified to a regular pointer
27252725
"const C * ( & f3 ) ( ) ; "
2726-
"const C * * f4 ; "
2727-
"const C * ( C :: * f5 ) ( ) const ; "
2728-
"const C * * f6 ; " // volatile is removed
2729-
"const C * ( C :: * f7 ) ( ) const ;"; // volatile is removed
2726+
"const C * * f4 ; " // this gets simplified to a regular pointer
2727+
"const C * * f5 ; " // this gets simplified to a regular pointer
2728+
"const C * * f6 ; " // this gets simplified to a regular pointer
2729+
"const C * * f7 ;"; // this gets simplified to a regular pointer
27302730
ASSERT_EQUALS(expected, tok(code, true, Settings::Unspecified, false));
27312731
ASSERT_EQUALS("", errout.str());
27322732
}
@@ -2752,10 +2752,10 @@ class TestSimplifyTypedef : public TestFixture {
27522752
const char expected[] = "const C * f1 ( ) ; "
27532753
"const C * * f2 ; " // this gets simplified to a regular pointer
27542754
"const C * ( & f3 ) ( ) ; "
2755-
"const C * * f4 ; "
2756-
"const C * ( C :: * f5 ) ( ) const ; "
2757-
"const C * * f6 ; " // volatile is removed
2758-
"const C * ( C :: * f7 ) ( ) const ;"; // volatile is removed
2755+
"const C * * f4 ; " // this gets simplified to a regular pointer
2756+
"const C * * f5 ; " // this gets simplified to a regular pointer
2757+
"const C * * f6 ; " // this gets simplified to a regular pointer
2758+
"const C * * f7 ;"; // this gets simplified to a regular pointer
27592759
ASSERT_EQUALS(expected, tok(code, true, Settings::Unspecified, false));
27602760
ASSERT_EQUALS("", errout.str());
27612761
}
@@ -2898,9 +2898,9 @@ class TestSimplifyTypedef : public TestFixture {
28982898

28992899
// The expected result..
29002900
const char expected[] = "B :: C * f1 ; "
2901-
"B :: C ( B :: C :: * f2 ) ( ) const ; "
2901+
"B :: C * f2 ; "
29022902
"B :: C * f3 ; "
2903-
"B :: C ( B :: C :: * f4 ) ( ) const ;";
2903+
"B :: C * f4 ;";
29042904
ASSERT_EQUALS(expected, tok(code, true, Settings::Unspecified, false));
29052905
ASSERT_EQUALS("", errout.str());
29062906
}
@@ -2936,9 +2936,9 @@ class TestSimplifyTypedef : public TestFixture {
29362936

29372937
// The expected result..
29382938
const char expected[] = "A :: B :: C * f1 ; "
2939-
"A :: B :: C ( A :: B :: C :: * f2 ) ( ) const ; "
2939+
"A :: B :: C * f2 ; "
29402940
"A :: B :: C * f3 ; "
2941-
"A :: B :: C ( A :: B :: C :: * f4 ) ( ) const ;";
2941+
"A :: B :: C * f4 ;";
29422942
ASSERT_EQUALS(expected, tok(code, true, Settings::Unspecified, false));
29432943
ASSERT_EQUALS("", errout.str());
29442944
}

test/testunusedvar.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class TestUnusedVar : public TestFixture {
9090
TEST_CASE(localvar44); // ticket #4020
9191
TEST_CASE(localvar45); // ticket #4899
9292
TEST_CASE(localvar46); // ticket #5491 (C++11 style initialization)
93+
TEST_CASE(localvar47); // ticket #6603
9394
TEST_CASE(localvaralias1);
9495
TEST_CASE(localvaralias2); // ticket #1637
9596
TEST_CASE(localvaralias3); // ticket #1639
@@ -1885,6 +1886,14 @@ class TestUnusedVar : public TestFixture {
18851886
ASSERT_EQUALS("", errout.str());
18861887
}
18871888

1889+
void localvar47() { // #6603
1890+
functionVariableUsage("void f() {\n"
1891+
" int (SfxUndoManager::*retrieveCount)(bool) const\n"
1892+
" = (flag) ? &SfxUndoManager::foo : &SfxUndoManager::bar;\n"
1893+
"}");
1894+
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'retrieveCount' is assigned a value that is never used.\n", errout.str());
1895+
}
1896+
18881897
void localvaralias1() {
18891898
functionVariableUsage("void foo()\n"
18901899
"{\n"

0 commit comments

Comments
 (0)