Skip to content

Commit

Permalink
Make isSystemFile() part of the parser options file. (#4888)
Browse files Browse the repository at this point in the history
* Remove a stray print in the smith code. (#4891)

Signed-off-by: fruffy <[email protected]>

* Make isSystemFile part of the parser options.

Signed-off-by: fruffy <[email protected]>

* Review comments.

Signed-off-by: fruffy <[email protected]>

---------

Signed-off-by: fruffy <[email protected]>
  • Loading branch information
fruffy authored Aug 30, 2024
1 parent d93d08e commit 3306162
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
2 changes: 2 additions & 0 deletions frontends/common/parser_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions frontends/common/parser_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 13 additions & 16 deletions frontends/p4/toP4/toP4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<cstring> 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 {
Expand Down Expand Up @@ -153,17 +150,17 @@ 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<IR::Type_Error>() && // errors can come from multiple files
sourceFile != nullptr) {
auto sourceFileOpt = ifSystemFile(a);
// Errors can come from multiple files
if (!a->is<IR::Type_Error>() && 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
* file that tries to include a P4-14 header. Unless we want to
* 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);
Expand Down Expand Up @@ -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) {
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 <optional>

#include "frontends/common/resolveReferences/resolveReferences.h"
#include "ir/ir.h"
#include "ir/visitor.h"
Expand Down Expand Up @@ -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<cstring> 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;

Expand Down

0 comments on commit 3306162

Please sign in to comment.