CmdLineParser: pass suppressions separately from settings (#5023)

This commit is contained in:
Oliver Stöneberg 2023-04-30 20:56:54 +02:00 committed by GitHub
parent 86757de4d5
commit 5a2c31a41c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 22 deletions

View File

@ -114,8 +114,10 @@ static bool addPathsToSet(const std::string& fileName, std::set<std::string>& se
return true;
}
CmdLineParser::CmdLineParser(Settings &settings)
CmdLineParser::CmdLineParser(Settings &settings, Suppressions &suppressions, Suppressions &suppressionsNoFail)
: mSettings(settings)
, mSuppressions(suppressions)
, mSuppressionsNoFail(suppressionsNoFail)
, mShowHelp(false)
, mShowVersion(false)
, mShowErrorMessages(false)
@ -406,7 +408,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
printError("couldn't open the file: \"" + filename + "\".");
return false;
}
const std::string errmsg(mSettings.nofail.parseFile(f));
const std::string errmsg(mSuppressionsNoFail.parseFile(f));
if (!errmsg.empty()) {
printError(errmsg);
return false;
@ -858,7 +860,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
else if (std::strncmp(argv[i], "--suppress=", 11) == 0) {
const std::string suppression = argv[i]+11;
const std::string errmsg(mSettings.nomsg.addSuppressionLine(suppression));
const std::string errmsg(mSuppressions.addSuppressionLine(suppression));
if (!errmsg.empty()) {
printError(errmsg);
return false;
@ -885,7 +887,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
printError(message);
return false;
}
const std::string errmsg(mSettings.nomsg.parseFile(f));
const std::string errmsg(mSuppressions.parseFile(f));
if (!errmsg.empty()) {
printError(errmsg);
return false;
@ -894,7 +896,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
else if (std::strncmp(argv[i], "--suppress-xml=", 15) == 0) {
const char * filename = argv[i] + 15;
const std::string errmsg(mSettings.nomsg.parseXmlFile(filename));
const std::string errmsg(mSuppressions.parseXmlFile(filename));
if (!errmsg.empty()) {
printError(errmsg);
return false;

View File

@ -25,6 +25,7 @@
#include "utils.h"
class Settings;
class Suppressions;
/// @addtogroup CLI
/// @{
@ -44,8 +45,10 @@ public:
* The constructor.
* @param settings Settings instance that will be modified according to
* options user has given.
* @param suppressions Suppressions instance that keeps the suppressions
* @param suppressionsNoFail Suppressions instance that keeps the "do not fail" suppressions
*/
explicit CmdLineParser(Settings &settings);
CmdLineParser(Settings &settings, Suppressions &suppressions, Suppressions &suppressionsNoFail);
/**
* Parse given command line.
@ -141,6 +144,8 @@ private:
std::vector<std::string> mPathNames;
std::vector<std::string> mIgnoredPaths;
Settings &mSettings;
Suppressions &mSuppressions;
Suppressions &mSuppressionsNoFail;
bool mShowHelp;
bool mShowVersion;
bool mShowErrorMessages;

View File

@ -80,7 +80,7 @@ CppCheckExecutor::~CppCheckExecutor()
bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* const argv[])
{
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
const bool success = parser.parseFromArgs(argc, argv);
if (success) {

View File

@ -39,7 +39,7 @@ class TestCmdlineParser : public TestFixture {
public:
TestCmdlineParser()
: TestFixture("TestCmdlineParser")
, defParser(settings) {
, defParser(settings, settings.nomsg, settings.nofail) {
#if defined(_WIN64) || defined(_WIN32)
CmdLineParser::SHOW_DEF_PLATFORM_MSG = false;
#endif
@ -244,7 +244,7 @@ private:
void nooptions() {
REDIRECT;
const char * const argv[] = {"cppcheck"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT(parser.parseFromArgs(1, argv));
ASSERT_EQUALS(true, parser.getShowHelp());
ASSERT(GET_REDIRECT_OUTPUT.find("Cppcheck - A tool for static C/C++ code analysis") == 0);
@ -253,7 +253,7 @@ private:
void helpshort() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-h"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT(parser.parseFromArgs(2, argv));
ASSERT_EQUALS(true, parser.getShowHelp());
ASSERT(GET_REDIRECT_OUTPUT.find("Cppcheck - A tool for static C/C++ code analysis") == 0);
@ -262,7 +262,7 @@ private:
void helplong() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--help"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT(parser.parseFromArgs(2, argv));
ASSERT_EQUALS(true, parser.getShowHelp());
ASSERT(GET_REDIRECT_OUTPUT.find("Cppcheck - A tool for static C/C++ code analysis") == 0);
@ -271,7 +271,7 @@ private:
void showversion() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--version"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT(parser.parseFromArgs(2, argv));
ASSERT_EQUALS(true, parser.getShowVersion());
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); // version is not actually shown
@ -280,7 +280,7 @@ private:
void onefile() {
REDIRECT;
const char * const argv[] = {"cppcheck", "file.cpp"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT(parser.parseFromArgs(2, argv));
ASSERT_EQUALS(1, (int)parser.getPathNames().size());
ASSERT_EQUALS("file.cpp", parser.getPathNames().at(0));
@ -290,7 +290,7 @@ private:
void onepath() {
REDIRECT;
const char * const argv[] = {"cppcheck", "src"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT(parser.parseFromArgs(2, argv));
ASSERT_EQUALS(1, (int)parser.getPathNames().size());
ASSERT_EQUALS("src", parser.getPathNames().at(0));
@ -300,7 +300,7 @@ private:
void optionwithoutfile() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-v"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT_EQUALS(false, parser.parseFromArgs(2, argv));
ASSERT_EQUALS(0, (int)parser.getPathNames().size());
ASSERT_EQUALS("cppcheck: error: no C or C++ source files found.\n", GET_REDIRECT_OUTPUT);
@ -1561,7 +1561,7 @@ private:
void ignorepathsnopath() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-i"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
// Fails since no ignored path given
ASSERT_EQUALS(false, parser.parseFromArgs(2, argv));
ASSERT_EQUALS("cppcheck: error: argument to '-i' is missing.\n", GET_REDIRECT_OUTPUT);
@ -1829,7 +1829,7 @@ private:
void ignorepaths1() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT(parser.parseFromArgs(3, argv));
ASSERT_EQUALS(1, parser.getIgnoredPaths().size());
ASSERT_EQUALS("src", parser.getIgnoredPaths()[0]);
@ -1839,7 +1839,7 @@ private:
void ignorepaths2() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-i", "src", "file.cpp"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT(parser.parseFromArgs(4, argv));
ASSERT_EQUALS(1, parser.getIgnoredPaths().size());
ASSERT_EQUALS("src", parser.getIgnoredPaths()[0]);
@ -1849,7 +1849,7 @@ private:
void ignorepaths3() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-isrc", "-imodule", "file.cpp"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT(parser.parseFromArgs(4, argv));
ASSERT_EQUALS(2, parser.getIgnoredPaths().size());
ASSERT_EQUALS("src", parser.getIgnoredPaths()[0]);
@ -1860,7 +1860,7 @@ private:
void ignorepaths4() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-i", "src", "-i", "module", "file.cpp"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT(parser.parseFromArgs(6, argv));
ASSERT_EQUALS(2, parser.getIgnoredPaths().size());
ASSERT_EQUALS("src", parser.getIgnoredPaths()[0]);
@ -1871,7 +1871,7 @@ private:
void ignorefilepaths1() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-ifoo.cpp", "file.cpp"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT(parser.parseFromArgs(3, argv));
ASSERT_EQUALS(1, parser.getIgnoredPaths().size());
ASSERT_EQUALS("foo.cpp", parser.getIgnoredPaths()[0]);
@ -1881,7 +1881,7 @@ private:
void ignorefilepaths2() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-isrc/foo.cpp", "file.cpp"};
CmdLineParser parser(settings);
CmdLineParser parser(settings, settings.nomsg, settings.nofail);
ASSERT(parser.parseFromArgs(3, argv));
ASSERT_EQUALS(1, parser.getIgnoredPaths().size());
ASSERT_EQUALS("src/foo.cpp", parser.getIgnoredPaths()[0]);