Skip to content

Commit

Permalink
Add explicit validation of string annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
vlstill committed Aug 8, 2024
1 parent b987579 commit c1fb8dd
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions frontends/p4/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ limitations under the License.
#include "uselessCasts.h"
#include "validateMatchAnnotations.h"
#include "validateParsedProgram.h"
#include "validateStringAnnotations.h"
#include "validateValueSets.h"

namespace P4 {
Expand Down Expand Up @@ -174,6 +175,8 @@ const IR::P4Program *FrontEnd::run(const CompilerOptions &options, const IR::P4P
// First pass of constant folding, before types are known --
// may be needed to compute types.
new ConstantFolding(constantFoldingPolicy),
// Validate @name/@deprecated/@noWarn. Should run after constant folding.
new ValidateStringAnnotations(),
// Desugars direct parser and control applications
// into instantiations followed by application
new InstantiateDirectCalls(),
Expand Down
55 changes: 55 additions & 0 deletions frontends/p4/validateStringAnnotations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2019 VMware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef P4_VALIDATESTRINGANNOTATIONS_H_
#define P4_VALIDATESTRINGANNOTATIONS_H_

#include "frontends/p4/typeMap.h"
#include "ir/ir.h"
#include "lib/error.h"

namespace P4 {

/**
* Checks that the build-in string annotations (\@name, \@deprecated, \@noWarn) have string
* arguments.
*/
class ValidateStringAnnotations final : public Inspector {
TypeMap *typeMap;

public:
explicit ValidateStringAnnotations() {}

void postorder(const IR::Annotation *annotation) override {
const auto name = annotation->name;
if (name != IR::Annotation::nameAnnotation &&
name != IR::Annotation::deprecatedAnnotation &&
name != IR::Annotation::noWarnAnnotation) {
return;
}
if (annotation->expr.size() != 1)
::error(ErrorType::ERR_INVALID, "%1%: annotation must have exactly 1 argument",
annotation);
auto e0 = annotation->expr.at(0);
if (!e0->is<IR::StringLiteral>())
::error(ErrorType::ERR_TYPE_ERROR, "%1%: @%2% annotation's value must be a string", e0,
annotation->name.originalName);
}
};

} // namespace P4

#endif /* P4_VALIDATESTRINGANNOTATIONS_H_ */
4 changes: 4 additions & 0 deletions testdata/p4_16_errors/spec-issue1297-string-cat-err-2-anno.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// should not compile -- not a string
@name(4)
void fn() {
}
4 changes: 4 additions & 0 deletions testdata/p4_16_errors/spec-issue1297-string-cat-err-3-anno.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// should not compile -- not valid string op
@deprecated("a" | "b")
void fn() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@name(4) void fn() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spec-issue1297-string-cat-err-2-anno.p4(2): [--Werror=type-error] error: 4: @name annotation's value must be a string
@name(4)
^
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@deprecated("a" | "b") void fn() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spec-issue1297-string-cat-err-3-anno.p4(2): [--Werror=type-error] error: "a" | "b": @deprecated annotation's value must be a string
@deprecated("a" | "b")
^^^^^^^

0 comments on commit c1fb8dd

Please sign in to comment.