Function list: Remove functions with duplicate names to prevent false positives

This commit is contained in:
Daniel Marjamäki 2008-11-20 19:18:55 +00:00
parent 2e445b195c
commit 63bc26d662
3 changed files with 409 additions and 353 deletions

View File

@ -1,4 +1,4 @@
/*
/*
* c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki
*
@ -22,6 +22,7 @@
#include "testsuite.h"
#define UNIT_TESTING // Get access to "private" data in Tokenizer
#include "tokenize.h"
class TestTokenizer : public TestFixture
@ -38,6 +39,8 @@ private:
TEST_CASE( longtok );
TEST_CASE( inlineasm );
TEST_CASE( dupfuncname );
}
@ -131,6 +134,27 @@ private:
tokenizer.DeallocateTokens();
}
void dupfuncname()
{
const char code[] = "void a()\n"
"{ }\n"
"void a(int i)\n"
"{ }\n"
"void b()\n"
"{ }\n";
// tokenize..
Tokenizer tokenizer;
tokenizer.getFiles()->push_back( "test.cpp" );
std::istringstream istr(code);
tokenizer.TokenizeCode(istr, 0);
tokenizer.FillFunctionList(0);
ASSERT_EQUALS( 1, tokenizer.FunctionList.size() );
ASSERT_EQUALS( std::string("b"), tokenizer.FunctionList[0]->str );
}
};
REGISTER_TEST( TestTokenizer )

View File

@ -1,4 +1,4 @@
/*
/*
* c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki
*
@ -1120,12 +1120,11 @@ void Tokenizer::DeallocateTokens()
const TOKEN *Tokenizer::GetFunctionTokenByName( const char funcname[] ) const
{
std::list<const TOKEN *>::const_iterator it;
for ( it = FunctionList.begin(); it != FunctionList.end(); it++ )
for ( unsigned int i = 0; i < FunctionList.size(); ++i )
{
if ( strcmp( (*it)->str, funcname ) == 0 )
if ( strcmp( FunctionList[i]->str, funcname ) == 0 )
{
return *it;
return FunctionList[i];
}
}
return NULL;
@ -1220,6 +1219,35 @@ void Tokenizer::FillFunctionList(const unsigned int file_id)
}
}
// If the FunctionList functions with duplicate names, remove them
// TODO this will need some better handling
for ( unsigned int func1 = 0; func1 < FunctionList.size(); )
{
bool hasDuplicates = false;
for ( unsigned int func2 = func1 + 1; func2 < FunctionList.size(); )
{
if ( strcmp(FunctionList[func1]->str, FunctionList[func2]->str) == 0 )
{
hasDuplicates = true;
FunctionList.erase( FunctionList.begin() + func2 );
}
else
{
++func2;
}
}
if ( ! hasDuplicates )
{
++func1;
}
else
{
FunctionList.erase( FunctionList.begin() + func1 );
}
}
for (std::list<const char *>::const_iterator it = _usedfunc.begin(); it != _usedfunc.end(); ++it)
{
if ( *it != 0 )

View File

@ -1,4 +1,4 @@
/*
/*
* c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki
*
@ -95,7 +95,11 @@ public:
void CheckGlobalFunctionUsage(const std::vector<std::string> &filenames);
void settings( const Settings &settings );
const TOKEN *tokens() const;
#ifndef UNIT_TESTING
private:
#endif
struct DefineSymbol
{
@ -135,7 +139,7 @@ private:
TOKEN *tokens_back;
std::map<std::string, unsigned int> TypeSize;
std::list<const TOKEN *> FunctionList;
std::vector<const TOKEN *> FunctionList;
std::list< GlobalFunction > GlobalFunctions;
std::list< GlobalFunction > UsedGlobalFunctions;
std::vector<std::string> Files;