"Fossies" - the Fresh Open Source Software Archive

Member "protobuf-3.12.3/src/google/protobuf/compiler/annotation_test_util.cc" (2 Jun 2020, 6330 Bytes) of package /linux/misc/protobuf-all-3.12.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "annotation_test_util.cc" see the Fossies "Dox" file reference documentation.

    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 <memory>
   34 #include <google/protobuf/compiler/code_generator.h>
   35 #include <google/protobuf/compiler/command_line_interface.h>
   36 #include <google/protobuf/io/printer.h>
   37 #include <google/protobuf/io/zero_copy_stream.h>
   38 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
   39 #include <google/protobuf/descriptor.pb.h>
   40 
   41 #include <google/protobuf/testing/file.h>
   42 #include <google/protobuf/testing/file.h>
   43 #include <google/protobuf/testing/googletest.h>
   44 #include <gtest/gtest.h>
   45 
   46 namespace google {
   47 namespace protobuf {
   48 namespace compiler {
   49 namespace annotation_test_util {
   50 namespace {
   51 
   52 // A CodeGenerator that captures the FileDescriptor it's passed as a
   53 // FileDescriptorProto.
   54 class DescriptorCapturingGenerator : public CodeGenerator {
   55  public:
   56   // Does not own file; file must outlive the Generator.
   57   explicit DescriptorCapturingGenerator(FileDescriptorProto* file)
   58       : file_(file) {}
   59 
   60   virtual bool Generate(const FileDescriptor* file,
   61                         const std::string& parameter, GeneratorContext* context,
   62                         std::string* error) const {
   63     file->CopyTo(file_);
   64     return true;
   65   }
   66 
   67  private:
   68   FileDescriptorProto* file_;
   69 };
   70 }  // namespace
   71 
   72 void AddFile(const std::string& filename, const std::string& data) {
   73   GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/" + filename, data,
   74                              true));
   75 }
   76 
   77 bool RunProtoCompiler(const std::string& filename,
   78                       const std::string& plugin_specific_args,
   79                       CommandLineInterface* cli, FileDescriptorProto* file) {
   80   cli->SetInputsAreProtoPathRelative(true);
   81 
   82   DescriptorCapturingGenerator capturing_generator(file);
   83   cli->RegisterGenerator("--capture_out", &capturing_generator, "");
   84 
   85   std::string proto_path = "-I" + TestTempDir();
   86   std::string capture_out = "--capture_out=" + TestTempDir();
   87 
   88   const char* argv[] = {"protoc", proto_path.c_str(),
   89                         plugin_specific_args.c_str(), capture_out.c_str(),
   90                         filename.c_str()};
   91 
   92   return cli->Run(5, argv) == 0;
   93 }
   94 
   95 bool DecodeMetadata(const std::string& path, GeneratedCodeInfo* info) {
   96   std::string data;
   97   GOOGLE_CHECK_OK(File::GetContents(path, &data, true));
   98   io::ArrayInputStream input(data.data(), data.size());
   99   return info->ParseFromZeroCopyStream(&input);
  100 }
  101 
  102 void FindAnnotationsOnPath(
  103     const GeneratedCodeInfo& info, const std::string& source_file,
  104     const std::vector<int>& path,
  105     std::vector<const GeneratedCodeInfo::Annotation*>* annotations) {
  106   for (int i = 0; i < info.annotation_size(); ++i) {
  107     const GeneratedCodeInfo::Annotation* annotation = &info.annotation(i);
  108     if (annotation->source_file() != source_file ||
  109         annotation->path_size() != path.size()) {
  110       continue;
  111     }
  112     int node = 0;
  113     for (; node < path.size(); ++node) {
  114       if (annotation->path(node) != path[node]) {
  115         break;
  116       }
  117     }
  118     if (node == path.size()) {
  119       annotations->push_back(annotation);
  120     }
  121   }
  122 }
  123 
  124 const GeneratedCodeInfo::Annotation* FindAnnotationOnPath(
  125     const GeneratedCodeInfo& info, const std::string& source_file,
  126     const std::vector<int>& path) {
  127   std::vector<const GeneratedCodeInfo::Annotation*> annotations;
  128   FindAnnotationsOnPath(info, source_file, path, &annotations);
  129   if (annotations.empty()) {
  130     return NULL;
  131   }
  132   return annotations[0];
  133 }
  134 
  135 bool AtLeastOneAnnotationMatchesSubstring(
  136     const std::string& file_content,
  137     const std::vector<const GeneratedCodeInfo::Annotation*>& annotations,
  138     const std::string& expected_text) {
  139   for (std::vector<const GeneratedCodeInfo::Annotation*>::const_iterator
  140            i = annotations.begin(),
  141            e = annotations.end();
  142        i != e; ++i) {
  143     const GeneratedCodeInfo::Annotation* annotation = *i;
  144     uint32 begin = annotation->begin();
  145     uint32 end = annotation->end();
  146     if (end < begin || end > file_content.size()) {
  147       return false;
  148     }
  149     if (file_content.substr(begin, end - begin) == expected_text) {
  150       return true;
  151     }
  152   }
  153   return false;
  154 }
  155 
  156 bool AnnotationMatchesSubstring(const std::string& file_content,
  157                                 const GeneratedCodeInfo::Annotation* annotation,
  158                                 const std::string& expected_text) {
  159   std::vector<const GeneratedCodeInfo::Annotation*> annotations;
  160   annotations.push_back(annotation);
  161   return AtLeastOneAnnotationMatchesSubstring(file_content, annotations,
  162                                               expected_text);
  163 }
  164 }  // namespace annotation_test_util
  165 }  // namespace compiler
  166 }  // namespace protobuf
  167 }  // namespace google