Skip to content

Commit 2a15428

Browse files
authored
fixed #12059 - added --fsigned-char and --funsigned-char command-line options (#5580)
1 parent cf64cce commit 2a15428

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

cli/cmdlineparser.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,12 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
418418
else if (std::strcmp(argv[i], "-f") == 0 || std::strcmp(argv[i], "--force") == 0)
419419
mSettings.force = true;
420420

421+
else if (std::strcmp(argv[i], "--fsigned-char") == 0)
422+
mSettings.platform.defaultSign = 's';
423+
424+
else if (std::strcmp(argv[i], "--funsigned-char") == 0)
425+
mSettings.platform.defaultSign = 'u';
426+
421427
// Print help
422428
else if (std::strcmp(argv[i], "-h") == 0 || std::strcmp(argv[i], "--help") == 0) {
423429
mPathNames.clear();
@@ -1191,6 +1197,8 @@ void CmdLineParser::printHelp() const
11911197
" -f, --force Force checking of all configurations in files. If used\n"
11921198
" together with '--max-configs=', the last option is the\n"
11931199
" one that is effective.\n"
1200+
" --fsigned-char Treat char type as signed.\n"
1201+
" --funsigned-char Treat char type as unsigned.\n"
11941202
" -h, --help Print this help.\n"
11951203
" -I <dir> Give path to search for include files. Give several -I\n"
11961204
" parameters to give several paths. First given path is\n"

lib/importproject.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command)
332332
defs += "__PIE__";
333333
defs += ";";
334334
}
335+
// TODO: support -fsigned-char and -funsigned-char?
336+
// we can only set it globally but in this context it needs to be treated per file
335337
}
336338
}
337339
fsSetDefines(fs, defs);
@@ -733,6 +735,8 @@ bool ImportProject::importVcxproj(const std::string &filename, std::map<std::str
733735
}
734736
}
735737
}
738+
// # TODO: support signedness of char via /J (and potential XML option for it)?
739+
// we can only set it globally but in this context it needs to be treated per file
736740

737741
for (const std::string &c : compileList) {
738742
const std::string cfilename = Path::simplifyPath(Path::isAbsolute(c) ? c : Path::getPathFromFilename(filename) + c);

man/cppcheck.1.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ man(1), man(7), http://www.tldp.org/HOWTO/Man-Page/
126126
<arg choice="opt">
127127
<option>--force</option>
128128
</arg>
129+
<arg choice="opt">
130+
<option>--fsigned-char</option>
131+
</arg>
132+
<arg choice="opt">
133+
<option>--funsigned-char</option>
134+
</arg>
129135
<arg choice="opt">
130136
<option>--help</option>
131137
</arg>
@@ -350,6 +356,22 @@ Example: '-UDEBUG'</para>
350356
default. If used together with --max-configs=, the last option is the one that is effective.</para>
351357
</listitem>
352358
</varlistentry>
359+
<varlistentry>
360+
<term>
361+
<option>--fsigned-char</option>
362+
</term>
363+
<listitem>
364+
<para>Treat char type as signed. This overrides previous --platform options and is overridden by following ones.</para>
365+
</listitem>
366+
</varlistentry>
367+
<varlistentry>
368+
<term>
369+
<option>--funsigned-char</option>
370+
</term>
371+
<listitem>
372+
<para>Treat char type as unsigned. This overrides previous --platform options and is overridden by following ones.</para>
373+
</listitem>
374+
</varlistentry>
353375
<varlistentry>
354376
<term>
355377
<option>-h</option>

releasenotes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ Other:
3131
- You can suppress warnings in current file using "-file".
3232
- You can suppress all warnings where macro is used using "-macro"
3333
- fixed CMake build with UBSAN and GCC
34+
- 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.

test/testcmdlineparser.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ class TestCmdlineParser : public TestFixture {
316316
#else
317317
TEST_CASE(ruleFileNotSupported);
318318
#endif
319+
TEST_CASE(signedChar);
320+
TEST_CASE(signedChar2);
321+
TEST_CASE(unsignedChar);
322+
TEST_CASE(unsignedChar2);
323+
TEST_CASE(signedCharUnsignedChar);
319324

320325
TEST_CASE(ignorepaths1);
321326
TEST_CASE(ignorepaths2);
@@ -2067,6 +2072,51 @@ class TestCmdlineParser : public TestFixture {
20672072
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
20682073
}
20692074

2075+
void signedChar() {
2076+
REDIRECT;
2077+
const char * const argv[] = {"cppcheck", "--fsigned-char", "file.cpp"};
2078+
settings->platform.defaultSign = '\0';
2079+
ASSERT(parser->parseFromArgs(3, argv));
2080+
ASSERT_EQUALS('s', settings->platform.defaultSign);
2081+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
2082+
}
2083+
2084+
void signedChar2() {
2085+
REDIRECT;
2086+
const char * const argv[] = {"cppcheck", "--platform=avr8", "--fsigned-char", "file.cpp"};
2087+
settings->platform.defaultSign = '\0';
2088+
ASSERT(parser->parseFromArgs(4, argv));
2089+
ASSERT_EQUALS('s', settings->platform.defaultSign);
2090+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
2091+
}
2092+
2093+
void unsignedChar() {
2094+
REDIRECT;
2095+
const char * const argv[] = {"cppcheck", "--funsigned-char", "file.cpp"};
2096+
settings->platform.defaultSign = '\0';
2097+
ASSERT(parser->parseFromArgs(3, argv));
2098+
ASSERT_EQUALS('u', settings->platform.defaultSign);
2099+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
2100+
}
2101+
2102+
void unsignedChar2() {
2103+
REDIRECT;
2104+
const char * const argv[] = {"cppcheck", "--platform=mips32", "--funsigned-char", "file.cpp"};
2105+
settings->platform.defaultSign = '\0';
2106+
ASSERT(parser->parseFromArgs(4, argv));
2107+
ASSERT_EQUALS('u', settings->platform.defaultSign);
2108+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
2109+
}
2110+
2111+
void signedCharUnsignedChar() {
2112+
REDIRECT;
2113+
const char * const argv[] = {"cppcheck", "--fsigned-char", "--funsigned-char", "file.cpp"};
2114+
settings->platform.defaultSign = '\0';
2115+
ASSERT(parser->parseFromArgs(4, argv));
2116+
ASSERT_EQUALS('u', settings->platform.defaultSign);
2117+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
2118+
}
2119+
20702120
#ifdef HAVE_RULES
20712121
void rule() {
20722122
REDIRECT;

0 commit comments

Comments
 (0)