kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/indexer/proto/proto_analyzer.h (about)

     1  /*
     2   * Copyright 2018 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_INDEXER_PROTO_PROTO_ANALYZER_H_
    18  #define KYTHE_CXX_INDEXER_PROTO_PROTO_ANALYZER_H_
    19  
    20  #include <memory>
    21  #include <string>
    22  
    23  #include "absl/container/flat_hash_map.h"
    24  #include "absl/container/node_hash_set.h"
    25  #include "absl/log/log.h"
    26  #include "google/protobuf/descriptor.h"
    27  #include "google/protobuf/descriptor_database.h"
    28  #include "kythe/cxx/common/file_vname_generator.h"
    29  #include "kythe/cxx/common/kythe_uri.h"
    30  #include "kythe/cxx/indexer/proto/proto_graph_builder.h"
    31  #include "kythe/proto/analysis.pb.h"
    32  #include "kythe/proto/common.pb.h"
    33  #include "kythe/proto/storage.pb.h"
    34  #include "kythe/proto/xref.pb.h"
    35  
    36  namespace kythe {
    37  namespace lang_proto {
    38  
    39  // Class to parse proto files, analyze them, create a proto graph builder and
    40  // output the serialized IndexArtifacts in the form of an SSTable.
    41  class ProtoAnalyzer {
    42   public:
    43    // Constructs a new analyzer with the given path and graph recorder. The
    44    // latter must remain alive/valid the entire duration of the ProtoAnalyzer
    45    // instance, but ownership is not transferred.
    46    ProtoAnalyzer(
    47        const proto::CompilationUnit* unit,
    48        google::protobuf::DescriptorDatabase* descriptor_db,
    49        KytheGraphRecorder* recorder,
    50        absl::flat_hash_map<std::string, std::string>* path_substitution_cache);
    51  
    52    // disallow copy and assign
    53    ProtoAnalyzer(const ProtoAnalyzer&) = delete;
    54    void operator=(const ProtoAnalyzer&) = delete;
    55  
    56    // A wrapper for AnalyzeFile that generates the VName and relativizes
    57    // the proto file path.
    58    bool Parse(const std::string& proto_file, const std::string& content);
    59  
    60    // Given a string which contains a proto file, analyze it and record the
    61    // results.
    62    bool AnalyzeFile(const std::string& rel_path, const proto::VName& v_name,
    63                     const std::string& content);
    64  
    65    // Returns a VName for the input 'simplified_path' joined with any prefix
    66    // (for example, a bazel-out/ subdirectory) needed to properly and fully
    67    // reference the true storage location.  If there is no such prefix path,
    68    // then the input path is simply returned.
    69    proto::VName VNameFromRelPath(const std::string& simplified_path) const;
    70  
    71   private:
    72    absl::node_hash_set<std::string> visited_files_;
    73  
    74    // Returns the VName associated with `path` in unit_'s required inputs, or
    75    // an empty vname if none is found.
    76    proto::VName VNameFromFullPath(const std::string& path) const;
    77  
    78    // Compilation unit to be analyzed.
    79    const proto::CompilationUnit* unit_;
    80  
    81    // Where we output Kythe artifacts.
    82    KytheGraphRecorder* recorder_;
    83  
    84    // Maps files to paths which include their prefix directories, the most
    85    // common being bazel-out/ derivatives.
    86    absl::flat_hash_map<std::string, std::string>* path_substitution_cache_;
    87  
    88    // Gives us properly linked together descriptors for proto files and their
    89    // contents.
    90    google::protobuf::DescriptorDatabase* descriptor_db_;
    91  };
    92  
    93  }  // namespace lang_proto
    94  }  // namespace kythe
    95  
    96  #endif  // KYTHE_CXX_INDEXER_PROTO_PROTO_ANALYZER_H_