fixed #12059 - added `--fsigned-char` and `--funsigned-char` command-line options (#5580)

This commit is contained in:
Oliver Stöneberg 2023-11-08 09:28:33 +01:00 committed by GitHub
parent cf64ccea22
commit 2a15428096
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 0 deletions

View File

@ -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 <dir> Give path to search for include files. Give several -I\n"
" parameters to give several paths. First given path is\n"

View File

@ -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<std::str
}
}
}
// # TODO: support signedness of char via /J (and potential XML option for it)?
// we can only set it globally but in this context it needs to be treated per file
for (const std::string &c : compileList) {
const std::string cfilename = Path::simplifyPath(Path::isAbsolute(c) ? c : Path::getPathFromFilename(filename) + c);

View File

@ -126,6 +126,12 @@ man(1), man(7), http://www.tldp.org/HOWTO/Man-Page/
<arg choice="opt">
<option>--force</option>
</arg>
<arg choice="opt">
<option>--fsigned-char</option>
</arg>
<arg choice="opt">
<option>--funsigned-char</option>
</arg>
<arg choice="opt">
<option>--help</option>
</arg>
@ -350,6 +356,22 @@ Example: '-UDEBUG'</para>
default. If used together with --max-configs=, the last option is the one that is effective.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>--fsigned-char</option>
</term>
<listitem>
<para>Treat char type as signed. This overrides previous --platform options and is overridden by following ones.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>--funsigned-char</option>
</term>
<listitem>
<para>Treat char type as unsigned. This overrides previous --platform options and is overridden by following ones.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>-h</option>

View File

@ -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.

View File

@ -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;