Skip to content

Commit c23c6f3

Browse files
Fix #13642 FP functionConst with reinterpret_cast of member address (danmar#7308)
1 parent 7a0804e commit c23c6f3

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/checkclass.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,8 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
24002400
!(argVar->valueType() && argVar->valueType()->isConst(argVar->valueType()->pointer)))) { // argument might be modified
24012401
const Token* arg = args[argIndex];
24022402
// Member variable given as parameter
2403+
while (Token::simpleMatch(arg, "(") && arg->astOperand2())
2404+
arg = arg->astOperand2();
24032405
const Token* varTok = previousBeforeAstLeftmostLeaf(arg);
24042406
if (!varTok)
24052407
return false;

test/testclass.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class TestClass : public TestFixture {
190190
TEST_CASE(const95); // #13320 - do not warn about r-value ref method
191191
TEST_CASE(const96);
192192
TEST_CASE(const97);
193+
TEST_CASE(const98);
193194

194195
TEST_CASE(const_handleDefaultParameters);
195196
TEST_CASE(const_passThisToMemberOfOtherClass);
@@ -6786,6 +6787,56 @@ class TestClass : public TestFixture {
67866787
errout_str());
67876788
}
67886789

6790+
void const98() { // #13642
6791+
checkConst("enum E {\n"
6792+
" E0,\n"
6793+
" E1\n"
6794+
"};\n"
6795+
"void set(int* p) {\n"
6796+
" *p = 1;\n"
6797+
"}\n"
6798+
"struct S {\n"
6799+
" E e;\n"
6800+
" void f() {\n"
6801+
" set(reinterpret_cast<int*>(&e));\n"
6802+
" }\n"
6803+
" void g() {\n"
6804+
" set(reinterpret_cast<int*>(reinterpret_cast<void*>(&e)));\n"
6805+
" }\n"
6806+
" void h() {\n"
6807+
" set((int*)(&e));\n"
6808+
" }\n"
6809+
"};\n");
6810+
ASSERT_EQUALS("", errout_str());
6811+
6812+
checkConst("enum E {\n"
6813+
" E0,\n"
6814+
" E1\n"
6815+
"};\n"
6816+
"void set1(int i, int* p) {\n"
6817+
" *p = i;\n"
6818+
"}\n"
6819+
"void set2(int* p, int i) {\n"
6820+
" *p = i;\n"
6821+
"}\n"
6822+
"struct S {\n"
6823+
" E e;\n"
6824+
" void f1() {\n"
6825+
" set1(1, reinterpret_cast<int*>(&e));\n"
6826+
" }\n"
6827+
" void f2() {\n"
6828+
" set2(reinterpret_cast<int*>(&e), 1);\n"
6829+
" }\n"
6830+
" void g1() {\n"
6831+
" set1(1, reinterpret_cast<int*>(reinterpret_cast<void*>(&e)));\n"
6832+
" }\n"
6833+
" void g2() {\n"
6834+
" set2(reinterpret_cast<int*>(reinterpret_cast<void*>(&e)), 1);\n"
6835+
" }\n"
6836+
"};\n");
6837+
ASSERT_EQUALS("", errout_str());
6838+
}
6839+
67896840
void const_handleDefaultParameters() {
67906841
checkConst("struct Foo {\n"
67916842
" void foo1(int i, int j = 0) {\n"

0 commit comments

Comments
 (0)