From a299411a82a8aea27905ec1553b7b027d5a52d23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 29 Jan 2011 16:14:09 +0100 Subject: [PATCH 1/3] Tokenizer: Better simplifications of static constants --- lib/tokenize.cpp | 12 ++++++------ test/testtokenize.cpp | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) 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/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;"; From d334a02801b52a6d5d372d05e03f619049fd5840 Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Sat, 29 Jan 2011 19:04:52 +0200 Subject: [PATCH 2/3] Make --exitcode-suppressions option consistent. The --exitcode-suppressions option was inconsistent with other long options by taking the filename as separate argument. Now it expects format --exitcode-suppressions=filename.txt like other long options. Ticket: #1837 (--suppresions file.txt inconsistent) --- cli/cmdlineparser.cpp | 30 ++++++++++++++++++++++-------- test/testcmdlineparser.cpp | 25 +++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 10 deletions(-) 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/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index c9bac0429..6b9bdf75c 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); @@ -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)); From b9a0e10b9a58091b3dc54b2a97b3f9216dddefde Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Sat, 29 Jan 2011 19:16:23 +0200 Subject: [PATCH 3/3] Several fixes to cmd line tests. There were several tests handling two parameters as one string. Eg. "-I path" which doesn't work correctly. Fixed these problematic tests. --- test/testcmdlineparser.cpp | 42 +++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 6b9bdf75c..009be82f0 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -312,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() @@ -352,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() @@ -462,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() @@ -480,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); } @@ -499,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() @@ -519,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()