From c5b9775e2544cc19f14e63fccf8675f5f04f9758 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 16 Apr 2024 08:42:51 +0200 Subject: [PATCH] cleanups around `PathWithDetails` --- gui/threadresult.cpp | 2 +- lib/filesettings.h | 8 +++++++- lib/importproject.cpp | 19 +++++++++---------- test/testcppcheck.cpp | 3 +-- test/testimportproject.cpp | 13 ++++++------- test/testprocessexecutor.cpp | 8 ++------ test/testsingleexecutor.cpp | 9 +++------ test/testsuppressions.cpp | 12 +++--------- test/testthreadexecutor.cpp | 8 ++------ 9 files changed, 34 insertions(+), 48 deletions(-) diff --git a/gui/threadresult.cpp b/gui/threadresult.cpp index e4eb99e6747a..729c6d9d373f 100644 --- a/gui/threadresult.cpp +++ b/gui/threadresult.cpp @@ -72,7 +72,7 @@ FileSettings ThreadResult::getNextFileSettings() { std::lock_guard locker(mutex); if (mFileSettings.empty()) { - return FileSettings(); + return FileSettings(""); } const FileSettings fs = mFileSettings.front(); mFileSettings.pop_front(); diff --git a/lib/filesettings.h b/lib/filesettings.h index f7262bc6a22f..05da8ea79bd1 100644 --- a/lib/filesettings.h +++ b/lib/filesettings.h @@ -55,7 +55,13 @@ class PathWithDetails /** File settings. Multiple configurations for a file is allowed. */ struct CPPCHECKLIB FileSettings { - FileSettings() = default; // TODO: call PathWithDetails c'tor + explicit FileSettings(std::string path) + : path(std::move(path)) + {} + + FileSettings(std::string path, std::size_t size) + : path(std::move(path), size) + {} std::string cfg; PathWithDetails path; diff --git a/lib/importproject.cpp b/lib/importproject.cpp index f375b088f13c..6e9cc0a13213 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -398,22 +398,23 @@ bool ImportProject::importCompileCommands(std::istream &istr) if (!Path::acceptFile(file)) continue; - FileSettings fs; + std::string path; if (Path::isAbsolute(file)) - fs.path = PathWithDetails{Path::simplifyPath(file)}; + path = Path::simplifyPath(file); #ifdef _WIN32 else if (file[0] == '/' && directory.size() > 2 && std::isalpha(directory[0]) && directory[1] == ':') // directory: C:\foo\bar // file: /xy/z.c // => c:/xy/z.c - fs.filename = Path::simplifyPath(directory.substr(0,2) + file); + path = Path::simplifyPath(directory.substr(0,2) + file); #endif else - fs.path = PathWithDetails{Path::simplifyPath(directory + file)}; - if (!sourceFileExists(fs.filename())) { - printError("'" + fs.filename() + "' from compilation database does not exist"); + path = Path::simplifyPath(directory + file); + if (!sourceFileExists(path)) { + printError("'" + path + "' from compilation database does not exist"); return false; } + FileSettings fs{std::move(path)}; fsParseCommand(fs, command); // read settings; -D, -I, -U, -std, -m*, -f* std::map variables; fsSetIncludePaths(fs, directory, fs.includePaths, variables); @@ -759,8 +760,7 @@ bool ImportProject::importVcxproj(const std::string &filename, std::map in(1, "../include"); std::map variables; ImportProject::fsSetIncludePaths(fs, "abc/def/", in, variables); @@ -94,7 +94,7 @@ class TestImportProject : public TestFixture { } void setIncludePaths2() const { - FileSettings fs; + FileSettings fs{""}; std::list in(1, "$(SolutionDir)other"); std::map variables; variables["SolutionDir"] = "c:/abc/"; @@ -104,7 +104,7 @@ class TestImportProject : public TestFixture { } void setIncludePaths3() const { // macro names are case insensitive - FileSettings fs; + FileSettings fs{""}; std::list in(1, "$(SOLUTIONDIR)other"); std::map variables; variables["SolutionDir"] = "c:/abc/"; @@ -371,9 +371,8 @@ class TestImportProject : public TestFixture { } void ignorePaths() const { - FileSettings fs1, fs2; - fs1.path = PathWithDetails{"foo/bar"}; - fs2.path = PathWithDetails{"qwe/rty"}; + FileSettings fs1{"foo/bar"}; + FileSettings fs2{"qwe/rty"}; TestImporter project; project.fileSettings = {std::move(fs1), std::move(fs2)}; diff --git a/test/testprocessexecutor.cpp b/test/testprocessexecutor.cpp index df1df02f0679..4941b3115a15 100644 --- a/test/testprocessexecutor.cpp +++ b/test/testprocessexecutor.cpp @@ -74,9 +74,7 @@ class TestProcessExecutorBase : public TestFixture { std::string f_s = fprefix() + "_" + std::to_string(i) + ".cpp"; filelist.emplace_back(f_s, data.size()); if (useFS) { - FileSettings fs; - fs.path = PathWithDetails{std::move(f_s)}; - fileSettings.emplace_back(std::move(fs)); + fileSettings.emplace_back(std::move(f_s), data.size()); } } } @@ -85,9 +83,7 @@ class TestProcessExecutorBase : public TestFixture { { filelist.emplace_back(f, data.size()); if (useFS) { - FileSettings fs; - fs.path = PathWithDetails{f}; - fileSettings.emplace_back(std::move(fs)); + fileSettings.emplace_back(f, data.size()); } } } diff --git a/test/testsingleexecutor.cpp b/test/testsingleexecutor.cpp index b20817e57264..9203644fab46 100644 --- a/test/testsingleexecutor.cpp +++ b/test/testsingleexecutor.cpp @@ -79,9 +79,7 @@ class TestSingleExecutorBase : public TestFixture { std::string f_s = fprefix() + "_" + zpad3(i) + ".cpp"; filelist.emplace_back(f_s, data.size()); if (useFS) { - FileSettings fs; - fs.path = PathWithDetails{std::move(f_s)}; - fileSettings.emplace_back(std::move(fs)); + fileSettings.emplace_back(std::move(f_s), data.size()); } } } @@ -90,9 +88,8 @@ class TestSingleExecutorBase : public TestFixture { { filelist.emplace_back(f, data.size()); if (useFS) { - FileSettings fs; - fs.path = PathWithDetails{f}; - fileSettings.emplace_back(std::move(fs)); + + fileSettings.emplace_back(f, data.size()); } } } diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index d60a0b7b3839..0e775e4c6486 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -233,9 +233,7 @@ class TestSuppressions : public TestFixture { for (std::map::const_iterator i = f.cbegin(); i != f.cend(); ++i) { filelist.emplace_back(i->first, i->second.size()); if (useFS) { - FileSettings fs; - fs.path = PathWithDetails{i->first}; - fileSettings.emplace_back(std::move(fs)); + fileSettings.emplace_back(i->first, i->second.size()); } } @@ -282,9 +280,7 @@ class TestSuppressions : public TestFixture { std::list filelist; filelist.emplace_back("test.cpp", strlen(code)); if (useFS) { - FileSettings fs; - fs.path = PathWithDetails{"test.cpp"}; - fileSettings.emplace_back(std::move(fs)); + fileSettings.emplace_back("test.cpp", strlen(code)); } /*const*/ auto settings = dinit(Settings, @@ -328,9 +324,7 @@ class TestSuppressions : public TestFixture { std::list filelist; filelist.emplace_back("test.cpp", strlen(code)); if (useFS) { - FileSettings fs; - fs.path = PathWithDetails{"test.cpp"}; - fileSettings.emplace_back(std::move(fs)); + fileSettings.emplace_back("test.cpp", strlen(code)); } /*const*/ auto settings = dinit(Settings, diff --git a/test/testthreadexecutor.cpp b/test/testthreadexecutor.cpp index c5961821c10f..ad33bbcc5fab 100644 --- a/test/testthreadexecutor.cpp +++ b/test/testthreadexecutor.cpp @@ -74,9 +74,7 @@ class TestThreadExecutorBase : public TestFixture { std::string f_s = fprefix() + "_" + std::to_string(i) + ".cpp"; filelist.emplace_back(f_s, data.size()); if (useFS) { - FileSettings fs; - fs.path = PathWithDetails{std::move(f_s)}; - fileSettings.emplace_back(std::move(fs)); + fileSettings.emplace_back(std::move(f_s), data.size()); } } } @@ -85,9 +83,7 @@ class TestThreadExecutorBase : public TestFixture { { filelist.emplace_back(f, data.size()); if (useFS) { - FileSettings fs; - fs.path = PathWithDetails{f}; - fileSettings.emplace_back(std::move(fs)); + fileSettings.emplace_back(f, data.size()); } } }