-
Notifications
You must be signed in to change notification settings - Fork 1.5k
generate AddonInfo
only once
#4958
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a9c687f
extracted `AddonInfo` into separate file / minor `AddonInfo` interfac…
firewave 3e2d7e1
fetch addon infos only once and store them in `Settings::addonInfos`
firewave 404e2a0
CppCheckExecutor: report all addon loading failures at once
firewave 23c4863
fixed `functionStatic` selfcheck warning
firewave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Cppcheck - A tool for static C/C++ code analysis | ||
* Copyright (C) 2007-2023 Cppcheck team. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "addoninfo.h" | ||
|
||
#include "path.h" | ||
#include "utils.h" | ||
|
||
#include <fstream> | ||
#include <sstream> | ||
#include <string> | ||
|
||
#include "json.h" | ||
|
||
static std::string getFullPath(const std::string &fileName, const std::string &exename) { | ||
if (Path::isFile(fileName)) | ||
return fileName; | ||
|
||
const std::string exepath = Path::getPathFromFilename(exename); | ||
if (Path::isFile(exepath + fileName)) | ||
return exepath + fileName; | ||
if (Path::isFile(exepath + "addons/" + fileName)) | ||
return exepath + "addons/" + fileName; | ||
|
||
#ifdef FILESDIR | ||
if (Path::isFile(FILESDIR + ("/" + fileName))) | ||
return FILESDIR + ("/" + fileName); | ||
if (Path::isFile(FILESDIR + ("/addons/" + fileName))) | ||
return FILESDIR + ("/addons/" + fileName); | ||
#endif | ||
return ""; | ||
} | ||
|
||
static std::string parseAddonInfo(AddonInfo& addoninfo, const picojson::value &json, const std::string &fileName, const std::string &exename) { | ||
const std::string& json_error = picojson::get_last_error(); | ||
if (!json_error.empty()) { | ||
return "Loading " + fileName + " failed. " + json_error; | ||
} | ||
if (!json.is<picojson::object>()) | ||
return "Loading " + fileName + " failed. Bad json."; | ||
picojson::object obj = json.get<picojson::object>(); | ||
if (obj.count("args")) { | ||
if (!obj["args"].is<picojson::array>()) | ||
return "Loading " + fileName + " failed. args must be array."; | ||
for (const picojson::value &v : obj["args"].get<picojson::array>()) | ||
addoninfo.args += " " + v.get<std::string>(); | ||
} | ||
|
||
if (obj.count("ctu")) { | ||
// ctu is specified in the config file | ||
if (!obj["ctu"].is<bool>()) | ||
return "Loading " + fileName + " failed. ctu must be boolean."; | ||
addoninfo.ctu = obj["ctu"].get<bool>(); | ||
} else { | ||
addoninfo.ctu = false; | ||
} | ||
|
||
if (obj.count("python")) { | ||
// Python was defined in the config file | ||
if (obj["python"].is<picojson::array>()) { | ||
return "Loading " + fileName +" failed. python must not be an array."; | ||
} | ||
addoninfo.python = obj["python"].get<std::string>(); | ||
} else { | ||
addoninfo.python = ""; | ||
} | ||
|
||
if (obj.count("executable")) { | ||
if (!obj["executable"].is<std::string>()) | ||
return "Loading " + fileName + " failed. executable must be a string."; | ||
addoninfo.executable = getFullPath(obj["executable"].get<std::string>(), fileName); | ||
return ""; | ||
} | ||
|
||
return addoninfo.getAddonInfo(obj["script"].get<std::string>(), exename); | ||
} | ||
|
||
std::string AddonInfo::getAddonInfo(const std::string &fileName, const std::string &exename) { | ||
if (fileName[0] == '{') { | ||
picojson::value json; | ||
const std::string err = picojson::parse(json, fileName); | ||
(void)err; // TODO: report | ||
return parseAddonInfo(*this, json, fileName, exename); | ||
} | ||
if (fileName.find('.') == std::string::npos) | ||
return getAddonInfo(fileName + ".py", exename); | ||
|
||
if (endsWith(fileName, ".py")) { | ||
scriptFile = Path::fromNativeSeparators(getFullPath(fileName, exename)); | ||
if (scriptFile.empty()) | ||
return "Did not find addon " + fileName; | ||
|
||
std::string::size_type pos1 = scriptFile.rfind('/'); | ||
if (pos1 == std::string::npos) | ||
pos1 = 0; | ||
else | ||
pos1++; | ||
std::string::size_type pos2 = scriptFile.rfind('.'); | ||
if (pos2 < pos1) | ||
pos2 = std::string::npos; | ||
name = scriptFile.substr(pos1, pos2 - pos1); | ||
|
||
runScript = getFullPath("runaddon.py", exename); | ||
|
||
return ""; | ||
} | ||
|
||
if (!endsWith(fileName, ".json")) | ||
return "Failed to open addon " + fileName; | ||
|
||
std::ifstream fin(fileName); | ||
if (!fin.is_open()) | ||
return "Failed to open " + fileName; | ||
picojson::value json; | ||
fin >> json; | ||
return parseAddonInfo(*this, json, fileName, exename); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Cppcheck - A tool for static C/C++ code analysis | ||
* Copyright (C) 2007-2023 Cppcheck team. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef addonInfoH | ||
#define addonInfoH | ||
|
||
#include "config.h" | ||
|
||
#include <string> | ||
|
||
struct CPPCHECKLIB AddonInfo { | ||
std::string name; | ||
std::string scriptFile; // addon script | ||
std::string executable; // addon executable | ||
std::string args; // special extra arguments | ||
std::string python; // script interpreter | ||
bool ctu = false; | ||
std::string runScript; | ||
|
||
std::string getAddonInfo(const std::string &fileName, const std::string &exename); | ||
}; | ||
|
||
#endif // addonInfoH |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have the feeling this should say
if (!obj["python"].is<std::string>())
instead .. I have no idea why the old code checked for the array instead :-(There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Will have a look and add some tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code made sure it is not an array. So that is fine albeit a rather specific handling. Will still adjust though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This requires a lot of new tests to be written. I will do this in a follow-up PR so this can be merged.