diff --git a/backends/p4fmt/attach.cpp b/backends/p4fmt/attach.cpp index 20cf5ad435..383d690f87 100644 --- a/backends/p4fmt/attach.cpp +++ b/backends/p4fmt/attach.cpp @@ -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) { @@ -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; } diff --git a/backends/p4fmt/attach.h b/backends/p4fmt/attach.h index cb0a9c00ad..c2b9afeab5 100644 --- a/backends/p4fmt/attach.h +++ b/backends/p4fmt/attach.h @@ -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; diff --git a/backends/p4fmt/p4fmt.cpp b/backends/p4fmt/p4fmt.cpp index c57685bfad..fccc1667c3 100644 --- a/backends/p4fmt/p4fmt.cpp +++ b/backends/p4fmt/p4fmt.cpp @@ -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."); @@ -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); diff --git a/frontends/parsers/parserDriver.h b/frontends/parsers/parserDriver.h index 350bd6af13..53d41bd8d5 100644 --- a/frontends/parsers/parserDriver.h +++ b/frontends/parsers/parserDriver.h @@ -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 parseProgramSources( std::istream &in, std::string_view sourceFile, unsigned sourceLine = 1); diff --git a/lib/source_file.h b/lib/source_file.h index 034a9b9719..3958dd8cca 100755 --- a/lib/source_file.h +++ b/lib/source_file.h @@ -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; } };