From 780bd7e63e1f9412d5ec0bb93b5cf9e975fe6bef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 9 Feb 2018 16:22:22 +0100 Subject: [PATCH] More flexible loading of platform files, when using --platform it should not be necessary to provide the full path --- cli/cmdlineparser.cpp | 2 +- lib/platform.cpp | 34 +++++++++++++++++++++++++++++----- lib/platform.h | 2 +- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index ee2249484..e27c5d38f 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -714,7 +714,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) _settings->platform(Settings::Native); else if (platform == "unspecified") _settings->platform(Settings::Unspecified); - else if (!_settings->platformFile(platform)) { + else if (!_settings->platformFile(argv[0], platform)) { std::string message("cppcheck: error: unrecognized platform: \""); message += platform; message += "\"."; diff --git a/lib/platform.cpp b/lib/platform.cpp index e2aab3632..551671a32 100644 --- a/lib/platform.cpp +++ b/lib/platform.cpp @@ -17,12 +17,13 @@ */ #include "platform.h" - +#include "path.h" #include "tinyxml2.h" - #include #include #include +#include +#include cppcheck::Platform::Platform() { @@ -154,12 +155,35 @@ bool cppcheck::Platform::platform(cppcheck::Platform::PlatformType type) return false; } -bool cppcheck::Platform::platformFile(const std::string &filename) +bool cppcheck::Platform::platformFile(const char exename[], const std::string &filename) { // open file.. tinyxml2::XMLDocument doc; - if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS) - return false; + if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS) { + std::vector filenames; + filenames.push_back(filename + ".xml"); + if (exename && strchr(exename, '/')) { + filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename); + filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename + ".xml"); + } +#ifdef CFGDIR + std::string cfgdir = CFGDIR; + if (cfgdir[cfgdir.size()-1] != '/') + cfgdir += '/'; + filenames.push_back(CFG_DIR + ("../platforms/" + filename)); + filenames.push_back(CFG_DIR + ("../platforms/" + filename + ".xml")); +#endif + bool success = false; + for (int i = 0; i < filenames.size(); ++i) { + std::cout << "platform:" << filenames[i] << std::endl; + if (doc.LoadFile(filenames[i].c_str()) == tinyxml2::XML_SUCCESS) { + success = true; + break; + } + } + if (!success) + return false; + } const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement(); diff --git a/lib/platform.h b/lib/platform.h index c95b9d1e0..42707cf37 100644 --- a/lib/platform.h +++ b/lib/platform.h @@ -97,7 +97,7 @@ namespace cppcheck { bool platform(PlatformType type); /** set the platform type for user specified platforms */ - bool platformFile(const std::string &filename); + bool platformFile(const char exename[], const std::string &filename); /** * @brief Returns true if platform type is Windows