From 7d45e9be7369b5500a3f31b3795a33b28e3e9a83 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Thu, 21 Nov 2013 05:39:23 +0100 Subject: [PATCH] Fixed #5122 (duplInheritedMember on private variables) --- lib/checkclass.cpp | 2 +- test/testclass.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index fafc78aa1..8f762dd4d 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1973,7 +1973,7 @@ void CheckClass::checkDuplInheritedMembers() for (std::list::const_iterator parentClassVarIt = parentClassIt->type->classScope->varlist.begin(); parentClassVarIt != parentClassIt->type->classScope->varlist.end(); ++parentClassVarIt) { - if (classVarIt->name() == parentClassVarIt->name()) { // Check if the class and its parent have a common variable + if (classVarIt->name() == parentClassVarIt->name() && !parentClassVarIt->isPrivate()) { // Check if the class and its parent have a common variable duplInheritedMembersError(classVarIt->nameToken(), parentClassVarIt->nameToken(), classIt->name(), parentClassIt->type->name(), classVarIt->name(), classIt->classScope->type == Scope::eStruct, diff --git a/test/testclass.cpp b/test/testclass.cpp index 9a80b9f61..f2026492f 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -205,7 +205,25 @@ private: "struct Derived : Base {\n" " int x;\n" "};"); - ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:2]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base'.\n", errout.str()); + ASSERT_EQUALS("", errout.str()); + + checkDuplInheritedMembers("class Base {\n" + " protected:\n" + " int x;\n" + "};\n" + "struct Derived : Base {\n" + " int x;\n" + "};"); + ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:3]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base'.\n", errout.str()); + + checkDuplInheritedMembers("class Base {\n" + " protected:\n" + " int x;\n" + "};\n" + "struct Derived : public Base {\n" + " int x;\n" + "};"); + ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:3]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base'.\n", errout.str()); checkDuplInheritedMembers("class Base0 {\n" " int x;\n" @@ -216,8 +234,33 @@ private: "struct Derived : Base0, Base1 {\n" " int x;\n" "};"); - ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:2]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base0'.\n" - "[test.cpp:8] -> [test.cpp:5]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base1'.\n", errout.str()); + ASSERT_EQUALS("", errout.str()); + + checkDuplInheritedMembers("class Base0 {\n" + " protected:\n" + " int x;\n" + "};\n" + "class Base1 {\n" + " int x;\n" + "};\n" + "struct Derived : Base0, Base1 {\n" + " int x;\n" + "};"); + ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base0'.\n", errout.str()); + + checkDuplInheritedMembers("class Base0 {\n" + " protected:\n" + " int x;\n" + "};\n" + "class Base1 {\n" + " public:\n" + " int x;\n" + "};\n" + "struct Derived : Base0, Base1 {\n" + " int x;\n" + "};"); + ASSERT_EQUALS("[test.cpp:10] -> [test.cpp:3]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base0'.\n" + "[test.cpp:10] -> [test.cpp:7]: (warning) The struct 'Derived' defines member variable with name 'x' also defined in its parent class 'Base1'.\n", errout.str()); checkDuplInheritedMembers("class Base {\n" " int x;\n"