Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed #12022 - disallow using --project with source files #5515

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,6 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
ImportProject::Type projType = mSettings.project.import(projectFile, &mSettings);
mSettings.project.projectType = projType;
if (projType == ImportProject::Type::CPPCHECK_GUI) {
mPathNames = mSettings.project.guiProject.pathNames;
for (const std::string &lib : mSettings.project.guiProject.libraries)
mSettings.libraries.emplace_back(lib);

Expand Down Expand Up @@ -1032,12 +1031,20 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
return true;
}

if (!mPathNames.empty() && mSettings.project.projectType != ImportProject::Type::NONE) {
mLogger.printError("--project cannot be used in conjunction with source files.");
return false;
}

// Print error only if we have "real" command and expect files
if (!mExitAfterPrint && mPathNames.empty() && mSettings.project.fileSettings.empty()) {
if (!mExitAfterPrint && mPathNames.empty() && mSettings.project.guiProject.pathNames.empty() && mSettings.project.fileSettings.empty()) {
mLogger.printError("no C or C++ source files found.");
return false;
}

if (!mSettings.project.guiProject.pathNames.empty())
mPathNames = mSettings.project.guiProject.pathNames;

// Use paths _pathnames if no base paths for relative path output are given
if (mSettings.basePaths.empty() && mSettings.relativePaths)
mSettings.basePaths = mPathNames;
Expand Down
3 changes: 2 additions & 1 deletion releasenotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ 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.
- Multiple "--project" options are now prohibited. These would have overlapped each other and lead to unexpected behavior. Please use separate runs with a single "--project" option instead.
- "--project" can also no longer be used in conjunction with additional source files.
31 changes: 28 additions & 3 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,10 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(templateMaxTime);
TEST_CASE(project);
TEST_CASE(projectMultiple);
TEST_CASE(projectAndSource);
TEST_CASE(projectEmpty);
TEST_CASE(projectMissing);
TEST_CASE(projectNoPaths);

TEST_CASE(ignorepaths1);
TEST_CASE(ignorepaths2);
Expand Down Expand Up @@ -1952,10 +1954,18 @@ class TestCmdlineParser : public TestFixture {

void project() {
REDIRECT;
ScopedFile file("project.cppcheck", "<project></project>");
const char * const argv[] = {"cppcheck", "--project=project.cppcheck", "file.cpp"};
ASSERT(parser->parseFromArgs(3, argv));
ScopedFile file("project.cppcheck",
"<project>\n"
"<paths>\n"
"<dir name=\"dir\"/>\n"
"</paths>\n"
"</project>");
const char * const argv[] = {"cppcheck", "--project=project.cppcheck"};
ASSERT(parser->parseFromArgs(2, argv));
ASSERT_EQUALS(static_cast<int>(ImportProject::Type::CPPCHECK_GUI), static_cast<int>(settings->project.projectType));
ASSERT_EQUALS(1, parser->getPathNames().size());
auto it = parser->getPathNames().cbegin();
ASSERT_EQUALS("dir", *it);
ASSERT_EQUALS("", logger->str());
}

Expand All @@ -1967,6 +1977,14 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("cppcheck: error: multiple --project options are not supported.\n", logger->str());
}

void projectAndSource() {
REDIRECT;
ScopedFile file("project.cppcheck", "<project></project>");
const char * const argv[] = {"cppcheck", "--project=project.cppcheck", "file.cpp"};
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: --project cannot be used in conjunction with source files.\n", logger->str());
}

void projectEmpty() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--project=", "file.cpp"};
Expand All @@ -1981,6 +1999,13 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("cppcheck: error: failed to open project 'project.cppcheck'. The file does not exist.\n", logger->str());
}

void projectNoPaths() {
ScopedFile file("project.cppcheck", "<project></project>");
const char * const argv[] = {"cppcheck", "--project=project.cppcheck"};
ASSERT(!parser->parseFromArgs(2, argv));
ASSERT_EQUALS("cppcheck: error: no C or C++ source files found.\n", logger->str());
}

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