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

If "p4include" is in the path, treat the file as system file. #4742

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 2 additions & 3 deletions frontends/common/parser_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,11 @@ void ParserOptions::dumpPass(const char *manager, unsigned seq, const char *pass
std::unique_ptr<std::ostream> 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;
Expand Down
3 changes: 1 addition & 2 deletions frontends/p4/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 20 additions & 4 deletions frontends/p4/toP4/toP4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
#include "toP4.h"

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

#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"
Expand All @@ -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;
}

Expand Down Expand Up @@ -148,9 +152,20 @@ void ToP4::dump(unsigned depth, const IR::Node *node, unsigned adjDepth) {

bool ToP4::preorder(const IR::P4Program *program) {
std::set<cstring> 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);
Expand Down Expand Up @@ -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();
}
Expand Down
8 changes: 5 additions & 3 deletions frontends/p4/toP4/toP4.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
#ifndef P4_TOP4_TOP4_H_
#define P4_TOP4_TOP4_H_

#include <filesystem>

#include "ir/ir.h"
#include "ir/visitor.h"
#include "lib/sourceCodeBuilder.h"
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down
Loading