|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "tools/descriptor_pool_builder.h" |
| 16 | + |
| 17 | +#include <memory> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +#include "google/protobuf/descriptor.pb.h" |
| 21 | +#include "absl/base/nullability.h" |
| 22 | +#include "absl/container/flat_hash_set.h" |
| 23 | +#include "absl/status/status.h" |
| 24 | +#include "absl/strings/str_cat.h" |
| 25 | +#include "absl/types/span.h" |
| 26 | +#include "common/minimal_descriptor_database.h" |
| 27 | +#include "internal/status_macros.h" |
| 28 | +#include "google/protobuf/descriptor.h" |
| 29 | + |
| 30 | +namespace cel { |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +absl::Status FindDeps( |
| 35 | + std::vector<const google::protobuf::FileDescriptor*>& to_resolve, |
| 36 | + absl::flat_hash_set<const google::protobuf::FileDescriptor*>& resolved, |
| 37 | + DescriptorPoolBuilder& builder) { |
| 38 | + while (!to_resolve.empty()) { |
| 39 | + const auto* file = to_resolve.back(); |
| 40 | + to_resolve.pop_back(); |
| 41 | + if (resolved.contains(file)) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + google::protobuf::FileDescriptorProto file_proto; |
| 45 | + file->CopyTo(&file_proto); |
| 46 | + // Note: order doesn't matter here as long as all the cross references are |
| 47 | + // correct in the final database. |
| 48 | + CEL_RETURN_IF_ERROR(builder.AddFileDescriptor(file_proto)); |
| 49 | + resolved.insert(file); |
| 50 | + for (int i = 0; i < file->dependency_count(); ++i) { |
| 51 | + to_resolve.push_back(file->dependency(i)); |
| 52 | + } |
| 53 | + } |
| 54 | + return absl::OkStatus(); |
| 55 | +} |
| 56 | + |
| 57 | +} // namespace |
| 58 | + |
| 59 | +DescriptorPoolBuilder::StateHolder::StateHolder( |
| 60 | + google::protobuf::DescriptorDatabase* base) |
| 61 | + : base(base), merged(base, &extensions), pool(&merged) {} |
| 62 | + |
| 63 | +DescriptorPoolBuilder::DescriptorPoolBuilder() |
| 64 | + : state_(std::make_shared<DescriptorPoolBuilder::StateHolder>( |
| 65 | + cel::GetMinimalDescriptorDatabase())) {} |
| 66 | + |
| 67 | +std::shared_ptr<const google::protobuf::DescriptorPool> |
| 68 | +DescriptorPoolBuilder::Build() && { |
| 69 | + auto alias = |
| 70 | + std::shared_ptr<const google::protobuf::DescriptorPool>(state_, &state_->pool); |
| 71 | + state_.reset(); |
| 72 | + return alias; |
| 73 | +} |
| 74 | + |
| 75 | +absl::Status DescriptorPoolBuilder::AddTransitiveDescriptorSet( |
| 76 | + absl::Nonnull<const google::protobuf::Descriptor*> desc) { |
| 77 | + absl::flat_hash_set<const google::protobuf::FileDescriptor*> resolved; |
| 78 | + std::vector<const google::protobuf::FileDescriptor*> to_resolve{desc->file()}; |
| 79 | + return FindDeps(to_resolve, resolved, *this); |
| 80 | +} |
| 81 | + |
| 82 | +absl::Status DescriptorPoolBuilder::AddTransitiveDescriptorSet( |
| 83 | + absl::Span<absl::Nonnull<const google::protobuf::Descriptor*>> descs) { |
| 84 | + absl::flat_hash_set<const google::protobuf::FileDescriptor*> resolved; |
| 85 | + std::vector<absl::Nonnull<const google::protobuf::FileDescriptor*>> to_resolve; |
| 86 | + to_resolve.reserve(descs.size()); |
| 87 | + for (const google::protobuf::Descriptor* desc : descs) { |
| 88 | + to_resolve.push_back(desc->file()); |
| 89 | + } |
| 90 | + |
| 91 | + return FindDeps(to_resolve, resolved, *this); |
| 92 | +} |
| 93 | + |
| 94 | +absl::Status DescriptorPoolBuilder::AddFileDescriptor( |
| 95 | + const google::protobuf::FileDescriptorProto& file) { |
| 96 | + if (!state_->extensions.Add(file)) { |
| 97 | + return absl::InvalidArgumentError( |
| 98 | + absl::StrCat("proto descriptor conflict: ", file.name())); |
| 99 | + } |
| 100 | + return absl::OkStatus(); |
| 101 | +} |
| 102 | + |
| 103 | +absl::Status DescriptorPoolBuilder::AddFileDescriptorSet( |
| 104 | + const google::protobuf::FileDescriptorSet& file) { |
| 105 | + for (const auto& file : file.file()) { |
| 106 | + CEL_RETURN_IF_ERROR(AddFileDescriptor(file)); |
| 107 | + } |
| 108 | + return absl::OkStatus(); |
| 109 | +} |
| 110 | + |
| 111 | +} // namespace cel |
0 commit comments