Revert "improved `Path` handling of mixed separators (#4808)"

This reverts commit 0797867758.
This commit is contained in:
Daniel Marjamäki 2023-02-24 06:21:02 +01:00
parent 50c8a0dbe1
commit e5a22e3436
6 changed files with 33 additions and 107 deletions

View File

@ -32,7 +32,6 @@
#include <unistd.h>
#else
#include <direct.h>
#include <stdexcept>
#endif
#if defined(__CYGWIN__)
#include <strings.h>
@ -151,12 +150,9 @@ bool Path::isAbsolute(const std::string& path)
return false;
}
std::string Path::getRelativePath(std::string absolutePath, const std::vector<std::string>& basePaths)
std::string Path::getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths)
{
absolutePath = Path::fromNativeSeparators(std::move(absolutePath));
for (std::string bp : basePaths) {
bp = Path::fromNativeSeparators(std::move(bp));
for (const std::string &bp : basePaths) {
if (absolutePath == bp || bp.empty()) // Seems to be a file, or path is empty
continue;
@ -165,7 +161,7 @@ std::string Path::getRelativePath(std::string absolutePath, const std::vector<st
if (endsWith(bp,'/'))
return absolutePath.substr(bp.length());
if (absolutePath.size() > bp.size() && absolutePath[bp.length()] == '/')
else if (absolutePath.size() > bp.size() && absolutePath[bp.length()] == '/')
return absolutePath.substr(bp.length() + 1);
}
return absolutePath;
@ -215,13 +211,9 @@ std::string Path::getAbsoluteFilePath(const std::string& filePath)
if (_fullpath(absolute, filePath.c_str(), _MAX_PATH))
absolute_path = absolute;
#elif defined(__linux__) || defined(__sun) || defined(__hpux) || defined(__GNUC__) || defined(__CPPCHECK__)
// realpath() only works with files that actually exist
char * absolute = realpath(filePath.c_str(), nullptr);
if (!absolute) {
const int err = errno;
throw std::runtime_error("realpath() failed with " + std::to_string(err));
}
absolute_path = absolute;
if (absolute)
absolute_path = absolute;
free(absolute);
#else
#error Platform absolute path function needed
@ -229,11 +221,15 @@ std::string Path::getAbsoluteFilePath(const std::string& filePath)
return absolute_path;
}
std::string Path::stripDirectoryPart(std::string file)
std::string Path::stripDirectoryPart(const std::string &file)
{
file = fromNativeSeparators(std::move(file));
#if defined(_WIN32) && !defined(__MINGW32__)
const char native = '\\';
#else
const char native = '/';
#endif
const std::string::size_type p = file.rfind('/');
const std::string::size_type p = file.rfind(native);
if (p != std::string::npos) {
return file.substr(p + 1);
}
@ -247,8 +243,6 @@ bool Path::fileExists(const std::string &file)
}
std::string Path::join(std::string path1, std::string path2) {
path1 = fromNativeSeparators(std::move(path1));
path2 = fromNativeSeparators(std::move(path2));
if (path1.empty() || path2.empty())
return path1 + path2;
if (path2.front() == '/')

View File

@ -117,7 +117,7 @@ public:
* @param basePaths Paths to which it may be made relative.
* @return relative path, if possible. Otherwise absolutePath is returned unchanged
*/
static std::string getRelativePath(std::string absolutePath, const std::vector<std::string>& basePaths);
static std::string getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths);
/**
* @brief Get an absolute file path from a relative one.
@ -172,7 +172,7 @@ public:
* @param file filename to be stripped. path info is optional
* @return filename without directory path part.
*/
static std::string stripDirectoryPart(std::string file);
static std::string stripDirectoryPart(const std::string &file);
/**
* @brief Checks if a File exists

View File

@ -178,8 +178,6 @@ bool cppcheck::Platform::platform(const std::string& platformstr, std::string& e
return false;
}
else {
if (verbose)
std::cout << "current working directory '" + Path::getCurrentPath() + "'" << std::endl;
bool found = false;
for (const std::string& path : paths) {
if (verbose)
@ -205,19 +203,19 @@ bool cppcheck::Platform::loadPlatformFile(const char exename[], const std::strin
std::vector<std::string> filenames;
filenames.push_back(filename);
filenames.push_back(filename + ".xml");
filenames.push_back(Path::join("platforms", filename));
filenames.push_back(Path::join("platforms", filename + ".xml"));
filenames.push_back("platforms/" + filename);
filenames.push_back("platforms/" + filename + ".xml");
if (exename && (std::string::npos != Path::fromNativeSeparators(exename).find('/'))) {
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + filename);
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + Path::join("platforms", filename));
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + Path::join("platforms", filename + ".xml"));
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename);
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename + ".xml");
}
#ifdef FILESDIR
std::string filesdir = FILESDIR;
if (!filesdir.empty() && filesdir[filesdir.size()-1] != '/')
filesdir += '/';
filenames.push_back(filesdir + Path::join("platforms", filename));
filenames.push_back(filesdir + Path::join("platforms", filename + ".xml"));
filenames.push_back(filesdir + ("platforms/" + filename));
filenames.push_back(filesdir + ("platforms/" + filename + ".xml"));
#endif
// open file..

View File

@ -7474,7 +7474,7 @@ void ValueType::setDebugPath(const Token* tok, SourceLocation ctx, SourceLocatio
std::string file = ctx.file_name();
if (file.empty())
return;
std::string s = Path::stripDirectoryPart(std::move(file)) + ":" + MathLib::toString(ctx.line()) + ": " + ctx.function_name() +
std::string s = Path::stripDirectoryPart(file) + ":" + MathLib::toString(ctx.line()) + ": " + ctx.function_name() +
" => " + local.function_name();
debugPath.emplace_back(tok, std::move(s));
}

View File

@ -168,7 +168,7 @@ static void setSourceLocation(ValueFlow::Value& v,
std::string file = ctx.file_name();
if (file.empty())
return;
std::string s = Path::stripDirectoryPart(std::move(file)) + ":" + MathLib::toString(ctx.line()) + ": " + ctx.function_name() +
std::string s = Path::stripDirectoryPart(file) + ":" + MathLib::toString(ctx.line()) + ": " + ctx.function_name() +
" => " + local.function_name() + ": " + debugString(v);
v.debugPath.emplace_back(tok, std::move(s));
}

View File

@ -38,8 +38,6 @@ private:
TEST_CASE(is_cpp);
TEST_CASE(get_path_from_filename);
TEST_CASE(join);
TEST_CASE(getAbsoluteFilePath);
TEST_CASE(stripDirectoryPart);
}
void removeQuotationMarks() const {
@ -95,48 +93,18 @@ private:
}
void getRelative() const {
{
const std::vector<std::string> basePaths = {
"", // Don't crash with empty paths
"C:/foo",
"C:/bar/",
"C:/test.cpp"
};
const std::vector<std::string> basePaths = {
"", // Don't crash with empty paths
"C:/foo",
"C:/bar/",
"C:/test.cpp"
};
ASSERT_EQUALS("x.c", Path::getRelativePath("C:/foo/x.c", basePaths));
ASSERT_EQUALS("y.c", Path::getRelativePath("C:/bar/y.c", basePaths));
ASSERT_EQUALS("foo/y.c", Path::getRelativePath("C:/bar/foo/y.c", basePaths));
ASSERT_EQUALS("C:/test.cpp", Path::getRelativePath("C:/test.cpp", basePaths));
ASSERT_EQUALS("C:/foobar/test.cpp", Path::getRelativePath("C:/foobar/test.cpp", basePaths));
}
{
const std::vector<std::string> basePaths = {
"", // Don't crash with empty paths
"C:\\foo",
"C:\\bar\\",
"C:\\test.cpp"
};
ASSERT_EQUALS("x.c", Path::getRelativePath("C:\\foo\\x.c", basePaths));
ASSERT_EQUALS("y.c", Path::getRelativePath("C:\\bar\\y.c", basePaths));
ASSERT_EQUALS("foo/y.c", Path::getRelativePath("C:\\bar\\foo\\y.c", basePaths));
ASSERT_EQUALS("C:/test.cpp", Path::getRelativePath("C:\\test.cpp", basePaths));
ASSERT_EQUALS("C:/foobar/test.cpp", Path::getRelativePath("C:\\foobar\\test.cpp", basePaths));
}
{
const std::vector<std::string> basePaths = {
"", // Don't crash with empty paths
"/c/foo",
"/c/bar/",
"/c/test.cpp"
};
ASSERT_EQUALS("x.c", Path::getRelativePath("/c/foo/x.c", basePaths));
ASSERT_EQUALS("y.c", Path::getRelativePath("/c/bar/y.c", basePaths));
ASSERT_EQUALS("foo/y.c", Path::getRelativePath("/c/bar/foo\\y.c", basePaths));
ASSERT_EQUALS("/c/test.cpp", Path::getRelativePath("/c/test.cpp", basePaths));
ASSERT_EQUALS("/c/foobar/test.cpp", Path::getRelativePath("/c/foobar/test.cpp", basePaths));
}
ASSERT_EQUALS("x.c", Path::getRelativePath("C:/foo/x.c", basePaths));
ASSERT_EQUALS("y.c", Path::getRelativePath("C:/bar/y.c", basePaths));
ASSERT_EQUALS("foo/y.c", Path::getRelativePath("C:/bar/foo/y.c", basePaths));
ASSERT_EQUALS("C:/test.cpp", Path::getRelativePath("C:/test.cpp", basePaths));
ASSERT_EQUALS("C:/foobar/test.cpp", Path::getRelativePath("C:/foobar/test.cpp", basePaths));
}
void is_c() const {
@ -173,12 +141,6 @@ private:
ASSERT_EQUALS("/tmp/", Path::getPathFromFilename("/tmp/index.h"));
ASSERT_EQUALS("a/b/c/", Path::getPathFromFilename("a/b/c/index.h"));
ASSERT_EQUALS("a/b/c/", Path::getPathFromFilename("a/b/c/"));
ASSERT_EQUALS("S:\\tmp\\", Path::getPathFromFilename("S:\\tmp\\index.h"));
ASSERT_EQUALS("a\\b\\c\\", Path::getPathFromFilename("a\\b\\c\\index.h"));
ASSERT_EQUALS("a\\b\\c\\", Path::getPathFromFilename("a\\b\\c\\"));
ASSERT_EQUALS("S:\\a\\b\\c\\", Path::getPathFromFilename("S:\\a\\b\\c\\"));
ASSERT_EQUALS("S:/tmp/", Path::getPathFromFilename("S:/tmp/index.h"));
ASSERT_EQUALS("S:/a/b/c/", Path::getPathFromFilename("S:/a/b/c/index.h"));
}
void join() const {
@ -186,35 +148,7 @@ private:
ASSERT_EQUALS("a", Path::join("", "a"));
ASSERT_EQUALS("a/b", Path::join("a", "b"));
ASSERT_EQUALS("a/b", Path::join("a/", "b"));
ASSERT_EQUALS("a/b", Path::join("a\\", "b"));
ASSERT_EQUALS("/b", Path::join("a", "/b"));
ASSERT_EQUALS("/b", Path::join("a", "\\b"));
}
// TODO: this is quite messy - should Path::getAbsoluteFilePath() return normalized separators?
void getAbsoluteFilePath() const {
// Path::getAbsoluteFilePath() only works with existing paths on Linux
#ifdef _WIN32
const std::string cwd = Path::getCurrentPath();
ASSERT_EQUALS(Path::join(cwd, "a.h"), Path::fromNativeSeparators(Path::getAbsoluteFilePath("a.h")));
ASSERT_EQUALS(Path::join(cwd, "inc/a.h"), Path::fromNativeSeparators(Path::getAbsoluteFilePath("inc/a.h")));
const std::string cwd_down = Path::getPathFromFilename(cwd);
ASSERT_EQUALS(Path::join(cwd_down, "a.h"), Path::fromNativeSeparators(Path::getAbsoluteFilePath("../a.h")));
ASSERT_EQUALS(Path::join(cwd_down, "inc/a.h"), Path::fromNativeSeparators(Path::getAbsoluteFilePath("../inc/a.h")));
ASSERT_EQUALS(Path::join(cwd_down, "inc/a.h"), Path::fromNativeSeparators(Path::getAbsoluteFilePath("../inc/../inc/a.h")));
#endif
}
void stripDirectoryPart() const {
ASSERT_EQUALS("a.h", Path::stripDirectoryPart("a.h"));
ASSERT_EQUALS("a.h", Path::stripDirectoryPart("a/a.h"));
ASSERT_EQUALS("a.h", Path::stripDirectoryPart("a/b/a.h"));
ASSERT_EQUALS("a.h", Path::stripDirectoryPart("/mnt/a/b/a.h"));
ASSERT_EQUALS("a.h", Path::stripDirectoryPart("a\\a.h"));
ASSERT_EQUALS("a.h", Path::stripDirectoryPart("a\\b\\a.h"));
ASSERT_EQUALS("a.h", Path::stripDirectoryPart("S:\\a\\b\\a.h"));
ASSERT_EQUALS("a.h", Path::stripDirectoryPart("S:/a/b/a.h"));
}
};