From e6048a1b3f522e9d7858fdd5199abf37b16575bd Mon Sep 17 00:00:00 2001 From: fruffy Date: Sat, 22 Jun 2024 19:26:43 -0400 Subject: [PATCH] If "p4include" is in the path, treat the file as system file. --- frontends/common/parser_options.cpp | 5 ++--- frontends/p4/frontend.cpp | 3 +-- frontends/p4/toP4/toP4.cpp | 24 ++++++++++++++++++++---- frontends/p4/toP4/toP4.h | 8 +++++--- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/frontends/common/parser_options.cpp b/frontends/common/parser_options.cpp index 649888bc97..666c8f8981 100644 --- a/frontends/common/parser_options.cpp +++ b/frontends/common/parser_options.cpp @@ -494,12 +494,11 @@ void ParserOptions::dumpPass(const char *manager, unsigned seq, const char *pass std::unique_ptr stream{openFile(fileName, true)}; if (stream != nullptr) { if (Log::verbose()) std::cerr << "Writing program to " << fileName << std::endl; - // FIXME: Accept path here - P4::ToP4 toP4(stream.get(), Log::verbose(), cstring(file)); + P4::ToP4 toP4(stream.get(), Log::verbose(), file); if (noIncludes) { toP4.setnoIncludesArg(true); } - if (node) { + if (node != nullptr) { node->apply(toP4); } else { *stream << "No P4 program returned by the pass" << std::endl; diff --git a/frontends/p4/frontend.cpp b/frontends/p4/frontend.cpp index c96fb9cd6a..3fb9f9491e 100644 --- a/frontends/p4/frontend.cpp +++ b/frontends/p4/frontend.cpp @@ -104,8 +104,7 @@ class PrettyPrint : public Inspector { bool preorder(const IR::P4Program *program) override { if (!ppfile.empty()) { std::ostream *ppStream = openFile(ppfile, true); - // FIXME: ToP4 should accept PathName - P4::ToP4 top4(ppStream, false, cstring(inputfile)); + P4::ToP4 top4(ppStream, false, inputfile); (void)program->apply(top4); } return false; // prune diff --git a/frontends/p4/toP4/toP4.cpp b/frontends/p4/toP4/toP4.cpp index fffd2702b1..143896cb46 100644 --- a/frontends/p4/toP4/toP4.cpp +++ b/frontends/p4/toP4/toP4.cpp @@ -17,10 +17,12 @@ limitations under the License. #include "toP4.h" #include +#include #include #include #include "frontends/common/options.h" +#include "frontends/common/parser_options.h" #include "frontends/p4/fromv1.0/v1model.h" #include "frontends/parsers/p4/p4parser.hpp" #include "ir/dump.h" @@ -46,10 +48,12 @@ void ToP4::end_apply(const IR::Node *) { "inconsistent vectorSeparator"); } -// Try to guess whether a file is a "system" file +// Try to guess whether a file is an included file. bool ToP4::isSystemFile(cstring file) { if (noIncludes) return false; - if (file.startsWith(p4includePath)) return true; + if (file != mainFile) { + return true; + } return false; } @@ -148,9 +152,20 @@ void ToP4::dump(unsigned depth, const IR::Node *node, unsigned adjDepth) { bool ToP4::preorder(const IR::P4Program *program) { std::set includesEmitted; - bool first = true; dump(2); + + // Try to initialize the mainFile from the program source info or the parser options. + if (mainFile.empty()) { + if (program->getSourceInfo().isValid()) { + mainFile = program->getSourceInfo().getSourceFile().c_str(); + } else if (!P4CContext::get().options().file.empty()) { + mainFile = P4CContext::get().options().file; + } else { + ::warning("No program base file specified. ToP4 might generate incorrect includes."); + } + } + for (auto a : program->objects) { // Check where this declaration originates cstring sourceFile = ifSystemFile(a); @@ -180,8 +195,9 @@ bool ToP4::preorder(const IR::P4Program *program) { builder.append(">"); builder.newline(); } else { + auto relativPath = std::filesystem::relative(sourceFile.c_str(), mainFile); builder.append("#include \""); - builder.append(sourceFile); + builder.append(relativPath); builder.append("\""); builder.newline(); } diff --git a/frontends/p4/toP4/toP4.h b/frontends/p4/toP4/toP4.h index 2541022c7d..18be32d911 100644 --- a/frontends/p4/toP4/toP4.h +++ b/frontends/p4/toP4/toP4.h @@ -17,6 +17,8 @@ limitations under the License. #ifndef P4_TOP4_TOP4_H_ #define P4_TOP4_TOP4_H_ +#include + #include "ir/ir.h" #include "ir/visitor.h" #include "lib/sourceCodeBuilder.h" @@ -88,9 +90,9 @@ class ToP4 : public Inspector { /** If this is set to non-nullptr, some declarations that come from libraries and models are not emitted. */ - cstring mainFile; + std::filesystem::path mainFile; - ToP4(Util::SourceCodeBuilder &builder, bool showIR, cstring mainFile = nullptr) + ToP4(Util::SourceCodeBuilder &builder, bool showIR, const std::filesystem::path &mainFile = {}) : expressionPrecedence(DBPrint::Prec_Low), isDeclaration(true), showIR(showIR), @@ -101,7 +103,7 @@ class ToP4 : public Inspector { visitDagOnce = false; setName("ToP4"); } - ToP4(std::ostream *outStream, bool showIR, cstring mainFile = nullptr) + ToP4(std::ostream *outStream, bool showIR, const std::filesystem::path &mainFile = {}) : expressionPrecedence(DBPrint::Prec_Low), isDeclaration(true), showIR(showIR),