kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/protobuf_metadata_file.h (about)

     1  /*
     2   * Copyright 2016 The Kythe Authors. All rights reserved.
     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   *   http://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  
    17  #ifndef KYTHE_CXX_COMMON_PROTOBUF_METADATA_FILE_H_
    18  #define KYTHE_CXX_COMMON_PROTOBUF_METADATA_FILE_H_
    19  
    20  #include <memory>
    21  #include <sstream>
    22  
    23  #include "absl/log/log.h"
    24  #include "google/protobuf/descriptor.pb.h"
    25  #include "kythe/cxx/common/file_vname_generator.h"
    26  #include "kythe/cxx/common/kythe_metadata_file.h"
    27  
    28  namespace kythe {
    29  
    30  /// \brief Generates a VName for the given proto path.
    31  /// \param file_vname the VName for the proto file. Its signature and language
    32  /// will be ignored.
    33  /// \param path the proto AST path.
    34  template <typename Path>
    35  proto::VName VNameForProtoPath(const proto::VName& file_vname,
    36                                 const Path& path) {
    37    proto::VName out(file_vname);
    38    std::string signature;
    39    std::stringstream sig(signature);
    40    bool first_node = true;
    41    for (const auto& node : path) {
    42      sig << (first_node ? "" : ".") << node;
    43      first_node = false;
    44    }
    45    out.set_signature(sig.str());
    46    out.set_language("protobuf");
    47    return out;
    48  }
    49  
    50  /// Provides metadata support for proto2.GeneratedCodeInfo metadata.
    51  class ProtobufMetadataSupport : public MetadataSupport {
    52   public:
    53    /// Parse and return a metadata file. Will return null if `filename` does not
    54    /// end in .pb.h.meta or .proto.h.meta. If this check passes and `buffer` does
    55    /// not parse into a `proto2.GeneratedCodeInfo` message, returns null and logs
    56    /// a warning.
    57    std::unique_ptr<kythe::MetadataFile> ParseFile(
    58        const std::string& raw_filename, const std::string& filename,
    59        absl::string_view buffer, absl::string_view target_buffer) override;
    60  
    61    void UseVNameLookup(VNameLookup lookup) override { vname_lookup_ = lookup; }
    62  
    63    void SetAliasesAsWrites(bool set_aliases_as_writes) {
    64      set_aliases_as_writes_ = set_aliases_as_writes;
    65    }
    66  
    67   private:
    68    /// Returns the VName for the node that should be used between
    69    /// generated semantic objects and the objects that generate them.
    70    /// \param context_vname the VName to use when a .proto's VName can't be
    71    /// identified.
    72    proto::VName VNameForAnnotation(
    73        const proto::VName& context_vname,
    74        const google::protobuf::GeneratedCodeInfo::Annotation& annotation);
    75  
    76    /// Used to build VNames for proto objects.
    77    VNameLookup vname_lookup_ = [](const std::string& path, proto::VName* out) {
    78      return false;
    79    };
    80  
    81    /// Use write semantics for aliases.
    82    bool set_aliases_as_writes_ = false;
    83  };
    84  
    85  }  // namespace kythe
    86  
    87  #endif  // KYTHE_CXX_COMMON_PROTOBUF_METADATA_FILE_H_