protobuf/src/google/protobuf/compiler/annotation_test_util.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <google/protobuf/compiler/annotation_test_util.h>
32 
33 #include <cstdint>
34 #include <memory>
35 
36 #include <google/protobuf/testing/file.h>
37 #include <google/protobuf/testing/file.h>
38 #include <google/protobuf/compiler/code_generator.h>
39 #include <google/protobuf/compiler/command_line_interface.h>
40 #include <google/protobuf/io/printer.h>
41 #include <google/protobuf/io/zero_copy_stream.h>
42 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
43 #include <google/protobuf/descriptor.pb.h>
44 #include <google/protobuf/testing/googletest.h>
45 #include <gtest/gtest.h>
46 
47 namespace google {
48 namespace protobuf {
49 namespace compiler {
50 namespace annotation_test_util {
51 namespace {
52 
53 // A CodeGenerator that captures the FileDescriptor it's passed as a
54 // FileDescriptorProto.
55 class DescriptorCapturingGenerator : public CodeGenerator {
56  public:
57  // Does not own file; file must outlive the Generator.
58  explicit DescriptorCapturingGenerator(FileDescriptorProto* file)
59  : file_(file) {}
60 
61  virtual bool Generate(const FileDescriptor* file,
62  const std::string& parameter, GeneratorContext* context,
63  std::string* error) const {
64  file->CopyTo(file_);
65  return true;
66  }
67 
68  private:
70 };
71 } // namespace
72 
73 void AddFile(const std::string& filename, const std::string& data) {
75  true));
76 }
77 
79  const std::string& plugin_specific_args,
80  CommandLineInterface* cli, FileDescriptorProto* file) {
81  cli->SetInputsAreProtoPathRelative(true);
82 
83  DescriptorCapturingGenerator capturing_generator(file);
84  cli->RegisterGenerator("--capture_out", &capturing_generator, "");
85 
86  std::string proto_path = "-I" + TestTempDir();
87  std::string capture_out = "--capture_out=" + TestTempDir();
88 
89  const char* argv[] = {"protoc", proto_path.c_str(),
90  plugin_specific_args.c_str(), capture_out.c_str(),
91  filename.c_str()};
92 
93  return cli->Run(5, argv) == 0;
94 }
95 
99  io::ArrayInputStream input(data.data(), data.size());
100  return info->ParseFromZeroCopyStream(&input);
101 }
102 
104  const GeneratedCodeInfo& info, const std::string& source_file,
105  const std::vector<int>& path,
106  std::vector<const GeneratedCodeInfo::Annotation*>* annotations) {
107  for (int i = 0; i < info.annotation_size(); ++i) {
108  const GeneratedCodeInfo::Annotation* annotation = &info.annotation(i);
109  if (annotation->source_file() != source_file ||
110  annotation->path_size() != path.size()) {
111  continue;
112  }
113  int node = 0;
114  for (; node < path.size(); ++node) {
115  if (annotation->path(node) != path[node]) {
116  break;
117  }
118  }
119  if (node == path.size()) {
120  annotations->push_back(annotation);
121  }
122  }
123 }
124 
126  const GeneratedCodeInfo& info, const std::string& source_file,
127  const std::vector<int>& path) {
128  std::vector<const GeneratedCodeInfo::Annotation*> annotations;
129  FindAnnotationsOnPath(info, source_file, path, &annotations);
130  if (annotations.empty()) {
131  return NULL;
132  }
133  return annotations[0];
134 }
135 
137  const std::string& file_content,
138  const std::vector<const GeneratedCodeInfo::Annotation*>& annotations,
139  const std::string& expected_text) {
140  for (std::vector<const GeneratedCodeInfo::Annotation*>::const_iterator
141  i = annotations.begin(),
142  e = annotations.end();
143  i != e; ++i) {
144  const GeneratedCodeInfo::Annotation* annotation = *i;
145  uint32_t begin = annotation->begin();
146  uint32_t end = annotation->end();
147  if (end < begin || end > file_content.size()) {
148  return false;
149  }
150  if (file_content.substr(begin, end - begin) == expected_text) {
151  return true;
152  }
153  }
154  return false;
155 }
156 
157 bool AnnotationMatchesSubstring(const std::string& file_content,
158  const GeneratedCodeInfo::Annotation* annotation,
159  const std::string& expected_text) {
160  std::vector<const GeneratedCodeInfo::Annotation*> annotations;
161  annotations.push_back(annotation);
162  return AtLeastOneAnnotationMatchesSubstring(file_content, annotations,
163  expected_text);
164 }
165 } // namespace annotation_test_util
166 } // namespace compiler
167 } // namespace protobuf
168 } // namespace google
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
google::protobuf::File::SetContents
static bool SetContents(const string &name, const string &contents, bool)
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/file.h:93
begin
char * begin
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1007
GeneratedCodeInfo::annotation
const PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation & annotation(int index) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:13952
FileDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:128
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
google::protobuf::compiler::annotation_test_util::AddFile
void AddFile(const std::string &filename, const std::string &data)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:72
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
check_documentation.path
path
Definition: check_documentation.py:57
GeneratedCodeInfo_Annotation::source_file
const std::string & source_file() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:13789
google::protobuf::compiler::annotation_test_util::FindAnnotationsOnPath
void FindAnnotationsOnPath(const GeneratedCodeInfo &info, const std::string &source_file, const std::vector< int > &path, std::vector< const GeneratedCodeInfo::Annotation * > *annotations)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:102
GeneratedCodeInfo::annotation_size
int annotation_size() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:13934
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc::protobuf::compiler::GeneratorContext
GRPC_CUSTOM_GENERATORCONTEXT GeneratorContext
Definition: src/compiler/config.h:42
grpc::protobuf::compiler::CodeGenerator
GRPC_CUSTOM_CODEGENERATOR CodeGenerator
Definition: src/compiler/config.h:41
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
file_
FileDescriptorProto * file_
Definition: protobuf/src/google/protobuf/compiler/annotation_test_util.cc:69
google::protobuf::compiler::annotation_test_util::AtLeastOneAnnotationMatchesSubstring
bool AtLeastOneAnnotationMatchesSubstring(const std::string &file_content, const std::vector< const GeneratedCodeInfo::Annotation * > &annotations, const std::string &expected_text)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:135
google::protobuf::File::GetContents
static bool GetContents(const string &name, string *output, bool)
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/file.h:83
GeneratedCodeInfo_Annotation
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:6853
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
GeneratedCodeInfo_Annotation::path_size
int path_size() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:13734
google::protobuf::compiler::annotation_test_util::RunProtoCompiler
bool RunProtoCompiler(const std::string &filename, const std::string &plugin_specific_args, CommandLineInterface *cli, FileDescriptorProto *file)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:77
FileDescriptorProto
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:501
google::protobuf::compiler::annotation_test_util::FindAnnotationOnPath
const GeneratedCodeInfo::Annotation * FindAnnotationOnPath(const GeneratedCodeInfo &info, const std::string &source_file, const std::vector< int > &path)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:124
GeneratedCodeInfo_Annotation::path
::PROTOBUF_NAMESPACE_ID::int32 path(int index) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:13743
google::protobuf::compiler::annotation_test_util::DecodeMetadata
bool DecodeMetadata(const std::string &path, GeneratedCodeInfo *info)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:95
GeneratedCodeInfo_Annotation::end
::PROTOBUF_NAMESPACE_ID::int32 end() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:13913
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
google::protobuf::TestTempDir
string TestTempDir()
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/googletest.cc:189
GeneratedCodeInfo
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:7087
compiler
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.pb.cc:21
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::compiler::annotation_test_util::AnnotationMatchesSubstring
bool AnnotationMatchesSubstring(const std::string &file_content, const GeneratedCodeInfo::Annotation *annotation, const std::string &expected_text)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/annotation_test_util.cc:156
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
GOOGLE_CHECK_OK
#define GOOGLE_CHECK_OK(A)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:155
GeneratedCodeInfo_Annotation::begin
::PROTOBUF_NAMESPACE_ID::int32 begin() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:13885


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:41