Skip to content

Commit

Permalink
chore: comment fixes & minor refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Nitish <snapdgnn@proton.me>

Signed-off-by: Nitish <snapdgnn@proton.me>
  • Loading branch information
snapdgn committed Sep 6, 2024
1 parent 979e8b5 commit 200cc5f
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 14 deletions.
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())) {
// 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);
// 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(
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

0 comments on commit 200cc5f

Please sign in to comment.