Skip to content

Commit

Permalink
moved exception output file descriptor to Settings / got rid of out…
Browse files Browse the repository at this point in the history
…put setters in SEH/signal code (#6868)
  • Loading branch information
firewave authored Oct 7, 2024
1 parent 629ec44 commit 1855f5c
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 68 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ $(libcppdir)/vf_unknownfunctionreturn.o: lib/vf_unknownfunctionreturn.cpp lib/ad
$(libcppdir)/vfvalue.o: lib/vfvalue.cpp lib/config.h lib/errortypes.h lib/mathlib.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vfvalue.cpp

cli/cmdlineparser.o: cli/cmdlineparser.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/filelister.h externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h lib/xml.h
cli/cmdlineparser.o: cli/cmdlineparser.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/filelister.h externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h lib/xml.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cmdlineparser.cpp

cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/cppcheckexecutorseh.h cli/executor.h cli/processexecutor.h cli/signalhandler.h cli/singleexecutor.h cli/threadexecutor.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/checkersreport.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h
Expand Down Expand Up @@ -840,7 +840,7 @@ test/testclangimport.o: test/testclangimport.cpp lib/addoninfo.h lib/check.h lib
test/testclass.o: test/testclass.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/checkclass.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testclass.cpp

test/testcmdlineparser.o: test/testcmdlineparser.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/cppcheckexecutor.h externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h test/redirect.h
test/testcmdlineparser.o: test/testcmdlineparser.cpp cli/cmdlinelogger.h cli/cmdlineparser.h externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h test/redirect.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testcmdlineparser.cpp

test/testcolor.o: test/testcolor.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h test/fixture.h
Expand Down
3 changes: 1 addition & 2 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "color.h"
#include "config.h"
#include "cppcheck.h"
#include "cppcheckexecutor.h"
#include "errorlogger.h"
#include "errortypes.h"
#include "filelister.h"
Expand Down Expand Up @@ -637,7 +636,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
return Result::Fail;
}
mSettings.exceptionHandling = true;
CppCheckExecutor::setExceptionOutput((exceptionOutfilename == "stderr") ? stderr : stdout);
mSettings.exceptionOutput = (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 Result::Fail;
Expand Down
24 changes: 1 addition & 23 deletions cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,6 @@ namespace {
};
}

// TODO: do not directly write to stdout

#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
/*static*/ FILE* CppCheckExecutor::mExceptionOutput = stdout;
#endif

int CppCheckExecutor::check(int argc, const char* const argv[])
{
Settings settings;
Expand Down Expand Up @@ -213,7 +207,7 @@ int CppCheckExecutor::check_wrapper(const Settings& settings)
return check_wrapper_seh(*this, &CppCheckExecutor::check_internal, settings);
#elif defined(USE_UNIX_SIGNAL_HANDLING)
if (settings.exceptionHandling)
register_signal_handler();
register_signal_handler(settings.exceptionOutput);
#endif
return check_internal(settings);
}
Expand Down Expand Up @@ -453,22 +447,6 @@ void StdLogger::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;
#if defined(USE_UNIX_SIGNAL_HANDLING)
set_signal_handler_output(mExceptionOutput);
#endif
}

// cppcheck-suppress unusedFunction - only used by USE_WINDOWS_SEH code
FILE* CppCheckExecutor::getExceptionOutput()
{
return mExceptionOutput;
}
#endif

/**
* Execute a shell command and read the output from it. Returns true if command terminated successfully.
*/
Expand Down
18 changes: 0 additions & 18 deletions cli/cppcheckexecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
#ifndef CPPCHECKEXECUTOR_H
#define CPPCHECKEXECUTOR_H

#include "config.h"
#include "filesettings.h"

#include <cstdio>
#include <list>
#include <string>
#include <vector>
Expand Down Expand Up @@ -62,15 +60,6 @@ class CppCheckExecutor {
*/
int check(int argc, const char* const argv[]);

/**
* @param exceptionOutput Output file
*/
static void setExceptionOutput(FILE* exceptionOutput);
/**
* @return file name to be used for output from exception handler. Has to be either "stdout" or "stderr".
*/
static FILE* getExceptionOutput();

private:

/**
Expand Down Expand Up @@ -108,13 +97,6 @@ class CppCheckExecutor {
std::list<FileWithDetails> mFiles;

std::list<FileSettings> mFileSettings;

#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
/**
* Output file name for exception handler
*/
static FILE* mExceptionOutput;
#endif
};

#endif // CPPCHECKEXECUTOR_H
2 changes: 1 addition & 1 deletion cli/cppcheckexecutorseh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ namespace {
*/
int check_wrapper_seh(CppCheckExecutor& executor, int (CppCheckExecutor::*f)(const Settings&) const, const Settings& settings)
{
FILE *outputFile = CppCheckExecutor::getExceptionOutput();
FILE * const outputFile = settings.exceptionOutput;
__try {
return (&executor->*f)(settings);
} __except (filterException(outputFile, GetExceptionCode(), GetExceptionInformation())) {
Expand Down
11 changes: 3 additions & 8 deletions cli/signalhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ static constexpr size_t MYSTACKSIZE = 16*1024+SIGSTKSZ; // wild guess about a re
#endif
static char mytstack[MYSTACKSIZE]= {0}; // alternative stack for signal handler
static bool bStackBelowHeap=false; // lame attempt to locate heap vs. stack address space. See CppCheckExecutor::check_wrapper()
static FILE* signalOutput = stdout;

void set_signal_handler_output(FILE* f)
{
signalOutput = f;
}
static FILE* signalOutput = stdout; // TODO: get rid of this

/**
* \param[in] ptr address to be examined.
Expand Down Expand Up @@ -299,9 +294,9 @@ static void CppcheckSignalHandler(int signo, siginfo_t * info, void * context)
}
}

void register_signal_handler()
void register_signal_handler(FILE * const output)
{
FILE * const output = signalOutput;
signalOutput = output;

// determine stack vs. heap
char stackVariable;
Expand Down
7 changes: 1 addition & 6 deletions cli/signalhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@

#include <cstdio>

/**
* @param f Output file
*/
void set_signal_handler_output(FILE* f);

void register_signal_handler();
void register_signal_handler(FILE* output);

#endif // USE_UNIX_SIGNAL_HANDLING

Expand Down
2 changes: 2 additions & 0 deletions lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ class CPPCHECKLIB WARN_UNUSED Settings {
#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
/** @brief Is --exception-handling given */
bool exceptionHandling{};

FILE* exceptionOutput = stdout;
#endif

enum class ExecutorType : std::uint8_t
Expand Down
2 changes: 1 addition & 1 deletion test/signal/test-signalhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ int main(int argc, const char * const argv[])
if (argc != 2)
return 1;

register_signal_handler();
register_signal_handler(stdout);

if (strcmp(argv[1], "assert") == 0)
my_assert();
Expand Down
10 changes: 3 additions & 7 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "cmdlinelogger.h"
#include "cmdlineparser.h"
#include "config.h"
#include "cppcheckexecutor.h"
#include "errorlogger.h"
#include "errortypes.h"
#include "helpers.h"
Expand Down Expand Up @@ -1911,28 +1910,25 @@ class TestCmdlineParser : public TestFixture {
void exceptionhandling() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--exception-handling", "file.cpp"};
CppCheckExecutor::setExceptionOutput(stderr);
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT(settings->exceptionHandling);
ASSERT_EQUALS(stderr, CppCheckExecutor::getExceptionOutput());
ASSERT_EQUALS(stdout, settings->exceptionOutput);
}

void exceptionhandling2() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--exception-handling=stderr", "file.cpp"};
CppCheckExecutor::setExceptionOutput(stdout);
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT(settings->exceptionHandling);
ASSERT_EQUALS(stderr, CppCheckExecutor::getExceptionOutput());
ASSERT_EQUALS(stderr, settings->exceptionOutput);
}

void exceptionhandling3() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--exception-handling=stdout", "file.cpp"};
CppCheckExecutor::setExceptionOutput(stderr);
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT(settings->exceptionHandling);
ASSERT_EQUALS(stdout, CppCheckExecutor::getExceptionOutput());
ASSERT_EQUALS(stdout, settings->exceptionOutput);
}

void exceptionhandlingInvalid() {
Expand Down

0 comments on commit 1855f5c

Please sign in to comment.