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

Introduce a frontend policy as a customization point for frontend #4406

Merged
merged 8 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix instatiation of frotend in tests
  • Loading branch information
vlstill committed Feb 13, 2024
commit 129ac0a8f3069bb3a1d6f14178fa9984d7bdc3a7
16 changes: 13 additions & 3 deletions test/gtest/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,22 @@ P4CTestEnvironment::P4CTestEnvironment() {

namespace Test {

struct FrontEndAnnoPolicy : P4::FrontEndPolicy {
P4::ParseAnnotations *parseAnnotations;

/// Pass given ParseAnnotations instance to frontend. The parameter can be null to indicate to
/// use the default.
explicit FrontEndAnnoPolicy(P4::ParseAnnotations *parseAnnotations) : parseAnnotations(parseAnnotations) {}

P4::ParseAnnotations *getParseAnnotations() const override { return parseAnnotations; }
};

/* static */ std::optional<FrontendTestCase> FrontendTestCase::create(
const std::string &source,
CompilerOptions::FrontendVersion langVersion
/* = CompilerOptions::FrontendVersion::P4_16 */,
const P4::ParseAnnotations &parseAnnotations
/* = P4::ParseAnnotations() */) {
P4::ParseAnnotations *parseAnnotations
/* = nullptr */) {
auto *program = P4::parseP4String(source, langVersion);
if (program == nullptr) {
std::cerr << "Couldn't parse test case source" << std::endl;
Expand All @@ -177,7 +187,7 @@ namespace Test {

CompilerOptions options;
options.langVersion = langVersion;
program = P4::FrontEnd(parseAnnotations).run(options, program, true);
program = P4::FrontEnd(new FrontEndAnnoPolicy(parseAnnotations)).run(options, program);
if (program == nullptr) {
std::cerr << "Frontend failed" << std::endl;
return std::nullopt;
Expand Down
4 changes: 2 additions & 2 deletions test/gtest/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ struct FrontendTestCase {
/// Create a test case that only requires the frontend to run.
static std::optional<FrontendTestCase> create(
const std::string &source, CompilerOptions::FrontendVersion langVersion = defaultVersion,
const P4::ParseAnnotations &parseAnnotations = P4::ParseAnnotations());
P4::ParseAnnotations *parseAnnotations = nullptr);

static std::optional<FrontendTestCase> create(const std::string &source,
const P4::ParseAnnotations &parseAnnotations) {
P4::ParseAnnotations *parseAnnotations) {
return create(source, defaultVersion, parseAnnotations);
}

Expand Down
8 changes: 4 additions & 4 deletions test/gtest/p4runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ std::optional<P4::P4RuntimeAPI> createP4RuntimeTestCase(
const std::string &source,
CompilerOptions::FrontendVersion langVersion = FrontendTestCase::defaultVersion,
const cstring arch = defaultArch,
P4::ParseAnnotations parseAnnotations = P4::ParseAnnotations()) {
P4::ParseAnnotations *parseAnnotations = new P4::ParseAnnotations()) {
auto frontendTestCase = FrontendTestCase::create(source, langVersion, parseAnnotations);
if (!frontendTestCase) return std::nullopt;
return P4::generateP4Runtime(frontendTestCase->program, arch);
}

std::optional<P4::P4RuntimeAPI> createP4RuntimeTestCase(const std::string &source,
P4::ParseAnnotations parseAnnotations) {
P4::ParseAnnotations *parseAnnotations) {
return createP4RuntimeTestCase(source, FrontendTestCase::defaultVersion, defaultArch,
parseAnnotations);
}
Expand Down Expand Up @@ -1251,7 +1251,7 @@ TEST_F(P4Runtime, ValueSet) {
V1Switch(parse(), verifyChecksum(), ingress(), egress(),
computeChecksum(), deparse()) main;
)"),
ParseAnnotations());
new ParseAnnotations());

ASSERT_TRUE(test);
EXPECT_EQ(0U, ::diagnosticCount());
Expand Down Expand Up @@ -1312,7 +1312,7 @@ TEST_F(P4Runtime, Register) {
V1Switch(parse(), verifyChecksum(), ingress(), egress(),
computeChecksum(), deparse()) main;
)"),
ParseAnnotations());
new ParseAnnotations());

ASSERT_TRUE(test);
EXPECT_EQ(0U, ::diagnosticCount());
Expand Down