Skip to content

Commit

Permalink
refs #12022 - disallow multiple --project options (#5499)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Oct 5, 2023
1 parent fe8730c commit 5a52fa8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 8 deletions.
6 changes: 6 additions & 0 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,12 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])

// --project
else if (std::strncmp(argv[i], "--project=", 10) == 0) {
if (mSettings.project.projectType != ImportProject::Type::NONE)
{
mLogger.printError("multiple --project options are not supported.");
return false;
}

mSettings.checkAllConfigurations = false; // Can be overridden with --max-configs or --force
std::string projectFile = argv[i]+10;
ImportProject::Type projType = mSettings.project.import(projectFile, &mSettings);
Expand Down
3 changes: 3 additions & 0 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,9 @@ void MainWindow::analyzeProject(const ProjectFile *projectFile, const bool check
case ImportProject::Type::FAILURE:
errorMessage = tr("Failed to import project file");
break;
case ImportProject::Type::NONE:
// can never happen
break;
}

if (!errorMessage.isEmpty()) {
Expand Down
1 change: 0 additions & 1 deletion gui/test/projectfile/testprojectfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const char Settings::SafeChecks::XmlInternalFunctions[] = "internal-functions";
const char Settings::SafeChecks::XmlExternalVariables[] = "external-variables";
Settings::Settings() : maxCtuDepth(10) {}
cppcheck::Platform::Platform() = default;
ImportProject::ImportProject() = default;
bool ImportProject::sourceFileExists(const std::string & /*file*/) {
return true;
}
Expand Down
5 changes: 0 additions & 5 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@

#include "json.h"

ImportProject::ImportProject()
{
projectType = Type::UNKNOWN;
}

void ImportProject::ignorePaths(const std::vector<std::string> &ipaths)
{
for (std::list<FileSettings>::iterator it = fileSettings.begin(); it != fileSettings.end();) {
Expand Down
5 changes: 3 additions & 2 deletions lib/importproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Settings;
class CPPCHECKLIB ImportProject {
public:
enum class Type {
NONE,
UNKNOWN,
MISSING,
FAILURE,
Expand Down Expand Up @@ -82,9 +83,9 @@ class CPPCHECKLIB ImportProject {
void setIncludePaths(const std::string &basepath, const std::list<std::string> &in, std::map<std::string, std::string, cppcheck::stricmp> &variables);
};
std::list<FileSettings> fileSettings;
Type projectType;
Type projectType{Type::NONE};

ImportProject();
ImportProject() = default;
virtual ~ImportProject() = default;
ImportProject(const ImportProject&) = default;
ImportProject& operator=(const ImportProject&) = default;
Expand Down
1 change: 1 addition & 0 deletions releasenotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Other:
- The undocumented and deprecated command-line options `--template <template>` and `--template-format <template>` has been removed. Please use `--template=` and `--template-format=` instead.
- "--showtime=summary" will now show a single summary at the end instead of showing it after each file when using the thread execution (default on Windows)
- added "--showtime=none" to disable any previously specified showtime report. "--showtime=" without parameter is no longer valid.
- Multiple "--project" options are now prohibited. These would have overlapped each other and leads to unexpected behavior. Please use separate runs with a single "--project" option instead.
35 changes: 35 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(typedefMaxTimeInvalid2);
TEST_CASE(templateMaxTime);
TEST_CASE(templateMaxTime);
TEST_CASE(project);
TEST_CASE(projectMultiple);
TEST_CASE(projectEmpty);
TEST_CASE(projectMissing);

TEST_CASE(ignorepaths1);
TEST_CASE(ignorepaths2);
Expand Down Expand Up @@ -1946,6 +1950,37 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("cppcheck: error: argument to '--typedef-max-time=' is not valid - needs to be positive.\n", logger->str());
}

void project() {
REDIRECT;
ScopedFile file("project.cppcheck", "<project></project>");
const char * const argv[] = {"cppcheck", "--project=project.cppcheck", "file.cpp"};
ASSERT(parser->parseFromArgs(3, argv));
ASSERT_EQUALS(static_cast<int>(ImportProject::Type::CPPCHECK_GUI), static_cast<int>(settings->project.projectType));
ASSERT_EQUALS("", logger->str());
}

void projectMultiple() {
REDIRECT;
ScopedFile file("project.cppcheck", "<project></project>");
const char * const argv[] = {"cppcheck", "--project=project.cppcheck", "--project=project.cppcheck", "file.cpp"};
ASSERT(!parser->parseFromArgs(4, argv));
ASSERT_EQUALS("cppcheck: error: multiple --project options are not supported.\n", logger->str());
}

void projectEmpty() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--project=", "file.cpp"};
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: failed to open project ''. The file does not exist.\n", logger->str());
}

void projectMissing() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--project=project.cppcheck", "file.cpp"};
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: failed to open project 'project.cppcheck'. The file does not exist.\n", logger->str());
}

void ignorepaths1() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};
Expand Down

0 comments on commit 5a52fa8

Please sign in to comment.