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]: attaching comments to IR Nodes #4845

Merged
merged 15 commits into from
Oct 24, 2024
Prev Previous commit
Next Next commit
chore: comment fixes & minor refactoring
Signed-off-by: Nitish <snapdgnn@proton.me>

Signed-off-by: Nitish <snapdgnn@proton.me>
  • Loading branch information
snapdgn committed Oct 24, 2024
commit 9ffa53bc48cb964e45c802c40b94cea6abcadd83
7 changes: 1 addition & 6 deletions backends/p4fmt/attach.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ void Attach::addSuffixComments(NodeId node, const Util::Comment *suffix) {
commentsMap[node].suffix.push_back(suffix);
}

bool Attach::isSystemFile(const std::filesystem::path &file) {
const std::filesystem::path p4include(p4includePath);
return file.parent_path() == p4include;
}

const Attach::CommentsMap &Attach::getCommentsMap() const { return commentsMap; }

const IR::Node *Attach::attachCommentsToNode(IR::Node *node, TraversalType ttype) {
Expand All @@ -26,7 +21,7 @@ const IR::Node *Attach::attachCommentsToNode(IR::Node *node, TraversalType ttype
}

std::filesystem::path sourceFile(node->srcInfo.getSourceFile().string_view());
if (isSystemFile(sourceFile)) {
if (isSystemFile(sourceFile.string())) {
snapdgn marked this conversation as resolved.
Show resolved Hide resolved
// Skip attachment for system files
return node;
}
Expand Down
2 changes: 0 additions & 2 deletions backends/p4fmt/attach.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class Attach : public Transform {
const IR::Node *preorder(IR::Node *node) override;
const IR::Node *postorder(IR::Node *node) override;

static bool isSystemFile(const std::filesystem::path &file);

void addPrefixComments(NodeId, const Util::Comment *);
void addSuffixComments(NodeId, const Util::Comment *);
const CommentsMap &getCommentsMap() const;
Expand Down
7 changes: 2 additions & 5 deletions backends/p4fmt/p4fmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ std::stringstream getFormattedOutput(std::filesystem::path inputFile) {

std::stringstream formattedOutput;

auto result = parseProgram(options);

const IR::P4Program *program = result.first;
const Util::InputSources *sources = result.second;
const auto &[program, sources] = parseProgram(options);

if (program == nullptr && ::P4::errorCount() != 0) {
::P4::error("Failed to parse P4 file.");
Expand All @@ -78,7 +75,7 @@ std::stringstream getFormattedOutput(std::filesystem::path inputFile) {

auto top4 = P4Fmt::P4Formatter(&formattedOutput);
auto attach = P4::P4Fmt::Attach(globalCommentsMap);
program = program->apply(attach);
program->apply(attach);
snapdgn marked this conversation as resolved.
Show resolved Hide resolved
// Print the program before running front end passes.
program->apply(top4);

Expand Down
3 changes: 3 additions & 0 deletions frontends/parsers/parserDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ class P4ParserDriver final : public AbstractParserDriver {
static const IR::P4Program *parse(FILE *in, std::string_view sourceFile,
unsigned sourceLine = 1);

/// Parses the input and returns a pair with the P4Program and InputSources.
/// Use this when both the parsed P4Program and InputSources are required,
/// as opposed to the `parse` method, which only returns the P4Program.
static std::pair<const IR::P4Program *, const Util::InputSources *> parseProgramSources(
snapdgn marked this conversation as resolved.
Show resolved Hide resolved
std::istream &in, std::string_view sourceFile, unsigned sourceLine = 1);

Expand Down
2 changes: 1 addition & 1 deletion lib/source_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class Comment final : IHasDbPrint, IHasSourceInfo {
if (!singleLine) out << "*/";
}

/// Retrieve the Source position associated with this Comment.
/// Retrieve the source position associated with this comment.
[[nodiscard]] SourceInfo getSourceInfo() const override { return srcInfo; }
};

Expand Down