Skip to content

Commit

Permalink
keep P4Control->body->srcInfo during init move (#4317)
Browse files Browse the repository at this point in the history
* keep P4Control->body->srcInfo during init move

Ensure that MoveInitializers keeps the srcInfo when creating a new body
for a P4Control.

Fix for issue #4315
  • Loading branch information
grg authored Jan 4, 2024
1 parent 96aa26a commit 8cc98af
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion frontends/p4/moveDeclarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ const IR::Node *MoveInitializers::postorder(IR::ParserState *state) {
const IR::Node *MoveInitializers::postorder(IR::P4Control *control) {
if (toMove->empty()) return control;
toMove->append(control->body->components);
auto newBody = new IR::BlockStatement(control->body->annotations, *toMove);
auto newBody =
new IR::BlockStatement(control->body->srcInfo, control->body->annotations, *toMove);
control->body = newBody;
toMove = new IR::IndexedVector<IR::StatOrDecl>();
return control;
Expand Down
32 changes: 32 additions & 0 deletions test/gtest/frontend_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "frontends/common/parseInput.h"
#include "frontends/common/resolveReferences/referenceMap.h"
#include "frontends/common/resolveReferences/resolveReferences.h"
#include "frontends/p4/moveDeclarations.h"
#include "frontends/p4/typeChecking/typeChecker.h"
#include "gtest/gtest.h"
#include "helpers.h"
Expand Down Expand Up @@ -168,4 +169,35 @@ TEST_F(P4CFrontendEnumValidation, InvalidType) {
ASSERT_TRUE(errors.contains("type-declared types"));
}

// Tests for MoveInitializers
struct P4CFrontendMoveInitializers : P4CFrontend {
P4CFrontendMoveInitializers() {
addPasses({new P4::ResolveReferences(&refMap), new P4::MoveInitializers(&refMap)});
}

P4::ReferenceMap refMap;
};

TEST_F(P4CFrontendMoveInitializers, P4ControlSrcInfo) {
std::string program = P4_SOURCE(R"(
control m() {
bool blah = false;
apply{}
}
)");
const auto *prog = parseAndProcess(program);
ASSERT_TRUE(prog);
ASSERT_EQ(::errorCount(), 0);

// The P4Control->body should have a valid srcInfo if the information
// is correctly maintained by MoveInitializers.
const auto *p4prog = prog->to<IR::P4Program>();
ASSERT_TRUE(p4prog);
for (const auto *node : p4prog->objects) {
if (const auto *control = node->to<IR::P4Control>()) {
ASSERT_TRUE(control->body->srcInfo.isValid());
}
}
}

} // namespace Test

0 comments on commit 8cc98af

Please sign in to comment.