diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index a333804a8..c3eaa5764 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -104,20 +104,34 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) } // 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"); - return false; + ++i; + + 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()) { - PrintMessage("cppcheck: Couldn't open the file \"" + std::string(argv[i]) + "\""); + PrintMessage("cppcheck: Couldn't open the file \"" + std::string(filename) + "\""); return false; } const std::string errmsg(_settings->nofail.parseFile(f)); @@ -565,7 +579,7 @@ void CmdLineParser::PrintHelp() " provided. Note that your operating system can\n" " modify this value, e.g. 256 can become 0.\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" " 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" diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9cbdcd6ea..576dacb84 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6231,22 +6231,22 @@ bool Tokenizer::simplifyKnownVariables() std::map constantValues; 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; // start of statement if (tok != _tokens && !Token::Match(tok->previous(),"[;{}]")) continue; - // skip "static" - if (tok->str() == "static") + // skip "const" and "static" + while (tok->str() == "const" || tok->str() == "static") tok = tok->next(); // pod type - if (!tok->next()->isStandardType()) + if (!tok->isStandardType()) continue; - const Token * const vartok = tok->tokAt(2); - const Token * const valuetok = tok->tokAt(4); + const Token * const vartok = tok->next(); + const Token * const valuetok = tok->tokAt(3); if (valuetok->isNumber() || Token::Match(valuetok, "%str% ;")) { constantValues[vartok->varId()] = valuetok->str(); diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index c9bac0429..009be82f0 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -65,7 +65,9 @@ private: TEST_CASE(errorExitcode); TEST_CASE(errorExitcodeMissing); 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(inlineSuppr); TEST_CASE(jobs); @@ -310,31 +312,31 @@ private: void includes() { REDIRECT; - const char *argv[] = {"cppcheck", "-I include", "file.cpp"}; + const char *argv[] = {"cppcheck", "-I", "include", "file.cpp"}; Settings settings; CmdLineParser parser(&settings); - ASSERT(parser.ParseFromArgs(3, argv)); - ASSERT_EQUALS(" include/", settings._includePaths.front()); + ASSERT(parser.ParseFromArgs(4, argv)); + ASSERT_EQUALS("include/", settings._includePaths.front()); } void includesslash() { REDIRECT; - const char *argv[] = {"cppcheck", "-I include/", "file.cpp"}; + const char *argv[] = {"cppcheck", "-I", "include/", "file.cpp"}; Settings settings; CmdLineParser parser(&settings); - ASSERT(parser.ParseFromArgs(3, argv)); - ASSERT_EQUALS(" include/", settings._includePaths.front()); + ASSERT(parser.ParseFromArgs(4, argv)); + ASSERT_EQUALS("include/", settings._includePaths.front()); } void includesbackslash() { REDIRECT; - const char *argv[] = {"cppcheck", "-I include\\", "file.cpp"}; + const char *argv[] = {"cppcheck", "-I", "include\\", "file.cpp"}; Settings settings; CmdLineParser parser(&settings); - ASSERT(parser.ParseFromArgs(3, argv)); - ASSERT_EQUALS(" include/", settings._includePaths.front()); + ASSERT(parser.ParseFromArgs(4, argv)); + ASSERT_EQUALS("include/", settings._includePaths.front()); } void includesnospace() @@ -350,13 +352,13 @@ private: void includes2() { REDIRECT; - const char *argv[] = {"cppcheck", "-I include/", "-I framework/", "file.cpp"}; + const char *argv[] = {"cppcheck", "-I", "include/", "-I", "framework/", "file.cpp"}; Settings settings; CmdLineParser parser(&settings); - ASSERT(parser.ParseFromArgs(4, argv)); - ASSERT_EQUALS(" include/", settings._includePaths.front()); + ASSERT(parser.ParseFromArgs(6, argv)); + ASSERT_EQUALS("include/", settings._includePaths.front()); settings._includePaths.pop_front(); - ASSERT_EQUALS(" framework/", settings._includePaths.front()); + ASSERT_EQUALS("framework/", settings._includePaths.front()); } void enabledAll() @@ -429,10 +431,29 @@ private: 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() { 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; CmdLineParser parser(&settings); ASSERT(!parser.ParseFromArgs(3, argv)); @@ -441,10 +462,10 @@ private: void fileList() { REDIRECT; - const char *argv[] = {"cppcheck", "--file-list files.txt", "file.cpp"}; + const char *argv[] = {"cppcheck", "--file-list", "files.txt", "file.cpp"}; Settings settings; CmdLineParser parser(&settings); - ASSERT(!parser.ParseFromArgs(3, argv)); + ASSERT(!parser.ParseFromArgs(4, argv)); } void inlineSuppr() @@ -459,10 +480,10 @@ private: void jobs() { REDIRECT; - const char *argv[] = {"cppcheck", "-j 3", "file.cpp"}; + const char *argv[] = {"cppcheck", "-j", "3", "file.cpp"}; Settings settings; CmdLineParser parser(&settings); - ASSERT(parser.ParseFromArgs(3, argv)); + ASSERT(parser.ParseFromArgs(4, argv)); ASSERT_EQUALS(3, settings._jobs); } @@ -478,10 +499,10 @@ private: void jobsInvalid() { REDIRECT; - const char *argv[] = {"cppcheck", "-j e", "file.cpp"}; + const char *argv[] = {"cppcheck", "-j", "e", "file.cpp"}; Settings settings; CmdLineParser parser(&settings); - ASSERT(!parser.ParseFromArgs(3, argv)); + ASSERT(!parser.ParseFromArgs(4, argv)); } void reportProgress() @@ -498,10 +519,10 @@ private: { // TODO: Fails because there is no suppr.txt file! REDIRECT; - const char *argv[] = {"cppcheck", "--suppressions suppr.txt", "file.cpp"}; + const char *argv[] = {"cppcheck", "--suppressions", "suppr.txt", "file.cpp"}; Settings settings; CmdLineParser parser(&settings); - ASSERT(!parser.ParseFromArgs(3, argv)); + ASSERT(!parser.ParseFromArgs(4, argv)); } void suppressions() diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index f6d07042f..f2f827fb5 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -211,6 +211,7 @@ private: TEST_CASE(tokenize_strings); TEST_CASE(simplify_constants); TEST_CASE(simplify_constants2); + TEST_CASE(simplify_constants3); TEST_CASE(vardecl1); TEST_CASE(vardecl2); @@ -3812,6 +3813,19 @@ private: 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() { const char code[] = "unsigned int a, b;";