Used enum instead of string in implementation for '--language='.

This commit is contained in:
PKEuS 2012-09-11 08:53:27 +02:00
parent 77b5175ec3
commit 50f9dd52ab
6 changed files with 39 additions and 26 deletions

View File

@ -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 (strcmp(argv[i], "-x") == 0) {
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;
}
_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.");
str = argv[i];
}
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;
}
}

View File

@ -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<std::string, std::string> 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<std::string> filenames;

View File

@ -37,6 +37,7 @@ Settings::Settings()
_exitCode(0),
_showtime(0),
_maxConfigs(12),
enforcedLang(None),
reportProgress(false),
checkConfiguration(false)
{

View File

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

View File

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

View File

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