From 3306162acd9e60e06c5574dc28d67df71a78cbad Mon Sep 17 00:00:00 2001 From: Fabian Ruffy <5960321+fruffy@users.noreply.github.com> Date: Fri, 30 Aug 2024 13:44:43 +0200 Subject: [PATCH] Make isSystemFile() part of the parser options file. (#4888) * Remove a stray print in the smith code. (#4891) Signed-off-by: fruffy * Make isSystemFile part of the parser options. Signed-off-by: fruffy * Review comments. Signed-off-by: fruffy --------- Signed-off-by: fruffy --- frontends/common/parser_options.cpp | 2 ++ frontends/common/parser_options.h | 3 +++ frontends/p4/toP4/toP4.cpp | 29 +++++++++++++---------------- frontends/p4/toP4/toP4.h | 8 +++++--- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/frontends/common/parser_options.cpp b/frontends/common/parser_options.cpp index 1a73dd1a470..d598987f0d4 100644 --- a/frontends/common/parser_options.cpp +++ b/frontends/common/parser_options.cpp @@ -48,6 +48,8 @@ const char *p4_14includePath = CONFIG_PKGDATADIR "/p4_14include"; using namespace P4::literals; +bool isSystemFile(cstring file) { return file.startsWith(p4includePath); } + void ParserOptions::closeFile(FILE *file) { if (file == nullptr) { return; diff --git a/frontends/common/parser_options.h b/frontends/common/parser_options.h index d57cb0066f0..6ed4ebb159f 100644 --- a/frontends/common/parser_options.h +++ b/frontends/common/parser_options.h @@ -36,6 +36,9 @@ namespace P4 { extern const char *p4includePath; extern const char *p4_14includePath; +/// Try to guess whether a file is a "system" file +bool isSystemFile(cstring filename); + /// Base class for compiler options. /// This class contains the options for the front-ends. /// Each back-end should subclass this file. diff --git a/frontends/p4/toP4/toP4.cpp b/frontends/p4/toP4/toP4.cpp index 6a8041f6f86..cad9e003b5f 100644 --- a/frontends/p4/toP4/toP4.cpp +++ b/frontends/p4/toP4/toP4.cpp @@ -46,18 +46,15 @@ void ToP4::end_apply(const IR::Node *) { "inconsistent vectorSeparator"); } -// Try to guess whether a file is a "system" file -bool ToP4::isSystemFile(cstring file) { - if (noIncludes) return false; - if (file.startsWith(p4includePath)) return true; - return false; -} - -cstring ToP4::ifSystemFile(const IR::Node *node) { - if (!node->srcInfo.isValid()) return nullptr; +std::optional ToP4::ifSystemFile(const IR::Node *node) { + if (!node->srcInfo.isValid() || noIncludes) { + return std::nullopt; + } auto sourceFile = node->srcInfo.getSourceFile(); - if (isSystemFile(sourceFile)) return sourceFile; - return nullptr; + if (isSystemFile(sourceFile)) { + return sourceFile; + } + return std::nullopt; } namespace { @@ -153,9 +150,9 @@ bool ToP4::preorder(const IR::P4Program *program) { dump(2); for (auto a : program->objects) { // Check where this declaration originates - cstring sourceFile = ifSystemFile(a); - if (!a->is() && // errors can come from multiple files - sourceFile != nullptr) { + auto sourceFileOpt = ifSystemFile(a); + // Errors can come from multiple files + if (!a->is() && sourceFileOpt.has_value()) { /* FIXME -- when including a user header file (sourceFile != * mainFile), do we want to emit an #include of it or not? Probably * not when translating from P4-14, as that would create a P4-16 @@ -163,7 +160,7 @@ bool ToP4::preorder(const IR::P4Program *program) { * allow converting headers independently (is that even possible?). * For now we ignore mainFile and don't emit #includes for any * non-system header */ - + auto sourceFile = sourceFileOpt.value(); if (includesEmitted.find(sourceFile) == includesEmitted.end()) { if (sourceFile.startsWith(p4includePath)) { const char *p = sourceFile.c_str() + strlen(p4includePath); @@ -691,7 +688,7 @@ bool ToP4::preorder(const IR::Type_Error *d) { dump(1); bool first = true; for (auto a : *d->getDeclarations()) { - if (ifSystemFile(a->getNode())) + if (ifSystemFile(a->getNode()).has_value()) // only print if not from a system file continue; if (!first) { diff --git a/frontends/p4/toP4/toP4.h b/frontends/p4/toP4/toP4.h index f0a8e63bd7e..87e78e7771e 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 "frontends/common/resolveReferences/resolveReferences.h" #include "ir/ir.h" #include "ir/visitor.h" @@ -72,9 +74,9 @@ class ToP4 : public Inspector, ResolutionContext { BUG_CHECK(!listTerminators.empty(), "Empty listTerminators"); listTerminators.pop_back(); } - bool isSystemFile(cstring file); - cstring ifSystemFile(const IR::Node *node); // return file containing node if system file - // dump node IR tree up to depth - in the form of a comment + /// @returns the file that contains the node, if the node is part of a system file. + std::optional ifSystemFile(const IR::Node *node); + /// dump node IR tree up to depth - in the form of a comment void dump(unsigned depth, const IR::Node *node = nullptr, unsigned adjDepth = 0); unsigned curDepth() const;