kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/indexer/proto/search_path.cc (about) 1 /* 2 * Copyright 2019 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 "search_path.h" 18 19 #include "absl/strings/str_cat.h" 20 #include "absl/strings/str_replace.h" 21 #include "absl/strings/str_split.h" 22 #include "absl/strings/strip.h" 23 #include "kythe/cxx/common/path_utils.h" 24 25 namespace kythe { 26 namespace lang_proto { 27 namespace { 28 29 void AddPathSubstitutions( 30 absl::string_view path_argument, 31 std::vector<std::pair<std::string, std::string>>* substitutions) { 32 std::vector<std::string> parts = 33 absl::StrSplit(path_argument, ':', absl::SkipEmpty()); 34 for (const std::string& path_or_substitution : parts) { 35 std::string::size_type equals_pos = path_or_substitution.find_first_of('='); 36 if (equals_pos == std::string::npos) { 37 substitutions->push_back({"", CleanPath(path_or_substitution)}); 38 } else { 39 substitutions->push_back( 40 {CleanPath(path_or_substitution.substr(0, equals_pos)), 41 CleanPath(path_or_substitution.substr(equals_pos + 1))}); 42 } 43 } 44 } 45 46 template <class ARGLIST> 47 void _ParsePathSubstitutions( 48 ARGLIST args, 49 std::vector<std::pair<std::string, std::string>>* substitutions, 50 std::vector<std::string>* unprocessed) { 51 bool expecting_path_arg = false; 52 for (const std::string& argument : args) { 53 if (expecting_path_arg) { 54 expecting_path_arg = false; 55 AddPathSubstitutions(argument, substitutions); 56 } else if (argument == "-I" || argument == "--proto_path") { 57 expecting_path_arg = true; 58 } else { 59 absl::string_view argument_value = argument; 60 if (absl::ConsumePrefix(&argument_value, "-I")) { 61 AddPathSubstitutions(argument_value, substitutions); 62 } else if (absl::ConsumePrefix(&argument_value, "--proto_path=")) { 63 AddPathSubstitutions(argument_value, substitutions); 64 } else if (unprocessed) { 65 unprocessed->push_back(argument); 66 } 67 } 68 } 69 } 70 71 } // namespace 72 73 void ParsePathSubstitutions( 74 std::vector<std::string> args, 75 std::vector<std::pair<std::string, std::string>>* substitutions, 76 std::vector<std::string>* unprocessed) { 77 _ParsePathSubstitutions(args, substitutions, unprocessed); 78 } 79 80 void ParsePathSubstitutions( 81 google::protobuf::RepeatedPtrField<std::string> args, 82 std::vector<std::pair<std::string, std::string>>* substitutions, 83 std::vector<std::string>* unprocessed) { 84 _ParsePathSubstitutions(args, substitutions, unprocessed); 85 } 86 87 std::vector<std::string> PathSubstitutionsToArgs( 88 const std::vector<std::pair<std::string, std::string>>& substitutions) { 89 std::vector<std::string> args; 90 for (const auto& sub : substitutions) { 91 // Record in compilation unit args so indexer gets the same paths. 92 args.push_back("--proto_path"); 93 if (sub.first.empty()) { 94 args.push_back(sub.second); 95 } else { 96 args.push_back(absl::StrCat(sub.first, "=", sub.second)); 97 } 98 } 99 return args; 100 } 101 102 } // namespace lang_proto 103 } // namespace kythe