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>
<command>&dhpackage;</command>
<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>--help</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>
</listitem>
</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>
<term><option>-f</option></term>
<term><option>--force</option></term>

View File

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

View File

@ -119,6 +119,20 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[])
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
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"
" -a, --all Make the checking more sensitive. More bugs are\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"
" configurations\n"
" -h, --help Print this help\n"

View File

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

View File

@ -48,12 +48,13 @@ public:
*
* @param argc from main()
* @param argv from main()
* @return amount of errors found in the checking or 0
* if no errors were found. If parsing of the arguments failed
* and checking is not even started, 1 is returned to indicate
* about an error.
* @return EXIT_FAILURE if arguments are invalid or no input files
* were found.
* If errors are found and --error-exitcode is used,
* 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/
*/
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
#include "cppcheckexecutor.h"
/**
@ -25,17 +24,10 @@
*
* @param argc Passed to CppCheck::parseFromArgs()
* @param argv Passed to CppCheck::parseFromArgs()
* @return EXIT_SUCCESS if no errors are found or
* EXIT_FAILURE if errors are found or checking was
* not done because of invalid arguments given.
* @return What CppCheckExecutor::check() returns.
*/
int main(int argc, char* argv[])
{
CppCheckExecutor exec;
if (exec.check(argc, argv) == 0)
{
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
return exec.check(argc, argv);
}

View File

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

View File

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