Skip to content

Commit

Permalink
fixed #12059 - added --fsigned-char and --funsigned-char command-…
Browse files Browse the repository at this point in the history
…line options
  • Loading branch information
firewave committed Oct 21, 2023
1 parent 27bd972 commit 2bce22c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
}
}

else if (std::strncmp(argv[i], "--fsigned-char", 14) == 0)
mSettings.platform.defaultSign = 's';

// use a file filter
else if (std::strncmp(argv[i], "--file-filter=", 14) == 0)
mSettings.fileFilters.emplace_back(argv[i] + 14);
Expand All @@ -415,6 +418,9 @@ 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::strncmp(argv[i], "--funsigned-char", 16) == 0)
mSettings.platform.defaultSign = 'u';

// Print help
else if (std::strcmp(argv[i], "-h") == 0 || std::strcmp(argv[i], "--help") == 0) {
mPathNames.clear();
Expand Down Expand Up @@ -1177,6 +1183,7 @@ void CmdLineParser::printHelp() const
" --exitcode-suppressions=<file>\n"
" Used when certain messages should be displayed but\n"
" should not cause a non-zero exitcode.\n"
" --fsigned-char Treat char type as signed.\n"
" --file-filter=<str> Analyze only those files matching the given filter str\n"
" Can be used multiple times\n"
" Example: --file-filter=*bar.cpp analyzes only files\n"
Expand All @@ -1187,6 +1194,7 @@ 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"
" --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"
Expand Down
4 changes: 4 additions & 0 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ void ImportProject::FileSettings::parseCommand(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
}
}
setDefines(defs);
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions man/cppcheck.1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,18 @@ man(1), man(7), http://www.tldp.org/HOWTO/Man-Page/
<arg choice="opt">
<option>--exitcode-suppressions=&lt;file&gt;</option>
</arg>
<arg choice="opt">
<option>--fsigned-char</option>
</arg>
<arg choice="opt">
<option>--file-list=&lt;file&gt;</option>
</arg>
<arg choice="opt">
<option>--force</option>
</arg>
<arg choice="opt">
<option>--funsigned-char</option>
</arg>
<arg choice="opt">
<option>--help</option>
</arg>
Expand Down Expand Up @@ -330,6 +336,14 @@ Example: '-UDEBUG'</para>
<para>Used when certain messages should be displayed but should not cause a non-zero exitcode.</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>--file-list=&lt;file&gt;</option>
Expand All @@ -350,6 +364,14 @@ 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>--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>
Expand Down
1 change: 1 addition & 0 deletions releasenotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ Other:
- You can suppress warnings in a block of code using "-begin" and "-end".
- You can suppress warnings in current file using "-file".
- You can suppress all warnings where macro is used using "-macro"
- 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.
50 changes: 50 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ class TestCmdlineParser : public TestFixture {
#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);
Expand Down Expand Up @@ -2069,6 +2074,51 @@ class TestCmdlineParser : public TestFixture {
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;
Expand Down

0 comments on commit 2bce22c

Please sign in to comment.