Skip to content

Commit 61a2b89

Browse files
authored
streamlined message/error printing of CmdLineParser (danmar#3524)
* cmdlineparser.cpp: removed message about deprecated --std=posix * streamlined message/error printing of CmdLineParser * test-helloworld.py: adjusted expected test result
1 parent b4704ba commit 61a2b89

4 files changed

Lines changed: 57 additions & 55 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ CmdLineParser::CmdLineParser(Settings *settings)
106106

107107
void CmdLineParser::printMessage(const std::string &message)
108108
{
109-
std::cout << message << std::endl;
109+
std::cout << "cppcheck: " << message << std::endl;
110110
}
111111

112-
void CmdLineParser::printMessage(const char* message)
112+
void CmdLineParser::printError(const std::string &message)
113113
{
114-
std::cout << message << std::endl;
114+
printMessage("error: " + message);
115115
}
116116

117117
bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
@@ -133,7 +133,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
133133
if (std::strcmp(argv[i], "-D") == 0) {
134134
++i;
135135
if (i >= argc || argv[i][0] == '-') {
136-
printMessage("cppcheck: argument to '-D' is missing.");
136+
printError("argument to '-D' is missing.");
137137
return false;
138138
}
139139

@@ -169,7 +169,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
169169
if (std::strcmp(argv[i], "-I") == 0) {
170170
++i;
171171
if (i >= argc || argv[i][0] == '-') {
172-
printMessage("cppcheck: argument to '-I' is missing.");
172+
printError("argument to '-I' is missing.");
173173
return false;
174174
}
175175
path = argv[i];
@@ -197,7 +197,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
197197
if (std::strcmp(argv[i], "-U") == 0) {
198198
++i;
199199
if (i >= argc || argv[i][0] == '-') {
200-
printMessage("cppcheck: argument to '-U' is missing.");
200+
printError("argument to '-U' is missing.");
201201
return false;
202202
}
203203

@@ -250,7 +250,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
250250
// open this file and read every input file (1 file name per line)
251251
const std::string cfgExcludesFile(23 + argv[i]);
252252
if (!addPathsToSet(cfgExcludesFile, &mSettings->configExcludePaths)) {
253-
printMessage("Cppcheck: unable to open config excludes file at '" + cfgExcludesFile + "'");
253+
printError("unable to open config excludes file at '" + cfgExcludesFile + "'");
254254
return false;
255255
}
256256
}
@@ -306,7 +306,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
306306
else if (std::strncmp(argv[i], "--enable=", 9) == 0) {
307307
const std::string errmsg = mSettings->addEnabled(argv[i] + 9);
308308
if (!errmsg.empty()) {
309-
printMessage(errmsg);
309+
printError(errmsg);
310310
return false;
311311
}
312312
// when "style" is enabled, also enable "warning", "performance" and "portability"
@@ -330,7 +330,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
330330
std::istringstream iss(temp);
331331
if (!(iss >> mSettings->exitCode)) {
332332
mSettings->exitCode = 0;
333-
printMessage("cppcheck: Argument must be an integer. Try something like '--error-exitcode=1'.");
333+
printError("argument must be an integer. Try something like '--error-exitcode=1'.");
334334
return false;
335335
}
336336
}
@@ -352,12 +352,12 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
352352

353353
std::ifstream f(filename);
354354
if (!f.is_open()) {
355-
printMessage("cppcheck: Couldn't open the file: \"" + filename + "\".");
355+
printError("couldn't open the file: \"" + filename + "\".");
356356
return false;
357357
}
358358
const std::string errmsg(mSettings->nofail.parseFile(f));
359359
if (!errmsg.empty()) {
360-
printMessage(errmsg);
360+
printError(errmsg);
361361
return false;
362362
}
363363
}
@@ -391,7 +391,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
391391
if (std::strcmp(argv[i], "-i") == 0) {
392392
++i;
393393
if (i >= argc || argv[i][0] == '-') {
394-
printMessage("cppcheck: argument to '-i' is missing.");
394+
printError("argument to '-i' is missing.");
395395
return false;
396396
}
397397
path = argv[i];
@@ -424,7 +424,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
424424
// open this file and read every input file (1 file name per line)
425425
const std::string includesFile(16 + argv[i]);
426426
if (!addIncludePathsToList(includesFile, &mSettings->includePaths)) {
427-
printMessage("Cppcheck: unable to open includes file at '" + includesFile + "'");
427+
printError("unable to open includes file at '" + includesFile + "'");
428428
return false;
429429
}
430430
}
@@ -445,7 +445,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
445445
if (std::strcmp(argv[i], "-j") == 0) {
446446
++i;
447447
if (i >= argc || argv[i][0] == '-') {
448-
printMessage("cppcheck: argument to '-j' is missing.");
448+
printError("argument to '-j' is missing.");
449449
return false;
450450
}
451451

@@ -458,14 +458,14 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
458458

459459
std::istringstream iss(numberString);
460460
if (!(iss >> mSettings->jobs)) {
461-
printMessage("cppcheck: argument to '-j' is not a number.");
461+
printError("argument to '-j' is not a number.");
462462
return false;
463463
}
464464

465465
if (mSettings->jobs > 10000) {
466466
// This limit is here just to catch typos. If someone has
467467
// need for more jobs, this value should be increased.
468-
printMessage("cppcheck: argument for '-j' is allowed to be 10000 at max.");
468+
printError("argument for '-j' is allowed to be 10000 at max.");
469469
return false;
470470
}
471471
}
@@ -477,7 +477,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
477477
if (std::strcmp(argv[i], "-l") == 0) {
478478
++i;
479479
if (i >= argc || argv[i][0] == '-') {
480-
printMessage("cppcheck: argument to '-l' is missing.");
480+
printError("argument to '-l' is missing.");
481481
return false;
482482
}
483483

@@ -490,7 +490,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
490490

491491
std::istringstream iss(numberString);
492492
if (!(iss >> mSettings->loadAverage)) {
493-
printMessage("cppcheck: argument to '-l' is not a number.");
493+
printError("argument to '-l' is not a number.");
494494
return false;
495495
}
496496
}
@@ -503,7 +503,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
503503
} else {
504504
i++;
505505
if (i >= argc || argv[i][0] == '-') {
506-
printMessage("cppcheck: No language given to '-x' option.");
506+
printError("no language given to '-x' option.");
507507
return false;
508508
}
509509
str = argv[i];
@@ -514,7 +514,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
514514
else if (str == "c++")
515515
mSettings->enforcedLang = Settings::CPP;
516516
else {
517-
printMessage("cppcheck: Unknown language '" + str + "' enforced.");
517+
printError("unknown language '" + str + "' enforced.");
518518
return false;
519519
}
520520
}
@@ -530,12 +530,12 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
530530

531531
std::istringstream iss(14+argv[i]);
532532
if (!(iss >> mSettings->maxConfigs)) {
533-
printMessage("cppcheck: argument to '--max-configs=' is not a number.");
533+
printError("argument to '--max-configs=' is not a number.");
534534
return false;
535535
}
536536

537537
if (mSettings->maxConfigs < 1) {
538-
printMessage("cppcheck: argument to '--max-configs=' must be greater than 0.");
538+
printError("argument to '--max-configs=' must be greater than 0.");
539539
return false;
540540
}
541541

@@ -569,10 +569,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
569569
else if (platform == "unspecified")
570570
mSettings->platform(Settings::Unspecified);
571571
else if (!mSettings->loadPlatformFile(argv[0], platform)) {
572-
std::string message("cppcheck: error: unrecognized platform: \"");
572+
std::string message("unrecognized platform: \"");
573573
message += platform;
574574
message += "\".";
575-
printMessage(message);
575+
printError(message);
576576
return false;
577577
}
578578
}
@@ -587,10 +587,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
587587

588588
const std::string plistOutput = Path::toNativeSeparators(mSettings->plistOutput);
589589
if (!FileLister::isDirectory(plistOutput)) {
590-
std::string message("cppcheck: error: plist folder does not exist: \"");
590+
std::string message("plist folder does not exist: \"");
591591
message += plistOutput;
592592
message += "\".";
593-
printMessage(message);
593+
printError(message);
594594
return false;
595595
}
596596
}
@@ -626,10 +626,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
626626
else if (platform == "unspecified" || platform == "Unspecified" || platform == "")
627627
;
628628
else if (!mSettings->loadPlatformFile(projectFile.c_str(), platform) && !mSettings->loadPlatformFile(argv[0], platform)) {
629-
std::string message("cppcheck: error: unrecognized platform: \"");
629+
std::string message("unrecognized platform: \"");
630630
message += platform;
631631
message += "\".";
632-
printMessage(message);
632+
printError(message);
633633
return false;
634634
}
635635

@@ -641,16 +641,16 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
641641
mSettings->project.selectOneVsConfig(mSettings->platformType);
642642
if (!CppCheckExecutor::tryLoadLibrary(mSettings->library, argv[0], "windows.cfg")) {
643643
// This shouldn't happen normally.
644-
printMessage("cppcheck: Failed to load 'windows.cfg'. Your Cppcheck installation is broken. Please re-install.");
644+
printError("failed to load 'windows.cfg'. Your Cppcheck installation is broken. Please re-install.");
645645
return false;
646646
}
647647
}
648648
if (projType == ImportProject::Type::MISSING) {
649-
printMessage("cppcheck: Failed to open project '" + projectFile + "'.");
649+
printError("failed to open project '" + projectFile + "'.");
650650
return false;
651651
}
652652
if (projType == ImportProject::Type::UNKNOWN) {
653-
printMessage("cppcheck: Failed to load project '" + projectFile + "'. The format is unknown.");
653+
printError("failed to load project '" + projectFile + "'. The format is unknown.");
654654
return false;
655655
}
656656
}
@@ -683,7 +683,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
683683
paths.erase(0, pos + 1);
684684
}
685685
} else {
686-
printMessage("cppcheck: No paths specified for the '" + std::string(argv[i]) + "' option.");
686+
printError("no paths specified for the '" + std::string(argv[i]) + "' option.");
687687
return false;
688688
}
689689
}
@@ -737,7 +737,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
737737
mSettings->rules.emplace_back(rule);
738738
}
739739
} else {
740-
printMessage("cppcheck: error: unable to load rule-file: " + std::string(12+argv[i]));
740+
printError("unable to load rule-file: " + std::string(12+argv[i]));
741741
return false;
742742
}
743743
}
@@ -755,15 +755,13 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
755755
else if (showtimeMode.empty())
756756
mSettings->showtime = SHOWTIME_MODES::SHOWTIME_NONE;
757757
else {
758-
printMessage("cppcheck: error: unrecognized showtime mode: \"" + showtimeMode + "\". Supported modes: file, summary, top5.");
758+
printError("unrecognized showtime mode: \"" + showtimeMode + "\". Supported modes: file, summary, top5.");
759759
return false;
760760
}
761761
}
762762

763763
// --std
764-
else if (std::strcmp(argv[i], "--std=posix") == 0) {
765-
printMessage("cppcheck: Option --std=posix is deprecated and will be removed in 2.05.");
766-
} else if (std::strcmp(argv[i], "--std=c89") == 0) {
764+
else if (std::strcmp(argv[i], "--std=c89") == 0) {
767765
mSettings->standards.c = Standards::C89;
768766
} else if (std::strcmp(argv[i], "--std=c99") == 0) {
769767
mSettings->standards.c = Standards::C99;
@@ -785,7 +783,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
785783
const std::string suppression = argv[i]+11;
786784
const std::string errmsg(mSettings->nomsg.addSuppressionLine(suppression));
787785
if (!errmsg.empty()) {
788-
printMessage(errmsg);
786+
printError(errmsg);
789787
return false;
790788
}
791789
}
@@ -795,7 +793,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
795793
std::string filename = argv[i]+20;
796794
std::ifstream f(filename);
797795
if (!f.is_open()) {
798-
std::string message("cppcheck: Couldn't open the file: \"");
796+
std::string message("couldn't open the file: \"");
799797
message += filename;
800798
message += "\".";
801799
if (std::count(filename.begin(), filename.end(), ',') > 0 ||
@@ -807,12 +805,12 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
807805
message += "\n cppcheck --suppressions-list=a.txt --suppressions-list=b.txt file.cpp";
808806
}
809807

810-
printMessage(message);
808+
printError(message);
811809
return false;
812810
}
813811
const std::string errmsg(mSettings->nomsg.parseFile(f));
814812
if (!errmsg.empty()) {
815-
printMessage(errmsg);
813+
printError(errmsg);
816814
return false;
817815
}
818816
}
@@ -821,7 +819,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
821819
const char * filename = argv[i] + 15;
822820
const std::string errmsg(mSettings->nomsg.parseXmlFile(filename));
823821
if (!errmsg.empty()) {
824-
printMessage(errmsg);
822+
printError(errmsg);
825823
return false;
826824
}
827825
}
@@ -836,7 +834,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
836834
++i;
837835
mSettings->templateFormat = argv[i];
838836
} else {
839-
printMessage("cppcheck: argument to '--template' is missing.");
837+
printError("argument to '--template' is missing.");
840838
return false;
841839
}
842840

@@ -869,7 +867,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
869867
++i;
870868
mSettings->templateLocation = argv[i];
871869
} else {
872-
printMessage("cppcheck: argument to '--template' is missing.");
870+
printError("argument to '--template' is missing.");
873871
return false;
874872
}
875873
}
@@ -893,13 +891,13 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
893891

894892
std::istringstream iss(numberString);
895893
if (!(iss >> mSettings->xml_version)) {
896-
printMessage("cppcheck: argument to '--xml-version' is not a number.");
894+
printError("argument to '--xml-version' is not a number.");
897895
return false;
898896
}
899897

900898
if (mSettings->xml_version != 2) {
901899
// We only have xml version 2
902-
printMessage("cppcheck: '--xml-version' can only be 2.");
900+
printError("'--xml-version' can only be 2.");
903901
return false;
904902
}
905903

@@ -908,10 +906,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
908906
}
909907

910908
else {
911-
std::string message("cppcheck: error: unrecognized command line option: \"");
909+
std::string message("unrecognized command line option: \"");
912910
message += argv[i];
913911
message += "\".";
914-
printMessage(message);
912+
printError(message);
915913
return false;
916914
}
917915
}
@@ -940,7 +938,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
940938
mSettings->maxConfigs = 1U;
941939

942940
if (mSettings->checks.isEnabled(Checks::unusedFunction) && mSettings->jobs > 1) {
943-
printMessage("cppcheck: unusedFunction check can't be used with '-j' option. Disabling unusedFunction check.");
941+
printMessage("unusedFunction check can't be used with '-j' option. Disabling unusedFunction check.");
944942
}
945943

946944
if (argc <= 1) {
@@ -955,7 +953,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
955953

956954
// Print error only if we have "real" command and expect files
957955
if (!mExitAfterPrint && mPathNames.empty() && mSettings->project.fileSettings.empty()) {
958-
printMessage("cppcheck: No C or C++ source files found.");
956+
printError("no C or C++ source files found.");
959957
return false;
960958
}
961959

cli/cmdlineparser.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ class CmdLineParser {
101101
static void printHelp();
102102

103103
/**
104-
* Print message (to console?).
104+
* Print message (to stdout).
105105
*/
106106
static void printMessage(const std::string &message);
107-
static void printMessage(const char* message);
107+
108+
/**
109+
* Print error message (to stdout).
110+
*/
111+
static void printError(const std::string &message);
108112

109113
private:
110114
std::vector<std::string> mPathNames;

0 commit comments

Comments
 (0)