Fix ticket #135 (Add option to control main() exit value)

This commit is contained in:
Reijo Tomperi 2009-03-06 02:03:31 +02:00
parent 6fcb89f136
commit 28475b2986
8 changed files with 50 additions and 19 deletions

View File

@ -103,6 +103,7 @@ man(1), man(7), http://www.tldp.org/HOWTO/Man-Page/
<cmdsynopsis> <cmdsynopsis>
<command>&dhpackage;</command> <command>&dhpackage;</command>
<arg choice="opt"><option>--all</option></arg> <arg choice="opt"><option>--all</option></arg>
<arg choice="opt"><option>--error-exitcode=[n]</option></arg>
<arg choice="opt"><option>--force</option></arg> <arg choice="opt"><option>--force</option></arg>
<arg choice="opt"><option>--help</option></arg> <arg choice="opt"><option>--help</option></arg>
<arg choice="opt"><option>-I[dir]</option></arg> <arg choice="opt"><option>-I[dir]</option></arg>
@ -138,6 +139,15 @@ man(1), man(7), http://www.tldp.org/HOWTO/Man-Page/
found a bug. When this option is given, all messages are shown.</para> found a bug. When this option is given, all messages are shown.</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>--error-exitcode=[n]</option></term>
<listitem>
<para>If errors are found, integer [n] is returned instead of default 0.
EXIT_FAILURE is returned if arguments are not valid or if no input files are
provided. Note that your operating system can modify this value, e.g.
256 can become 0.</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><option>-f</option></term> <term><option>-f</option></term>
<term><option>--force</option></term> <term><option>--force</option></term>

View File

@ -100,7 +100,7 @@ void CheckStl::stlOutOfBounds()
tok = tok->next(); tok = tok->next();
} }
if( !tok ) if (!tok)
return; return;
tok = tok->next(); tok = tok->next();

View File

@ -119,6 +119,20 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
break; break;
} }
// --error-exitcode=1
else if (strncmp(argv[i], "--error-exitcode=", 17) == 0)
{
std::string temp = argv[i];
temp = temp.substr(17);
std::istringstream iss(temp);
if (!(iss >> _settings._exitCode))
{
_settings._exitCode = 0;
return "cppcheck: Argument must be an integer. Try something like '--error-exitcode=1'\n";
}
}
// Include paths // Include paths
else if (strcmp(argv[i], "-I") == 0 || strncmp(argv[i], "-I", 2) == 0) else if (strcmp(argv[i], "-I") == 0 || strncmp(argv[i], "-I", 2) == 0)
{ {
@ -216,6 +230,11 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
"Options:\n" "Options:\n"
" -a, --all Make the checking more sensitive. More bugs are\n" " -a, --all Make the checking more sensitive. More bugs are\n"
" detected, but there are also more false positives\n" " detected, but there are also more false positives\n"
" --error-exitcode=[n] If errors are found, integer [n] is returned instead\n"
" of default 0. EXIT_FAILURE is returned\n"
" if arguments are not valid or if no input files are\n"
" provided. Note that your operating system can\n"
" modify this value, e.g. 256 can become 0.\n"
" -f, --force Force checking on files that have \"too many\"\n" " -f, --force Force checking on files that have \"too many\"\n"
" configurations\n" " configurations\n"
" -h, --help Print this help\n" " -h, --help Print this help\n"

View File

@ -22,6 +22,7 @@
#include "threadexecutor.h" #include "threadexecutor.h"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
CppCheckExecutor::CppCheckExecutor() CppCheckExecutor::CppCheckExecutor()
{ {
@ -33,7 +34,7 @@ CppCheckExecutor::~CppCheckExecutor()
} }
unsigned 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); std::string result = cppCheck.parseFromArgs(argc, argv);
@ -70,12 +71,15 @@ unsigned int CppCheckExecutor::check(int argc, const char* const argv[])
reportErr("</results>"); reportErr("</results>");
} }
return returnValue; if (returnValue)
return _settings._exitCode;
else
return 0;
} }
else else
{ {
std::cout << result; std::cout << result;
return 1; return EXIT_FAILURE;
} }
} }

View File

@ -48,12 +48,13 @@ public:
* *
* @param argc from main() * @param argc from main()
* @param argv from main() * @param argv from main()
* @return amount of errors found in the checking or 0 * @return EXIT_FAILURE if arguments are invalid or no input files
* if no errors were found. If parsing of the arguments failed * were found.
* and checking is not even started, 1 is returned to indicate * If errors are found and --error-exitcode is used,
* about an error. * given value is returned instead of default 0.
* If no errors are found, 0 is returned.
*/ */
unsigned int check(int argc, const char* const argv[]); int check(int argc, const char* const argv[]);

View File

@ -17,7 +17,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/ * along with this program. If not, see <http://www.gnu.org/licenses/
*/ */
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
#include "cppcheckexecutor.h" #include "cppcheckexecutor.h"
/** /**
@ -25,17 +24,10 @@
* *
* @param argc Passed to CppCheck::parseFromArgs() * @param argc Passed to CppCheck::parseFromArgs()
* @param argv Passed to CppCheck::parseFromArgs() * @param argv Passed to CppCheck::parseFromArgs()
* @return EXIT_SUCCESS if no errors are found or * @return What CppCheckExecutor::check() returns.
* EXIT_FAILURE if errors are found or checking was
* not done because of invalid arguments given.
*/ */
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
CppCheckExecutor exec; CppCheckExecutor exec;
if (exec.check(argc, argv) == 0) return exec.check(argc, argv);
{
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
} }

View File

@ -32,6 +32,7 @@ Settings::Settings()
_security = false; _security = false;
_vcl = false; _vcl = false;
_jobs = 1; _jobs = 1;
_exitCode = 0;
} }
Settings::~Settings() Settings::~Settings()

View File

@ -55,6 +55,10 @@ public:
/** How many processes/threads should do checking at the same /** How many processes/threads should do checking at the same
time. Default is 1. */ time. Default is 1. */
unsigned int _jobs; unsigned int _jobs;
/** If errors are found, this value is returned from main().
Default value is 0. */
int _exitCode;
}; };
#endif // SETTINGS_H #endif // SETTINGS_H