Fixed bug, paths like "/path/../index.h" created duplicate warnings with files like "/index.h". Relative paths are now simplified to

look a like. Test cases added.
This commit is contained in:
Reijo Tomperi 2008-12-14 19:03:34 +00:00
parent fec777057d
commit a15381dcbe
4 changed files with 66 additions and 6 deletions

View File

@ -17,7 +17,9 @@
*/
#include "FileLister.h"
#include <sstream>
#include <sstream>
#include <vector>
#include <string>
#ifdef __GNUC__
#include <glob.h>
@ -26,6 +28,61 @@
#if defined(__BORLANDC__) || defined(_MSC_VER)
#include <windows.h>
#endif
std::string FileLister::simplifyPath( const char *originalPath )
{
std::string subPath = "";
std::vector<std::string> pathParts;
for( ; *originalPath; ++originalPath )
{
if( *originalPath == '/' )
{
if( subPath.length() > 0 )
{
pathParts.push_back( subPath );
subPath = "";
}
pathParts.push_back( "/" );
}
else
subPath.append( 1, *originalPath );
}
if( subPath.length() > 0 )
pathParts.push_back( subPath );
for( std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i )
{
if( pathParts[i] == ".." && i > 1 )
{
pathParts.erase( pathParts.begin() + i );
pathParts.erase( pathParts.begin()+i-1 );
pathParts.erase( pathParts.begin()+i-2 );
i = 0;
}
else if( i > 0 && pathParts[i] == "." )
{
pathParts.erase( pathParts.begin()+i );
i = 0;
}
else if( pathParts[i] == "/" && i > 0 && pathParts[i-1] == "/" )
{
pathParts.erase( pathParts.begin()+i-1 );
i = 0;
}
}
std::ostringstream oss;
for( std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i )
{
oss << pathParts[i];
}
return oss.str();
}
bool FileLister::AcceptFile( const std::string &filename )
{

View File

@ -39,7 +39,7 @@ class FileLister
{
public:
static void RecursiveAddFiles( std::vector<std::string> &filenames, const std::string &path, bool recursive );
static std::string simplifyPath( const char *originalPath );
private:
static bool AcceptFile( const std::string &filename );
};

View File

@ -1,6 +1,6 @@
SRCS=CheckBufferOverrun.cpp CheckClass.cpp CheckHeaders.cpp CheckMemoryLeak.cpp CheckFunctionUsage.cpp CheckOther.cpp FileLister.cpp preprocessor.cpp tokenize.cpp cppcheck.cpp settings.cpp token.cpp cppcheckexecutor.cpp
OBJS=$(SRCS:%.cpp=%.o)
TESTS=testbufferoverrun.o testcharvar.o testclass.o testconstructors.o testdivision.o testfunctionusage.o testincompletestatement.o testmemleak.o testpreprocessor.o testsimplifytokens.o testtokenize.o testtoken.o testunusedprivfunc.o testunusedvar.o
TESTS=testbufferoverrun.o testcharvar.o testclass.o testconstructors.o testdivision.o testfunctionusage.o testincompletestatement.o testmemleak.o testpreprocessor.o testsimplifytokens.o testtokenize.o testtoken.o testunusedprivfunc.o testunusedvar.o testfilelister.o
BIN = ${DESTDIR}/usr/bin
all: ${OBJS} main.o
@ -63,9 +63,11 @@ testunusedprivfunc.o: testunusedprivfunc.cpp tokenize.h settings.h errorlogger.h
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
testunusedvar.o: testunusedvar.cpp testsuite.h errorlogger.h tokenize.h settings.h token.h CheckOther.h
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
testfilelister.o: testfilelister.cpp FileLister.h
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
token.o: token.cpp token.h
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
tokenize.o: tokenize.cpp tokenize.h settings.h errorlogger.h token.h
tokenize.o: tokenize.cpp tokenize.h settings.h errorlogger.h token.h FileLister.h
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
clean:

View File

@ -33,7 +33,8 @@
#include <list>
#include <algorithm>
#include <stdlib.h> // <- strtoul
#include <stdio.h>
#include <stdio.h>
#include <FileLister.h>
#ifdef __BORLANDC__
#include <ctype.h>
@ -237,7 +238,7 @@ void Tokenizer::tokenize(std::istream &code, const char FileName[])
}
// The "_files" vector remembers what files have been tokenized..
_files.push_back(FileName);
_files.push_back( FileLister::simplifyPath( FileName ) );
// Tokenize the file..
tokenizeCode( code, (unsigned int)(_files.size() - 1) );