This commit is contained in:
Sébastien Debrard 2011-01-29 18:50:00 +01:00
commit d4f3f5e286
4 changed files with 86 additions and 37 deletions

View File

@ -104,20 +104,34 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
} }
// Filter errors // Filter errors
else if (strcmp(argv[i], "--exitcode-suppressions") == 0) else if (strncmp(argv[i], "--exitcode-suppressions", 23) == 0)
{ {
++i; std::string filename;
if (i >= argc) // exitcode-suppressions filename.txt
// Deprecated
if (strcmp(argv[i], "--exitcode-suppressions") == 0)
{ {
PrintMessage("cppcheck: No file specified for the --exitcode-suppressions option"); ++i;
return false;
if (i >= argc || strncmp(argv[i], "-", 1) == 0 ||
strncmp(argv[i], "--", 2) == 0)
{
PrintMessage("cppcheck: No filename specified for the --exitcode-suppressions option");
return false;
}
filename = argv[i];
}
// exitcode-suppressions=filename.txt
else
{
filename = 24 + argv[i];
} }
std::ifstream f(argv[i]); std::ifstream f(filename.c_str());
if (!f.is_open()) if (!f.is_open())
{ {
PrintMessage("cppcheck: Couldn't open the file \"" + std::string(argv[i]) + "\""); PrintMessage("cppcheck: Couldn't open the file \"" + std::string(filename) + "\"");
return false; return false;
} }
const std::string errmsg(_settings->nofail.parseFile(f)); const std::string errmsg(_settings->nofail.parseFile(f));
@ -565,7 +579,7 @@ void CmdLineParser::PrintHelp()
" provided. Note that your operating system can\n" " provided. Note that your operating system can\n"
" modify this value, e.g. 256 can become 0.\n" " modify this value, e.g. 256 can become 0.\n"
" --errorlist Print a list of all error messages in XML format.\n" " --errorlist Print a list of all error messages in XML format.\n"
" --exitcode-suppressions file\n" " --exitcode-suppressions=file\n"
" Used when certain messages should be displayed but\n" " Used when certain messages should be displayed but\n"
" should not cause a non-zero exitcode.\n" " should not cause a non-zero exitcode.\n"
" --file-list=file Specify the files to check in a text file. One Filename per line.\n" " --file-list=file Specify the files to check in a text file. One Filename per line.\n"

View File

@ -6231,22 +6231,22 @@ bool Tokenizer::simplifyKnownVariables()
std::map<unsigned int, std::string> constantValues; std::map<unsigned int, std::string> constantValues;
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
{ {
if (Token::Match(tok, "static| const %type% %var% = %any% ;")) if (Token::Match(tok, "static| const static| %type% %var% = %any% ;"))
{ {
Token *tok1 = tok; Token *tok1 = tok;
// start of statement // start of statement
if (tok != _tokens && !Token::Match(tok->previous(),"[;{}]")) if (tok != _tokens && !Token::Match(tok->previous(),"[;{}]"))
continue; continue;
// skip "static" // skip "const" and "static"
if (tok->str() == "static") while (tok->str() == "const" || tok->str() == "static")
tok = tok->next(); tok = tok->next();
// pod type // pod type
if (!tok->next()->isStandardType()) if (!tok->isStandardType())
continue; continue;
const Token * const vartok = tok->tokAt(2); const Token * const vartok = tok->next();
const Token * const valuetok = tok->tokAt(4); const Token * const valuetok = tok->tokAt(3);
if (valuetok->isNumber() || Token::Match(valuetok, "%str% ;")) if (valuetok->isNumber() || Token::Match(valuetok, "%str% ;"))
{ {
constantValues[vartok->varId()] = valuetok->str(); constantValues[vartok->varId()] = valuetok->str();

View File

@ -65,7 +65,9 @@ private:
TEST_CASE(errorExitcode); TEST_CASE(errorExitcode);
TEST_CASE(errorExitcodeMissing); TEST_CASE(errorExitcodeMissing);
TEST_CASE(errorExitcodeStr); TEST_CASE(errorExitcodeStr);
TEST_CASE(exitcodeSuppressions); // TODO: Create and test real suppression file TEST_CASE(exitcodeSuppressionsOld); // TODO: Create and test real suppression file
TEST_CASE(exitcodeSuppressions);
TEST_CASE(exitcodeSuppressionsNoFile);
TEST_CASE(fileList); // TODO: Create and test real file listing file TEST_CASE(fileList); // TODO: Create and test real file listing file
TEST_CASE(inlineSuppr); TEST_CASE(inlineSuppr);
TEST_CASE(jobs); TEST_CASE(jobs);
@ -310,31 +312,31 @@ private:
void includes() void includes()
{ {
REDIRECT; REDIRECT;
const char *argv[] = {"cppcheck", "-I include", "file.cpp"}; const char *argv[] = {"cppcheck", "-I", "include", "file.cpp"};
Settings settings; Settings settings;
CmdLineParser parser(&settings); CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(3, argv)); ASSERT(parser.ParseFromArgs(4, argv));
ASSERT_EQUALS(" include/", settings._includePaths.front()); ASSERT_EQUALS("include/", settings._includePaths.front());
} }
void includesslash() void includesslash()
{ {
REDIRECT; REDIRECT;
const char *argv[] = {"cppcheck", "-I include/", "file.cpp"}; const char *argv[] = {"cppcheck", "-I", "include/", "file.cpp"};
Settings settings; Settings settings;
CmdLineParser parser(&settings); CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(3, argv)); ASSERT(parser.ParseFromArgs(4, argv));
ASSERT_EQUALS(" include/", settings._includePaths.front()); ASSERT_EQUALS("include/", settings._includePaths.front());
} }
void includesbackslash() void includesbackslash()
{ {
REDIRECT; REDIRECT;
const char *argv[] = {"cppcheck", "-I include\\", "file.cpp"}; const char *argv[] = {"cppcheck", "-I", "include\\", "file.cpp"};
Settings settings; Settings settings;
CmdLineParser parser(&settings); CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(3, argv)); ASSERT(parser.ParseFromArgs(4, argv));
ASSERT_EQUALS(" include/", settings._includePaths.front()); ASSERT_EQUALS("include/", settings._includePaths.front());
} }
void includesnospace() void includesnospace()
@ -350,13 +352,13 @@ private:
void includes2() void includes2()
{ {
REDIRECT; REDIRECT;
const char *argv[] = {"cppcheck", "-I include/", "-I framework/", "file.cpp"}; const char *argv[] = {"cppcheck", "-I", "include/", "-I", "framework/", "file.cpp"};
Settings settings; Settings settings;
CmdLineParser parser(&settings); CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(4, argv)); ASSERT(parser.ParseFromArgs(6, argv));
ASSERT_EQUALS(" include/", settings._includePaths.front()); ASSERT_EQUALS("include/", settings._includePaths.front());
settings._includePaths.pop_front(); settings._includePaths.pop_front();
ASSERT_EQUALS(" framework/", settings._includePaths.front()); ASSERT_EQUALS("framework/", settings._includePaths.front());
} }
void enabledAll() void enabledAll()
@ -429,10 +431,29 @@ private:
ASSERT(!parser.ParseFromArgs(3, argv)); ASSERT(!parser.ParseFromArgs(3, argv));
} }
void exitcodeSuppressionsOld()
{
// TODO: Fails since cannot open the file
REDIRECT;
const char *argv[] = {"cppcheck", "--exitcode-suppressions", "suppr.txt", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(!parser.ParseFromArgs(4, argv));
}
void exitcodeSuppressions() void exitcodeSuppressions()
{ {
REDIRECT; REDIRECT;
const char *argv[] = {"cppcheck", "--error-exitcode-suppressions suppr.txt", "file.cpp"}; const char *argv[] = {"cppcheck", "--exitcode-suppressions=suppr.txt", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(!parser.ParseFromArgs(3, argv));
}
void exitcodeSuppressionsNoFile()
{
REDIRECT;
const char *argv[] = {"cppcheck", "--exitcode-suppressions", "file.cpp"};
Settings settings; Settings settings;
CmdLineParser parser(&settings); CmdLineParser parser(&settings);
ASSERT(!parser.ParseFromArgs(3, argv)); ASSERT(!parser.ParseFromArgs(3, argv));
@ -441,10 +462,10 @@ private:
void fileList() void fileList()
{ {
REDIRECT; REDIRECT;
const char *argv[] = {"cppcheck", "--file-list files.txt", "file.cpp"}; const char *argv[] = {"cppcheck", "--file-list", "files.txt", "file.cpp"};
Settings settings; Settings settings;
CmdLineParser parser(&settings); CmdLineParser parser(&settings);
ASSERT(!parser.ParseFromArgs(3, argv)); ASSERT(!parser.ParseFromArgs(4, argv));
} }
void inlineSuppr() void inlineSuppr()
@ -459,10 +480,10 @@ private:
void jobs() void jobs()
{ {
REDIRECT; REDIRECT;
const char *argv[] = {"cppcheck", "-j 3", "file.cpp"}; const char *argv[] = {"cppcheck", "-j", "3", "file.cpp"};
Settings settings; Settings settings;
CmdLineParser parser(&settings); CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(3, argv)); ASSERT(parser.ParseFromArgs(4, argv));
ASSERT_EQUALS(3, settings._jobs); ASSERT_EQUALS(3, settings._jobs);
} }
@ -478,10 +499,10 @@ private:
void jobsInvalid() void jobsInvalid()
{ {
REDIRECT; REDIRECT;
const char *argv[] = {"cppcheck", "-j e", "file.cpp"}; const char *argv[] = {"cppcheck", "-j", "e", "file.cpp"};
Settings settings; Settings settings;
CmdLineParser parser(&settings); CmdLineParser parser(&settings);
ASSERT(!parser.ParseFromArgs(3, argv)); ASSERT(!parser.ParseFromArgs(4, argv));
} }
void reportProgress() void reportProgress()
@ -498,10 +519,10 @@ private:
{ {
// TODO: Fails because there is no suppr.txt file! // TODO: Fails because there is no suppr.txt file!
REDIRECT; REDIRECT;
const char *argv[] = {"cppcheck", "--suppressions suppr.txt", "file.cpp"}; const char *argv[] = {"cppcheck", "--suppressions", "suppr.txt", "file.cpp"};
Settings settings; Settings settings;
CmdLineParser parser(&settings); CmdLineParser parser(&settings);
ASSERT(!parser.ParseFromArgs(3, argv)); ASSERT(!parser.ParseFromArgs(4, argv));
} }
void suppressions() void suppressions()

View File

@ -211,6 +211,7 @@ private:
TEST_CASE(tokenize_strings); TEST_CASE(tokenize_strings);
TEST_CASE(simplify_constants); TEST_CASE(simplify_constants);
TEST_CASE(simplify_constants2); TEST_CASE(simplify_constants2);
TEST_CASE(simplify_constants3);
TEST_CASE(vardecl1); TEST_CASE(vardecl1);
TEST_CASE(vardecl2); TEST_CASE(vardecl2);
@ -3812,6 +3813,19 @@ private:
ASSERT_EQUALS(oss.str(), ostr.str()); ASSERT_EQUALS(oss.str(), ostr.str());
} }
void simplify_constants3()
{
const char code[] =
"static const char str[] = \"abcd\";\n"
"static const unsigned int SZ = sizeof(str);\n"
"void f() {\n"
"a = SZ;\n"
"}\n";
const char expected[] =
"static const char str [ 5 ] = \"abcd\" ;\n\nvoid f ( ) {\na = 5 ;\n}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code,true));
}
void vardecl1() void vardecl1()
{ {
const char code[] = "unsigned int a, b;"; const char code[] = "unsigned int a, b;";