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
[WIP]: comment formatting for structs and typedefs
Signed-off-by: Nitish <snapdgnn@proton.me>
  • Loading branch information
snapdgn committed Aug 27, 2024
commit 59ff8479cf0926bceee176d2498ee0e6de5ae628
2 changes: 1 addition & 1 deletion backends/p4fmt/p4fmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ std::stringstream getFormattedOutput(std::filesystem::path inputFile) {
}
}

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

if (::P4::errorCount() > 0) {
Expand Down
33 changes: 30 additions & 3 deletions backends/p4fmt/p4formatter.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include "p4formatter.h"

#include <deque>
#include <sstream>
#include <string>

#include "frontends/common/options.h"
#include "frontends/p4/fromv1.0/v1model.h"
#include "frontends/parsers/p4/p4parser.hpp"
#include "ir/dump.h"
Expand Down Expand Up @@ -43,6 +41,18 @@ std::filesystem::path P4Formatter::ifSystemFile(const IR::Node *node) {
return {};
}

std::pair<cstring, cstring> P4Formatter::extractNodeComments(int nodeId) {
auto commsIt = comMap.find(nodeId);
if (commsIt != comMap.end()) {
const Attach::Comments &comms = commsIt->second;
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);
}
// Return empty strings if node ID not found
return {cstring(""), cstring("")};
}

bool P4Formatter::preorder(const IR::Node *node) {
P4C_UNIMPLEMENTED("Unhandled IR node type: ", node->node_type_name());
return false;
Expand Down Expand Up @@ -174,6 +184,11 @@ bool P4Formatter::preorder(const IR::Argument *arg) {
}

bool P4Formatter::preorder(const IR::Type_Typedef *t) {
auto [prefixc, suffixc] = extractNodeComments(t->id);
if (*prefixc != 0) {
builder.append(prefixc);
builder.append("\n");
}
if (!t->annotations->annotations.empty()) {
visit(t->annotations);
builder.spc();
Expand Down Expand Up @@ -386,6 +401,10 @@ bool P4Formatter::preorder(const IR::Type_Package *package) {
}

bool P4Formatter::process(const IR::Type_StructLike *t, const char *name) {
auto [prefixc, suffixc] = extractNodeComments(t->id);

builder.append(prefixc);
builder.append("\n");
if (isDeclaration) {
builder.emitIndent();
if (!t->annotations->annotations.empty()) {
Expand Down Expand Up @@ -413,6 +432,13 @@ bool P4Formatter::process(const IR::Type_StructLike *t, const char *name) {
}

for (auto f : t->fields) {
auto [fieldPrefixc, fieldSuffixc] = extractNodeComments(f->id);
if (*fieldPrefixc != 0) {
builder.emitIndent();
builder.append(fieldPrefixc);
builder.newline();
}

if (f->annotations->size() > 0) {
builder.emitIndent();
if (!f->annotations->annotations.empty()) {
Expand Down Expand Up @@ -1456,7 +1482,8 @@ bool P4Formatter::preorder(const IR::Path *p) {

std::string toP4(const IR::INode *node) {
std::stringstream stream;
P4Fmt::P4Formatter toP4(&stream);
Attach::CommentsMap comMap;
P4Fmt::P4Formatter toP4(&stream, comMap);
node->getNode()->apply(toP4);
return stream.str();
}
Expand Down
15 changes: 12 additions & 3 deletions backends/p4fmt/p4formatter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#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"
#include "ir/visitor.h"
Expand Down Expand Up @@ -123,13 +126,15 @@ class P4Formatter : public Inspector, ::P4::ResolutionContext {
visitDagOnce = false;
setName("P4Formatter");
}
explicit P4Formatter(std::ostream *outStream, cstring mainFile = nullptr)
explicit P4Formatter(std::ostream *outStream, Attach::Attach::CommentsMap comMap,
cstring mainFile = nullptr)
: expressionPrecedence(DBPrint::Prec_Low),
isDeclaration(true),
withinArgument(false),
builder(*new Util::SourceCodeBuilder()),
outStream(outStream),
mainFile(mainFile) {
mainFile(mainFile),
comMap(std::move(comMap)) {
visitDagOnce = false;
setName("P4Formatter");
}
Expand All @@ -145,10 +150,11 @@ class P4Formatter : public Inspector, ::P4::ResolutionContext {
setName("P4Formatter");
}

std::pair<cstring, cstring> extractNodeComments(int nodeId);
void setnoIncludesArg(bool condition) { noIncludes = condition; }

void setListTerm(const char *start, const char *end) {
listTerminators.push_back(ListPrint(start, end));
listTerminators.emplace_back(start, end);
}
Visitor::profile_t init_apply(const IR::Node *node) override;
void end_apply(const IR::Node *node) override;
Expand Down Expand Up @@ -290,6 +296,9 @@ class P4Formatter : public Inspector, ::P4::ResolutionContext {

// in case it is accidentally called on a V1Program
bool preorder(const IR::V1Program *) override { return false; }

private:
Attach::Attach::CommentsMap comMap;
};

std::string toP4(const IR::INode *node);
Expand Down
Loading