Fixed #2067 (Template methods do not 'use' private ones)

This commit is contained in:
Daniel Marjamäki 2010-09-30 21:22:49 +02:00
parent 415cbc63c3
commit 6eeed00888
4 changed files with 43 additions and 0 deletions

View File

@ -1245,6 +1245,11 @@ void CheckClass::privateFunctions()
if (!_settings->_checkCodingStyle)
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..
if (Token::findmatch(_tokenizer->tokens(), "; __property ;"))
return;

View File

@ -50,6 +50,7 @@ Tokenizer::Tokenizer()
{
_tokens = 0;
_tokensBack = 0;
_codeWithTemplates = false;
}
Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger)
@ -57,6 +58,7 @@ Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger)
{
_tokens = 0;
_tokensBack = 0;
_codeWithTemplates = false;
}
Tokenizer::~Tokenizer()
@ -2308,6 +2310,8 @@ void Tokenizer::simplifyTemplates()
{
if (Token::simpleMatch(tok, "template <"))
{
_codeWithTemplates = true;
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
{
// Just a declaration => ignore this

View File

@ -516,6 +516,12 @@ public:
void unsupportedTypedef(const Token *tok) const;
/** Was there templates in the code? */
bool codeWithTemplates() const
{
return _codeWithTemplates;
}
private:
/** Disable copy constructor, no implementation */
Tokenizer(const Tokenizer &);
@ -532,6 +538,12 @@ private:
/** E.g. "A" for code where "#ifdef A" is true. This is used to
print additional information in error situations. */
std::string _configuration;
/**
* was there any templates? templates that are "unused" are
* removed from the token list
*/
bool _codeWithTemplates;
};
/// @}

View File

@ -54,6 +54,9 @@ private:
TEST_CASE(derivedClass); // skip warning for derived classes. It might be a virtual function.
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());
}
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)