Skip to content

Commit

Permalink
cleanups around PathWithDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Apr 21, 2024
1 parent 20aec12 commit af1a646
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 48 deletions.
2 changes: 1 addition & 1 deletion gui/threadresult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ FileSettings ThreadResult::getNextFileSettings()
{
std::lock_guard<std::mutex> locker(mutex);
if (mFileSettings.empty()) {
return FileSettings();
return FileSettings("");
}
const FileSettings fs = mFileSettings.front();
mFileSettings.pop_front();
Expand Down
8 changes: 7 additions & 1 deletion lib/filesettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 9 additions & 10 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string, cppcheck::stricmp> variables;
fsSetIncludePaths(fs, directory, fs.includePaths, variables);
Expand Down Expand Up @@ -759,8 +760,7 @@ bool ImportProject::importVcxproj(const std::string &filename, std::map<std::str
continue;
}

FileSettings fs;
fs.path = PathWithDetails{cfilename};
FileSettings fs{cfilename};
fs.cfg = p.name;
// TODO: detect actual MSC version
fs.msc = true;
Expand Down Expand Up @@ -1055,10 +1055,9 @@ bool ImportProject::importBcb6Prj(const std::string &projectFilename)
//
// We can also force C++ compilation for all files using the -P command line switch.
const bool cppMode = forceCppMode || Path::getFilenameExtensionInLowerCase(c) == ".cpp";
FileSettings fs;
FileSettings fs{Path::simplifyPath(Path::isAbsolute(c) ? c : projectDir + c)};
fsSetIncludePaths(fs, projectDir, toStringList(includePath), variables);
fsSetDefines(fs, cppMode ? cppDefines : defines);
fs.path = PathWithDetails{Path::simplifyPath(Path::isAbsolute(c) ? c : projectDir + c)};
fileSettings.push_back(std::move(fs));
}

Expand Down
3 changes: 1 addition & 2 deletions test/testcppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ class TestCppcheck : public TestFixture {

ErrorLogger2 errorLogger;
CppCheck cppcheck(errorLogger, false, {});
FileSettings fs;
fs.path = PathWithDetails{file.path()};
FileSettings fs{file.path()};
ASSERT_EQUALS(1, cppcheck.check(fs));
// TODO: how to properly disable these warnings?
errorLogger.ids.erase(std::remove_if(errorLogger.ids.begin(), errorLogger.ids.end(), [](const std::string& id) {
Expand Down
13 changes: 6 additions & 7 deletions test/testimportproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class TestImportProject : public TestFixture {
}

void setDefines() const {
FileSettings fs;
FileSettings fs{""};

ImportProject::fsSetDefines(fs, "A");
ASSERT_EQUALS("A=1", fs.defines);
Expand All @@ -85,7 +85,7 @@ class TestImportProject : public TestFixture {
}

void setIncludePaths1() const {
FileSettings fs;
FileSettings fs{""};
std::list<std::string> in(1, "../include");
std::map<std::string, std::string, cppcheck::stricmp> variables;
ImportProject::fsSetIncludePaths(fs, "abc/def/", in, variables);
Expand All @@ -94,7 +94,7 @@ class TestImportProject : public TestFixture {
}

void setIncludePaths2() const {
FileSettings fs;
FileSettings fs{""};
std::list<std::string> in(1, "$(SolutionDir)other");
std::map<std::string, std::string, cppcheck::stricmp> variables;
variables["SolutionDir"] = "c:/abc/";
Expand All @@ -104,7 +104,7 @@ class TestImportProject : public TestFixture {
}

void setIncludePaths3() const { // macro names are case insensitive
FileSettings fs;
FileSettings fs{""};
std::list<std::string> in(1, "$(SOLUTIONDIR)other");
std::map<std::string, std::string, cppcheck::stricmp> variables;
variables["SolutionDir"] = "c:/abc/";
Expand Down Expand Up @@ -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)};

Expand Down
8 changes: 2 additions & 6 deletions test/testprocessexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand All @@ -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());
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions test/testsingleexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand All @@ -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());
}
}
}
Expand Down
12 changes: 3 additions & 9 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,7 @@ class TestSuppressions : public TestFixture {
for (std::map<std::string, std::string>::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());
}
}

Expand Down Expand Up @@ -282,9 +280,7 @@ class TestSuppressions : public TestFixture {
std::list<PathWithDetails> 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,
Expand Down Expand Up @@ -328,9 +324,7 @@ class TestSuppressions : public TestFixture {
std::list<PathWithDetails> 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,
Expand Down
8 changes: 2 additions & 6 deletions test/testthreadexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand All @@ -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());
}
}
}
Expand Down

0 comments on commit af1a646

Please sign in to comment.