fixed #12045 - print error when using an option which has not been compiled in instead of treating it as non-existent or a no-op (#5508)

Also disabled more internal code around those options and did some
cleanups.
This commit is contained in:
Oliver Stöneberg 2023-10-21 09:12:59 +02:00 committed by GitHub
parent 41bd28c0b3
commit 7086ffaa1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 320 additions and 187 deletions

View File

@ -356,11 +356,17 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
// Exception handling inside cppcheck client // Exception handling inside cppcheck client
else if (std::strcmp(argv[i], "--exception-handling") == 0) { else if (std::strcmp(argv[i], "--exception-handling") == 0) {
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
mSettings.exceptionHandling = true; mSettings.exceptionHandling = true;
#else
mLogger.printError("Option --exception-handling is not supported since Cppcheck has not been built with any exception handling enabled.");
return false;
#endif
} }
// Exception handling inside cppcheck client // Exception handling inside cppcheck client
else if (std::strncmp(argv[i], "--exception-handling=", 21) == 0) { else if (std::strncmp(argv[i], "--exception-handling=", 21) == 0) {
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
const std::string exceptionOutfilename = argv[i] + 21; const std::string exceptionOutfilename = argv[i] + 21;
if (exceptionOutfilename != "stderr" && exceptionOutfilename != "stdout") { if (exceptionOutfilename != "stderr" && exceptionOutfilename != "stdout") {
mLogger.printError("invalid '--exception-handling' argument"); mLogger.printError("invalid '--exception-handling' argument");
@ -368,6 +374,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
} }
mSettings.exceptionHandling = true; mSettings.exceptionHandling = true;
CppCheckExecutor::setExceptionOutput((exceptionOutfilename == "stderr") ? stderr : stdout); CppCheckExecutor::setExceptionOutput((exceptionOutfilename == "stderr") ? stderr : stdout);
#else
mLogger.printError("Option --exception-handling is not supported since Cppcheck has not been built with any exception handling enabled.");
return false;
#endif
} }
// Filter errors // Filter errors
@ -508,8 +518,8 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
mSettings.jobs = tmp; mSettings.jobs = tmp;
} }
#ifdef THREADING_MODEL_FORK
else if (std::strncmp(argv[i], "-l", 2) == 0) { else if (std::strncmp(argv[i], "-l", 2) == 0) {
#ifdef THREADING_MODEL_FORK
std::string numberString; std::string numberString;
// "-l 3" // "-l 3"
@ -534,8 +544,11 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
return false; return false;
} }
mSettings.loadAverage = tmp; mSettings.loadAverage = tmp;
} #else
mLogger.printError("Option -l cannot be used as Cppcheck has not been built with fork threading model.");
return false;
#endif #endif
}
// Enforce language (--language=, -x) // Enforce language (--language=, -x)
else if (std::strncmp(argv[i], "--language=", 11) == 0 || std::strcmp(argv[i], "-x") == 0) { else if (std::strncmp(argv[i], "--language=", 11) == 0 || std::strcmp(argv[i], "-x") == 0) {
@ -760,18 +773,25 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
mSettings.reportProgress = tmp; mSettings.reportProgress = tmp;
} }
#ifdef HAVE_RULES
// Rule given at command line // Rule given at command line
else if (std::strncmp(argv[i], "--rule=", 7) == 0) { else if (std::strncmp(argv[i], "--rule=", 7) == 0) {
#ifdef HAVE_RULES
Settings::Rule rule; Settings::Rule rule;
rule.pattern = 7 + argv[i]; rule.pattern = 7 + argv[i];
mSettings.rules.emplace_back(std::move(rule)); mSettings.rules.emplace_back(std::move(rule));
#else
mLogger.printError("Option --rule cannot be used as Cppcheck has not been built with rules support.");
return false;
#endif
} }
// Rule file // Rule file
else if (std::strncmp(argv[i], "--rule-file=", 12) == 0) { else if (std::strncmp(argv[i], "--rule-file=", 12) == 0) {
#ifdef HAVE_RULES
const std::string ruleFile = argv[i] + 12;
tinyxml2::XMLDocument doc; tinyxml2::XMLDocument doc;
if (doc.LoadFile(12+argv[i]) == tinyxml2::XML_SUCCESS) { const tinyxml2::XMLError err = doc.LoadFile(ruleFile.c_str());
if (err == tinyxml2::XML_SUCCESS) {
tinyxml2::XMLElement *node = doc.FirstChildElement(); tinyxml2::XMLElement *node = doc.FirstChildElement();
if (node && strcmp(node->Value(), "rules") == 0) if (node && strcmp(node->Value(), "rules") == 0)
node = node->FirstChildElement("rule"); node = node->FirstChildElement("rule");
@ -806,11 +826,14 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
mSettings.rules.emplace_back(std::move(rule)); mSettings.rules.emplace_back(std::move(rule));
} }
} else { } else {
mLogger.printError("unable to load rule-file: " + std::string(12+argv[i])); mLogger.printError("unable to load rule-file '" + ruleFile + "' (" + tinyxml2::XMLDocument::ErrorIDToName(err) + ").");
return false; return false;
} }
} #else
mLogger.printError("Option --rule-file cannot be used as Cppcheck has not been built with rules support.");
return false;
#endif #endif
}
// show timing information.. // show timing information..
else if (std::strncmp(argv[i], "--showtime=", 11) == 0) { else if (std::strncmp(argv[i], "--showtime=", 11) == 0) {
@ -1051,7 +1074,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
return true; return true;
} }
void CmdLineParser::printHelp() void CmdLineParser::printHelp() const
{ {
const std::string manualUrl(isCppcheckPremium() ? const std::string manualUrl(isCppcheckPremium() ?
"https://cppcheck.sourceforge.io/manual.pdf" : "https://cppcheck.sourceforge.io/manual.pdf" :
@ -1190,64 +1213,62 @@ void CmdLineParser::printHelp()
" 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"
#ifdef THREADING_MODEL_FORK " -l <load> Specifies that no new threads should be started if\n"
" -l <load> Specifies that no new threads should be started if\n" " there are other threads running and the load average is\n"
" there are other threads running and the load average is\n" " at least <load>.\n"
" at least <load>.\n" " --language=<language>, -x <language>\n"
#endif " Forces cppcheck to check all files as the given\n"
" --language=<language>, -x <language>\n" " language. Valid values are: c, c++\n"
" Forces cppcheck to check all files as the given\n" " --library=<cfg> Load file <cfg> that contains information about types\n"
" language. Valid values are: c, c++\n" " and functions. With such information Cppcheck\n"
" --library=<cfg> Load file <cfg> that contains information about types\n" " understands your code better and therefore you\n"
" and functions. With such information Cppcheck\n" " get better results. The std.cfg file that is\n"
" understands your code better and therefore you\n" " distributed with Cppcheck is loaded automatically.\n"
" get better results. The std.cfg file that is\n" " For more information about library files, read the\n"
" distributed with Cppcheck is loaded automatically.\n" " manual.\n"
" For more information about library files, read the\n" " --max-configs=<limit>\n"
" manual.\n" " Maximum number of configurations to check in a file\n"
" --max-configs=<limit>\n" " before skipping it. Default is '12'. If used together\n"
" Maximum number of configurations to check in a file\n" " with '--force', the last option is the one that is\n"
" before skipping it. Default is '12'. If used together\n" " effective.\n"
" with '--force', the last option is the one that is\n" " --max-ctu-depth=N Max depth in whole program analysis. The default value\n"
" effective.\n" " is 2. A larger value will mean more errors can be found\n"
" --max-ctu-depth=N Max depth in whole program analysis. The default value\n" " but also means the analysis will be slower.\n"
" is 2. A larger value will mean more errors can be found\n" " --output-file=<file> Write results to file, rather than standard error.\n"
" but also means the analysis will be slower.\n" " --platform=<type>, --platform=<file>\n"
" --output-file=<file> Write results to file, rather than standard error.\n" " Specifies platform specific types and sizes. The\n"
" --platform=<type>, --platform=<file>\n" " available builtin platforms are:\n"
" Specifies platform specific types and sizes. The\n" " * unix32\n"
" available builtin platforms are:\n" " 32 bit unix variant\n"
" * unix32\n" " * unix64\n"
" 32 bit unix variant\n" " 64 bit unix variant\n"
" * unix64\n" " * win32A\n"
" 64 bit unix variant\n" " 32 bit Windows ASCII character encoding\n"
" * win32A\n" " * win32W\n"
" 32 bit Windows ASCII character encoding\n" " 32 bit Windows UNICODE character encoding\n"
" * win32W\n" " * win64\n"
" 32 bit Windows UNICODE character encoding\n" " 64 bit Windows\n"
" * win64\n" " * avr8\n"
" 64 bit Windows\n" " 8 bit AVR microcontrollers\n"
" * avr8\n" " * elbrus-e1cp\n"
" 8 bit AVR microcontrollers\n" " Elbrus e1c+ architecture\n"
" * elbrus-e1cp\n" " * pic8\n"
" Elbrus e1c+ architecture\n" " 8 bit PIC microcontrollers\n"
" * pic8\n" " Baseline and mid-range architectures\n"
" 8 bit PIC microcontrollers\n" " * pic8-enhanced\n"
" Baseline and mid-range architectures\n" " 8 bit PIC microcontrollers\n"
" * pic8-enhanced\n" " Enhanced mid-range and high end (PIC18) architectures\n"
" 8 bit PIC microcontrollers\n" " * pic16\n"
" Enhanced mid-range and high end (PIC18) architectures\n" " 16 bit PIC microcontrollers\n"
" * pic16\n" " * mips32\n"
" 16 bit PIC microcontrollers\n" " 32 bit MIPS microcontrollers\n"
" * mips32\n" " * native\n"
" 32 bit MIPS microcontrollers\n" " Type sizes of host system are assumed, but no\n"
" * native\n" " further assumptions.\n"
" Type sizes of host system are assumed, but no\n" " * unspecified\n"
" further assumptions.\n" " Unknown type sizes\n"
" * unspecified\n" " --plist-output=<path>\n"
" Unknown type sizes\n" " Generate Clang-plist output files in folder.\n";
" --plist-output=<path>\n"
" Generate Clang-plist output files in folder.\n";
if (isCppcheckPremium()) { if (isCppcheckPremium()) {
oss << oss <<
@ -1287,117 +1308,115 @@ void CmdLineParser::printHelp()
" currently only possible to apply the base paths to\n" " currently only possible to apply the base paths to\n"
" files that are on a lower level in the directory tree.\n" " files that are on a lower level in the directory tree.\n"
" --report-progress Report progress messages while checking a file (single job only).\n" " --report-progress Report progress messages while checking a file (single job only).\n"
#ifdef HAVE_RULES " --rule=<rule> Match regular expression.\n"
" --rule=<rule> Match regular expression.\n" " --rule-file=<file> Use given rule file. For more information, see:\n"
" --rule-file=<file> Use given rule file. For more information, see:\n" " http://sourceforge.net/projects/cppcheck/files/Articles/\n"
" http://sourceforge.net/projects/cppcheck/files/Articles/\n" " --showtime=<mode> Show timing information.\n"
#endif " The available modes are:\n"
" --showtime=<mode> Show timing information.\n" " * none\n"
" The available modes are:\n" " Show nothing (default)\n"
" * none\n" " * file\n"
" Show nothing (default)\n" " Show for each processed file\n"
" * file\n" " * file-total\n"
" Show for each processed file\n" " Show total time only for each processed file\n"
" * file-total\n" " * summary\n"
" Show total time only for each processed file\n" " Show a summary at the end\n"
" * summary\n" " * top5_file\n"
" Show a summary at the end\n" " Show the top 5 for each processed file\n"
" * top5_file\n" " * top5_summary\n"
" Show the top 5 for each processed file\n" " Show the top 5 summary at the end\n"
" * top5_summary\n" " * top5\n"
" Show the top 5 summary at the end\n" " Alias for top5_file (deprecated)\n"
" * top5\n" " --std=<id> Set standard.\n"
" Alias for top5_file (deprecated)\n" " The available options are:\n"
" --std=<id> Set standard.\n" " * c89\n"
" The available options are:\n" " C code is C89 compatible\n"
" * c89\n" " * c99\n"
" C code is C89 compatible\n" " C code is C99 compatible\n"
" * c99\n" " * c11\n"
" C code is C99 compatible\n" " C code is C11 compatible (default)\n"
" * c11\n" " * c++03\n"
" C code is C11 compatible (default)\n" " C++ code is C++03 compatible\n"
" * c++03\n" " * c++11\n"
" C++ code is C++03 compatible\n" " C++ code is C++11 compatible\n"
" * c++11\n" " * c++14\n"
" C++ code is C++11 compatible\n" " C++ code is C++14 compatible\n"
" * c++14\n" " * c++17\n"
" C++ code is C++14 compatible\n" " C++ code is C++17 compatible\n"
" * c++17\n" " * c++20\n"
" C++ code is C++17 compatible\n" " C++ code is C++20 compatible (default)\n"
" * c++20\n" " --suppress=<spec> Suppress warnings that match <spec>. The format of\n"
" C++ code is C++20 compatible (default)\n" " <spec> is:\n"
" --suppress=<spec> Suppress warnings that match <spec>. The format of\n" " [error id]:[filename]:[line]\n"
" <spec> is:\n" " The [filename] and [line] are optional. If [error id]\n"
" [error id]:[filename]:[line]\n" " is a wildcard '*', all error ids match.\n"
" The [filename] and [line] are optional. If [error id]\n" " --suppressions-list=<file>\n"
" is a wildcard '*', all error ids match.\n" " Suppress warnings listed in the file. Each suppression\n"
" --suppressions-list=<file>\n" " is in the same format as <spec> above.\n"
" Suppress warnings listed in the file. Each suppression\n" " --suppress-xml=<file>\n"
" is in the same format as <spec> above.\n" " Suppress warnings listed in a xml file. XML file should\n"
" --suppress-xml=<file>\n" " follow the manual.pdf format specified in section.\n"
" Suppress warnings listed in a xml file. XML file should\n" " `6.4 XML suppressions` .\n"
" follow the manual.pdf format specified in section.\n" " --template='<text>' Format the error messages. Available fields:\n"
" `6.4 XML suppressions` .\n" " {file} file name\n"
" --template='<text>' Format the error messages. Available fields:\n" " {line} line number\n"
" {file} file name\n" " {column} column number\n"
" {line} line number\n" " {callstack} show a callstack. Example:\n"
" {column} column number\n" " [file.c:1] -> [file.c:100]\n"
" {callstack} show a callstack. Example:\n" " {inconclusive:text} if warning is inconclusive, text\n"
" [file.c:1] -> [file.c:100]\n" " is written\n"
" {inconclusive:text} if warning is inconclusive, text\n" " {severity} severity\n"
" is written\n" " {message} warning message\n"
" {severity} severity\n" " {id} warning id\n"
" {message} warning message\n" " {cwe} CWE id (Common Weakness Enumeration)\n"
" {id} warning id\n" " {code} show the real code\n"
" {cwe} CWE id (Common Weakness Enumeration)\n" " \\t insert tab\n"
" {code} show the real code\n" " \\n insert newline\n"
" \\t insert tab\n" " \\r insert carriage return\n"
" \\n insert newline\n" " Example formats:\n"
" \\r insert carriage return\n" " '{file}:{line},{severity},{id},{message}' or\n"
" Example formats:\n" " '{file}({line}):({severity}) {message}' or\n"
" '{file}:{line},{severity},{id},{message}' or\n" " '{callstack} {message}'\n"
" '{file}({line}):({severity}) {message}' or\n" " Pre-defined templates: gcc (default), cppcheck1 (old default), vs, edit.\n"
" '{callstack} {message}'\n" // Note: template daca2 also exists, but is for internal use (cppcheck scripts).
" Pre-defined templates: gcc (default), cppcheck1 (old default), vs, edit.\n" " --template-location='<text>'\n"
// Note: template daca2 also exists, but is for internal use (cppcheck scripts). " Format error message location. If this is not provided\n"
" --template-location='<text>'\n" " then no extra location info is shown.\n"
" Format error message location. If this is not provided\n" " Available fields:\n"
" then no extra location info is shown.\n" " {file} file name\n"
" Available fields:\n" " {line} line number\n"
" {file} file name\n" " {column} column number\n"
" {line} line number\n" " {info} location info\n"
" {column} column number\n" " {code} show the real code\n"
" {info} location info\n" " \\t insert tab\n"
" {code} show the real code\n" " \\n insert newline\n"
" \\t insert tab\n" " \\r insert carriage return\n"
" \\n insert newline\n" " Example format (gcc-like):\n"
" \\r insert carriage return\n" " '{file}:{line}:{column}: note: {info}\\n{code}'\n"
" Example format (gcc-like):\n" " -U<ID> Undefine preprocessor symbol. Use -U to explicitly\n"
" '{file}:{line}:{column}: note: {info}\\n{code}'\n" " hide certain #ifdef <ID> code paths from checking.\n"
" -U<ID> Undefine preprocessor symbol. Use -U to explicitly\n" " Example: '-UDEBUG'\n"
" hide certain #ifdef <ID> code paths from checking.\n" " -v, --verbose Output more detailed error information.\n"
" Example: '-UDEBUG'\n" " Note that this option is not mutually exclusive with --quiet.\n"
" -v, --verbose Output more detailed error information.\n" " --version Print out version number.\n"
" Note that this option is not mutually exclusive with --quiet.\n" " --xml Write results in xml format to error stream (stderr).\n"
" --version Print out version number.\n" "\n"
" --xml Write results in xml format to error stream (stderr).\n" "Example usage:\n"
"\n" " # Recursively check the current folder. Print the progress on the screen and\n"
"Example usage:\n" " # write errors to a file:\n"
" # Recursively check the current folder. Print the progress on the screen and\n" " cppcheck . 2> err.txt\n"
" # write errors to a file:\n" "\n"
" cppcheck . 2> err.txt\n" " # Recursively check ../myproject/ and don't print progress:\n"
"\n" " cppcheck --quiet ../myproject/\n"
" # Recursively check ../myproject/ and don't print progress:\n" "\n"
" cppcheck --quiet ../myproject/\n" " # Check test.cpp, enable all checks:\n"
"\n" " cppcheck --enable=all --inconclusive --library=posix test.cpp\n"
" # Check test.cpp, enable all checks:\n" "\n"
" cppcheck --enable=all --inconclusive --library=posix test.cpp\n" " # Check f.cpp and search include files from inc1/ and inc2/:\n"
"\n" " cppcheck -I inc1/ -I inc2/ f.cpp\n"
" # Check f.cpp and search include files from inc1/ and inc2/:\n" "\n"
" cppcheck -I inc1/ -I inc2/ f.cpp\n" "For more information:\n"
"\n" " " << manualUrl << "\n"
"For more information:\n"
" " << manualUrl << "\n"
"\n" "\n"
"Many thanks to the 3rd party libraries we use:\n" "Many thanks to the 3rd party libraries we use:\n"
" * tinyxml2 -- loading project/library/ctu files.\n" " * tinyxml2 -- loading project/library/ctu files.\n"

View File

@ -99,7 +99,7 @@ protected:
/** /**
* Print help text to the console. * Print help text to the console.
*/ */
void printHelp(); void printHelp() const;
private: private:
bool isCppcheckPremium() const; bool isCppcheckPremium() const;

View File

@ -108,7 +108,9 @@ public:
// TODO: do not directly write to stdout // TODO: do not directly write to stdout
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
/*static*/ FILE* CppCheckExecutor::mExceptionOutput = stdout; /*static*/ FILE* CppCheckExecutor::mExceptionOutput = stdout;
#endif
CppCheckExecutor::~CppCheckExecutor() CppCheckExecutor::~CppCheckExecutor()
{ {
@ -257,11 +259,7 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
cppCheck.settings() = settings; cppCheck.settings() = settings;
mSettings = &settings; mSettings = &settings;
int ret; const int ret = check_wrapper(cppCheck);
if (settings.exceptionHandling)
ret = check_wrapper(cppCheck);
else
ret = check_internal(cppCheck);
mSettings = nullptr; mSettings = nullptr;
return ret; return ret;
@ -270,12 +268,13 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
int CppCheckExecutor::check_wrapper(CppCheck& cppcheck) int CppCheckExecutor::check_wrapper(CppCheck& cppcheck)
{ {
#ifdef USE_WINDOWS_SEH #ifdef USE_WINDOWS_SEH
return check_wrapper_seh(*this, &CppCheckExecutor::check_internal, cppcheck); if (cppcheck.settings().exceptionHandling)
return check_wrapper_seh(*this, &CppCheckExecutor::check_internal, cppcheck);
#elif defined(USE_UNIX_SIGNAL_HANDLING) #elif defined(USE_UNIX_SIGNAL_HANDLING)
return check_wrapper_sig(*this, &CppCheckExecutor::check_internal, cppcheck); if (cppcheck.settings().exceptionHandling)
#else return check_wrapper_sig(*this, &CppCheckExecutor::check_internal, cppcheck);
return check_internal(cppcheck);
#endif #endif
return check_internal(cppcheck);
} }
bool CppCheckExecutor::reportSuppressions(const Settings &settings, bool unusedFunctionCheckEnabled, const std::map<std::string, std::size_t> &files, ErrorLogger& errorLogger) { bool CppCheckExecutor::reportSuppressions(const Settings &settings, bool unusedFunctionCheckEnabled, const std::map<std::string, std::size_t> &files, ErrorLogger& errorLogger) {
@ -522,6 +521,7 @@ void CppCheckExecutor::reportErr(const ErrorMessage &msg)
reportErr(msg.toString(mSettings->verbose, mSettings->templateFormat, mSettings->templateLocation)); reportErr(msg.toString(mSettings->verbose, mSettings->templateFormat, mSettings->templateLocation));
} }
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
void CppCheckExecutor::setExceptionOutput(FILE* exceptionOutput) void CppCheckExecutor::setExceptionOutput(FILE* exceptionOutput)
{ {
mExceptionOutput = exceptionOutput; mExceptionOutput = exceptionOutput;
@ -531,6 +531,7 @@ FILE* CppCheckExecutor::getExceptionOutput()
{ {
return mExceptionOutput; return mExceptionOutput;
} }
#endif
bool CppCheckExecutor::tryLoadLibrary(Library& destination, const std::string& basepath, const char* filename) bool CppCheckExecutor::tryLoadLibrary(Library& destination, const std::string& basepath, const char* filename)
{ {

View File

@ -185,10 +185,12 @@ private:
*/ */
std::time_t mLatestProgressOutputTime{}; std::time_t mLatestProgressOutputTime{};
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
/** /**
* Output file name for exception handler * Output file name for exception handler
*/ */
static FILE* mExceptionOutput; static FILE* mExceptionOutput;
#endif
/** /**
* Error output * Error output

View File

@ -190,8 +190,10 @@ public:
/** @brief Name of the language that is enforced. Empty per default. */ /** @brief Name of the language that is enforced. Empty per default. */
Language enforcedLang{}; Language enforcedLang{};
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
/** @brief Is --exception-handling given */ /** @brief Is --exception-handling given */
bool exceptionHandling{}; bool exceptionHandling{};
#endif
// argv[0] // argv[0]
std::string exename; std::string exename;
@ -276,6 +278,7 @@ public:
/** @brief --report-progress */ /** @brief --report-progress */
int reportProgress{-1}; int reportProgress{-1};
#ifdef HAVE_RULES
/** Rule */ /** Rule */
struct CPPCHECKLIB Rule { struct CPPCHECKLIB Rule {
std::string tokenlist = "normal"; // use normal tokenlist std::string tokenlist = "normal"; // use normal tokenlist
@ -285,7 +288,6 @@ public:
Severity severity = Severity::style; // default severity Severity severity = Severity::style; // default severity
}; };
#ifdef HAVE_RULES
/** /**
* @brief Extra rules * @brief Extra rules
*/ */

View File

@ -255,11 +255,16 @@ private:
TEST_CASE(errorlistverbose1); TEST_CASE(errorlistverbose1);
TEST_CASE(errorlistverbose2); TEST_CASE(errorlistverbose2);
TEST_CASE(ignorepathsnopath); TEST_CASE(ignorepathsnopath);
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
TEST_CASE(exceptionhandling); TEST_CASE(exceptionhandling);
TEST_CASE(exceptionhandling2); TEST_CASE(exceptionhandling2);
TEST_CASE(exceptionhandling3); TEST_CASE(exceptionhandling3);
TEST_CASE(exceptionhandlingInvalid); TEST_CASE(exceptionhandlingInvalid);
TEST_CASE(exceptionhandlingInvalid2); TEST_CASE(exceptionhandlingInvalid2);
#else
TEST_CASE(exceptionhandlingNotSupported);
TEST_CASE(exceptionhandlingNotSupported2);
#endif
TEST_CASE(clang); TEST_CASE(clang);
TEST_CASE(clang2); TEST_CASE(clang2);
TEST_CASE(clangInvalid); TEST_CASE(clangInvalid);
@ -275,6 +280,8 @@ private:
TEST_CASE(loadAverage); TEST_CASE(loadAverage);
TEST_CASE(loadAverage2); TEST_CASE(loadAverage2);
TEST_CASE(loadAverageInvalid); TEST_CASE(loadAverageInvalid);
#else
TEST_CASE(loadAverageNotSupported);
#endif #endif
TEST_CASE(maxCtuDepth); TEST_CASE(maxCtuDepth);
TEST_CASE(maxCtuDepthInvalid); TEST_CASE(maxCtuDepthInvalid);
@ -297,6 +304,19 @@ private:
TEST_CASE(projectMissing); TEST_CASE(projectMissing);
TEST_CASE(projectNoPaths); TEST_CASE(projectNoPaths);
TEST_CASE(addon); TEST_CASE(addon);
#ifdef HAVE_RULES
TEST_CASE(rule);
#else
TEST_CASE(ruleNotSupported);
#endif
#ifdef HAVE_RULES
TEST_CASE(ruleFile);
TEST_CASE(ruleFileEmpty);
TEST_CASE(ruleFileMissing);
TEST_CASE(ruleFileInvalid);
#else
TEST_CASE(ruleFileNotSupported);
#endif
TEST_CASE(ignorepaths1); TEST_CASE(ignorepaths1);
TEST_CASE(ignorepaths2); TEST_CASE(ignorepaths2);
@ -1703,6 +1723,7 @@ private:
ASSERT_EQUALS("cppcheck: error: argument to '-i' is missing.\n", logger->str()); ASSERT_EQUALS("cppcheck: error: argument to '-i' is missing.\n", logger->str());
} }
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
void exceptionhandling() { void exceptionhandling() {
REDIRECT; REDIRECT;
const char * const argv[] = {"cppcheck", "--exception-handling", "file.cpp"}; const char * const argv[] = {"cppcheck", "--exception-handling", "file.cpp"};
@ -1749,6 +1770,21 @@ private:
ASSERT_EQUALS(false, parser->parseFromArgs(2, argv)); ASSERT_EQUALS(false, parser->parseFromArgs(2, argv));
ASSERT_EQUALS("cppcheck: error: unrecognized command line option: \"--exception-handling-foo\".\n", logger->str()); ASSERT_EQUALS("cppcheck: error: unrecognized command line option: \"--exception-handling-foo\".\n", logger->str());
} }
#else
void exceptionhandlingNotSupported() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--exception-handling", "file.cpp"};
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: Option --exception-handling is not supported since Cppcheck has not been built with any exception handling enabled.\n", logger->str());
}
void exceptionhandlingNotSupported2() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--exception-handling=stderr", "file.cpp"};
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: Option --exception-handling is not supported since Cppcheck has not been built with any exception handling enabled.\n", logger->str());
}
#endif
void clang() { void clang() {
REDIRECT; REDIRECT;
@ -1866,6 +1902,13 @@ private:
ASSERT(!parser->parseFromArgs(4, argv)); ASSERT(!parser->parseFromArgs(4, argv));
ASSERT_EQUALS("cppcheck: error: argument to '-l' is not valid - not an integer.\n", logger->str()); ASSERT_EQUALS("cppcheck: error: argument to '-l' is not valid - not an integer.\n", logger->str());
} }
#else
void loadAverageNotSupported() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-l", "12", "file.cpp"};
ASSERT(!parser->parseFromArgs(4, argv));
ASSERT_EQUALS("cppcheck: error: Option -l cannot be used as Cppcheck has not been built with fork threading model.\n", logger->str());
}
#endif #endif
void maxCtuDepth() { void maxCtuDepth() {
@ -2026,6 +2069,72 @@ private:
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
} }
#ifdef HAVE_RULES
void rule() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--rule=.+", "file.cpp"};
ASSERT(parser->parseFromArgs(3, argv));
ASSERT_EQUALS(1, settings->rules.size());
auto it = settings->rules.cbegin();
ASSERT_EQUALS(".+", it->pattern);
ASSERT_EQUALS("", logger->str());
}
#else
void ruleNotSupported() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--rule=.+", "file.cpp"};
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: Option --rule cannot be used as Cppcheck has not been built with rules support.\n", logger->str());
}
#endif
#ifdef HAVE_RULES
void ruleFile() {
REDIRECT;
ScopedFile file("rule.xml",
"<rules>\n"
"<rule>\n"
"<pattern>.+</pattern>\n"
"</rule>\n"
"</rules>");
const char * const argv[] = {"cppcheck", "--rule-file=rule.xml", "file.cpp"};
ASSERT(parser->parseFromArgs(3, argv));
ASSERT_EQUALS(1, settings->rules.size());
auto it = settings->rules.cbegin();
ASSERT_EQUALS(".+", it->pattern);
ASSERT_EQUALS("", logger->str());
}
void ruleFileEmpty() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--rule-file=", "file.cpp"};
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: unable to load rule-file '' (XML_ERROR_FILE_NOT_FOUND).\n", logger->str());
}
void ruleFileMissing() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--rule-file=rule.xml", "file.cpp"};
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: unable to load rule-file 'rule.xml' (XML_ERROR_FILE_NOT_FOUND).\n", logger->str());
}
void ruleFileInvalid() {
REDIRECT;
ScopedFile file("rule.xml", "");
const char * const argv[] = {"cppcheck", "--rule-file=rule.xml", "file.cpp"};
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: unable to load rule-file 'rule.xml' (XML_ERROR_EMPTY_DOCUMENT).\n", logger->str());
}
#else
void ruleFileNotSupported() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--rule-file=rule.xml", "file.cpp"};
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: Option --rule-file cannot be used as Cppcheck has not been built with rules support.\n", logger->str());
}
#endif
void ignorepaths1() { void ignorepaths1() {
REDIRECT; REDIRECT;
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"}; const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};