Skip to content
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

[P4fmt]: Add comment printing capability to P4Formatter #4887

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: resolve issue with modifying container during iteration
Signed-off-by: Nitish <snapdgnn@proton.me>
  • Loading branch information
snapdgn committed Aug 26, 2024
commit e9f53dc63cce64f1b41fcdc8767e57244f0a1e56
47 changes: 22 additions & 25 deletions backends/p4fmt/attach.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ bool Attach::isSystemFile(const std::filesystem::path &file) {
const Attach::CommentsMap &Attach::getCommentsMap() const { return commentsMap; }

const IR::Node *Attach::attachCommentsToNode(IR::Node *node, TraversalType ttype) {
if (node == nullptr || !node->srcInfo.isValid() || clist.empty()) {
if (node == nullptr || !node->srcInfo.isValid() || processedComments.empty()) {
return node;
}

std::filesystem::path sourceFile(node->srcInfo.getSourceFile().c_str());
if (isSystemFile(sourceFile)) {
// Skip attachment for system files
Expand All @@ -34,37 +35,33 @@ const IR::Node *Attach::attachCommentsToNode(IR::Node *node, TraversalType ttype

const auto nodeStart = node->srcInfo.getStart();

auto itr = clist.begin();
while (itr != clist.end()) {
const Util::Comment *comment = *itr;
const auto &commentEnd = comment->getEndPosition();
for (auto &[comment, isVisited] : processedComments) {
// Skip if already attached
if (isVisited) {
continue;
}

bool shouldRemove = false;
const auto &commentEnd = comment->getEndPosition();

switch (ttype) {
{
case TraversalType::Preorder:
if (commentEnd.getLineNumber() < nodeStart.getLineNumber()) {
addPrefixComments(node->id, comment);
shouldRemove = true;
}
break;
}

{
case TraversalType::Postorder:
if (commentEnd.getLineNumber() == nodeStart.getLineNumber()) {
addSuffixComments(node->id, comment);
shouldRemove = true;
}
break;
}
case TraversalType::Preorder:
if (commentEnd.getLineNumber() == nodeStart.getLineNumber() - 1) {
addPrefixComments(node->id, comment);
isVisited = true; // Mark the comment as attached
}
break;

case TraversalType::Postorder:
if (commentEnd.getLineNumber() == nodeStart.getLineNumber()) {
addSuffixComments(node->id, comment);
isVisited = true;
}
break;

default:
::P4::error(ErrorType::ERR_INVALID, "traversal type unknown/unsupported.");
return node;
}

itr = shouldRemove ? clist.erase(itr) : std::next(itr);
}

return node;
Expand Down
5 changes: 3 additions & 2 deletions backends/p4fmt/attach.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Attach : public Transform {
using CommentsMap = std::unordered_map<NodeId, Comments>;
enum class TraversalType { Preorder, Postorder };

explicit Attach(const std::unordered_set<const Util::Comment *> &clist) : clist(clist){};
explicit Attach(const std::unordered_map <const Util::Comment *, bool> &processedComments)
: processedComments(processedComments){};
~Attach() override;

const IR::Node *attachCommentsToNode(IR::Node *, TraversalType);
Expand All @@ -40,7 +41,7 @@ class Attach : public Transform {
const CommentsMap &getCommentsMap() const;

private:
std::unordered_set<const Util::Comment *> clist;
std::unordered_map<const Util::Comment *, bool> processedComments;
CommentsMap commentsMap;
};

Expand Down
12 changes: 8 additions & 4 deletions backends/p4fmt/p4fmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ std::stringstream getFormattedOutput(std::filesystem::path inputFile) {
return formattedOutput;
}

std::unordered_set<const Util::Comment *> globalCommentsList;
// get the global list of comments
std::unordered_map<const Util::Comment *, bool> globalCommentsMap;

// Initialize the global comments map from the list of comments in the program.
// The map associates each comment with a boolean value that tracks whether the comment
// has been processed for attachment to IR nodes. Initially, all comments are set to 'false',
// indicating that they have not yet been processed.
if (!program->objects.empty()) {
const auto *firstNode = program->objects.front();
if (firstNode->srcInfo.isValid()) {
for (const auto *comment : firstNode->srcInfo.getAllFileComments()) {
globalCommentsList.insert(comment);
globalCommentsMap[comment] = false; // Initialize all comments as not visited
}
}
}

auto top4 = P4Fmt::P4Formatter(&formattedOutput);
auto attach = P4::P4Fmt::Attach(globalCommentsList);
auto attach = P4::P4Fmt::Attach(globalCommentsMap);
program = program->apply(attach);
// Print the program before running front end passes.
program->apply(top4);
Expand Down
Loading