Skip to content

Commit 160c237

Browse files
jnthntatumcopybara-github
authored andcommitted
Refactor factory functions for TypeCheckerBuilders.
- move the factories to their own cc_library - update return value to be std::unique_ptr<Builder> PiperOrigin-RevId: 698067791
1 parent 77a8b0f commit 160c237

12 files changed

+244
-159
lines changed

checker/BUILD

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,44 @@ cc_library(
9191
"//checker/internal:type_checker_impl",
9292
"//common:decl",
9393
"//common:type",
94-
"//internal:noop_delete",
9594
"//internal:status_macros",
96-
"//internal:well_known_types",
9795
"//parser:macro",
9896
"@com_google_absl//absl/base:no_destructor",
9997
"@com_google_absl//absl/base:nullability",
10098
"@com_google_absl//absl/container:flat_hash_map",
10199
"@com_google_absl//absl/container:flat_hash_set",
102100
"@com_google_absl//absl/functional:any_invocable",
103-
"@com_google_absl//absl/log:absl_check",
104101
"@com_google_absl//absl/status",
105102
"@com_google_absl//absl/status:statusor",
106103
"@com_google_absl//absl/strings",
107104
"@com_google_protobuf//:protobuf",
108105
],
109106
)
110107

108+
cc_library(
109+
name = "type_checker_builder_factory",
110+
srcs = ["type_checker_builder_factory.cc"],
111+
hdrs = ["type_checker_builder_factory.h"],
112+
deps = [
113+
":checker_options",
114+
":type_checker_builder",
115+
"//internal:noop_delete",
116+
"//internal:status_macros",
117+
"//internal:well_known_types",
118+
"@com_google_absl//absl/base:nullability",
119+
"@com_google_absl//absl/log:absl_check",
120+
"@com_google_absl//absl/memory",
121+
"@com_google_absl//absl/status:statusor",
122+
"@com_google_protobuf//:protobuf",
123+
],
124+
)
125+
111126
cc_test(
112-
name = "type_checker_builder_test",
113-
srcs = ["type_checker_builder_test.cc"],
127+
name = "type_checker_builder_factory_test",
128+
srcs = ["type_checker_builder_factory_test.cc"],
114129
deps = [
115130
":type_checker_builder",
131+
":type_checker_builder_factory",
116132
":validation_result",
117133
"//checker/internal:test_ast_helpers",
118134
"//common:decl",
@@ -145,9 +161,11 @@ cc_test(
145161
name = "standard_library_test",
146162
srcs = ["standard_library_test.cc"],
147163
deps = [
164+
":checker_options",
148165
":standard_library",
149166
":type_checker",
150167
":type_checker_builder",
168+
":type_checker_builder_factory",
151169
":validation_result",
152170
"//base/ast_internal:ast_impl",
153171
"//base/ast_internal:expr",
@@ -190,6 +208,7 @@ cc_test(
190208
":type_check_issue",
191209
":type_checker",
192210
":type_checker_builder",
211+
":type_checker_builder_factory",
193212
"//base/ast_internal:ast_impl",
194213
"//base/ast_internal:expr",
195214
"//checker/internal:test_ast_helpers",

checker/optional_test.cc

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "checker/type_check_issue.h"
3030
#include "checker/type_checker.h"
3131
#include "checker/type_checker_builder.h"
32+
#include "checker/type_checker_builder_factory.h"
3233
#include "internal/testing.h"
3334
#include "internal/testing_descriptor_pool.h"
3435

@@ -77,13 +78,13 @@ MATCHER_P(IsOptionalType, inner_type, "") {
7778

7879
TEST(OptionalTest, OptSelectDoesNotAnnotateFieldType) {
7980
ASSERT_OK_AND_ASSIGN(
80-
TypeCheckerBuilder builder,
81+
std::unique_ptr<TypeCheckerBuilder> builder,
8182
CreateTypeCheckerBuilder(GetSharedTestingDescriptorPool()));
82-
ASSERT_THAT(builder.AddLibrary(StandardCheckerLibrary()), IsOk());
83-
ASSERT_THAT(builder.AddLibrary(OptionalCheckerLibrary()), IsOk());
84-
builder.set_container("cel.expr.conformance.proto3");
83+
ASSERT_THAT(builder->AddLibrary(StandardCheckerLibrary()), IsOk());
84+
ASSERT_THAT(builder->AddLibrary(OptionalCheckerLibrary()), IsOk());
85+
builder->set_container("cel.expr.conformance.proto3");
8586
ASSERT_OK_AND_ASSIGN(std::unique_ptr<TypeChecker> checker,
86-
std::move(builder).Build());
87+
std::move(*builder).Build());
8788

8889
ASSERT_OK_AND_ASSIGN(auto ast,
8990
MakeTestParsedAst("TestAllTypes{}.?single_int64"));
@@ -113,13 +114,13 @@ class OptionalTest : public testing::TestWithParam<TestCase> {};
113114

114115
TEST_P(OptionalTest, Runner) {
115116
ASSERT_OK_AND_ASSIGN(
116-
TypeCheckerBuilder builder,
117+
std::unique_ptr<TypeCheckerBuilder> builder,
117118
CreateTypeCheckerBuilder(GetSharedTestingDescriptorPool()));
118119
const TestCase& test_case = GetParam();
119-
ASSERT_THAT(builder.AddLibrary(StandardCheckerLibrary()), IsOk());
120-
ASSERT_THAT(builder.AddLibrary(OptionalCheckerLibrary()), IsOk());
120+
ASSERT_THAT(builder->AddLibrary(StandardCheckerLibrary()), IsOk());
121+
ASSERT_THAT(builder->AddLibrary(OptionalCheckerLibrary()), IsOk());
121122
ASSERT_OK_AND_ASSIGN(std::unique_ptr<TypeChecker> checker,
122-
std::move(builder).Build());
123+
std::move(*builder).Build());
123124

124125
ASSERT_OK_AND_ASSIGN(auto ast, MakeTestParsedAst(test_case.expr));
125126

@@ -284,13 +285,13 @@ TEST_P(OptionalStrictNullAssignmentTest, Runner) {
284285
CheckerOptions options;
285286
options.enable_legacy_null_assignment = false;
286287
ASSERT_OK_AND_ASSIGN(
287-
TypeCheckerBuilder builder,
288+
std::unique_ptr<TypeCheckerBuilder> builder,
288289
CreateTypeCheckerBuilder(GetSharedTestingDescriptorPool(), options));
289290
const TestCase& test_case = GetParam();
290-
ASSERT_THAT(builder.AddLibrary(StandardCheckerLibrary()), IsOk());
291-
ASSERT_THAT(builder.AddLibrary(OptionalCheckerLibrary()), IsOk());
291+
ASSERT_THAT(builder->AddLibrary(StandardCheckerLibrary()), IsOk());
292+
ASSERT_THAT(builder->AddLibrary(OptionalCheckerLibrary()), IsOk());
292293
ASSERT_OK_AND_ASSIGN(std::unique_ptr<TypeChecker> checker,
293-
std::move(builder).Build());
294+
std::move(*builder).Build());
294295

295296
ASSERT_OK_AND_ASSIGN(auto ast, MakeTestParsedAst(test_case.expr));
296297

checker/standard_library_test.cc

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
#include "absl/status/status_matchers.h"
2323
#include "base/ast_internal/ast_impl.h"
2424
#include "base/ast_internal/expr.h"
25+
#include "checker/checker_options.h"
2526
#include "checker/internal/test_ast_helpers.h"
2627
#include "checker/type_checker.h"
2728
#include "checker/type_checker_builder.h"
29+
#include "checker/type_checker_builder_factory.h"
2830
#include "checker/validation_result.h"
2931
#include "common/ast.h"
3032
#include "common/constant.h"
@@ -50,27 +52,27 @@ using AstType = cel::ast_internal::Type;
5052

5153
TEST(StandardLibraryTest, StandardLibraryAddsDecls) {
5254
ASSERT_OK_AND_ASSIGN(
53-
TypeCheckerBuilder builder,
55+
std::unique_ptr<TypeCheckerBuilder> builder,
5456
CreateTypeCheckerBuilder(GetSharedTestingDescriptorPool()));
55-
EXPECT_THAT(builder.AddLibrary(StandardCheckerLibrary()), IsOk());
56-
EXPECT_THAT(std::move(builder).Build(), IsOk());
57+
EXPECT_THAT(builder->AddLibrary(StandardCheckerLibrary()), IsOk());
58+
EXPECT_THAT(std::move(*builder).Build(), IsOk());
5759
}
5860

5961
TEST(StandardLibraryTest, StandardLibraryErrorsIfAddedTwice) {
6062
ASSERT_OK_AND_ASSIGN(
61-
TypeCheckerBuilder builder,
63+
std::unique_ptr<TypeCheckerBuilder> builder,
6264
CreateTypeCheckerBuilder(GetSharedTestingDescriptorPool()));
63-
EXPECT_THAT(builder.AddLibrary(StandardCheckerLibrary()), IsOk());
64-
EXPECT_THAT(builder.AddLibrary(StandardCheckerLibrary()),
65+
EXPECT_THAT(builder->AddLibrary(StandardCheckerLibrary()), IsOk());
66+
EXPECT_THAT(builder->AddLibrary(StandardCheckerLibrary()),
6567
StatusIs(absl::StatusCode::kAlreadyExists));
6668
}
6769

6870
TEST(StandardLibraryTest, ComprehensionVarsIndirectCyclicParamAssignability) {
6971
google::protobuf::Arena arena;
7072
ASSERT_OK_AND_ASSIGN(
71-
TypeCheckerBuilder builder,
73+
std::unique_ptr<TypeCheckerBuilder> builder,
7274
CreateTypeCheckerBuilder(GetSharedTestingDescriptorPool()));
73-
ASSERT_THAT(builder.AddLibrary(StandardCheckerLibrary()), IsOk());
75+
ASSERT_THAT(builder->AddLibrary(StandardCheckerLibrary()), IsOk());
7476

7577
// Note: this is atypical -- parameterized variables aren't well supported
7678
// outside of built-in syntax.
@@ -83,13 +85,13 @@ TEST(StandardLibraryTest, ComprehensionVarsIndirectCyclicParamAssignability) {
8385
Type list_type = ListType(&arena, TypeParamType("V"));
8486
Type map_type = MapType(&arena, TypeParamType("K"), TypeParamType("V"));
8587

86-
ASSERT_THAT(builder.AddVariable(MakeVariableDecl("list_var", list_type)),
88+
ASSERT_THAT(builder->AddVariable(MakeVariableDecl("list_var", list_type)),
8789
IsOk());
88-
ASSERT_THAT(builder.AddVariable(MakeVariableDecl("map_var", map_type)),
90+
ASSERT_THAT(builder->AddVariable(MakeVariableDecl("map_var", map_type)),
8991
IsOk());
9092

9193
ASSERT_OK_AND_ASSIGN(std::unique_ptr<TypeChecker> type_checker,
92-
std::move(builder).Build());
94+
std::move(*builder).Build());
9395

9496
ASSERT_OK_AND_ASSIGN(
9597
auto ast, checker_internal::MakeTestParsedAst(
@@ -108,10 +110,10 @@ class StandardLibraryDefinitionsTest : public ::testing::Test {
108110
public:
109111
void SetUp() override {
110112
ASSERT_OK_AND_ASSIGN(
111-
TypeCheckerBuilder builder,
113+
std::unique_ptr<TypeCheckerBuilder> builder,
112114
CreateTypeCheckerBuilder(GetSharedTestingDescriptorPool()));
113-
ASSERT_THAT(builder.AddLibrary(StandardCheckerLibrary()), IsOk());
114-
ASSERT_OK_AND_ASSIGN(stdlib_type_checker_, std::move(builder).Build());
115+
ASSERT_THAT(builder->AddLibrary(StandardCheckerLibrary()), IsOk());
116+
ASSERT_OK_AND_ASSIGN(stdlib_type_checker_, std::move(*builder).Build());
115117
}
116118

117119
protected:
@@ -212,12 +214,12 @@ class StdLibDefinitionsTest
212214
// Type-parameterized functions are not yet checkable.
213215
TEST_P(StdLibDefinitionsTest, Runner) {
214216
ASSERT_OK_AND_ASSIGN(
215-
TypeCheckerBuilder builder,
217+
std::unique_ptr<TypeCheckerBuilder> builder,
216218
CreateTypeCheckerBuilder(GetSharedTestingDescriptorPool(),
217219
GetParam().options));
218-
ASSERT_THAT(builder.AddLibrary(StandardCheckerLibrary()), IsOk());
220+
ASSERT_THAT(builder->AddLibrary(StandardCheckerLibrary()), IsOk());
219221
ASSERT_OK_AND_ASSIGN(std::unique_ptr<TypeChecker> type_checker,
220-
std::move(builder).Build());
222+
std::move(*builder).Build());
221223

222224
ASSERT_OK_AND_ASSIGN(std::unique_ptr<Ast> ast,
223225
checker_internal::MakeTestParsedAst(GetParam().expr));

checker/type_checker_builder.cc

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,19 @@
2020
#include <vector>
2121

2222
#include "absl/base/no_destructor.h"
23-
#include "absl/base/nullability.h"
2423
#include "absl/container/flat_hash_map.h"
25-
#include "absl/log/absl_check.h"
2624
#include "absl/status/status.h"
2725
#include "absl/status/statusor.h"
2826
#include "absl/strings/str_cat.h"
2927
#include "absl/strings/string_view.h"
30-
#include "checker/checker_options.h"
3128
#include "checker/internal/type_check_env.h"
3229
#include "checker/internal/type_checker_impl.h"
3330
#include "checker/type_checker.h"
3431
#include "common/decl.h"
3532
#include "common/type.h"
3633
#include "common/type_introspector.h"
37-
#include "internal/noop_delete.h"
3834
#include "internal/status_macros.h"
39-
#include "internal/well_known_types.h"
4035
#include "parser/macro.h"
41-
#include "google/protobuf/descriptor.h"
4236

4337
namespace cel {
4438
namespace {
@@ -83,29 +77,6 @@ absl::Status CheckStdMacroOverlap(const FunctionDecl& decl) {
8377

8478
} // namespace
8579

86-
absl::StatusOr<TypeCheckerBuilder> CreateTypeCheckerBuilder(
87-
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
88-
const CheckerOptions& options) {
89-
ABSL_DCHECK(descriptor_pool != nullptr);
90-
return CreateTypeCheckerBuilder(
91-
std::shared_ptr<const google::protobuf::DescriptorPool>(
92-
descriptor_pool,
93-
internal::NoopDeleteFor<const google::protobuf::DescriptorPool>()),
94-
options);
95-
}
96-
97-
absl::StatusOr<TypeCheckerBuilder> CreateTypeCheckerBuilder(
98-
absl::Nonnull<std::shared_ptr<const google::protobuf::DescriptorPool>>
99-
descriptor_pool,
100-
const CheckerOptions& options) {
101-
ABSL_DCHECK(descriptor_pool != nullptr);
102-
// Verify the standard descriptors, we do not need to keep
103-
// `well_known_types::Reflection` at the moment here.
104-
CEL_RETURN_IF_ERROR(
105-
well_known_types::Reflection().Initialize(descriptor_pool.get()));
106-
return TypeCheckerBuilder(std::move(descriptor_pool), options);
107-
}
108-
10980
absl::StatusOr<std::unique_ptr<TypeChecker>> TypeCheckerBuilder::Build() && {
11081
auto checker = std::make_unique<checker_internal::TypeCheckerImpl>(
11182
std::move(env_), options_);

checker/type_checker_builder.h

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,6 @@ namespace cel {
3838

3939
class TypeCheckerBuilder;
4040

41-
// Creates a new `TypeCheckerBuilder`.
42-
//
43-
// When passing a raw pointer to a descriptor pool, the descriptor pool must
44-
// outlive the type checker builder and the type checker builder it creates.
45-
//
46-
// The descriptor pool must include the minimally necessary
47-
// descriptors required by CEL. Those are the following:
48-
// - google.protobuf.NullValue
49-
// - google.protobuf.BoolValue
50-
// - google.protobuf.Int32Value
51-
// - google.protobuf.Int64Value
52-
// - google.protobuf.UInt32Value
53-
// - google.protobuf.UInt64Value
54-
// - google.protobuf.FloatValue
55-
// - google.protobuf.DoubleValue
56-
// - google.protobuf.BytesValue
57-
// - google.protobuf.StringValue
58-
// - google.protobuf.Any
59-
// - google.protobuf.Duration
60-
// - google.protobuf.Timestamp
61-
absl::StatusOr<TypeCheckerBuilder> CreateTypeCheckerBuilder(
62-
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
63-
const CheckerOptions& options = {});
64-
absl::StatusOr<TypeCheckerBuilder> CreateTypeCheckerBuilder(
65-
absl::Nonnull<std::shared_ptr<const google::protobuf::DescriptorPool>>
66-
descriptor_pool,
67-
const CheckerOptions& options = {});
68-
6941
// Functional implementation to apply the library features to a
7042
// TypeCheckerBuilder.
7143
using TypeCheckerBuilderConfigurer =
@@ -109,7 +81,8 @@ class TypeCheckerBuilder {
10981
const CheckerOptions& options() const { return options_; }
11082

11183
private:
112-
friend absl::StatusOr<TypeCheckerBuilder> CreateTypeCheckerBuilder(
84+
friend absl::StatusOr<std::unique_ptr<TypeCheckerBuilder>>
85+
CreateTypeCheckerBuilder(
11386
absl::Nonnull<std::shared_ptr<const google::protobuf::DescriptorPool>>
11487
descriptor_pool,
11588
const CheckerOptions& options);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
// Copyright 2024 Google LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#include "checker/type_checker_builder_factory.h"
17+
18+
#include <memory>
19+
#include <utility>
20+
21+
#include "absl/base/nullability.h"
22+
#include "absl/log/absl_check.h"
23+
#include "absl/memory/memory.h"
24+
#include "absl/status/statusor.h"
25+
#include "checker/checker_options.h"
26+
#include "checker/type_checker_builder.h"
27+
#include "internal/noop_delete.h"
28+
#include "internal/status_macros.h"
29+
#include "internal/well_known_types.h"
30+
#include "google/protobuf/descriptor.h"
31+
32+
namespace cel {
33+
34+
absl::StatusOr<std::unique_ptr<TypeCheckerBuilder>> CreateTypeCheckerBuilder(
35+
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
36+
const CheckerOptions& options) {
37+
ABSL_DCHECK(descriptor_pool != nullptr);
38+
return CreateTypeCheckerBuilder(
39+
std::shared_ptr<const google::protobuf::DescriptorPool>(
40+
descriptor_pool,
41+
internal::NoopDeleteFor<const google::protobuf::DescriptorPool>()),
42+
options);
43+
}
44+
45+
absl::StatusOr<std::unique_ptr<TypeCheckerBuilder>> CreateTypeCheckerBuilder(
46+
absl::Nonnull<std::shared_ptr<const google::protobuf::DescriptorPool>>
47+
descriptor_pool,
48+
const CheckerOptions& options) {
49+
ABSL_DCHECK(descriptor_pool != nullptr);
50+
// Verify the standard descriptors, we do not need to keep
51+
// `well_known_types::Reflection` at the moment here.
52+
CEL_RETURN_IF_ERROR(
53+
well_known_types::Reflection().Initialize(descriptor_pool.get()));
54+
auto* builder = new TypeCheckerBuilder(std::move(descriptor_pool), options);
55+
return absl::WrapUnique(builder);
56+
}
57+
58+
} // namespace cel

0 commit comments

Comments
 (0)