From 2a15428096b2b13d2dcc6ca18485ae4833488ca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Wed, 8 Nov 2023 09:28:33 +0100 Subject: [PATCH] fixed #12059 - added `--fsigned-char` and `--funsigned-char` command-line options (#5580) --- cli/cmdlineparser.cpp | 8 ++++++ lib/importproject.cpp | 4 +++ man/cppcheck.1.xml | 22 +++++++++++++++++ releasenotes.txt | 1 + test/testcmdlineparser.cpp | 50 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 14ef48ccb..442fd7daf 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -418,6 +418,12 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) else if (std::strcmp(argv[i], "-f") == 0 || std::strcmp(argv[i], "--force") == 0) mSettings.force = true; + else if (std::strcmp(argv[i], "--fsigned-char") == 0) + mSettings.platform.defaultSign = 's'; + + else if (std::strcmp(argv[i], "--funsigned-char") == 0) + mSettings.platform.defaultSign = 'u'; + // Print help else if (std::strcmp(argv[i], "-h") == 0 || std::strcmp(argv[i], "--help") == 0) { mPathNames.clear(); @@ -1191,6 +1197,8 @@ void CmdLineParser::printHelp() const " -f, --force Force checking of all configurations in files. If used\n" " together with '--max-configs=', the last option is the\n" " one that is effective.\n" + " --fsigned-char Treat char type as signed.\n" + " --funsigned-char Treat char type as unsigned.\n" " -h, --help Print this help.\n" " -I Give path to search for include files. Give several -I\n" " parameters to give several paths. First given path is\n" diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 008ae010e..2b7790e59 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -332,6 +332,8 @@ void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command) defs += "__PIE__"; defs += ";"; } + // TODO: support -fsigned-char and -funsigned-char? + // we can only set it globally but in this context it needs to be treated per file } } fsSetDefines(fs, defs); @@ -733,6 +735,8 @@ bool ImportProject::importVcxproj(const std::string &filename, std::map + + + + + + @@ -350,6 +356,22 @@ Example: '-UDEBUG' default. If used together with --max-configs=, the last option is the one that is effective. + + + + + + Treat char type as signed. This overrides previous --platform options and is overridden by following ones. + + + + + + + + Treat char type as unsigned. This overrides previous --platform options and is overridden by following ones. + + diff --git a/releasenotes.txt b/releasenotes.txt index d31909f82..6144ba869 100644 --- a/releasenotes.txt +++ b/releasenotes.txt @@ -31,3 +31,4 @@ Other: - You can suppress warnings in current file using "-file". - You can suppress all warnings where macro is used using "-macro" - fixed CMake build with UBSAN and GCC +- Added command-line options "--fsigned-char" and "--funsigned-char" to control the signess of the "char" type. This overrides previously specified "--platform" options and is overrides by following ones. diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index a07322cab..3d9690464 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -316,6 +316,11 @@ private: #else TEST_CASE(ruleFileNotSupported); #endif + TEST_CASE(signedChar); + TEST_CASE(signedChar2); + TEST_CASE(unsignedChar); + TEST_CASE(unsignedChar2); + TEST_CASE(signedCharUnsignedChar); TEST_CASE(ignorepaths1); TEST_CASE(ignorepaths2); @@ -2067,6 +2072,51 @@ private: ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); } + void signedChar() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--fsigned-char", "file.cpp"}; + settings->platform.defaultSign = '\0'; + ASSERT(parser->parseFromArgs(3, argv)); + ASSERT_EQUALS('s', settings->platform.defaultSign); + ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); + } + + void signedChar2() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--platform=avr8", "--fsigned-char", "file.cpp"}; + settings->platform.defaultSign = '\0'; + ASSERT(parser->parseFromArgs(4, argv)); + ASSERT_EQUALS('s', settings->platform.defaultSign); + ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); + } + + void unsignedChar() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--funsigned-char", "file.cpp"}; + settings->platform.defaultSign = '\0'; + ASSERT(parser->parseFromArgs(3, argv)); + ASSERT_EQUALS('u', settings->platform.defaultSign); + ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); + } + + void unsignedChar2() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--platform=mips32", "--funsigned-char", "file.cpp"}; + settings->platform.defaultSign = '\0'; + ASSERT(parser->parseFromArgs(4, argv)); + ASSERT_EQUALS('u', settings->platform.defaultSign); + ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); + } + + void signedCharUnsignedChar() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--fsigned-char", "--funsigned-char", "file.cpp"}; + settings->platform.defaultSign = '\0'; + ASSERT(parser->parseFromArgs(4, argv)); + ASSERT_EQUALS('u', settings->platform.defaultSign); + ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); + } + #ifdef HAVE_RULES void rule() { REDIRECT;