Skip to content

Commit 3121025

Browse files
authored
Revert "Implemented QuoteString which mimics QuoteSourceLocation but uses string with offset as source (#73)" (#74)
This reverts commit d5ab5e6.
1 parent d5ab5e6 commit 3121025

12 files changed

+241
-540
lines changed

p4_constraints/BUILD.bazel

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ cc_library(
2121
],
2222
)
2323

24-
cc_library(
25-
name = "constraint_source",
26-
hdrs = ["constraint_source.h"],
27-
deps = [":ast_cc_proto"],
28-
)
29-
3024
cc_test(
3125
name = "ast_test",
3226
size = "small",
@@ -58,11 +52,7 @@ cc_library(
5852
hdrs = ["quote.h"],
5953
deps = [
6054
":ast_cc_proto",
61-
":constraint_source",
62-
"//gutils:status",
6355
"@com_github_google_glog//:glog",
64-
"@com_google_absl//absl/status:statusor",
6556
"@com_google_absl//absl/strings",
66-
"@com_google_absl//absl/strings:str_format",
6757
],
6858
)

p4_constraints/backend/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ cc_library(
2121
"//gutils:status",
2222
"//p4_constraints:ast",
2323
"//p4_constraints:ast_cc_proto",
24-
"//p4_constraints:constraint_source",
2524
"//p4_constraints:quote",
2625
"@com_github_google_glog//:glog",
2726
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
@@ -74,7 +73,6 @@ cc_library(
7473
"//gutils:status",
7574
"//p4_constraints:ast",
7675
"//p4_constraints:ast_cc_proto",
77-
"//p4_constraints:constraint_source",
7876
"//p4_constraints:quote",
7977
"//p4_constraints/frontend:lexer",
8078
"//p4_constraints/frontend:parser",
@@ -101,6 +99,7 @@ cc_test(
10199
"//gutils:ordered_map",
102100
"//gutils:parse_text_proto",
103101
"//gutils:status",
102+
"//net/proto2/io/public:io",
104103
"//p4_constraints:ast_cc_proto",
105104
"//p4_constraints/frontend:lexer",
106105
"//p4_constraints/frontend:parser",

p4_constraints/backend/constraint_info.cc

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "p4/config/v1/p4info.pb.h"
3131
#include "p4_constraints/ast.pb.h"
3232
#include "p4_constraints/backend/type_checker.h"
33-
#include "p4_constraints/constraint_source.h"
3433
#include "p4_constraints/frontend/lexer.h"
3534
#include "p4_constraints/frontend/parser.h"
3635
#include "re2/re2.h"
@@ -42,7 +41,8 @@ namespace {
4241
using p4::config::v1::MatchField;
4342
using p4::config::v1::Table;
4443

45-
absl::StatusOr<ConstraintSource> ExtractConstraint(const Table& table) {
44+
absl::StatusOr<absl::optional<ast::Expression>> ParseTableConstraint(
45+
const Table& table) {
4646
// We expect .p4 files to have the following format:
4747
// ```p4
4848
// @file(__FILE__) // optional
@@ -58,38 +58,38 @@ absl::StatusOr<ConstraintSource> ExtractConstraint(const Table& table) {
5858
const RE2 line_annotation = {R"RE(@line[(](\d+)[)])RE"};
5959
const RE2 constraint_annotation = {R"RE(@entry_restriction)RE"};
6060

61-
absl::string_view constraint_string = "";
62-
ast::SourceLocation constraint_location;
61+
ast::SourceLocation location;
6362
int line = 0;
63+
absl::string_view constraint = "";
6464
for (absl::string_view annotation : table.preamble().annotations()) {
6565
if (RE2::Consume(&annotation, file_annotation,
66-
constraint_location.mutable_file_path()))
66+
location.mutable_file_path()))
6767
continue;
6868
if (RE2::Consume(&annotation, line_annotation, &line)) continue;
6969
if (RE2::Consume(&annotation, constraint_annotation)) {
70-
constraint_string = annotation.data();
70+
constraint = annotation.data();
7171
break;
7272
}
7373
}
74-
75-
if (constraint_string.empty()) return ConstraintSource();
76-
77-
constraint_location.set_line(line);
78-
if (constraint_location.file_path().empty()) {
79-
constraint_location.set_table_name(table.preamble().name());
74+
location.set_line(line);
75+
if (location.file_path().empty()) {
76+
location.set_table_name(table.preamble().name());
8077
}
8178

82-
if (!absl::ConsumePrefix(&constraint_string, "(\"") ||
83-
!absl::ConsumeSuffix(&constraint_string, "\")")) {
79+
if (constraint.empty()) {
80+
return absl::optional<ast::Expression>();
81+
}
82+
if (!absl::ConsumePrefix(&constraint, "(\"") ||
83+
!absl::ConsumeSuffix(&constraint, "\")")) {
8484
return gutils::InvalidArgumentErrorBuilder(GUTILS_LOC)
8585
<< "In table " << table.preamble().name() << ":\n"
8686
<< "Syntax error: @entry_restriction must be enclosed in "
8787
"'(\"' and '\")'";
8888
}
89-
return ConstraintSource{
90-
.constraint_string = std::string(constraint_string),
91-
.constraint_location = constraint_location,
92-
};
89+
// TODO(smolkaj): With C++17, we can use std::string_view throughout and
90+
// won't need to make an extra copy here.
91+
const auto constraint_str = std::string(constraint);
92+
return ParseConstraint(Tokenize(constraint_str, location));
9393
}
9494

9595
absl::StatusOr<ast::Type> ParseKeyType(const MatchField& key) {
@@ -145,26 +145,14 @@ absl::StatusOr<TableInfo> ParseTableInfo(const Table& table) {
145145
}
146146
}
147147

148-
ASSIGN_OR_RETURN(ConstraintSource constraint_source,
149-
ExtractConstraint(table));
150-
151-
absl::optional<ast::Expression> constraint = absl::nullopt;
152-
if (!constraint_source.constraint_string.empty()) {
153-
ASSIGN_OR_RETURN(constraint, ParseConstraint(Tokenize(
154-
constraint_source.constraint_string,
155-
constraint_source.constraint_location)));
156-
}
157-
158-
TableInfo table_info{
159-
.id = table.preamble().id(),
160-
.name = table.preamble().name(),
161-
.constraint = constraint,
162-
.constraint_source = constraint_source,
163-
.keys_by_id = keys_by_id,
164-
.keys_by_name = keys_by_name,
165-
};
148+
TableInfo table_info{.id = table.preamble().id(),
149+
.name = table.preamble().name(),
150+
.constraint = {}, // Inserted in a second.
151+
.keys_by_id = keys_by_id,
152+
.keys_by_name = keys_by_name};
166153

167-
// Type check constraint.
154+
// Parse and type check constraint.
155+
ASSIGN_OR_RETURN(table_info.constraint, ParseTableConstraint(table));
168156
if (table_info.constraint.has_value()) {
169157
RETURN_IF_ERROR(
170158
InferAndCheckTypes(&table_info.constraint.value(), table_info));

p4_constraints/backend/constraint_info.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "absl/types/variant.h"
3333
#include "p4/config/v1/p4info.pb.h"
3434
#include "p4_constraints/ast.pb.h"
35-
#include "p4_constraints/constraint_source.h"
3635

3736
namespace p4_constraints {
3837

@@ -51,9 +50,6 @@ struct TableInfo {
5150

5251
// An optional constraint (aka entry_restriction) on table entries.
5352
absl::optional<ast::Expression> constraint;
54-
// If member `constraint` is present, this captures its source. Arbitrary
55-
// otherwise.
56-
ConstraintSource constraint_source;
5753

5854
// Maps from key IDs/names to KeyInfo.
5955
// Derives from Table.match_fields in p4info.proto.

p4_constraints/backend/interpreter.cc

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#include "p4_constraints/ast.h"
4242
#include "p4_constraints/ast.pb.h"
4343
#include "p4_constraints/backend/constraint_info.h"
44-
#include "p4_constraints/constraint_source.h"
4544
#include "p4_constraints/quote.h"
4645

4746
namespace p4_constraints {
@@ -523,8 +522,7 @@ absl::StatusOr<const Expression*> MinimalSubexpressionLeadingToEvalResult(
523522
}
524523

525524
if (candidate_subexpressions.empty()) return &expression;
526-
// Returns `MinimalSubexpressionLeadingToEvalResult` of sole
527-
// candidate.
525+
// Returns `MinimalSubexpressionLeadingToEvalResult` of sole candidate
528526
if (candidate_subexpressions.size() == 1) {
529527
return MinimalSubexpressionLeadingToEvalResult(
530528
*candidate_subexpressions[0], entry, eval_cache, size_cache);
@@ -563,34 +561,32 @@ absl::StatusOr<const Expression*> MinimalSubexpressionLeadingToEvalResult(
563561

564562
// Returns human readable explanation of constraint violation.
565563
absl::StatusOr<std::string> ExplainConstraintViolation(
566-
const Expression& expr, const ConstraintSource& source,
567-
const TableEntry& entry, EvaluationCache& eval_cache,
568-
ast::SizeCache& size_cache) {
564+
const Expression& expr, const TableEntry& entry,
565+
EvaluationCache& eval_cache, ast::SizeCache& size_cache) {
569566
ASSIGN_OR_RETURN(const ast::Expression* explanation,
570567
MinimalSubexpressionLeadingToEvalResult(
571568
expr, entry, eval_cache, size_cache));
572569
ASSIGN_OR_RETURN(bool truth_value,
573570
EvalToBool(*explanation, entry, &eval_cache));
574-
ASSIGN_OR_RETURN(std::string reason,
575-
QuoteSubConstraint(source, explanation->start_location(),
576-
explanation->end_location()));
577571
// Ordered for determinism when golden testing.
578572
std::string key_info = absl::StrJoin(
579573
gutils::Ordered(entry.keys), "\n", [](std::string* out, auto pair) {
580574
absl::StrAppend(out, "Key:\"", pair.first,
581575
"\" -> Value: ", EvalResultToString(pair.second));
582576
});
583-
577+
// TODO(b/241957581): Find better representation for explanation. Consider
578+
// modifying quote function to use string in constraint info
584579
return absl::StrFormat(
585580
"All entries must %ssatisfy:"
586581
"\n\n%s\n"
587-
"But your entry does%s.\n"
582+
"But your entry did%s\n"
588583
">>>Entry Info<<<\n"
589-
"Table Name: \"%s\"\n"
584+
"Table Name:\"%s\"\n"
590585
"Priority:%d\n"
586+
"Key Info\n"
591587
"%s\n",
592-
(truth_value ? "not " : ""), reason, (truth_value ? "" : " not"),
593-
entry.table_name, entry.priority, key_info);
588+
(truth_value ? "not " : ""), explanation->DebugString(),
589+
(truth_value ? "" : " not"), entry.table_name, entry.priority, key_info);
594590
}
595591
// -- Main evaluator -----------------------------------------------------------
596592

@@ -804,8 +800,8 @@ absl::StatusOr<std::string> ReasonEntryViolatesConstraint(
804800
return "";
805801
}
806802
SizeCache size_cache;
807-
return ExplainConstraintViolation(constraint, table_info.constraint_source,
808-
parsed_entry, eval_cache, size_cache);
803+
return ExplainConstraintViolation(constraint, parsed_entry, eval_cache,
804+
size_cache);
809805
}
810806

811807
} // namespace p4_constraints

0 commit comments

Comments
 (0)