From ca698ed640506162a83f3cb93de072512dde1628 Mon Sep 17 00:00:00 2001 From: Nate Foster Date: Wed, 10 May 2017 21:14:00 -0700 Subject: [PATCH 1/4] Add check that verify is only used in parsers. --- frontends/p4/validateParsedProgram.cpp | 12 ++++++++++++ frontends/p4/validateParsedProgram.h | 2 ++ testdata/p4_16_errors/control-verify.p4 | 7 +++++++ testdata/p4_16_errors_outputs/control-verify.p4 | 10 ++++++++++ .../p4_16_errors_outputs/control-verify.p4-stderr | 3 +++ 5 files changed, 34 insertions(+) create mode 100644 testdata/p4_16_errors/control-verify.p4 create mode 100644 testdata/p4_16_errors_outputs/control-verify.p4 create mode 100644 testdata/p4_16_errors_outputs/control-verify.p4-stderr diff --git a/frontends/p4/validateParsedProgram.cpp b/frontends/p4/validateParsedProgram.cpp index 89468e38fe2..38616d9ac9c 100644 --- a/frontends/p4/validateParsedProgram.cpp +++ b/frontends/p4/validateParsedProgram.cpp @@ -48,6 +48,18 @@ void ValidateParsedProgram::postorder(const IR::Method* m) { } } +/// Check that verify is not used outside of parsers +void ValidateParsedProgram::postorder(const IR::MethodCallExpression* e) { + if (e->method->is()) { + auto m = e->method->to(); + if (m->path->asString() == IR::ParserState::verify) { + auto inParser = findContext(); + if (inParser == nullptr) + ::error("%1%: may only be invoked in parsers", m); + } + } +} + /// Struct field names cannot be underscore void ValidateParsedProgram::postorder(const IR::StructField* f) { if (f->name.isDontCare()) diff --git a/frontends/p4/validateParsedProgram.h b/frontends/p4/validateParsedProgram.h index 0ad42e79a7f..ee92450c342 100644 --- a/frontends/p4/validateParsedProgram.h +++ b/frontends/p4/validateParsedProgram.h @@ -42,6 +42,7 @@ namespace P4 { - instantiations do not occur in actions - constructors are not invoked in actions - returns and exits do not appear in parsers + - verify only appears in parsers - extern constructors have the same name as the enclosing extern */ class ValidateParsedProgram final : public Inspector { @@ -65,6 +66,7 @@ class ValidateParsedProgram final : public Inspector { void postorder(const IR::Type_Union* type) override; void postorder(const IR::Type_Bits* type) override; void postorder(const IR::ConstructorCallExpression* expression) override; + void postorder(const IR::MethodCallExpression* expression) override; void postorder(const IR::Declaration_Variable* decl) override; void postorder(const IR::Declaration_Instance* inst) override; void postorder(const IR::Declaration_Constant* decl) override; diff --git a/testdata/p4_16_errors/control-verify.p4 b/testdata/p4_16_errors/control-verify.p4 new file mode 100644 index 00000000000..ccf9adcdca3 --- /dev/null +++ b/testdata/p4_16_errors/control-verify.p4 @@ -0,0 +1,7 @@ +error { Oops } + +control C() { + apply { + verify(8w0 == 8w1, error.Oops); + } +} diff --git a/testdata/p4_16_errors_outputs/control-verify.p4 b/testdata/p4_16_errors_outputs/control-verify.p4 new file mode 100644 index 00000000000..2c21eb4193a --- /dev/null +++ b/testdata/p4_16_errors_outputs/control-verify.p4 @@ -0,0 +1,10 @@ +error { + Oops +} + +control C() { + apply { + verify(8w0 == 8w1, error.Oops); + } +} + diff --git a/testdata/p4_16_errors_outputs/control-verify.p4-stderr b/testdata/p4_16_errors_outputs/control-verify.p4-stderr new file mode 100644 index 00000000000..24300cbbe0c --- /dev/null +++ b/testdata/p4_16_errors_outputs/control-verify.p4-stderr @@ -0,0 +1,3 @@ +../testdata/p4_16_errors/control-verify.p4(5): error: verify: may only be invoked in parsers + verify(8w0 == 8w1, error.Oops); + ^^^^^^ From 09ff44ff7710ab52646ef52b91f49cef05ecbad0 Mon Sep 17 00:00:00 2001 From: Nate Foster Date: Wed, 10 May 2017 21:48:31 -0700 Subject: [PATCH 2/4] cpplint fixes --- frontends/p4/validateParsedProgram.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontends/p4/validateParsedProgram.cpp b/frontends/p4/validateParsedProgram.cpp index 38616d9ac9c..ddb44b63d2b 100644 --- a/frontends/p4/validateParsedProgram.cpp +++ b/frontends/p4/validateParsedProgram.cpp @@ -55,11 +55,11 @@ void ValidateParsedProgram::postorder(const IR::MethodCallExpression* e) { if (m->path->asString() == IR::ParserState::verify) { auto inParser = findContext(); if (inParser == nullptr) - ::error("%1%: may only be invoked in parsers", m); + ::error("%1%: may only be invoked in parsers", m); } } } - + /// Struct field names cannot be underscore void ValidateParsedProgram::postorder(const IR::StructField* f) { if (f->name.isDontCare()) From bf66626dc737732927fcb7e095f2fd43e542c34a Mon Sep 17 00:00:00 2001 From: Nate Foster Date: Thu, 11 May 2017 11:42:33 -0700 Subject: [PATCH 3/4] Move verify check to typeChecker --- frontends/p4/typeChecking/typeChecker.cpp | 7 +++++++ frontends/p4/validateParsedProgram.cpp | 12 ------------ frontends/p4/validateParsedProgram.h | 2 -- testdata/p4_16_errors/control-verify.p4 | 2 ++ 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/frontends/p4/typeChecking/typeChecker.cpp b/frontends/p4/typeChecking/typeChecker.cpp index 4f3b440ff36..17ddc47cc9c 100644 --- a/frontends/p4/typeChecking/typeChecker.cpp +++ b/frontends/p4/typeChecking/typeChecker.cpp @@ -2527,6 +2527,13 @@ const IR::Node* TypeInference::postorder(IR::MethodCallExpression* expression) { ::error("%1%: tables cannot be invoked from actions", expression); } + // Check that verify is only invoked from parsers. + if (auto ef = mi->to()) { + if (ef->method->name == IR::ParserState::verify) + if (!findContext()) + ::error("%1%: may only be invoked in parsers", ef->expr); + } + if (mi->is()) checkCorelibMethods(mi->to()); diff --git a/frontends/p4/validateParsedProgram.cpp b/frontends/p4/validateParsedProgram.cpp index ddb44b63d2b..89468e38fe2 100644 --- a/frontends/p4/validateParsedProgram.cpp +++ b/frontends/p4/validateParsedProgram.cpp @@ -48,18 +48,6 @@ void ValidateParsedProgram::postorder(const IR::Method* m) { } } -/// Check that verify is not used outside of parsers -void ValidateParsedProgram::postorder(const IR::MethodCallExpression* e) { - if (e->method->is()) { - auto m = e->method->to(); - if (m->path->asString() == IR::ParserState::verify) { - auto inParser = findContext(); - if (inParser == nullptr) - ::error("%1%: may only be invoked in parsers", m); - } - } -} - /// Struct field names cannot be underscore void ValidateParsedProgram::postorder(const IR::StructField* f) { if (f->name.isDontCare()) diff --git a/frontends/p4/validateParsedProgram.h b/frontends/p4/validateParsedProgram.h index ee92450c342..0ad42e79a7f 100644 --- a/frontends/p4/validateParsedProgram.h +++ b/frontends/p4/validateParsedProgram.h @@ -42,7 +42,6 @@ namespace P4 { - instantiations do not occur in actions - constructors are not invoked in actions - returns and exits do not appear in parsers - - verify only appears in parsers - extern constructors have the same name as the enclosing extern */ class ValidateParsedProgram final : public Inspector { @@ -66,7 +65,6 @@ class ValidateParsedProgram final : public Inspector { void postorder(const IR::Type_Union* type) override; void postorder(const IR::Type_Bits* type) override; void postorder(const IR::ConstructorCallExpression* expression) override; - void postorder(const IR::MethodCallExpression* expression) override; void postorder(const IR::Declaration_Variable* decl) override; void postorder(const IR::Declaration_Instance* inst) override; void postorder(const IR::Declaration_Constant* decl) override; diff --git a/testdata/p4_16_errors/control-verify.p4 b/testdata/p4_16_errors/control-verify.p4 index ccf9adcdca3..55df2260934 100644 --- a/testdata/p4_16_errors/control-verify.p4 +++ b/testdata/p4_16_errors/control-verify.p4 @@ -1,3 +1,5 @@ +#include + error { Oops } control C() { From a17945dd329ffb7e4ee0ff655d8d0b5d864d3d7a Mon Sep 17 00:00:00 2001 From: Nate Foster Date: Thu, 11 May 2017 12:14:07 -0700 Subject: [PATCH 4/4] Update test outputs --- testdata/p4_16_errors_outputs/control-verify.p4 | 1 + testdata/p4_16_errors_outputs/control-verify.p4-stderr | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/testdata/p4_16_errors_outputs/control-verify.p4 b/testdata/p4_16_errors_outputs/control-verify.p4 index 2c21eb4193a..0c76e6b76ff 100644 --- a/testdata/p4_16_errors_outputs/control-verify.p4 +++ b/testdata/p4_16_errors_outputs/control-verify.p4 @@ -1,6 +1,7 @@ error { Oops } +#include control C() { apply { diff --git a/testdata/p4_16_errors_outputs/control-verify.p4-stderr b/testdata/p4_16_errors_outputs/control-verify.p4-stderr index 24300cbbe0c..907d094ac07 100644 --- a/testdata/p4_16_errors_outputs/control-verify.p4-stderr +++ b/testdata/p4_16_errors_outputs/control-verify.p4-stderr @@ -1,3 +1,3 @@ -../testdata/p4_16_errors/control-verify.p4(5): error: verify: may only be invoked in parsers +../testdata/p4_16_errors/control-verify.p4(7): error: verify: may only be invoked in parsers verify(8w0 == 8w1, error.Oops); - ^^^^^^ + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^