Skip to content

Commit 3ba53c6

Browse files
authored
fixed #12022 - disallow using --project with source files (#5515)
1 parent 8dee551 commit 3ba53c6

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

cli/cmdlineparser.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,6 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
665665
ImportProject::Type projType = mSettings.project.import(projectFile, &mSettings);
666666
mSettings.project.projectType = projType;
667667
if (projType == ImportProject::Type::CPPCHECK_GUI) {
668-
mPathNames = mSettings.project.guiProject.pathNames;
669668
for (const std::string &lib : mSettings.project.guiProject.libraries)
670669
mSettings.libraries.emplace_back(lib);
671670

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

1034+
if (!mPathNames.empty() && mSettings.project.projectType != ImportProject::Type::NONE) {
1035+
mLogger.printError("--project cannot be used in conjunction with source files.");
1036+
return false;
1037+
}
1038+
10351039
// Print error only if we have "real" command and expect files
1036-
if (!mExitAfterPrint && mPathNames.empty() && mSettings.project.fileSettings.empty()) {
1040+
if (!mExitAfterPrint && mPathNames.empty() && mSettings.project.guiProject.pathNames.empty() && mSettings.project.fileSettings.empty()) {
10371041
mLogger.printError("no C or C++ source files found.");
10381042
return false;
10391043
}
10401044

1045+
if (!mSettings.project.guiProject.pathNames.empty())
1046+
mPathNames = mSettings.project.guiProject.pathNames;
1047+
10411048
// Use paths _pathnames if no base paths for relative path output are given
10421049
if (mSettings.basePaths.empty() && mSettings.relativePaths)
10431050
mSettings.basePaths = mPathNames;

releasenotes.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ Other:
2020
- The undocumented and deprecated command-line options `--template <template>` and `--template-format <template>` has been removed. Please use `--template=` and `--template-format=` instead.
2121
- "--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)
2222
- added "--showtime=none" to disable any previously specified showtime report. "--showtime=" without parameter is no longer valid.
23-
- 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.
23+
- 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.
24+
- "--project" can also no longer be used in conjunction with additional source files.

test/testcmdlineparser.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,10 @@ class TestCmdlineParser : public TestFixture {
283283
TEST_CASE(templateMaxTime);
284284
TEST_CASE(project);
285285
TEST_CASE(projectMultiple);
286+
TEST_CASE(projectAndSource);
286287
TEST_CASE(projectEmpty);
287288
TEST_CASE(projectMissing);
289+
TEST_CASE(projectNoPaths);
288290

289291
TEST_CASE(ignorepaths1);
290292
TEST_CASE(ignorepaths2);
@@ -1952,10 +1954,18 @@ class TestCmdlineParser : public TestFixture {
19521954

19531955
void project() {
19541956
REDIRECT;
1955-
ScopedFile file("project.cppcheck", "<project></project>");
1956-
const char * const argv[] = {"cppcheck", "--project=project.cppcheck", "file.cpp"};
1957-
ASSERT(parser->parseFromArgs(3, argv));
1957+
ScopedFile file("project.cppcheck",
1958+
"<project>\n"
1959+
"<paths>\n"
1960+
"<dir name=\"dir\"/>\n"
1961+
"</paths>\n"
1962+
"</project>");
1963+
const char * const argv[] = {"cppcheck", "--project=project.cppcheck"};
1964+
ASSERT(parser->parseFromArgs(2, argv));
19581965
ASSERT_EQUALS(static_cast<int>(ImportProject::Type::CPPCHECK_GUI), static_cast<int>(settings->project.projectType));
1966+
ASSERT_EQUALS(1, parser->getPathNames().size());
1967+
auto it = parser->getPathNames().cbegin();
1968+
ASSERT_EQUALS("dir", *it);
19591969
ASSERT_EQUALS("", logger->str());
19601970
}
19611971

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

1980+
void projectAndSource() {
1981+
REDIRECT;
1982+
ScopedFile file("project.cppcheck", "<project></project>");
1983+
const char * const argv[] = {"cppcheck", "--project=project.cppcheck", "file.cpp"};
1984+
ASSERT(!parser->parseFromArgs(3, argv));
1985+
ASSERT_EQUALS("cppcheck: error: --project cannot be used in conjunction with source files.\n", logger->str());
1986+
}
1987+
19701988
void projectEmpty() {
19711989
REDIRECT;
19721990
const char * const argv[] = {"cppcheck", "--project=", "file.cpp"};
@@ -1981,6 +1999,13 @@ class TestCmdlineParser : public TestFixture {
19811999
ASSERT_EQUALS("cppcheck: error: failed to open project 'project.cppcheck'. The file does not exist.\n", logger->str());
19822000
}
19832001

2002+
void projectNoPaths() {
2003+
ScopedFile file("project.cppcheck", "<project></project>");
2004+
const char * const argv[] = {"cppcheck", "--project=project.cppcheck"};
2005+
ASSERT(!parser->parseFromArgs(2, argv));
2006+
ASSERT_EQUALS("cppcheck: error: no C or C++ source files found.\n", logger->str());
2007+
}
2008+
19842009
void ignorepaths1() {
19852010
REDIRECT;
19862011
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};

0 commit comments

Comments
 (0)