Skip to content

Commit cdc3f03

Browse files
authored
fixed #13990/#13991 - reworked platform lookup (danmar#7639)
- provide a list of paths to look into to the platform loading - look relative to project file first (fixes #13990) - do not look into CWD for each path provided (fixes #13991)
1 parent 36dbff5 commit cdc3f03

File tree

6 files changed

+116
-114
lines changed

6 files changed

+116
-114
lines changed

cli/cmdlineparser.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,10 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
394394
std::string platform;
395395
char defaultSign = '\0';
396396

397-
std::vector<std::string> lookupPaths{argv[0]};
397+
std::vector<std::string> lookupPaths{
398+
Path::getCurrentPath(), // TODO: do we want to look in CWD?
399+
Path::getPathFromFilename(argv[0])
400+
};
398401

399402
bool executorAuto = true;
400403

@@ -1160,7 +1163,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11601163
platform = project.guiProject.platform;
11611164

11621165
// look for external files relative to project first
1163-
lookupPaths.insert(lookupPaths.cbegin(), projectFile);
1166+
lookupPaths.insert(lookupPaths.cbegin(), Path::getPathFromFilename(projectFile));
11641167

11651168
const auto& projectFileGui = project.guiProject.projectFile;
11661169
if (!projectFileGui.empty()) {

gui/mainwindow.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,11 @@ bool MainWindow::getCppcheckSettings(Settings& settings, Suppressions& supprs)
11541154

11551155
const QString platform = mProjectFile->getPlatform();
11561156
if (platform.endsWith(".xml")) {
1157-
const QString applicationFilePath = QCoreApplication::applicationFilePath();
1158-
settings.platform.loadFromFile(applicationFilePath.toStdString().c_str(), platform.toStdString());
1157+
const std::vector<std::string> paths = {
1158+
Path::getCurrentPath(), // TODO: do we want to look in CWD?
1159+
QCoreApplication::applicationFilePath().toStdString(),
1160+
};
1161+
settings.platform.loadFromFile(paths, platform.toStdString());
11591162
} else {
11601163
for (int i = Platform::Type::Native; i <= Platform::Type::Unix64; i++) {
11611164
const auto p = static_cast<Platform::Type>(i);

gui/projectfiledialog.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,12 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, bool premium, QWi
208208
for (const QFileInfo& item : dir.entryInfoList()) {
209209
const QString platformFile = item.fileName();
210210

211+
const std::vector<std::string> paths = {
212+
Path::getCurrentPath(), // TODO: do we want to look in CWD?
213+
applicationFilePath.toStdString(),
214+
};
211215
Platform plat2;
212-
if (!plat2.loadFromFile(applicationFilePath.toStdString().c_str(), platformFile.toStdString()))
216+
if (!plat2.loadFromFile(paths, platformFile.toStdString()))
213217
continue;
214218

215219
if (platformFiles.indexOf(platformFile) == -1)

lib/platform.cpp

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -155,27 +155,19 @@ bool Platform::set(const std::string& platformstr, std::string& errstr, const st
155155
errstr = "unrecognized platform: '" + platformstr + "' (no lookup).";
156156
return false;
157157
}
158-
else {
159-
bool found = false;
160-
for (const std::string& path : paths) {
161-
if (debug)
162-
std::cout << "looking for platform '" + platformstr + "' relative to '" + path + "'" << std::endl;
163-
if (loadFromFile(path.c_str(), platformstr, debug)) {
164-
found = true;
165-
break;
166-
}
167-
}
168-
if (!found) {
169-
errstr = "unrecognized platform: '" + platformstr + "'.";
170-
return false;
171-
}
158+
else if (!loadFromFile(paths, platformstr, debug)) {
159+
errstr = "unrecognized platform: '" + platformstr + "'.";
160+
return false;
172161
}
173162

174163
return true;
175164
}
176165

177-
bool Platform::loadFromFile(const char exename[], const std::string &filename, bool debug)
166+
bool Platform::loadFromFile(const std::vector<std::string>& paths, const std::string &filename, bool debug)
178167
{
168+
if (debug)
169+
std::cout << "looking for platform '" + filename + "'" << std::endl;
170+
179171
const bool is_abs_path = Path::isAbsolute(filename);
180172

181173
std::string fullfilename(filename);
@@ -185,20 +177,33 @@ bool Platform::loadFromFile(const char exename[], const std::string &filename, b
185177
fullfilename += ".xml";
186178

187179
// TODO: use native separators
188-
std::vector<std::string> filenames{
189-
fullfilename,
190-
};
191-
if (!is_abs_path) {
192-
filenames.push_back("platforms/" + fullfilename);
193-
if (exename && (std::string::npos != Path::fromNativeSeparators(exename).find('/'))) {
194-
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + fullfilename);
195-
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + fullfilename);
180+
std::vector<std::string> filenames;
181+
if (is_abs_path)
182+
{
183+
filenames.push_back(fullfilename);
184+
}
185+
else {
186+
// TODO: drop duplicated paths
187+
for (const std::string& path : paths)
188+
{
189+
if (path.empty())
190+
continue; // TODO: error out instead?
191+
192+
std::string ppath = Path::fromNativeSeparators(path);
193+
if (ppath.back() != '/')
194+
ppath += '/';
195+
// TODO: look in platforms first?
196+
filenames.push_back(ppath + fullfilename);
197+
filenames.push_back(ppath + "platforms/" + fullfilename);
196198
}
197199
#ifdef FILESDIR
198200
std::string filesdir = FILESDIR;
199-
if (!filesdir.empty() && filesdir[filesdir.size()-1] != '/')
200-
filesdir += '/';
201-
filenames.push_back(filesdir + ("platforms/" + fullfilename));
201+
if (!filesdir.empty()) {
202+
if (filesdir.back() != '/')
203+
filesdir += '/';
204+
// TODO: look in filesdir?
205+
filenames.push_back(filesdir + "platforms/" + fullfilename);
206+
}
202207
#endif
203208
}
204209

lib/platform.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace tinyxml2 {
4444
* @brief Platform settings
4545
*/
4646
class CPPCHECKLIB Platform {
47+
friend class TestPlatform;
4748
private:
4849
static long long min_value(std::uint8_t bit) {
4950
assert(bit > 0);
@@ -78,6 +79,9 @@ class CPPCHECKLIB Platform {
7879

7980
/** provides list of defines specified by the limit.h/climits includes */
8081
std::string getLimitsDefines(bool c99) const;
82+
83+
/** load platform from xml document, primarily for testing */
84+
bool loadFromXmlDocument(const tinyxml2::XMLDocument *doc);
8185
public:
8286
Platform();
8387

@@ -150,15 +154,13 @@ class CPPCHECKLIB Platform {
150154

151155
/**
152156
* load platform file
153-
* @param exename application path
157+
* @param paths the additional paths to look into
154158
* @param filename platform filename
155159
* @param debug log verbose information about the lookup
156160
* @return returns true if file was loaded successfully
157161
*/
158-
bool loadFromFile(const char exename[], const std::string &filename, bool debug = false);
162+
bool loadFromFile(const std::vector<std::string>& paths, const std::string &filename, bool debug = false);
159163

160-
/** load platform from xml document, primarily for testing */
161-
bool loadFromXmlDocument(const tinyxml2::XMLDocument *doc);
162164

163165
/**
164166
* @brief Returns true if platform type is Windows

0 commit comments

Comments
 (0)