Added new cmdline option --language= (alias of GCC-like -x) to enforce a specific language. Valid values: c, c++, java, c#. (#3994)

This commit is contained in:
PKEuS 2012-09-10 18:51:32 +02:00
parent 88f9e9991d
commit 87131f6105
5 changed files with 96 additions and 5 deletions

View File

@ -125,6 +125,26 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
else if (strcmp(argv[i], "--inconclusive") == 0)
_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) {
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.");
return false;
}
}
// Filter errors
else if (strncmp(argv[i], "--exitcode-suppressions", 23) == 0) {
std::string filename;
@ -757,6 +777,9 @@ void CmdLineParser::PrintHelp() const
" more comments, like: '// cppcheck-suppress warningId'\n"
" on the lines before the warning to suppress.\n"
" -j <jobs> Start [jobs] threads to do the checking simultaneously.\n"
" --language=<language>, -x <language>\n"
" Forces cppcheck to check all files as the given\n"
" language. Valid values are: c, c++, java, c#\n"
" --max-configs=<limit>\n"
" Maximum number of configurations to check in a file\n"
" before skipping it. Default is '12'. If used together\n"

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 (Path::isCPP(filename) && cfgmap.find("__cplusplus") == cfgmap.end())
if (((_settings && _settings->enforcedLang == "c++") || ((!_settings || _settings->enforcedLang.empty()) && Path::isCPP(filename))) && cfgmap.find("__cplusplus") == cfgmap.end())
cfgmap["__cplusplus"] = "1";
std::stack<std::string> filenames;

View File

@ -149,6 +149,9 @@ public:
*/
std::string addEnabled(const std::string &str);
/** @brief Name of the language that is enforced. Empty per default. */
std::string enforcedLang;
/** @brief suppress message (--suppressions) */
Suppressions nomsg;

View File

@ -9334,12 +9334,12 @@ const std::string& Tokenizer::getSourceFilePath() const
bool Tokenizer::isJava() const
{
return Path::isJava(getSourceFilePath());
return _settings->enforcedLang == "java" || (_settings->enforcedLang.empty() && Path::isJava(getSourceFilePath()));
}
bool Tokenizer::isCSharp() const
{
return Path::isCSharp(getSourceFilePath());
return _settings->enforcedLang == "c#" || (_settings->enforcedLang.empty() && Path::isCSharp(getSourceFilePath()));
}
bool Tokenizer::isJavaOrCSharp() const
@ -9349,12 +9349,12 @@ bool Tokenizer::isJavaOrCSharp() const
bool Tokenizer::isC() const
{
return Path::isC(getSourceFilePath());
return _settings->enforcedLang == "c" || (_settings->enforcedLang.empty() && Path::isC(getSourceFilePath()));
}
bool Tokenizer::isCPP() const
{
return Path::isCPP(getSourceFilePath());
return _settings->enforcedLang == "c++" || (_settings->enforcedLang.empty() && 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

@ -52,6 +52,7 @@ private:
TEST_CASE(defines2);
TEST_CASE(defines3);
//TEST_CASE(defines4);
TEST_CASE(enforceLanguage);
TEST_CASE(includesnopath);
TEST_CASE(includes);
TEST_CASE(includesslash);
@ -367,6 +368,70 @@ private:
ASSERT_EQUALS("DEBUG", settings.userDefines);
}
void enforceLanguage() {
REDIRECT;
{
const char *argv[] = {"cppcheck", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(2, argv));
ASSERT_EQUALS("", 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);
}
{
const char *argv[] = {"cppcheck", "-x"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(!parser.ParseFromArgs(2, argv));
}
{
const char *argv[] = {"cppcheck", "-x", "--inconclusive", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(!parser.ParseFromArgs(4, argv));
}
{
const char *argv[] = {"cppcheck", "--language=c++", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(3, argv));
ASSERT_EQUALS("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);
}
{
const char *argv[] = {"cppcheck", "--language=c#", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(3, argv));
ASSERT_EQUALS("c#", 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);
}
{
const char *argv[] = {"cppcheck", "--language=unknwonLanguage", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(!parser.ParseFromArgs(3, argv));
}
}
void includesnopath() {
REDIRECT;
const char *argv[] = {"cppcheck", "-I", "file.cpp"};