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
add support for control blocks, property and action blocks
Signed-off-by: Nitish <snapdgnn@proton.me>
  • Loading branch information
snapdgn committed Aug 27, 2024
commit e40857586a3178c030f0c951facfb4b5cc2359c0
25 changes: 24 additions & 1 deletion backends/p4fmt/p4formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ std::pair<cstring, cstring> P4Formatter::extractNodeComments(int nodeId) {
auto commsIt = comMap.find(nodeId);
if (commsIt != comMap.end()) {
const Attach::Comments &comms = commsIt->second;
// Since we have only one prefix & suffix comment per node for now
cstring prefixComm = comms.prefix.empty() ? cstring("") : comms.prefix.front()->toString();
cstring suffixComm = comms.suffix.empty() ? cstring("") : comms.suffix.front()->toString();
return std::make_pair(prefixComm, suffixComm);
Expand Down Expand Up @@ -479,6 +480,11 @@ bool P4Formatter::preorder(const IR::Type_Control *t) {
visit(t->annotations);
builder.spc();
}
auto [prefixc, suffixc] = extractNodeComments(t->id);
if (*prefixc != 0) {
builder.append(prefixc);
builder.append("\n");
}
builder.append("control ");
builder.append(t->name);
visit(t->typeParameters);
Expand Down Expand Up @@ -1253,6 +1259,12 @@ bool P4Formatter::preorder(const IR::Parameter *p) {
}

bool P4Formatter::preorder(const IR::P4Control *c) {
auto [prefixc, suffixc] = extractNodeComments(c->id);
if (*prefixc != 0) {
builder.append(prefixc);
builder.append("\n");
builder.emitIndent();
}
bool decl = isDeclaration;
isDeclaration = false;
visit(c->type);
Expand All @@ -1265,7 +1277,6 @@ bool P4Formatter::preorder(const IR::P4Control *c) {
visit(s);
builder.newline();
}

builder.emitIndent();
builder.append("apply ");
visit(c->body);
Expand All @@ -1282,6 +1293,12 @@ bool P4Formatter::preorder(const IR::ParameterList *p) {
}

bool P4Formatter::preorder(const IR::P4Action *c) {
auto [prefixc, suffixc] = extractNodeComments(c->id);
if (*prefixc != 0) {
builder.append(prefixc);
builder.append("\n");
builder.emitIndent();
}
if (!c->annotations->annotations.empty()) {
visit(c->annotations);
builder.spc();
Expand Down Expand Up @@ -1404,6 +1421,12 @@ bool P4Formatter::preorder(const IR::Key *v) {
}

bool P4Formatter::preorder(const IR::Property *p) {
auto [prefixc, suffixc] = extractNodeComments(p->id);
if (*prefixc != 0) {
builder.append(prefixc);
builder.append("\n");
builder.emitIndent();
}
if (!p->annotations->annotations.empty()) {
visit(p->annotations);
builder.spc();
Expand Down
2 changes: 0 additions & 2 deletions backends/p4fmt/p4formatter.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef BACKENDS_P4FMT_P4FORMATTER_H_
#define BACKENDS_P4FMT_P4FORMATTER_H_

#include <utility>

#include "backends/p4fmt/attach.h"
#include "frontends/common/resolveReferences/resolveReferences.h"
#include "ir/ir.h"
Expand Down
Loading