Skip to content

Commit 780bd7e

Browse files
committed
More flexible loading of platform files, when using --platform it should not be necessary to provide the full path
1 parent 8e6ac60 commit 780bd7e

3 files changed

Lines changed: 31 additions & 7 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
714714
_settings->platform(Settings::Native);
715715
else if (platform == "unspecified")
716716
_settings->platform(Settings::Unspecified);
717-
else if (!_settings->platformFile(platform)) {
717+
else if (!_settings->platformFile(argv[0], platform)) {
718718
std::string message("cppcheck: error: unrecognized platform: \"");
719719
message += platform;
720720
message += "\".";

lib/platform.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
*/
1818

1919
#include "platform.h"
20-
20+
#include "path.h"
2121
#include "tinyxml2.h"
22-
2322
#include <cstdlib>
2423
#include <cstring>
2524
#include <limits>
25+
#include <vector>
26+
#include <iostream>
2627

2728
cppcheck::Platform::Platform()
2829
{
@@ -154,12 +155,35 @@ bool cppcheck::Platform::platform(cppcheck::Platform::PlatformType type)
154155
return false;
155156
}
156157

157-
bool cppcheck::Platform::platformFile(const std::string &filename)
158+
bool cppcheck::Platform::platformFile(const char exename[], const std::string &filename)
158159
{
159160
// open file..
160161
tinyxml2::XMLDocument doc;
161-
if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS)
162-
return false;
162+
if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS) {
163+
std::vector<std::string> filenames;
164+
filenames.push_back(filename + ".xml");
165+
if (exename && strchr(exename, '/')) {
166+
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename);
167+
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + filename + ".xml");
168+
}
169+
#ifdef CFGDIR
170+
std::string cfgdir = CFGDIR;
171+
if (cfgdir[cfgdir.size()-1] != '/')
172+
cfgdir += '/';
173+
filenames.push_back(CFG_DIR + ("../platforms/" + filename));
174+
filenames.push_back(CFG_DIR + ("../platforms/" + filename + ".xml"));
175+
#endif
176+
bool success = false;
177+
for (int i = 0; i < filenames.size(); ++i) {
178+
std::cout << "platform:" << filenames[i] << std::endl;
179+
if (doc.LoadFile(filenames[i].c_str()) == tinyxml2::XML_SUCCESS) {
180+
success = true;
181+
break;
182+
}
183+
}
184+
if (!success)
185+
return false;
186+
}
163187

164188
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
165189

lib/platform.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ namespace cppcheck {
9797
bool platform(PlatformType type);
9898

9999
/** set the platform type for user specified platforms */
100-
bool platformFile(const std::string &filename);
100+
bool platformFile(const char exename[], const std::string &filename);
101101

102102
/**
103103
* @brief Returns true if platform type is Windows

0 commit comments

Comments
 (0)