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
else if (std::strcmp(argv[i], "--exception-handling") == 0) {
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
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
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;
if (exceptionOutfilename != "stderr" && exceptionOutfilename != "stdout") {
mLogger.printError("invalid '--exception-handling' argument");
@ -368,6 +374,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
}
mSettings.exceptionHandling = true;
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
@ -508,8 +518,8 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
mSettings.jobs = tmp;
}
#ifdef THREADING_MODEL_FORK
else if (std::strncmp(argv[i], "-l", 2) == 0) {
#ifdef THREADING_MODEL_FORK
std::string numberString;
// "-l 3"
@ -534,8 +544,11 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
return false;
}
mSettings.loadAverage = tmp;
}
#else
mLogger.printError("Option -l cannot be used as Cppcheck has not been built with fork threading model.");
return false;
#endif
}
// Enforce language (--language=, -x)
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;
}
#ifdef HAVE_RULES
// Rule given at command line
else if (std::strncmp(argv[i], "--rule=", 7) == 0) {
#ifdef HAVE_RULES
Settings::Rule rule;
rule.pattern = 7 + argv[i];
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
else if (std::strncmp(argv[i], "--rule-file=", 12) == 0) {
#ifdef HAVE_RULES
const std::string ruleFile = argv[i] + 12;
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();
if (node && strcmp(node->Value(), "rules") == 0)
node = node->FirstChildElement("rule");
@ -806,11 +826,14 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
mSettings.rules.emplace_back(std::move(rule));
}
} 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;
}
}
#else
mLogger.printError("Option --rule-file cannot be used as Cppcheck has not been built with rules support.");
return false;
#endif
}
// show timing information..
else if (std::strncmp(argv[i], "--showtime=", 11) == 0) {
@ -1051,7 +1074,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
return true;
}
void CmdLineParser::printHelp()
void CmdLineParser::printHelp() const
{
const std::string manualUrl(isCppcheckPremium() ?
"https://cppcheck.sourceforge.io/manual.pdf" :
@ -1190,11 +1213,9 @@ void CmdLineParser::printHelp()
" more comments, like: '// cppcheck-suppress warningId'\n"
" on the lines before the warning to suppress.\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"
" there are other threads running and the load average is\n"
" at least <load>.\n"
#endif
" --language=<language>, -x <language>\n"
" Forces cppcheck to check all files as the given\n"
" language. Valid values are: c, c++\n"
@ -1287,11 +1308,9 @@ void CmdLineParser::printHelp()
" currently only possible to apply the base paths to\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"
#ifdef HAVE_RULES
" --rule=<rule> Match regular expression.\n"
" --rule-file=<file> Use given rule file. For more information, see:\n"
" http://sourceforge.net/projects/cppcheck/files/Articles/\n"
#endif
" --showtime=<mode> Show timing information.\n"
" The available modes are:\n"
" * none\n"

View File

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

View File

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

View File

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

View File

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

View File

@ -255,11 +255,16 @@ private:
TEST_CASE(errorlistverbose1);
TEST_CASE(errorlistverbose2);
TEST_CASE(ignorepathsnopath);
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
TEST_CASE(exceptionhandling);
TEST_CASE(exceptionhandling2);
TEST_CASE(exceptionhandling3);
TEST_CASE(exceptionhandlingInvalid);
TEST_CASE(exceptionhandlingInvalid2);
#else
TEST_CASE(exceptionhandlingNotSupported);
TEST_CASE(exceptionhandlingNotSupported2);
#endif
TEST_CASE(clang);
TEST_CASE(clang2);
TEST_CASE(clangInvalid);
@ -275,6 +280,8 @@ private:
TEST_CASE(loadAverage);
TEST_CASE(loadAverage2);
TEST_CASE(loadAverageInvalid);
#else
TEST_CASE(loadAverageNotSupported);
#endif
TEST_CASE(maxCtuDepth);
TEST_CASE(maxCtuDepthInvalid);
@ -297,6 +304,19 @@ private:
TEST_CASE(projectMissing);
TEST_CASE(projectNoPaths);
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(ignorepaths2);
@ -1703,6 +1723,7 @@ private:
ASSERT_EQUALS("cppcheck: error: argument to '-i' is missing.\n", logger->str());
}
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
void exceptionhandling() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--exception-handling", "file.cpp"};
@ -1749,6 +1770,21 @@ private:
ASSERT_EQUALS(false, parser->parseFromArgs(2, argv));
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() {
REDIRECT;
@ -1866,6 +1902,13 @@ private:
ASSERT(!parser->parseFromArgs(4, argv));
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
void maxCtuDepth() {
@ -2026,6 +2069,72 @@ private:
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() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};