diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 50a65992d..ebf6b6059 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -225,12 +225,28 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) // User define else if (strncmp(argv[i], "-D", 2) == 0) { + std::string define; + + // "-D define" + if (strcmp(argv[i], "-D") == 0) + { + ++i; + if (i >= argc) + { + PrintMessage("cppcheck: argument to '-D' is missing"); + return false; + } + define = argv[i]; + } + // "-Ddefine" + else + { + define = 2 + argv[i]; + } + if (!_settings->userDefines.empty()) _settings->userDefines += ";"; - if (strcmp(argv[i], "-D") == 0) - _settings->userDefines += argv[++i]; - else - _settings->userDefines += 2 + argv[i]; + _settings->userDefines += define; } // Include paths diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 3caba1749..2a979ab9a 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -46,6 +46,7 @@ private: TEST_CASE(forcelong); TEST_CASE(quietshort); TEST_CASE(quietlong); + TEST_CASE(defines_noarg); TEST_CASE(defines); TEST_CASE(defines2); TEST_CASE(defines3); @@ -230,6 +231,15 @@ private: ASSERT_EQUALS(true, settings._errorsOnly); } + void defines_noarg() + { + REDIRECT; + const char *argv[] = {"cppcheck", "-D"}; + Settings settings; + CmdLineParser parser(&settings); + ASSERT_EQUALS(false, parser.ParseFromArgs(2, argv)); + } + void defines() { REDIRECT;