Skip to content

Commit da09adc

Browse files
committed
Fix danmar#3510 (Improve error message for --suppressions-list)
http://sourceforge.net/apps/trac/cppcheck/ticket/3510 Print additional info to error message if we suspect that multiple files were given.
1 parent 34105cb commit da09adc

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

cli/cmdlineparser.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
#include <algorithm>
1920
#include <iostream>
2021
#include <sstream>
2122
#include <fstream>
@@ -178,6 +179,15 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
178179
std::string message("cppcheck: Couldn't open the file: \"");
179180
message += std::string(filename);
180181
message += "\".";
182+
if (count(filename.begin(), filename.end(), ',') > 0 ||
183+
count(filename.begin(), filename.end(), '.') > 1) {
184+
// If user tried to pass multiple files (we can only guess that)
185+
// e.g. like this: --suppressions-list=a.txt,b.txt
186+
// print more detailed error message to tell user how he can solve the problem
187+
message += "\nIf you want to pass two files, you can do it e.g. like this:";
188+
message += "\n cppcheck --suppressions-list=a.txt --suppressions-list=b.txt file.cpp";
189+
}
190+
181191
PrintMessage(message);
182192
return false;
183193
}

gui/selectfilesdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class SelectFilesModel : public QFileSystemModel {
6060
if (selindex >= 0 && unselindex == -1)
6161
return Qt::Checked;
6262
if (selindex >= 0 && unselindex >= 0 &&
63-
selected[selindex].size() > unselected[unselindex].size())
63+
selected[selindex].size() > unselected[unselindex].size())
6464
return Qt::Checked;
6565

6666
return Qt::Unchecked;

test/redirect.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ class RedirectOutputError {
5050
output << _out.str();
5151
}
5252

53+
/** Return what would be printed to cout. See also clearOutput() */
54+
std::string getOutput() {
55+
return _out.str();
56+
}
57+
58+
/** Normally called after getOutput() to prevent same text to be returned
59+
twice. */
60+
void clearOutput() {
61+
_out.str("");
62+
}
63+
5364
private:
5465
std::stringstream _out;
5566
std::stringstream _err;
@@ -58,5 +69,7 @@ class RedirectOutputError {
5869
};
5970

6071
#define REDIRECT RedirectOutputError redir;
72+
#define GET_REDIRECT_OUTPUT redir.getOutput()
73+
#define CLEAR_REDIRECT_OUTPUT redir.clearOutput()
6174

6275
#endif

test/testcmdlineparser.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,33 @@ class TestCmdlineParser : public TestFixture {
665665
}
666666

667667
void suppressionsNoFile() {
668-
// TODO: Fails because there is no suppr.txt file!
669668
REDIRECT;
670-
const char *argv[] = {"cppcheck", "--suppressions-list=", "file.cpp"};
671-
Settings settings;
672-
CmdLineParser parser(&settings);
673-
TODO_ASSERT_EQUALS(true, false, parser.ParseFromArgs(3, argv));
669+
{
670+
CLEAR_REDIRECT_OUTPUT;
671+
const char *argv[] = {"cppcheck", "--suppressions-list=", "file.cpp"};
672+
Settings settings;
673+
CmdLineParser parser(&settings);
674+
ASSERT_EQUALS(false, parser.ParseFromArgs(3, argv));
675+
ASSERT_EQUALS(false, GET_REDIRECT_OUTPUT.find("If you want to pass two files") != std::string::npos);
676+
}
677+
678+
{
679+
CLEAR_REDIRECT_OUTPUT;
680+
const char *argv[] = {"cppcheck", "--suppressions-list=a.suppr,b.suppr", "file.cpp"};
681+
Settings settings;
682+
CmdLineParser parser(&settings);
683+
ASSERT_EQUALS(false, parser.ParseFromArgs(3, argv));
684+
ASSERT_EQUALS(true, GET_REDIRECT_OUTPUT.find("If you want to pass two files") != std::string::npos);
685+
}
686+
687+
{
688+
CLEAR_REDIRECT_OUTPUT;
689+
const char *argv[] = {"cppcheck", "--suppressions-list=a.suppr b.suppr", "file.cpp"};
690+
Settings settings;
691+
CmdLineParser parser(&settings);
692+
ASSERT_EQUALS(false, parser.ParseFromArgs(3, argv));
693+
ASSERT_EQUALS(true, GET_REDIRECT_OUTPUT.find("If you want to pass two files") != std::string::npos);
694+
}
674695
}
675696

676697
void suppressionSingle() {

0 commit comments

Comments
 (0)