refs #12022 - disallow multiple `--project` options (#5499)

This commit is contained in:
Oliver Stöneberg 2023-10-05 21:36:44 +02:00 committed by GitHub
parent fe8730cf0f
commit 5a52fa80fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 8 deletions

View File

@ -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);

View File

@ -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()) {

View File

@ -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;
}

View File

@ -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();) {

View File

@ -51,6 +51,7 @@ class Settings;
class CPPCHECKLIB ImportProject {
public:
enum class Type {
NONE,
UNKNOWN,
MISSING,
FAILURE,
@ -82,9 +83,9 @@ public:
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;

View File

@ -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.

View File

@ -281,6 +281,10 @@ private:
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);
@ -1946,6 +1950,37 @@ private:
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"};