Little improvements with cli messages and added new '--template' mode: 'edit'.

This option makes it possible opening correctly some editors like 'gedit' or 'vim' by copy-pasting the filename and the line to the command sequence.
This commit is contained in:
Edoardo Prezioso 2011-10-29 23:26:35 +02:00
parent 42fd4e2f52
commit 0bb1ad8782
2 changed files with 54 additions and 42 deletions

View File

@ -117,8 +117,8 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// Enable all checks - will be removed in future // Enable all checks - will be removed in future
else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--all") == 0) { else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--all") == 0) {
PrintMessage("cppcheck: -a/--all option is deprecated and will be removed in 1.55 release."); PrintMessage("cppcheck: '-a/--all' option is deprecated and will be removed in 1.55 release.");
PrintMessage("cppcheck: please use --enable=all instead."); PrintMessage("cppcheck: please use '--enable=all' instead.");
} }
// Inconclusive checking (still in testing phase) // Inconclusive checking (still in testing phase)
@ -127,8 +127,8 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// Checking coding style - will be removed in the future // Checking coding style - will be removed in the future
else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--style") == 0) { else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--style") == 0) {
PrintMessage("cppcheck: -s/--style option is deprecated and will be removed in 1.55 release."); PrintMessage("cppcheck: '-s/--style' option is deprecated and will be removed in 1.55 release.");
PrintMessage("cppcheck: please use --enable=style instead."); PrintMessage("cppcheck: please use '--enable=style' instead.");
const std::string errmsg = _settings->addEnabled("style"); const std::string errmsg = _settings->addEnabled("style");
if (!errmsg.empty()) { if (!errmsg.empty()) {
@ -148,7 +148,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
if (i >= argc || strncmp(argv[i], "-", 1) == 0 || if (i >= argc || strncmp(argv[i], "-", 1) == 0 ||
strncmp(argv[i], "--", 2) == 0) { strncmp(argv[i], "--", 2) == 0) {
PrintMessage("cppcheck: No filename specified for the --exitcode-suppressions option"); PrintMessage("cppcheck: No filename specified for the '--exitcode-suppressions' option.");
return false; return false;
} }
filename = argv[i]; filename = argv[i];
@ -160,7 +160,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
std::ifstream f(filename.c_str()); std::ifstream f(filename.c_str());
if (!f.is_open()) { if (!f.is_open()) {
PrintMessage("cppcheck: Couldn't open the file \"" + std::string(filename) + "\""); PrintMessage("cppcheck: Couldn't open the file: \"" + std::string(filename) + "\".");
return false; return false;
} }
const std::string errmsg(_settings->nofail.parseFile(f)); const std::string errmsg(_settings->nofail.parseFile(f));
@ -176,9 +176,9 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
filename = filename.substr(20); filename = filename.substr(20);
std::ifstream f(filename.c_str()); std::ifstream f(filename.c_str());
if (!f.is_open()) { if (!f.is_open()) {
std::string message("cppcheck: Couldn't open the file \""); std::string message("cppcheck: Couldn't open the file: \"");
message += std::string(filename); message += std::string(filename);
message += "\""; message += "\".";
PrintMessage(message); PrintMessage(message);
return false; return false;
} }
@ -196,15 +196,15 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
++i; ++i;
if (i >= argc) { if (i >= argc) {
PrintMessage("cppcheck: No file specified for the --suppressions option"); PrintMessage("cppcheck: No file specified for the '--suppressions' option.");
return false; return false;
} }
std::ifstream f(argv[i]); std::ifstream f(argv[i]);
if (!f.is_open()) { if (!f.is_open()) {
std::string message("cppcheck: Couldn't open the file \""); std::string message("cppcheck: Couldn't open the file: \"");
message += std::string(argv[i]); message += std::string(argv[i]);
message += "\""; message += "\".";
PrintMessage(message); PrintMessage(message);
return false; return false;
} }
@ -248,13 +248,13 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
std::istringstream iss(numberString); std::istringstream iss(numberString);
if (!(iss >> _settings->_xml_version)) { if (!(iss >> _settings->_xml_version)) {
PrintMessage("cppcheck: argument to '--xml-version' is not a number"); PrintMessage("cppcheck: argument to '--xml-version' is not a number.");
return false; return false;
} }
if (_settings->_xml_version < 0 || _settings->_xml_version > 2) { if (_settings->_xml_version < 0 || _settings->_xml_version > 2) {
// We only have xml versions 1 and 2 // We only have xml versions 1 and 2
PrintMessage("cppcheck: --xml-version can only be 1 or 2."); PrintMessage("cppcheck: '--xml-version' can only be 1 or 2.");
return false; return false;
} }
@ -290,7 +290,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
std::istringstream iss(temp); std::istringstream iss(temp);
if (!(iss >> _settings->_exitCode)) { if (!(iss >> _settings->_exitCode)) {
_settings->_exitCode = 0; _settings->_exitCode = 0;
PrintMessage("cppcheck: Argument must be an integer. Try something like '--error-exitcode=1'"); PrintMessage("cppcheck: Argument must be an integer. Try something like '--error-exitcode=1'.");
return false; return false;
} }
} }
@ -304,7 +304,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
++i; ++i;
if (i >= argc || strncmp(argv[i], "-", 1) == 0 || if (i >= argc || strncmp(argv[i], "-", 1) == 0 ||
strncmp(argv[i], "--", 2) == 0) { strncmp(argv[i], "--", 2) == 0) {
PrintMessage("cppcheck: argument to '-D' is missing"); PrintMessage("cppcheck: argument to '-D' is missing.");
return false; return false;
} }
@ -328,7 +328,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
if (strcmp(argv[i], "-I") == 0) { if (strcmp(argv[i], "-I") == 0) {
++i; ++i;
if (i >= argc) { if (i >= argc) {
PrintMessage("cppcheck: argument to '-I' is missing"); PrintMessage("cppcheck: argument to '-I' is missing.");
return false; return false;
} }
path = argv[i]; path = argv[i];
@ -365,7 +365,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
if (strcmp(argv[i], "-i") == 0) { if (strcmp(argv[i], "-i") == 0) {
++i; ++i;
if (i >= argc) { if (i >= argc) {
PrintMessage("cppcheck: argument to '-i' is missing"); PrintMessage("cppcheck: argument to '-i' is missing.");
return false; return false;
} }
path = argv[i]; path = argv[i];
@ -414,7 +414,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// "--template path/" // "--template path/"
++i; ++i;
if (i >= argc) { if (i >= argc) {
PrintMessage("cppcheck: argument to '--template' is missing"); PrintMessage("cppcheck: argument to '--template' is missing.");
return false; return false;
} }
@ -423,6 +423,8 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
_settings->_outputFormat = "{file}:{line}: {severity}: {message}"; _settings->_outputFormat = "{file}:{line}: {severity}: {message}";
else if (_settings->_outputFormat == "vs") else if (_settings->_outputFormat == "vs")
_settings->_outputFormat = "{file}({line}): {severity}: {message}"; _settings->_outputFormat = "{file}({line}): {severity}: {message}";
else if (_settings->_outputFormat == "edit")
_settings->_outputFormat = "{file} +{line}: {severity}: {message}";
} }
// Checking threads // Checking threads
@ -434,7 +436,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
if (strcmp(argv[i], "-j") == 0) { if (strcmp(argv[i], "-j") == 0) {
++i; ++i;
if (i >= argc) { if (i >= argc) {
PrintMessage("cppcheck: argument to '-j' is missing"); PrintMessage("cppcheck: argument to '-j' is missing.");
return false; return false;
} }
@ -449,14 +451,14 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
std::istringstream iss(numberString); std::istringstream iss(numberString);
if (!(iss >> _settings->_jobs)) { if (!(iss >> _settings->_jobs)) {
PrintMessage("cppcheck: argument to '-j' is not a number"); PrintMessage("cppcheck: argument to '-j' is not a number.");
return false; return false;
} }
if (_settings->_jobs > 10000) { if (_settings->_jobs > 10000) {
// This limit is here just to catch typos. If someone has // This limit is here just to catch typos. If someone has
// need for more jobs, this value should be increased. // need for more jobs, this value should be increased.
PrintMessage("cppcheck: argument for '-j' is allowed to be 10000 at max"); PrintMessage("cppcheck: argument for '-j' is allowed to be 10000 at max.");
return false; return false;
} }
} }
@ -464,7 +466,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// deprecated: auto deallocated classes.. // deprecated: auto deallocated classes..
else if (strcmp(argv[i], "--auto-dealloc") == 0) { else if (strcmp(argv[i], "--auto-dealloc") == 0) {
++i; ++i;
PrintMessage("cppcheck: --auto-dealloc option is deprecated and will be removed in 1.55 release."); PrintMessage("cppcheck: '--auto-dealloc' option is deprecated and will be removed in 1.55 release.");
} }
// print all possible error messages.. // print all possible error messages..
@ -573,9 +575,9 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
else if (platform == "unix64") else if (platform == "unix64")
_settings->platform(Settings::Unix64); _settings->platform(Settings::Unix64);
else { else {
std::string message("cppcheck: error: unrecognized platform\""); std::string message("cppcheck: error: unrecognized platform: \"");
message += argv[i]; message += argv[i];
message += "\""; message += "\".";
PrintMessage(message); PrintMessage(message);
return false; return false;
} }
@ -587,12 +589,12 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
std::istringstream iss(14+argv[i]); std::istringstream iss(14+argv[i]);
if (!(iss >> _settings->_maxConfigs)) { if (!(iss >> _settings->_maxConfigs)) {
PrintMessage("cppcheck: argument to '--max-configs=' is not a number"); PrintMessage("cppcheck: argument to '--max-configs=' is not a number.");
return false; return false;
} }
if (_settings->_maxConfigs < 1) { if (_settings->_maxConfigs < 1) {
PrintMessage("cppcheck: argument to '--max-configs=' must be greater than 0"); PrintMessage("cppcheck: argument to '--max-configs=' must be greater than 0.");
return false; return false;
} }
} }
@ -606,9 +608,9 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
} }
else if (strncmp(argv[i], "-", 1) == 0 || strncmp(argv[i], "--", 2) == 0) { else if (strncmp(argv[i], "-", 1) == 0 || strncmp(argv[i], "--", 2) == 0) {
std::string message("cppcheck: error: unrecognized command line option \""); std::string message("cppcheck: error: unrecognized command line option: \"");
message += argv[i]; message += argv[i];
message += "\""; message += "\".";
PrintMessage(message); PrintMessage(message);
return false; return false;
} }
@ -621,7 +623,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
} }
if (_settings->isEnabled("unusedFunction") && _settings->_jobs > 1) { if (_settings->isEnabled("unusedFunction") && _settings->_jobs > 1) {
PrintMessage("cppcheck: unusedFunction check can't be used with -j option, so it was disabled."); PrintMessage("cppcheck: unusedFunction check can't be used with '-j' option, so it's disabled.");
} }
// FIXME: Make the _settings.test_2_pass thread safe // FIXME: Make the _settings.test_2_pass thread safe
@ -661,9 +663,9 @@ void CmdLineParser::PrintHelp()
" --check-config Check cppcheck configuration. The normal code\n" " --check-config Check cppcheck configuration. The normal code\n"
" analysis is disabled by this flag.\n" " analysis is disabled by this flag.\n"
" -D<ID> By default Cppcheck checks all configurations.\n" " -D<ID> By default Cppcheck checks all configurations.\n"
" Use -D to limit the checking. When -D is used the\n" " Use '-D' to limit the checking. When '-D' is used the\n"
" checking is limited to the given configuration.\n" " checking is limited to the given configuration.\n"
" Example: -DDEBUG=1 -D__cplusplus\n" " Example: '-DDEBUG=1 -D__cplusplus'.\n"
" --enable=<id> Enable additional checks. The available ids are:\n" " --enable=<id> Enable additional checks. The available ids are:\n"
" * all\n" " * all\n"
" Enable all checks\n" " Enable all checks\n"
@ -681,26 +683,26 @@ void CmdLineParser::PrintHelp()
" Check for unused functions\n" " Check for unused functions\n"
" * missingInclude\n" " * missingInclude\n"
" Warn if there are missing includes.\n" " Warn if there are missing includes.\n"
" For detailed information use --check-config\n" " For detailed information, use '--check-config'.\n"
" Several ids can be given if you separate them with\n" " Several ids can be given if you separate them with\n"
" commas.\n" " commas.\n"
" --error-exitcode=<n> If errors are found, integer [n] is returned instead\n" " --error-exitcode=<n> If errors are found, integer [n] is returned instead\n"
" of the default 0. " << EXIT_FAILURE << " is returned\n" " of the default '0'. '" << EXIT_FAILURE << "' is returned\n"
" if arguments are not valid or if no input files are\n" " if arguments are not valid or if no input files are\n"
" provided. Note that your operating system can\n" " provided. Note that your operating system can\n"
" modify this value, e.g. 256 can become 0.\n" " modify this value, e.g. '256' can become '0'.\n"
" --errorlist Print a list of all the error messages in XML format.\n" " --errorlist Print a list of all the error messages in XML format.\n"
" --exitcode-suppressions=<file>\n" " --exitcode-suppressions=<file>\n"
" Used when certain messages should be displayed but\n" " Used when certain messages should be displayed but\n"
" should not cause a non-zero exitcode.\n" " should not cause a non-zero exitcode.\n"
" --file-list=<file> Specify the files to check in a text file. Add one\n" " --file-list=<file> Specify the files to check in a text file. Add one\n"
" filename per line. When file is -, the file list will\n" " filename per line. When file is '-,' the file list will\n"
" be read from standard input.\n" " be read from standard input.\n"
" -f, --force Force checking of all configurations in files. If used\n" " -f, --force Force checking of all configurations in files. If used\n"
" together with --max-ifdefs=, the last option is the one\n" " together with '--max-ifdefs=', the last option is the one\n"
" that is effective.\n" " that is effective.\n"
" -h, --help Print this help.\n" " -h, --help Print this help.\n"
" -I <dir> Give include path. Give several -I parameters to give\n" " -I <dir> Give include path. Give several '-I' parameters to give\n"
" several paths. First given path is checked first. If\n" " several paths. First given path is checked first. If\n"
" paths are relative to source files, this is not needed.\n" " paths are relative to source files, this is not needed.\n"
" --includes-file=<file>\n" " --includes-file=<file>\n"
@ -711,13 +713,13 @@ void CmdLineParser::PrintHelp()
" header files included by source files are not matched.\n" " header files included by source files are not matched.\n"
" Directory name is matched to all parts of the path.\n" " Directory name is matched to all parts of the path.\n"
" --inline-suppr Enable inline suppressions. Use them by placing one or\n" " --inline-suppr Enable inline suppressions. Use them by placing one or\n"
" more comments, like: // cppcheck-suppress warningId\n" " more comments, like: '// cppcheck-suppress warningId'\n"
" on the lines before the warning to suppress.\n" " on the lines before the warning to suppress.\n"
" -j <jobs> Start [jobs] threads to do the checking simultaneously.\n" " -j <jobs> Start [jobs] threads to do the checking simultaneously.\n"
" --max-configs=<limit>\n" " --max-configs=<limit>\n"
" Maximum number of configurations to check in a file\n" " Maximum number of configurations to check in a file\n"
" before skipping it. Default is 12. If used together\n" " before skipping it. Default is '12'. If used together\n"
" with --force, the last option is the one that is\n" " with '--force', the last option is the one that is\n"
" effective.\n" " effective.\n"
" --platform=<type> Specifies platform specific types and sizes. The\n" " --platform=<type> Specifies platform specific types and sizes. The\n"
" available platforms are:\n" " available platforms are:\n"
@ -738,7 +740,7 @@ void CmdLineParser::PrintHelp()
" --rule-file=<file> Use given rule file. For more information, see: \n" " --rule-file=<file> Use given rule file. For more information, see: \n"
" https://sourceforge.net/projects/cppcheck/files/Articles/\n" " https://sourceforge.net/projects/cppcheck/files/Articles/\n"
#endif #endif
" -s, --style Deprecated, use --enable=style\n" " -s, --style Deprecated, please use '--enable=style' instead\n"
" --std=<id> Environment. The available options are:\n" " --std=<id> Environment. The available options are:\n"
" * posix\n" " * posix\n"
" Use this if a posix environment is available\n" " Use this if a posix environment is available\n"
@ -758,7 +760,7 @@ void CmdLineParser::PrintHelp()
" --template '<text>' Format the error messages. E.g.\n" " --template '<text>' Format the error messages. E.g.\n"
" '{file}:{line},{severity},{id},{message}' or\n" " '{file}:{line},{severity},{id},{message}' or\n"
" '{file}({line}):({severity}) {message}'\n" " '{file}({line}):({severity}) {message}'\n"
" Pre-defined templates: gcc, vs\n" " Pre-defined templates: gcc, vs, edit.\n"
" -v, --verbose Output more detailed error information.\n" " -v, --verbose Output more detailed error information.\n"
" --version Print out version number.\n" " --version Print out version number.\n"
" --xml Write results in xml format to error stream (stderr).\n" " --xml Write results in xml format to error stream (stderr).\n"

View File

@ -96,6 +96,7 @@ private:
TEST_CASE(templates); TEST_CASE(templates);
TEST_CASE(templatesGcc); TEST_CASE(templatesGcc);
TEST_CASE(templatesVs); TEST_CASE(templatesVs);
TEST_CASE(templatesEdit);
TEST_CASE(xml); TEST_CASE(xml);
TEST_CASE(xmlver1); TEST_CASE(xmlver1);
TEST_CASE(xmlver2); TEST_CASE(xmlver2);
@ -731,6 +732,15 @@ private:
ASSERT_EQUALS("{file}({line}): {severity}: {message}", settings._outputFormat); ASSERT_EQUALS("{file}({line}): {severity}: {message}", settings._outputFormat);
} }
void templatesEdit() {
REDIRECT;
const char *argv[] = {"cppcheck", "--template", "edit", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(4, argv));
ASSERT_EQUALS("{file} +{line}: {severity}: {message}", settings._outputFormat);
}
void xml() { void xml() {
REDIRECT; REDIRECT;
const char *argv[] = {"cppcheck", "--xml", "file.cpp"}; const char *argv[] = {"cppcheck", "--xml", "file.cpp"};