diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 8fcc1f1ba..93ac303ad 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -197,6 +197,18 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) } } + else if (strncmp(argv[i], "--suppress=", 11) == 0) + { + std::string suppression = argv[i]; + suppression = suppression.substr(11); + const std::string errmsg(_settings->nomsg.addSuppressionLine(suppression)); + if (!errmsg.empty()) + { + PrintMessage(errmsg); + return false; + } + } + // Enables inline suppressions. else if (strcmp(argv[i], "--inline-suppr") == 0) _settings->_inlineSuppressions = true; @@ -661,11 +673,12 @@ void CmdLineParser::PrintHelp() " --rule-file= Use given rule file. For more information, see: \n" " https://sourceforge.net/projects/cppcheck/files/Articles/\n" " -s, --style Deprecated, use --enable=style\n" - " --suppressions-list=\n" - " Suppress warnings listed in the file. Filename and line\n" - " are optional in the suppression file. The format of the\n" - " single line in the suppression file is:\n" + " --suppress= Suppress a specific warning. The format of is:\n" " [error id]:[filename]:[line]\n" + " The [filename] and [line] are optional.\n" + " --suppressions-list=\n" + " Suppress warnings listed in the file. Each suppression\n" + " is in the same format as above.\n" " --template '' Format the error messages. E.g.\n" " '{file}:{line},{severity},{id},{message}' or\n" " '{file}({line}):({severity}) {message}'\n" diff --git a/man/cppcheck.1.xml b/man/cppcheck.1.xml index 9332a09ae..d0aae70a0 100644 --- a/man/cppcheck.1.xml +++ b/man/cppcheck.1.xml @@ -120,6 +120,7 @@ man(1), man(7), http://www.tldp.org/HOWTO/Man-Page/ + @@ -303,12 +304,19 @@ Directory name is matched to all parts of the path. Deprecated, use --enable=style + + + + Suppress a specific warning. The format of <spec> is: [error id]:[filename]:[line]. + The [filename] and [line] are optional. + [filename] may contain the wildcard characters * or ?. + + - Suppress warnings listed in the file. Filename and line are optional. The format of the single line in file is: [error id]:[filename]:[line]. - You can use --template or --xml to see the error id. - The filename may contain the wildcard characters * or ?. + Suppress warnings listed in the file. + Each suppression is in the format of <spec> above. diff --git a/man/manual.docbook b/man/manual.docbook index 07285513a..c3c16bb61 100644 --- a/man/manual.docbook +++ b/man/manual.docbook @@ -374,7 +374,8 @@ gui/test.cpp,16,error,mismatchAllocDealloc,Mismatching allocation and deallocati Suppressions If you want to filter out certain errors you can suppress these. - First you need to create a suppressions file. The format is: + The --suppress= command line option is used to specify + suppressions on the command line. The format is one of: [error id]:[filename]:[line] [error id]:[filename2] @@ -389,6 +390,11 @@ gui/test.cpp,16,error,mismatchAllocDealloc,Mismatching allocation and deallocati * or ?, which match any sequence of characters or any single character respectively. + cppcheck --suppress=memleak:file1.cpp src/ + + If you have more than a couple of suppressions, create a suppressions + file with one line per suppression as above. + Here is an example: memleak:file1.cpp diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index bdbd1e4d7..ad2a03b89 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -77,6 +77,8 @@ private: TEST_CASE(suppressionsOld); // TODO: Create and test real suppression file TEST_CASE(suppressions) TEST_CASE(suppressionsNoFile) + TEST_CASE(suppressionSingle) + TEST_CASE(suppressionSingleFile) TEST_CASE(templates); TEST_CASE(templatesGcc); TEST_CASE(templatesVs); @@ -554,6 +556,26 @@ private: ASSERT(!parser.ParseFromArgs(3, argv)); } + void suppressionSingle() + { + REDIRECT; + const char *argv[] = {"cppcheck", "--suppress=uninitvar", "file.cpp"}; + Settings settings; + CmdLineParser parser(&settings); + ASSERT(parser.ParseFromArgs(3, argv)); + ASSERT_EQUALS(true, settings.nomsg.isSuppressed("uninitvar", "file.cpp", 1U)); + } + + void suppressionSingleFile() + { + REDIRECT; + const char *argv[] = {"cppcheck", "--suppress=uninitvar:file.cpp", "file.cpp"}; + Settings settings; + CmdLineParser parser(&settings); + ASSERT(parser.ParseFromArgs(3, argv)); + ASSERT_EQUALS(true, settings.nomsg.isSuppressed("uninitvar", "file.cpp", 1U)); + } + void templates() { REDIRECT;