-
Notifications
You must be signed in to change notification settings - Fork 10
feat(tdl): Add DetectStructCircularDependency analysis pass.
#237
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
LinZhihao-723
wants to merge
4
commits into
y-scope:main
Choose a base branch
from
LinZhihao-723:circular-dependency
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
85 changes: 85 additions & 0 deletions
85
src/spider/tdl/pass/analysis/DetectStructCircularDependency.cpp
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,85 @@ | ||
| #include "DetectStructCircularDependency.hpp" | ||
|
|
||
| #include <algorithm> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <boost/outcome/std_result.hpp> | ||
| #include <boost/outcome/success_failure.hpp> | ||
| #include <fmt/format.h> | ||
| #include <fmt/ranges.h> | ||
|
|
||
| #include <spider/tdl/parser/ast/nodes.hpp> | ||
| #include <spider/tdl/pass/Pass.hpp> | ||
|
|
||
| namespace spider::tdl::pass::analysis { | ||
| auto DetectStructCircularDependency::Error::to_string() const -> std::string { | ||
| std::vector<std::string> circular_dependency_group_error_messages; | ||
| circular_dependency_group_error_messages.reserve(m_strongly_connected_components.size()); | ||
| for (auto const& group : m_strongly_connected_components) { | ||
| std::vector<std::string> struct_descriptions; | ||
| struct_descriptions.reserve(group.size()); | ||
| for (auto const& struct_spec : group) { | ||
| struct_descriptions.emplace_back( | ||
| fmt::format( | ||
| " `{}` at {}", | ||
| struct_spec->get_name(), | ||
| struct_spec->get_source_location().serialize_to_str() | ||
| ) | ||
| ); | ||
| } | ||
| circular_dependency_group_error_messages.emplace_back( | ||
| fmt::format( | ||
| "Found a circular dependency group of {} struct spec(s):\n{}", | ||
| group.size(), | ||
| fmt::join(struct_descriptions, "\n") | ||
| ) | ||
| ); | ||
| } | ||
| return fmt::format( | ||
| "Found {} circular dependency group(s):\n{}", | ||
| m_strongly_connected_components.size(), | ||
| fmt::join(circular_dependency_group_error_messages, "\n") | ||
| ); | ||
| } | ||
|
|
||
| auto DetectStructCircularDependency::run() | ||
| -> boost::outcome_v2::std_checked<void, std::unique_ptr<Pass::Error>> { | ||
| auto const& strongly_connected_components{ | ||
| m_struct_spec_dependency_graph->get_strongly_connected_components() | ||
| }; | ||
| if (strongly_connected_components.empty()) { | ||
| return boost::outcome_v2::success(); | ||
| } | ||
|
|
||
| std::vector<std::vector<std::shared_ptr<parser::ast::StructSpec const>>> | ||
| circular_dependency_groups; | ||
| circular_dependency_groups.reserve(strongly_connected_components.size()); | ||
| for (auto const& scc : strongly_connected_components) { | ||
| std::vector<std::shared_ptr<parser::ast::StructSpec const>> group; | ||
| group.reserve(scc.size()); | ||
| for (auto const id : scc) { | ||
| group.emplace_back(m_struct_spec_dependency_graph->get_struct_spec_from_id(id)); | ||
| } | ||
| std::ranges::sort(group, [](auto const& lhs, auto const& rhs) -> bool { | ||
| return lhs->get_source_location() < rhs->get_source_location(); | ||
| }); | ||
| circular_dependency_groups.emplace_back(std::move(group)); | ||
| } | ||
|
|
||
| std::ranges::sort(circular_dependency_groups, [](auto const& lhs, auto const& rhs) -> bool { | ||
| // Compare by the source location of the first struct spec in each group. This is safe | ||
| // because: | ||
| // - Each group is guaranteed to be non-empty. | ||
| // - Each struct spec should only appear in one SCC, which guarantees the source locations | ||
| // are unique. | ||
| return lhs.front()->get_source_location() < rhs.front()->get_source_location(); | ||
| }); | ||
|
|
||
| return boost::outcome_v2::failure( | ||
| std::make_unique<Error>(std::move(circular_dependency_groups)) | ||
| ); | ||
| } | ||
| } // namespace spider::tdl::pass::analysis | ||
63 changes: 63 additions & 0 deletions
63
src/spider/tdl/pass/analysis/DetectStructCircularDependency.hpp
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,63 @@ | ||
| #ifndef SPIDER_TDL_PASS_ANALYSIS_DETECTSTRUCTCIRCULARDEPENDENCY_HPP | ||
| #define SPIDER_TDL_PASS_ANALYSIS_DETECTSTRUCTCIRCULARDEPENDENCY_HPP | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <boost/outcome/std_result.hpp> | ||
|
|
||
| #include <spider/tdl/parser/ast/nodes.hpp> | ||
| #include <spider/tdl/pass/analysis/StructSpecDependencyGraph.hpp> | ||
| #include <spider/tdl/pass/Pass.hpp> | ||
|
|
||
| namespace spider::tdl::pass::analysis { | ||
| /** | ||
| * Wrapper of `StructSpecDependencyGraph` to detect circular dependencies among struct specs. | ||
| */ | ||
| class DetectStructCircularDependency : public Pass { | ||
| public: | ||
| // Types | ||
| /** | ||
| * Represents an error including all circular dependency groups (reported as strongly connected | ||
| * components). | ||
| */ | ||
| class Error : public Pass::Error { | ||
| public: | ||
| // Constructor | ||
| explicit Error( | ||
| std::vector<std::vector<std::shared_ptr<parser::ast::StructSpec const>>> | ||
| strongly_connected_components | ||
| ) | ||
| : m_strongly_connected_components{std::move(strongly_connected_components)} {} | ||
|
|
||
| // Methods implementing `Pass::Error` | ||
| [[nodiscard]] auto to_string() const -> std::string override; | ||
|
|
||
| private: | ||
| // Variables | ||
| std::vector<std::vector<std::shared_ptr<parser::ast::StructSpec const>>> | ||
| m_strongly_connected_components; | ||
| }; | ||
|
|
||
| // Constructor | ||
| explicit DetectStructCircularDependency( | ||
| std::shared_ptr<StructSpecDependencyGraph> struct_spec_dependency_graph | ||
| ) | ||
| : m_struct_spec_dependency_graph{std::move(struct_spec_dependency_graph)} {} | ||
|
|
||
| // Methods implementing `Pass` | ||
| /** | ||
| * @return A void result on success, or a pointer to `DetectStructCircularDependency::Error` | ||
| * on failure. | ||
| */ | ||
| [[nodiscard]] auto run() | ||
| -> boost::outcome_v2::std_checked<void, std::unique_ptr<Pass::Error>> override; | ||
|
|
||
| private: | ||
| std::shared_ptr<StructSpecDependencyGraph> m_struct_spec_dependency_graph; | ||
| }; | ||
| } // namespace spider::tdl::pass::analysis | ||
|
|
||
| #endif // SPIDER_TDL_PASS_ANALYSIS_DETECTSTRUCTCIRCULARDEPENDENCY_HPP |
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
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.
The comment here is a little confusing. How does uniqueness have to do with the safety of the sort?
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.
Yeah sorry my bad. I think instead of "is safe", I should say "is deterministic and non-ambiguous"