exename: Set proper exename when cppcheck is executed from PATH and argv[0] does not contain path information

This commit is contained in:
Daniel Marjamäki 2022-03-21 18:35:53 +01:00
parent 18d24e2420
commit 1fe7cd348a
7 changed files with 27 additions and 45 deletions

View File

@ -937,7 +937,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
} }
} }
mSettings->loadCppcheckCfg(argv[0]); mSettings->loadCppcheckCfg();
// Default template format.. // Default template format..
if (mSettings->templateFormat.empty()) { if (mSettings->templateFormat.empty()) {

View File

@ -243,9 +243,9 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
int ret; int ret;
if (cppCheck.settings().exceptionHandling) if (cppCheck.settings().exceptionHandling)
ret = check_wrapper(cppCheck, argc, argv); ret = check_wrapper(cppCheck);
else else
ret = check_internal(cppCheck, argc, argv); ret = check_internal(cppCheck);
mSettings = nullptr; mSettings = nullptr;
return ret; return ret;
@ -821,12 +821,12 @@ namespace {
* TODO Check for multi-threading issues! * TODO Check for multi-threading issues!
* *
*/ */
int CppCheckExecutor::check_wrapper(CppCheck& cppcheck, int argc, const char* const argv[]) int CppCheckExecutor::check_wrapper(CppCheck& cppcheck)
{ {
#ifdef USE_WINDOWS_SEH #ifdef USE_WINDOWS_SEH
FILE *outputFile = stdout; FILE *outputFile = stdout;
__try { __try {
return check_internal(cppcheck, argc, argv); return check_internal(cppcheck);
} __except (filterException(GetExceptionCode(), GetExceptionInformation())) { } __except (filterException(GetExceptionCode(), GetExceptionInformation())) {
// reporting to stdout may not be helpful within a GUI application... // reporting to stdout may not be helpful within a GUI application...
fputs("Please report this to the cppcheck developers!\n", outputFile); fputs("Please report this to the cppcheck developers!\n", outputFile);
@ -854,23 +854,23 @@ int CppCheckExecutor::check_wrapper(CppCheck& cppcheck, int argc, const char* co
for (std::map<int, std::string>::const_iterator sig=listofsignals.begin(); sig!=listofsignals.end(); ++sig) { for (std::map<int, std::string>::const_iterator sig=listofsignals.begin(); sig!=listofsignals.end(); ++sig) {
sigaction(sig->first, &act, nullptr); sigaction(sig->first, &act, nullptr);
} }
return check_internal(cppcheck, argc, argv); return check_internal(cppcheck);
#else #else
return check_internal(cppcheck, argc, argv); return check_internal(cppcheck);
#endif #endif
} }
/* /*
* That is a method which gets called from check_wrapper * That is a method which gets called from check_wrapper
* */ * */
int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const char* const argv[]) int CppCheckExecutor::check_internal(CppCheck& cppcheck)
{ {
Settings& settings = cppcheck.settings(); Settings& settings = cppcheck.settings();
mSettings = &settings; mSettings = &settings;
const bool std = tryLoadLibrary(settings.library, argv[0], "std.cfg"); const bool std = tryLoadLibrary(settings.library, settings.exename, "std.cfg");
for (const std::string &lib : settings.libraries) { for (const std::string &lib : settings.libraries) {
if (!tryLoadLibrary(settings.library, argv[0], lib.c_str())) { if (!tryLoadLibrary(settings.library, settings.exename, lib.c_str())) {
const std::string msg("Failed to load the library " + lib); const std::string msg("Failed to load the library " + lib);
const std::list<ErrorMessage::FileLocation> callstack; const std::list<ErrorMessage::FileLocation> callstack;
ErrorMessage errmsg(callstack, emptyString, Severity::information, msg, "failedToLoadCfg", Certainty::normal); ErrorMessage errmsg(callstack, emptyString, Severity::information, msg, "failedToLoadCfg", Certainty::normal);
@ -879,22 +879,15 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
} }
} }
bool posix = true; if (!std) {
if (settings.posix())
posix = tryLoadLibrary(settings.library, argv[0], "posix.cfg");
bool windows = true;
if (settings.isWindowsPlatform())
windows = tryLoadLibrary(settings.library, argv[0], "windows.cfg");
if (!std || !posix || !windows) {
const std::list<ErrorMessage::FileLocation> callstack; const std::list<ErrorMessage::FileLocation> callstack;
const std::string msg("Failed to load " + std::string(!std ? "std.cfg" : !posix ? "posix.cfg" : "windows.cfg") + ". Your Cppcheck installation is broken, please re-install."); const std::string msg("Failed to load std.cfg. Your Cppcheck installation is broken, please re-install.");
#ifdef FILESDIR #ifdef FILESDIR
const std::string details("The Cppcheck binary was compiled with FILESDIR set to \"" const std::string details("The Cppcheck binary was compiled with FILESDIR set to \""
FILESDIR "\" and will therefore search for " FILESDIR "\" and will therefore search for "
"std.cfg in " FILESDIR "/cfg."); "std.cfg in " FILESDIR "/cfg.");
#else #else
const std::string cfgfolder(Path::fromNativeSeparators(Path::getPathFromFilename(argv[0])) + "cfg"); const std::string cfgfolder(Path::fromNativeSeparators(Path::getPathFromFilename(settings.exename)) + "cfg");
const std::string details("The Cppcheck binary was compiled without FILESDIR set. Either the " const std::string details("The Cppcheck binary was compiled without FILESDIR set. Either the "
"std.cfg should be available in " + cfgfolder + " or the FILESDIR " "std.cfg should be available in " + cfgfolder + " or the FILESDIR "
"should be configured."); "should be configured.");
@ -1143,9 +1136,9 @@ FILE* CppCheckExecutor::getExceptionOutput()
return mExceptionOutput; return mExceptionOutput;
} }
bool CppCheckExecutor::tryLoadLibrary(Library& destination, const char* basepath, const char* filename) bool CppCheckExecutor::tryLoadLibrary(Library& destination, const std::string& basepath, const char* filename)
{ {
const Library::Error err = destination.load(basepath, filename); const Library::Error err = destination.load(basepath.c_str(), filename);
if (err.errorcode == Library::ErrorCode::UNKNOWN_ELEMENT) if (err.errorcode == Library::ErrorCode::UNKNOWN_ELEMENT)
std::cout << "cppcheck: Found unknown elements in configuration file '" << filename << "': " << err.reason << std::endl; std::cout << "cppcheck: Found unknown elements in configuration file '" << filename << "': " << err.reason << std::endl;

View File

@ -111,7 +111,7 @@ public:
* Tries to load a library and prints warning/error messages * Tries to load a library and prints warning/error messages
* @return false, if an error occurred (except unknown XML elements) * @return false, if an error occurred (except unknown XML elements)
*/ */
static bool tryLoadLibrary(Library& destination, const char* basepath, const char* filename); static bool tryLoadLibrary(Library& destination, const std::string& basepath, const char* filename);
/** /**
* Execute a shell command and read the output from it. Returns true if command terminated successfully. * Execute a shell command and read the output from it. Returns true if command terminated successfully.
@ -150,24 +150,20 @@ private:
* - installs optional platform dependent signal handling * - installs optional platform dependent signal handling
* *
* @param cppcheck cppcheck instance * @param cppcheck cppcheck instance
* @param argc from main()
* @param argv from main()
**/ **/
int check_wrapper(CppCheck& cppcheck, int argc, const char* const argv[]); int check_wrapper(CppCheck& cppcheck);
/** /**
* Starts the checking. * Starts the checking.
* *
* @param cppcheck cppcheck instance * @param cppcheck cppcheck instance
* @param argc from main()
* @param argv from main()
* @return EXIT_FAILURE if arguments are invalid or no input files * @return EXIT_FAILURE if arguments are invalid or no input files
* were found. * were found.
* If errors are found and --error-exitcode is used, * If errors are found and --error-exitcode is used,
* given value is returned instead of default 0. * given value is returned instead of default 0.
* If no errors are found, 0 is returned. * If no errors are found, 0 is returned.
*/ */
int check_internal(CppCheck& cppcheck, int argc, const char* const argv[]); int check_internal(CppCheck& cppcheck);
/** /**
* Pointer to current settings; set while check() is running. * Pointer to current settings; set while check() is running.

View File

@ -511,8 +511,6 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLibrar
checkSettings.checkLibrary = checkLibrary; checkSettings.checkLibrary = checkLibrary;
checkSettings.checkConfiguration = checkConfiguration; checkSettings.checkConfiguration = checkConfiguration;
checkSettings.loadCppcheckCfg(QCoreApplication::applicationFilePath().toStdString());
if (mProjectFile) if (mProjectFile)
qDebug() << "Checking project file" << mProjectFile->getFilename(); qDebug() << "Checking project file" << mProjectFile->getFilename();
@ -857,15 +855,10 @@ Settings MainWindow::getCppcheckSettings()
result.exename = QCoreApplication::applicationFilePath().toStdString(); result.exename = QCoreApplication::applicationFilePath().toStdString();
const bool std = tryLoadLibrary(&result.library, "std.cfg"); const bool std = tryLoadLibrary(&result.library, "std.cfg");
bool posix = true; if (!std)
if (result.posix()) QMessageBox::critical(this, tr("Error"), tr("Failed to load %1. Your Cppcheck installation is broken. You can use --data-dir=<directory> at the command line to specify where this file is located. Please note that --data-dir is supposed to be used by installation scripts and therefore the GUI does not start when it is used, all that happens is that the setting is configured.").arg("std.cfg"));
posix = tryLoadLibrary(&result.library, "posix.cfg");
bool windows = true;
if (result.isWindowsPlatform())
windows = tryLoadLibrary(&result.library, "windows.cfg");
if (!std || !posix || !windows) result.loadCppcheckCfg();
QMessageBox::critical(this, tr("Error"), tr("Failed to load %1. Your Cppcheck installation is broken. You can use --data-dir=<directory> at the command line to specify where this file is located. Please note that --data-dir is supposed to be used by installation scripts and therefore the GUI does not start when it is used, all that happens is that the setting is configured.").arg(!std ? "std.cfg" : !posix ? "posix.cfg" : "windows.cfg"));
// If project file loaded, read settings from it // If project file loaded, read settings from it
if (mProjectFile) { if (mProjectFile) {

View File

@ -78,9 +78,9 @@ Settings::Settings()
certainty.setEnabled(Certainty::normal, true); certainty.setEnabled(Certainty::normal, true);
} }
void Settings::loadCppcheckCfg(const std::string &executable) void Settings::loadCppcheckCfg()
{ {
std::string fileName = Path::getPathFromFilename(executable) + "cppcheck.cfg"; std::string fileName = Path::getPathFromFilename(exename) + "cppcheck.cfg";
#ifdef FILESDIR #ifdef FILESDIR
if (Path::fileExists(FILESDIR "/cppcheck.cfg")) if (Path::fileExists(FILESDIR "/cppcheck.cfg"))
fileName = FILESDIR "/cppcheck.cfg"; fileName = FILESDIR "/cppcheck.cfg";

View File

@ -97,7 +97,7 @@ private:
public: public:
Settings(); Settings();
void loadCppcheckCfg(const std::string &executable); void loadCppcheckCfg();
/** @brief addons, either filename of python/json file or json data */ /** @brief addons, either filename of python/json file or json data */
std::list<std::string> addons; std::list<std::string> addons;

View File

@ -70,9 +70,9 @@ ${CPPCHECK} ${CPPCHECK_OPT} --inconclusive ${DIR}std.cpp
# windows.cpp # windows.cpp
# Syntax check via g++ does not work because it can not find a valid windows.h # Syntax check via g++ does not work because it can not find a valid windows.h
#${CXX} ${CXX_OPT} ${DIR}windows.cpp #${CXX} ${CXX_OPT} ${DIR}windows.cpp
${CPPCHECK} ${CPPCHECK_OPT} --inconclusive --platform=win32A ${DIR}windows.cpp ${CPPCHECK} ${CPPCHECK_OPT} --inconclusive --platform=win32A --library=windows ${DIR}windows.cpp
${CPPCHECK} ${CPPCHECK_OPT} --inconclusive --platform=win32W ${DIR}windows.cpp ${CPPCHECK} ${CPPCHECK_OPT} --inconclusive --platform=win32W --library=windows ${DIR}windows.cpp
${CPPCHECK} ${CPPCHECK_OPT} --inconclusive --platform=win64 ${DIR}windows.cpp ${CPPCHECK} ${CPPCHECK_OPT} --inconclusive --platform=win64 --library=windows ${DIR}windows.cpp
# wxwidgets.cpp # wxwidgets.cpp
set +e set +e