Fixed #2067 (Template methods do not 'use' private ones)
This commit is contained in:
parent
415cbc63c3
commit
6eeed00888
|
@ -1245,6 +1245,11 @@ void CheckClass::privateFunctions()
|
||||||
if (!_settings->_checkCodingStyle)
|
if (!_settings->_checkCodingStyle)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// don't check code that contains templates. Templates that are
|
||||||
|
// "unused" are removed from the code. #2067
|
||||||
|
if (_tokenizer->codeWithTemplates())
|
||||||
|
return;
|
||||||
|
|
||||||
// dont check borland classes with properties..
|
// dont check borland classes with properties..
|
||||||
if (Token::findmatch(_tokenizer->tokens(), "; __property ;"))
|
if (Token::findmatch(_tokenizer->tokens(), "; __property ;"))
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -50,6 +50,7 @@ Tokenizer::Tokenizer()
|
||||||
{
|
{
|
||||||
_tokens = 0;
|
_tokens = 0;
|
||||||
_tokensBack = 0;
|
_tokensBack = 0;
|
||||||
|
_codeWithTemplates = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger)
|
Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger)
|
||||||
|
@ -57,6 +58,7 @@ Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger)
|
||||||
{
|
{
|
||||||
_tokens = 0;
|
_tokens = 0;
|
||||||
_tokensBack = 0;
|
_tokensBack = 0;
|
||||||
|
_codeWithTemplates = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Tokenizer::~Tokenizer()
|
Tokenizer::~Tokenizer()
|
||||||
|
@ -2308,6 +2310,8 @@ void Tokenizer::simplifyTemplates()
|
||||||
{
|
{
|
||||||
if (Token::simpleMatch(tok, "template <"))
|
if (Token::simpleMatch(tok, "template <"))
|
||||||
{
|
{
|
||||||
|
_codeWithTemplates = true;
|
||||||
|
|
||||||
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
||||||
{
|
{
|
||||||
// Just a declaration => ignore this
|
// Just a declaration => ignore this
|
||||||
|
|
|
@ -516,6 +516,12 @@ public:
|
||||||
|
|
||||||
void unsupportedTypedef(const Token *tok) const;
|
void unsupportedTypedef(const Token *tok) const;
|
||||||
|
|
||||||
|
/** Was there templates in the code? */
|
||||||
|
bool codeWithTemplates() const
|
||||||
|
{
|
||||||
|
return _codeWithTemplates;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/** Disable copy constructor, no implementation */
|
/** Disable copy constructor, no implementation */
|
||||||
Tokenizer(const Tokenizer &);
|
Tokenizer(const Tokenizer &);
|
||||||
|
@ -532,6 +538,12 @@ private:
|
||||||
/** E.g. "A" for code where "#ifdef A" is true. This is used to
|
/** E.g. "A" for code where "#ifdef A" is true. This is used to
|
||||||
print additional information in error situations. */
|
print additional information in error situations. */
|
||||||
std::string _configuration;
|
std::string _configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* was there any templates? templates that are "unused" are
|
||||||
|
* removed from the token list
|
||||||
|
*/
|
||||||
|
bool _codeWithTemplates;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
|
|
|
@ -54,6 +54,9 @@ private:
|
||||||
TEST_CASE(derivedClass); // skip warning for derived classes. It might be a virtual function.
|
TEST_CASE(derivedClass); // skip warning for derived classes. It might be a virtual function.
|
||||||
|
|
||||||
TEST_CASE(borland); // skip FP when using __property
|
TEST_CASE(borland); // skip FP when using __property
|
||||||
|
|
||||||
|
// No false positives when there are "unused" templates that are removed in the simplified token list
|
||||||
|
TEST_CASE(template1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -391,6 +394,25 @@ private:
|
||||||
"};");
|
"};");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void template1()
|
||||||
|
{
|
||||||
|
// ticket #2067 - Template methods do not "use" private ones
|
||||||
|
check("class A {\n"
|
||||||
|
"public:\n"
|
||||||
|
" template <class T>\n"
|
||||||
|
" T getVal() const;\n"
|
||||||
|
"\n"
|
||||||
|
"private:\n"
|
||||||
|
" int internalGetVal() const { return 8000; }\n"
|
||||||
|
"};\n"
|
||||||
|
"\n"
|
||||||
|
"template <class T>\n"
|
||||||
|
"T A::getVal() const {\n"
|
||||||
|
" return internalGetVal();\n"
|
||||||
|
"};\n");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
REGISTER_TEST(TestUnusedPrivateFunction)
|
REGISTER_TEST(TestUnusedPrivateFunction)
|
||||||
|
|
Loading…
Reference in New Issue