3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2026-07-14 19:25:40 +00:00

Fixup help contents source_file

Stop empty source files `""` from being exported to json.
Move source path fixing to a separate function.
`ContentListing::to_json` now takes the source root, recursively calling the source path fixup function on all source paths in the content listing.
This commit is contained in:
Krystine Sherwin 2026-07-09 16:05:15 +12:00
parent 0e763f320b
commit 53d1e7615b
No known key found for this signature in database
3 changed files with 46 additions and 21 deletions

View file

@ -21,16 +21,45 @@
USING_YOSYS_NAMESPACE
Json ContentListing::to_json() const {
std::filesystem::path Yosys::fixup_source_path (const char* source_file,
const std::filesystem::path &source_root, bool* skip_source_group)
{
std::filesystem::path source_path(source_file);
if (source_path.is_absolute()) {
// using proximate instead of relative means that we
// still get the source path if they aren't relative
auto proximate_path = std::filesystem::proximate(source_path, source_root);
if (proximate_path == proximate_path.lexically_normal()) {
// we're only interested if it's a subpath of our root dir
// if the normal form differs, then the proximate path includes ".."
return proximate_path;
} else if (skip_source_group != nullptr) {
// don't try to group external paths
*skip_source_group = true;
}
}
return source_path;
}
Json ContentListing::to_json(const std::filesystem::path &source_root) const {
Json::object object;
object["type"] = type;
if (body.length()) object["body"] = body;
if (strcmp(source_file, "unknown") != 0) object["source_file"] = source_file;
if (source_line != 0) object["source_line"] = source_line;
if (body.length())
object["body"] = body;
if (has_source())
object["source_file"] = fixup_source_path(source_file, source_root).string();
if (source_line != 0)
object["source_line"] = source_line;
object["options"] = Json(options);
Json::array content_array;
for (auto child : _content) content_array.push_back(child.to_json());
for (auto child : _content)
content_array.push_back(child.to_json(source_root));
object["content"] = content_array;
return object;
}