From 50f9dd52ab0d9cc4ebfb327326070497e667a06d Mon Sep 17 00:00:00 2001 From: PKEuS Date: Tue, 11 Sep 2012 08:53:27 +0200 Subject: [PATCH] Used enum instead of string in implementation for '--language='. --- cli/cmdlineparser.cpp | 36 ++++++++++++++++++++++-------------- lib/preprocessor.cpp | 2 +- lib/settings.cpp | 1 + lib/settings.h | 6 +++++- lib/tokenize.cpp | 8 ++++---- test/testcmdlineparser.cpp | 12 ++++++------ 6 files changed, 39 insertions(+), 26 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 80a900d6c..a67c4b1d6 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -126,21 +126,29 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) _settings->inconclusive = true; // Enforce language (--language=, -x) - else if (strncmp(argv[i], "--language=", 11) == 0) { - _settings->enforcedLang = argv[i] + 11; - if (_settings->enforcedLang != "c" && _settings->enforcedLang != "c++" && _settings->enforcedLang != "java" && _settings->enforcedLang != "c#") { // Valid values - PrintMessage("cppcheck: Unknown language '" + _settings->enforcedLang + "' enforced."); - return false; + else if (strncmp(argv[i], "--language=", 11) == 0 || strcmp(argv[i], "-x") == 0) { + std::string str; + if (argv[i][2]) { + str = argv[i]+11; + } else { + i++; + if (i >= argc || argv[i][0] == '-') { + PrintMessage("cppcheck: No language given to '-x' option."); + return false; + } + str = argv[i]; } - } else if (strcmp(argv[i], "-x") == 0) { - i++; - if (i >= argc || argv[i][0] == '-') { - PrintMessage("cppcheck: No language given to '-x' option."); - return false; - } - _settings->enforcedLang = argv[i]; - if (_settings->enforcedLang != "c" && _settings->enforcedLang != "c++" && _settings->enforcedLang != "java" && _settings->enforcedLang != "c#") { // Valid values - PrintMessage("cppcheck: Unknown language '" + _settings->enforcedLang + "' enforced."); + + if (str == "c") + _settings->enforcedLang = Settings::C; + else if (str == "c++") + _settings->enforcedLang = Settings::CPP; + else if (str == "c#") + _settings->enforcedLang = Settings::CSharp; + else if (str == "java") + _settings->enforcedLang = Settings::Java; + else { + PrintMessage("cppcheck: Unknown language '" + str + "' enforced."); return false; } } diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index dea204177..6faaca93d 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1552,7 +1552,7 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string // Create a map for the cfg for faster access to defines std::map cfgmap(getcfgmap(cfg)); - if (((_settings && _settings->enforcedLang == "c++") || ((!_settings || _settings->enforcedLang.empty()) && Path::isCPP(filename))) && cfgmap.find("__cplusplus") == cfgmap.end()) + if (((_settings && _settings->enforcedLang == Settings::CPP) || ((!_settings || _settings->enforcedLang == Settings::None) && Path::isCPP(filename))) && cfgmap.find("__cplusplus") == cfgmap.end()) cfgmap["__cplusplus"] = "1"; std::stack filenames; diff --git a/lib/settings.cpp b/lib/settings.cpp index 34ffa3230..5f0ef279e 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -37,6 +37,7 @@ Settings::Settings() _exitCode(0), _showtime(0), _maxConfigs(12), + enforcedLang(None), reportProgress(false), checkConfiguration(false) { diff --git a/lib/settings.h b/lib/settings.h index 2039bacc5..4ec6bd2b2 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -149,8 +149,12 @@ public: */ std::string addEnabled(const std::string &str); + enum Language { + None, C, CPP, CSharp, Java + }; + /** @brief Name of the language that is enforced. Empty per default. */ - std::string enforcedLang; + Language enforcedLang; /** @brief suppress message (--suppressions) */ Suppressions nomsg; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index b3f963287..1b4da66b6 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9355,12 +9355,12 @@ const std::string& Tokenizer::getSourceFilePath() const bool Tokenizer::isJava() const { - return _settings->enforcedLang == "java" || (_settings->enforcedLang.empty() && Path::isJava(getSourceFilePath())); + return _settings->enforcedLang == Settings::Java || (_settings->enforcedLang == Settings::None && Path::isJava(getSourceFilePath())); } bool Tokenizer::isCSharp() const { - return _settings->enforcedLang == "c#" || (_settings->enforcedLang.empty() && Path::isCSharp(getSourceFilePath())); + return _settings->enforcedLang == Settings::CSharp || (_settings->enforcedLang == Settings::None && Path::isCSharp(getSourceFilePath())); } bool Tokenizer::isJavaOrCSharp() const @@ -9370,12 +9370,12 @@ bool Tokenizer::isJavaOrCSharp() const bool Tokenizer::isC() const { - return _settings->enforcedLang == "c" || (_settings->enforcedLang.empty() && Path::isC(getSourceFilePath())); + return _settings->enforcedLang == Settings::C || (_settings->enforcedLang == Settings::None && Path::isC(getSourceFilePath())); } bool Tokenizer::isCPP() const { - return _settings->enforcedLang == "c++" || (_settings->enforcedLang.empty() && Path::isCPP(getSourceFilePath())); + return _settings->enforcedLang == Settings::CPP || (_settings->enforcedLang == Settings::None && Path::isCPP(getSourceFilePath())); } void Tokenizer::reportError(const Token* tok, const Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive) const diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 9b963b76a..471940428 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -375,14 +375,14 @@ private: Settings settings; CmdLineParser parser(&settings); ASSERT(parser.ParseFromArgs(2, argv)); - ASSERT_EQUALS("", settings.enforcedLang); + ASSERT_EQUALS(Settings::None, settings.enforcedLang); } { const char *argv[] = {"cppcheck", "-x", "c++", "file.cpp"}; Settings settings; CmdLineParser parser(&settings); ASSERT(parser.ParseFromArgs(4, argv)); - ASSERT_EQUALS("c++", settings.enforcedLang); + ASSERT_EQUALS(Settings::CPP, settings.enforcedLang); } { const char *argv[] = {"cppcheck", "-x"}; @@ -401,28 +401,28 @@ private: Settings settings; CmdLineParser parser(&settings); ASSERT(parser.ParseFromArgs(3, argv)); - ASSERT_EQUALS("c++", settings.enforcedLang); + ASSERT_EQUALS(Settings::CPP, settings.enforcedLang); } { const char *argv[] = {"cppcheck", "--language=c", "file.cpp"}; Settings settings; CmdLineParser parser(&settings); ASSERT(parser.ParseFromArgs(3, argv)); - ASSERT_EQUALS("c", settings.enforcedLang); + ASSERT_EQUALS(Settings::C, settings.enforcedLang); } { const char *argv[] = {"cppcheck", "--language=c#", "file.cpp"}; Settings settings; CmdLineParser parser(&settings); ASSERT(parser.ParseFromArgs(3, argv)); - ASSERT_EQUALS("c#", settings.enforcedLang); + ASSERT_EQUALS(Settings::CSharp, settings.enforcedLang); } { const char *argv[] = {"cppcheck", "--language=java", "file.cpp"}; Settings settings; CmdLineParser parser(&settings); ASSERT(parser.ParseFromArgs(3, argv)); - ASSERT_EQUALS("java", settings.enforcedLang); + ASSERT_EQUALS(Settings::Java, settings.enforcedLang); } { const char *argv[] = {"cppcheck", "--language=unknwonLanguage", "file.cpp"};