Fixed #2059 (cppcheck gives wrong path in error description)

This commit is contained in:
Daniel Marjamäki 2010-09-22 19:52:14 +02:00
parent 7e954ebb57
commit 3f79faac2a
6 changed files with 7 additions and 49 deletions

View File

@ -108,7 +108,7 @@ unsigned int CppCheck::check()
if (_settings._errorsOnly == false) if (_settings._errorsOnly == false)
{ {
std::string fixedpath(fname); std::string fixedpath(fname);
fixedpath = Path::simplifyPath(fixedpath); fixedpath = getFileLister()->simplifyPath(fixedpath.c_str());
fixedpath = Path::toNativeSeparators(fixedpath); fixedpath = Path::toNativeSeparators(fixedpath);
_errorLogger.reportOut(std::string("Checking ") + fixedpath + std::string("...")); _errorLogger.reportOut(std::string("Checking ") + fixedpath + std::string("..."));
} }
@ -166,7 +166,7 @@ unsigned int CppCheck::check()
// If only errors are printed, print filename after the check // If only errors are printed, print filename after the check
if (_settings._errorsOnly == false && it != configurations.begin()) if (_settings._errorsOnly == false && it != configurations.begin())
{ {
std::string fixedpath = Path::simplifyPath(fname); std::string fixedpath = getFileLister()->simplifyPath(fname.c_str());
fixedpath = Path::toNativeSeparators(fixedpath); fixedpath = Path::toNativeSeparators(fixedpath);
_errorLogger.reportOut(std::string("Checking ") + fixedpath + ": " + cfg + std::string("...")); _errorLogger.reportOut(std::string("Checking ") + fixedpath + ": " + cfg + std::string("..."));
} }

View File

@ -17,6 +17,7 @@
*/ */
#include "errorlogger.h" #include "errorlogger.h"
#include "filelister.h"
#include "path.h" #include "path.h"
#include <sstream> #include <sstream>
@ -219,8 +220,7 @@ std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMes
std::string ErrorLogger::ErrorMessage::FileLocation::getfile(bool convert) const std::string ErrorLogger::ErrorMessage::FileLocation::getfile(bool convert) const
{ {
std::string f(_file); std::string f = getFileLister()->simplifyPath(_file.c_str());
f = Path::simplifyPath(f);
if (convert) if (convert)
f = Path::toNativeSeparators(f); f = Path::toNativeSeparators(f);

View File

@ -73,7 +73,7 @@ std::string FileLister::simplifyPath(const char *originalPath)
for (unsigned int i = 0; i < pathParts.size(); ++i) for (unsigned int i = 0; i < pathParts.size(); ++i)
{ {
if (pathParts[i] == ".." && i > 1 && pathParts.size() > i + 1) if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1)
{ {
pathParts.erase(pathParts.begin() + static_cast<int>(i) + 1); pathParts.erase(pathParts.begin() + static_cast<int>(i) + 1);
pathParts.erase(pathParts.begin() + static_cast<int>(i)); pathParts.erase(pathParts.begin() + static_cast<int>(i));

View File

@ -42,39 +42,3 @@ std::string Path::fromNativeSeparators(const std::string &path)
return modified; return modified;
} }
std::string Path::simplifyPath(const std::string &path)
{
std::string f(path);
// Remove './' from begin of the path
if (f.size() > 2 && f[0] == '.' && f[1] == '/')
f = f.erase(0, 2);
// replace "/ab/../" with "/"..
std::string::size_type pos = 0;
while ((pos = f.find("..", pos + 1)) != std::string::npos)
{
// position must be at least 4..
if (pos < 4)
continue;
// Previous char must be a separator..
if (f[pos-1] != '/')
continue;
// Next char must be a separator..
if (f[pos+2] != '/')
continue;
// Locate previous separator..
std::string::size_type sep = f.find_last_of("/", pos - 2);
if (sep == std::string::npos)
continue;
// Delete substring..
f.erase(sep, pos + 2 - sep);
pos = sep;
}
return f;
}

View File

@ -47,14 +47,6 @@ public:
* @return converted path. * @return converted path.
*/ */
static std::string fromNativeSeparators(const std::string &path); static std::string fromNativeSeparators(const std::string &path);
/**
* Simplify given path.
* This method simplifies the path removing reduntant parts.
* @param path Path string to simplify.
* @return simplified path.
*/
static std::string simplifyPath(const std::string &path);
}; };
/// @} /// @}

View File

@ -52,6 +52,8 @@ private:
ASSERT_EQUALS("../path/index.h", getFileLister()->simplifyPath("../path/other/../index.h")); ASSERT_EQUALS("../path/index.h", getFileLister()->simplifyPath("../path/other/../index.h"));
ASSERT_EQUALS("a/index.h", getFileLister()->simplifyPath("a/../a/index.h")); ASSERT_EQUALS("a/index.h", getFileLister()->simplifyPath("a/../a/index.h"));
ASSERT_EQUALS("a/..", getFileLister()->simplifyPath("a/..")); ASSERT_EQUALS("a/..", getFileLister()->simplifyPath("a/.."));
ASSERT_EQUALS("../../src/test.cpp", getFileLister()->simplifyPath("../../src/test.cpp"));
ASSERT_EQUALS("../../../src/test.cpp", getFileLister()->simplifyPath("../../../src/test.cpp"));
} }
}; };