Skip to content

fixed #13990/#13991 - reworked platform lookup #7639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,10 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
std::string platform;
char defaultSign = '\0';

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

bool executorAuto = true;

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

// look for external files relative to project first
lookupPaths.insert(lookupPaths.cbegin(), projectFile);
lookupPaths.insert(lookupPaths.cbegin(), Path::getPathFromFilename(projectFile));

const auto& projectFileGui = project.guiProject.projectFile;
if (!projectFileGui.empty()) {
Expand Down
7 changes: 5 additions & 2 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1142,8 +1142,11 @@ bool MainWindow::getCppcheckSettings(Settings& settings, Suppressions& supprs)

const QString platform = mProjectFile->getPlatform();
if (platform.endsWith(".xml")) {
const QString applicationFilePath = QCoreApplication::applicationFilePath();
settings.platform.loadFromFile(applicationFilePath.toStdString().c_str(), platform.toStdString());
const std::vector<std::string> paths = {
Path::getCurrentPath(), // TODO: do we want to look in CWD?
QCoreApplication::applicationFilePath().toStdString(),
};
settings.platform.loadFromFile(paths, platform.toStdString());
} else {
for (int i = Platform::Type::Native; i <= Platform::Type::Unix64; i++) {
const auto p = static_cast<Platform::Type>(i);
Expand Down
6 changes: 5 additions & 1 deletion gui/projectfiledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,12 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, bool premium, QWi
for (const QFileInfo& item : dir.entryInfoList()) {
const QString platformFile = item.fileName();

const std::vector<std::string> paths = {
Path::getCurrentPath(), // TODO: do we want to look in CWD?
applicationFilePath.toStdString(),
};
Platform plat2;
if (!plat2.loadFromFile(applicationFilePath.toStdString().c_str(), platformFile.toStdString()))
if (!plat2.loadFromFile(paths, platformFile.toStdString()))
continue;

if (platformFiles.indexOf(platformFile) == -1)
Expand Down
57 changes: 31 additions & 26 deletions lib/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,27 +170,19 @@ bool Platform::set(const std::string& platformstr, std::string& errstr, const st
errstr = "unrecognized platform: '" + platformstr + "' (no lookup).";
return false;
}
else {
bool found = false;
for (const std::string& path : paths) {
if (debug)
std::cout << "looking for platform '" + platformstr + "' relative to '" + path + "'" << std::endl;
if (loadFromFile(path.c_str(), platformstr, debug)) {
found = true;
break;
}
}
if (!found) {
errstr = "unrecognized platform: '" + platformstr + "'.";
return false;
}
else if (!loadFromFile(paths, platformstr, debug)) {
errstr = "unrecognized platform: '" + platformstr + "'.";
return false;
}

return true;
}

bool Platform::loadFromFile(const char exename[], const std::string &filename, bool debug)
bool Platform::loadFromFile(const std::vector<std::string>& paths, const std::string &filename, bool debug)
{
if (debug)
std::cout << "looking for platform '" + filename + "'" << std::endl;

const bool is_abs_path = Path::isAbsolute(filename);

std::string fullfilename(filename);
Expand All @@ -200,20 +192,33 @@ bool Platform::loadFromFile(const char exename[], const std::string &filename, b
fullfilename += ".xml";

// TODO: use native separators
std::vector<std::string> filenames{
fullfilename,
};
if (!is_abs_path) {
filenames.push_back("platforms/" + fullfilename);
if (exename && (std::string::npos != Path::fromNativeSeparators(exename).find('/'))) {
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + fullfilename);
filenames.push_back(Path::getPathFromFilename(Path::fromNativeSeparators(exename)) + "platforms/" + fullfilename);
std::vector<std::string> filenames;
if (is_abs_path)
{
filenames.push_back(fullfilename);
}
else {
// TODO: drop duplicated paths
for (const std::string& path : paths)
{
if (path.empty())
continue; // TODO: error out instead?

std::string ppath = Path::fromNativeSeparators(path);
if (ppath.back() != '/')
ppath += '/';
// TODO: look in platforms first?
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the platforms folder is the proper location for these files it is weird that we do not look there first.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filenames.push_back(ppath + fullfilename);
filenames.push_back(ppath + "platforms/" + fullfilename);
}
#ifdef FILESDIR
std::string filesdir = FILESDIR;
if (!filesdir.empty() && filesdir[filesdir.size()-1] != '/')
filesdir += '/';
filenames.push_back(filesdir + ("platforms/" + fullfilename));
if (!filesdir.empty()) {
if (filesdir.back() != '/')
filesdir += '/';
// TODO: look in filesdir?
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we would also look into that folder it could just be one of the lookup paths above.

filenames.push_back(filesdir + "platforms/" + fullfilename);
}
#endif
}

Expand Down
10 changes: 6 additions & 4 deletions lib/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace tinyxml2 {
* @brief Platform settings
*/
class CPPCHECKLIB Platform {
friend class TestPlatform;
private:
static long long min_value(std::uint8_t bit) {
assert(bit > 0);
Expand All @@ -68,6 +69,9 @@ class CPPCHECKLIB Platform {

/** provides list of defines specified by the limit.h/climits includes */
std::string getLimitsDefines(bool c99) const;

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

Expand Down Expand Up @@ -137,15 +141,13 @@ class CPPCHECKLIB Platform {

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

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

/**
* @brief Returns true if platform type is Windows
Expand Down
Loading