-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add explicit validation of string annotations
- Loading branch information
Showing
8 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
4
testdata/p4_16_errors/spec-issue1297-string-cat-err-2-anno.p4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
4
testdata/p4_16_errors/spec-issue1297-string-cat-err-3-anno.p4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} |
2 changes: 2 additions & 0 deletions
2
testdata/p4_16_errors_outputs/spec-issue1297-string-cat-err-2-anno.p4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@name(4) void fn() { | ||
} |
3 changes: 3 additions & 0 deletions
3
testdata/p4_16_errors_outputs/spec-issue1297-string-cat-err-2-anno.p4-stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
^ |
2 changes: 2 additions & 0 deletions
2
testdata/p4_16_errors_outputs/spec-issue1297-string-cat-err-3-anno.p4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@deprecated("a" | "b") void fn() { | ||
} |
3 changes: 3 additions & 0 deletions
3
testdata/p4_16_errors_outputs/spec-issue1297-string-cat-err-3-anno.p4-stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
^^^^^^^ |