Fixed #824 (Exit code of cppcheck)

This commit is contained in:
Daniel Marjamäki 2009-10-17 08:19:37 +02:00
parent ffbf8e38ac
commit 48669e61a1
3 changed files with 61 additions and 63 deletions

View File

@ -79,17 +79,20 @@ void CppCheck::clearFiles()
std::string CppCheck::parseFromArgs(int argc, const char* const argv[]) void CppCheck::parseFromArgs(int argc, const char* const argv[])
{ {
std::vector<std::string> pathnames; std::vector<std::string> pathnames;
bool showHelp = false; bool showHelp = false;
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
if (strcmp(argv[i], "--version") == 0) if (strcmp(argv[i], "--version") == 0)
return "Cppcheck 1.37\n"; {
reportOut("Cppcheck 1.37");
return;
}
// Flag used for various purposes during debugging // Flag used for various purposes during debugging
if (strcmp(argv[i], "--debug") == 0) else if (strcmp(argv[i], "--debug") == 0)
_settings._debug = true; _settings._debug = true;
// Show all messages // Show all messages
@ -110,11 +113,11 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
++i; ++i;
if (i >= argc) if (i >= argc)
return "No file specified for the --suppressions option\n"; throw std::runtime_error("No file specified for the --suppressions option");
std::ifstream f(argv[i]); std::ifstream f(argv[i]);
if (!f.is_open()) if (!f.is_open())
return "couldn't open the file \"" + std::string(argv[i]) + "\"\n"; throw std::runtime_error("couldn't open the file \"" + std::string(argv[i]) + "\"");
_settings.suppressions(f); _settings.suppressions(f);
} }
@ -161,7 +164,7 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
if (!(iss >> _settings._exitCode)) if (!(iss >> _settings._exitCode))
{ {
_settings._exitCode = 0; _settings._exitCode = 0;
return "cppcheck: Argument must be an integer. Try something like '--error-exitcode=1'\n"; throw std::runtime_error("cppcheck: Argument must be an integer. Try something like '--error-exitcode=1'");
} }
} }
@ -175,7 +178,7 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
{ {
++i; ++i;
if (i >= argc) if (i >= argc)
return "cppcheck: argument to '-I' is missing\n"; throw std::runtime_error("cppcheck: argument to '-I' is missing");
path = argv[i]; path = argv[i];
} }
@ -201,7 +204,7 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
// "--template path/" // "--template path/"
++i; ++i;
if (i >= argc) if (i >= argc)
return "cppcheck: argument to '--template' is missing\n"; throw std::runtime_error("cppcheck: argument to '--template' is missing");
_settings._outputFormat = argv[i]; _settings._outputFormat = argv[i];
if (_settings._outputFormat == "gcc") if (_settings._outputFormat == "gcc")
@ -221,7 +224,7 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
{ {
++i; ++i;
if (i >= argc) if (i >= argc)
return "cppcheck: argument to '-j' is missing\n"; throw std::runtime_error("cppcheck: argument to '-j' is missing");
numberString = argv[i]; numberString = argv[i];
} }
@ -235,11 +238,11 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
std::istringstream iss(numberString); std::istringstream iss(numberString);
if (!(iss >> _settings._jobs)) if (!(iss >> _settings._jobs))
return "cppcheck: argument to '-j' is not a number\n"; throw std::runtime_error("cppcheck: argument to '-j' is not a number");
if (_settings._jobs > 1000) if (_settings._jobs > 1000)
{ {
return "cppcheck: argument for '-j' is allowed to be 1000 at max\n"; throw std::runtime_error("cppcheck: argument for '-j' is allowed to be 1000 at max");
} }
} }
@ -249,11 +252,11 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
++i; ++i;
if (i >= argc || !strstr(argv[i], ".lst")) if (i >= argc || !strstr(argv[i], ".lst"))
return "No .lst file specified for the --auto-dealloc option\n"; throw std::runtime_error("No .lst file specified for the --auto-dealloc option");
std::ifstream f(argv[i]); std::ifstream f(argv[i]);
if (!f.is_open()) if (!f.is_open())
return "couldn't open the file \"" + std::string(argv[i+1]) + "\"\n"; throw std::runtime_error("couldn't open the file \"" + std::string(argv[i+1]) + "\"");
_settings.autoDealloc(f); _settings.autoDealloc(f);
} }
@ -267,7 +270,6 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
(*it)->getErrorMessages(); (*it)->getErrorMessages();
} }
std::cout << ErrorLogger::ErrorMessage::getXMLFooter() << std::endl; std::cout << ErrorLogger::ErrorMessage::getXMLFooter() << std::endl;
return "";
} }
// documentation.. // documentation..
@ -286,12 +288,12 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
while (doc2.find("\n\n\n") != std::string::npos) while (doc2.find("\n\n\n") != std::string::npos)
doc2.erase(doc2.find("\n\n\n"), 1); doc2.erase(doc2.find("\n\n\n"), 1);
std::cout << doc2; std::cout << doc2;
return ""; return;
} }
else if (strncmp(argv[i], "-", 1) == 0 || strncmp(argv[i], "--", 2) == 0) else if (strncmp(argv[i], "-", 1) == 0 || strncmp(argv[i], "--", 2) == 0)
{ {
return "cppcheck: error: unrecognized command line option \"" + std::string(argv[i]) + "\"\n"; throw std::runtime_error("cppcheck: error: unrecognized command line option \"" + std::string(argv[i]) + "\"");
} }
else else
@ -300,7 +302,7 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
if (_settings._unusedFunctions && _settings._jobs > 1) if (_settings._unusedFunctions && _settings._jobs > 1)
{ {
return "cppcheck: error: --unused-functions can't be used with -j option.\n"; throw std::runtime_error("cppcheck: error: --unused-functions can't be used with -j option.");
} }
if (pathnames.size() > 0) if (pathnames.size() > 0)
@ -371,15 +373,12 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
" cppcheck -v -a -s one.cpp two.cpp\n" " cppcheck -v -a -s one.cpp two.cpp\n"
" # Check f.cpp and search include files from inc1/ and inc2/:\n" " # Check f.cpp and search include files from inc1/ and inc2/:\n"
" cppcheck -I inc1/ -I inc2/ f.cpp\n"; " cppcheck -I inc1/ -I inc2/ f.cpp\n";
return oss.str(); reportOut(oss.str());
} }
else if (_filenames.empty()) else if (_filenames.empty())
{ {
return "cppcheck: No C or C++ source files found.\n"; throw std::runtime_error("cppcheck: No C or C++ source files found.");
} }
return "";
} }
unsigned int CppCheck::check() unsigned int CppCheck::check()

View File

@ -102,12 +102,9 @@ public:
* *
* @param argc argc from main() * @param argc argc from main()
* @param argv argv from main() * @param argv argv from main()
* @return Empty string if parameters were accepted, or * @throw std::runtime_error when errors are found in the input
* string containing "help" text if no parameters were given or
* -h or --help parameters was given. Or error message if no
* files were found or if invalid parameter was given.
*/ */
std::string parseFromArgs(int argc, const char* const argv[]); void parseFromArgs(int argc, const char* const argv[]);
const std::vector<std::string> &filenames() const; const std::vector<std::string> &filenames() const;

View File

@ -22,6 +22,7 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE #include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
#include <stdexcept>
CppCheckExecutor::CppCheckExecutor() CppCheckExecutor::CppCheckExecutor()
{ {
@ -36,9 +37,16 @@ CppCheckExecutor::~CppCheckExecutor()
int CppCheckExecutor::check(int argc, const char* const argv[]) int CppCheckExecutor::check(int argc, const char* const argv[])
{ {
CppCheck cppCheck(*this); CppCheck cppCheck(*this);
std::string result = cppCheck.parseFromArgs(argc, argv); try
if (result.length() == 0)
{ {
cppCheck.parseFromArgs(argc, argv);
}
catch (std::runtime_error &e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
_settings = cppCheck.settings(); _settings = cppCheck.settings();
if (_settings._xml) if (_settings._xml)
{ {
@ -73,12 +81,6 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
return _settings._exitCode; return _settings._exitCode;
else else
return 0; return 0;
}
else
{
std::cout << result;
return EXIT_FAILURE;
}
} }
void CppCheckExecutor::reportErr(const std::string &errmsg) void CppCheckExecutor::reportErr(const std::string &errmsg)