diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 42c1f3bdf..960aef88d 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -108,7 +108,7 @@ unsigned int CppCheck::check() if (_settings._errorsOnly == false) { std::string fixedpath(fname); - fixedpath = Path::simplifyPath(fixedpath); + fixedpath = getFileLister()->simplifyPath(fixedpath.c_str()); fixedpath = Path::toNativeSeparators(fixedpath); _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 (_settings._errorsOnly == false && it != configurations.begin()) { - std::string fixedpath = Path::simplifyPath(fname); + std::string fixedpath = getFileLister()->simplifyPath(fname.c_str()); fixedpath = Path::toNativeSeparators(fixedpath); _errorLogger.reportOut(std::string("Checking ") + fixedpath + ": " + cfg + std::string("...")); } diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 33db7144b..6ca014d00 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -17,6 +17,7 @@ */ #include "errorlogger.h" +#include "filelister.h" #include "path.h" #include @@ -219,8 +220,7 @@ std::string ErrorLogger::callStackToString(const std::listsimplifyPath(_file.c_str()); if (convert) f = Path::toNativeSeparators(f); diff --git a/lib/filelister.cpp b/lib/filelister.cpp index 8aff600e4..6e5748309 100644 --- a/lib/filelister.cpp +++ b/lib/filelister.cpp @@ -73,7 +73,7 @@ std::string FileLister::simplifyPath(const char *originalPath) 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(i) + 1); pathParts.erase(pathParts.begin() + static_cast(i)); diff --git a/lib/path.cpp b/lib/path.cpp index c2d4ac3f0..2f3374b3f 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -42,39 +42,3 @@ std::string Path::fromNativeSeparators(const std::string &path) 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; -} diff --git a/lib/path.h b/lib/path.h index 21781f8b1..f38213112 100644 --- a/lib/path.h +++ b/lib/path.h @@ -47,14 +47,6 @@ public: * @return converted 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); }; /// @} diff --git a/test/testfilelister.cpp b/test/testfilelister.cpp index 7d8c6cca5..08cc64b39 100644 --- a/test/testfilelister.cpp +++ b/test/testfilelister.cpp @@ -52,6 +52,8 @@ private: 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/..", getFileLister()->simplifyPath("a/..")); + ASSERT_EQUALS("../../src/test.cpp", getFileLister()->simplifyPath("../../src/test.cpp")); + ASSERT_EQUALS("../../../src/test.cpp", getFileLister()->simplifyPath("../../../src/test.cpp")); } };