kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/indexer/proto/indexer_frontend.cc (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  #include "kythe/cxx/indexer/proto/indexer_frontend.h"
    18  
    19  #include "absl/container/flat_hash_map.h"
    20  #include "kythe/cxx/common/file_vname_generator.h"
    21  #include "kythe/cxx/common/indexing/KytheGraphRecorder.h"
    22  #include "kythe/cxx/common/path_utils.h"
    23  #include "kythe/cxx/indexer/proto/proto_analyzer.h"
    24  #include "kythe/cxx/indexer/proto/search_path.h"
    25  #include "kythe/cxx/indexer/proto/source_tree.h"
    26  #include "kythe/proto/analysis.pb.h"
    27  
    28  namespace kythe {
    29  
    30  std::string IndexProtoCompilationUnit(const proto::CompilationUnit& unit,
    31                                        const std::vector<proto::FileData>& files,
    32                                        KytheOutputStream* output) {
    33    KytheGraphRecorder recorder(output);
    34  
    35    std::vector<std::string> unprocessed_args;
    36    std::vector<std::pair<std::string, std::string>> path_substitutions;
    37    ::kythe::lang_proto::ParsePathSubstitutions(
    38        unit.argument(), &path_substitutions, &unprocessed_args);
    39    if (path_substitutions.empty() && !unit.working_directory().empty()) {
    40      path_substitutions.push_back({"", CleanPath(unit.working_directory())});
    41    }
    42  
    43    absl::flat_hash_map<std::string, std::string> file_substitution_cache;
    44    PreloadedProtoFileTree file_reader(&path_substitutions,
    45                                       &file_substitution_cache);
    46    google::protobuf::compiler::SourceTreeDescriptorDatabase descriptor_db(
    47        &file_reader);
    48    for (const auto& file_data : files) {
    49      file_reader.AddFile(file_data.info().path(), file_data.content());
    50    }
    51    lang_proto::ProtoAnalyzer analyzer(&unit, &descriptor_db, &recorder,
    52                                       &file_substitution_cache);
    53    if (unit.source_file().empty()) {
    54      return "Error: no source_files in CompilationUnit.";
    55    }
    56    std::string errors;
    57    for (const std::string& file_path : unit.source_file()) {
    58      if (file_path.empty()) {
    59        errors += "\n empty source_file.";
    60        continue;
    61      }
    62      std::string file_contents;
    63      if (!file_reader.Read(file_path, &file_contents)) {
    64        errors += "\n source_file " + file_path + " not in FileData.";
    65        continue;
    66      }
    67      if (!analyzer.Parse(file_path, file_contents)) {
    68        errors += "\n Analyzer failed on " + file_path;
    69      }
    70    }
    71    if (!errors.empty()) {
    72      return "Errors during indexing:" + errors;
    73    }
    74  
    75    return "";
    76  }
    77  
    78  }  // namespace kythe