Skip to content

Commit de2bd89

Browse files
committed
reworked platform lookup
1 parent dfa16af commit de2bd89

File tree

6 files changed

+98
-109
lines changed

6 files changed

+98
-109
lines changed

cli/cmdlineparser.cpp

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

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

396399
bool executorAuto = true;
397400

@@ -1142,7 +1145,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11421145
platform = project.guiProject.platform;
11431146

11441147
// look for external files relative to project first
1145-
lookupPaths.insert(lookupPaths.cbegin(), projectFile);
1148+
lookupPaths.insert(lookupPaths.cbegin(), Path::getPathFromFilename(projectFile));
11461149

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

gui/mainwindow.cpp

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

11431143
const QString platform = mProjectFile->getPlatform();
11441144
if (platform.endsWith(".xml")) {
1145-
const QString applicationFilePath = QCoreApplication::applicationFilePath();
1146-
settings.platform.loadFromFile(applicationFilePath.toStdString().c_str(), platform.toStdString());
1145+
const std::vector<std::string> paths = {
1146+
Path::getCurrentPath(), // TODO: do we want to look in CWD?
1147+
QCoreApplication::applicationFilePath().toStdString(),
1148+
};
1149+
settings.platform.loadFromFile(paths, platform.toStdString());
11471150
} else {
11481151
for (int i = Platform::Type::Native; i <= Platform::Type::Unix64; i++) {
11491152
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: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -170,27 +170,19 @@ bool Platform::set(const std::string& platformstr, std::string& errstr, const st
170170
errstr = "unrecognized platform: '" + platformstr + "' (no lookup).";
171171
return false;
172172
}
173-
else {
174-
bool found = false;
175-
for (const std::string& path : paths) {
176-
if (debug)
177-
std::cout << "looking for platform '" + platformstr + "' relative to '" + path + "'" << std::endl;
178-
if (loadFromFile(path.c_str(), platformstr, debug)) {
179-
found = true;
180-
break;
181-
}
182-
}
183-
if (!found) {
184-
errstr = "unrecognized platform: '" + platformstr + "'.";
185-
return false;
186-
}
173+
else if (!loadFromFile(paths, platformstr, debug)) {
174+
errstr = "unrecognized platform: '" + platformstr + "'.";
175+
return false;
187176
}
188177

189178
return true;
190179
}
191180

192-
bool Platform::loadFromFile(const char exename[], const std::string &filename, bool debug)
181+
bool Platform::loadFromFile(const std::vector<std::string>& paths, const std::string &filename, bool debug)
193182
{
183+
if (debug)
184+
std::cout << "looking for platform '" + filename + "'" << std::endl;
185+
194186
const bool is_abs_path = Path::isAbsolute(filename);
195187

196188
std::string fullfilename(filename);
@@ -200,18 +192,23 @@ bool Platform::loadFromFile(const char exename[], const std::string &filename, b
200192
fullfilename += ".xml";
201193

202194
// TODO: use native separators
203-
std::vector<std::string> filenames{
204-
fullfilename,
205-
};
206-
if (!is_abs_path) {
207-
filenames.push_back("platforms/" + fullfilename);
208-
if (exename && (std::string::npos != Path::fromNativeSeparators(exename).find('/'))) {
209-
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + fullfilename);
210-
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + fullfilename);
195+
std::vector<std::string> filenames;
196+
if (is_abs_path)
197+
{
198+
filenames.push_back(fullfilename);
199+
}
200+
else {
201+
for (const std::string& path : paths)
202+
{
203+
std::string ppath = Path::fromNativeSeparators(path);
204+
if (ppath.back() != '/')
205+
ppath += '/';
206+
filenames.push_back(ppath + fullfilename);
207+
filenames.push_back(ppath + "platforms/" + fullfilename);
211208
}
212209
#ifdef FILESDIR
213210
std::string filesdir = FILESDIR;
214-
if (!filesdir.empty() && filesdir[filesdir.size()-1] != '/')
211+
if (!filesdir.empty() && filesdir.back() != '/')
215212
filesdir += '/';
216213
filenames.push_back(filesdir + ("platforms/" + fullfilename));
217214
#endif

lib/platform.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ class CPPCHECKLIB Platform {
137137

138138
/**
139139
* load platform file
140-
* @param exename application path
140+
* @param paths the additional paths to look into
141141
* @param filename platform filename
142142
* @param debug log verbose information about the lookup
143143
* @return returns true if file was loaded successfully
144144
*/
145-
bool loadFromFile(const char exename[], const std::string &filename, bool debug = false);
145+
bool loadFromFile(const std::vector<std::string>& paths, const std::string &filename, bool debug = false);
146146

147147
/** load platform from xml document, primarily for testing */
148148
bool loadFromXmlDocument(const tinyxml2::XMLDocument *doc);

0 commit comments

Comments
 (0)