diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 145cb6d79..1789ec624 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -367,6 +367,14 @@ bool CppCheck::parseFromArgs(int argc, const char* const argv[]) } } + // User define + else if (strncmp(argv[i], "-D", 2) == 0) + { + if (!_settings.userDefines.empty()) + _settings.userDefines += ";"; + _settings.userDefines += 2 + argv[i]; + } + // Include paths else if (strcmp(argv[i], "-I") == 0 || strncmp(argv[i], "-I", 2) == 0) { @@ -632,6 +640,12 @@ unsigned int CppCheck::check() preprocessor.preprocess(fin, filedata, configurations, fname, _settings._includePaths); } + if (!_settings.userDefines.empty()) + { + configurations.clear(); + configurations.push_back(_settings.userDefines); + } + int checkCount = 0; for (std::list::const_iterator it = configurations.begin(); it != configurations.end(); ++it) { diff --git a/lib/settings.h b/lib/settings.h index 772748e75..c899272da 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -156,7 +156,6 @@ public: * @return true if this error is suppressed. */ bool isSuppressed(const std::string &errorId, const std::string &file, unsigned int line); - }; /** @brief suppress message (--suppressions) */ @@ -164,6 +163,9 @@ public: /** @brief suppress exitcode */ Suppressions nofail; + + /** @brief defines given by the user */ + std::string userDefines; }; /// @} diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index 5ebd37dbc..878ffcf73 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -65,6 +65,8 @@ private: TEST_CASE(parseOutputtingInvalidArgs); TEST_CASE(parseArgsAndCheck); TEST_CASE(parseArgsAndCheckSettings); + + TEST_CASE(userdefines); } bool argCheck(int argc, const char *argv[]) @@ -406,6 +408,18 @@ private: CppCheck cppCheck(*this); cppCheck.getErrorMessages(); } + + void userdefines() + { + { + const char *argv[] = {"cppcheck", "-DA", "-DB"}; + Settings settings; + ASSERT_EQUALS(true, argCheckReturnSettings(3, argv, settings)); + ASSERT_EQUALS("A;B", settings.userDefines); + ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("", output.str()); + } + } }; REGISTER_TEST(TestCppcheck)