kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/indexing/KytheGraphRecorder.cc (about) 1 /* 2 * Copyright 2014 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 "KytheGraphRecorder.h" 18 19 #include <cstddef> 20 #include <cstdint> 21 #include <string> 22 #include <vector> 23 24 #include "absl/strings/string_view.h" 25 #include "kythe/cxx/common/indexing/KytheOutputStream.h" 26 27 namespace kythe { 28 29 static const std::string* const kNodeKindSpellings[] = { 30 new std::string("anchor"), new std::string("file"), 31 new std::string("variable"), new std::string("talias"), 32 new std::string("tapp"), new std::string("tnominal"), 33 new std::string("record"), new std::string("sum"), 34 new std::string("constant"), new std::string("function"), 35 new std::string("lookup"), new std::string("macro"), 36 new std::string("interface"), new std::string("package"), 37 new std::string("tsigma"), new std::string("doc"), 38 new std::string("tbuiltin"), new std::string("meta"), 39 new std::string("diagnostic"), new std::string("clang/usr"), 40 new std::string("tvar"), new std::string("name")}; 41 42 static const std::string* kEdgeKindSpellings[] = { 43 new std::string("/kythe/edge/defines"), 44 new std::string("/kythe/edge/typed"), 45 new std::string("/kythe/edge/ref"), 46 new std::string("/kythe/edge/ref/implicit"), 47 new std::string("/kythe/edge/ref/imports"), 48 new std::string("/kythe/edge/param"), 49 new std::string("/kythe/edge/aliases"), 50 new std::string("/kythe/edge/aliases/root"), 51 new std::string("/kythe/edge/childof"), 52 new std::string("/kythe/edge/specializes"), 53 new std::string("/kythe/edge/ref/call"), 54 new std::string("/kythe/edge/ref/call/implicit"), 55 new std::string("/kythe/edge/ref/expands"), 56 new std::string("/kythe/edge/undefines"), 57 new std::string("/kythe/edge/ref/includes"), 58 new std::string("/kythe/edge/ref/queries"), 59 new std::string("/kythe/edge/instantiates"), 60 new std::string("/kythe/edge/ref/expands/transitive"), 61 new std::string("/kythe/edge/extends/public"), 62 new std::string("/kythe/edge/extends/protected"), 63 new std::string("/kythe/edge/extends/private"), 64 new std::string("/kythe/edge/extends"), 65 new std::string("/kythe/edge/extends/public/virtual"), 66 new std::string("/kythe/edge/extends/protected/virtual"), 67 new std::string("/kythe/edge/extends/private/virtual"), 68 new std::string("/kythe/edge/extends/virtual"), 69 new std::string("/kythe/edge/extends/category"), 70 new std::string("/kythe/edge/specializes/speculative"), 71 new std::string("/kythe/edge/instantiates/speculative"), 72 new std::string("/kythe/edge/documents"), 73 new std::string("/kythe/edge/ref/doc"), 74 new std::string("/kythe/edge/generates"), 75 new std::string("/kythe/edge/defines/binding"), 76 new std::string("/kythe/edge/overrides"), 77 new std::string("/kythe/edge/overrides/root"), 78 new std::string("/kythe/edge/childof/context"), 79 new std::string("/kythe/edge/bounded/upper"), 80 new std::string("/kythe/edge/ref/init"), 81 new std::string("/kythe/edge/ref/init/implicit"), 82 new std::string("/kythe/edge/imputes"), 83 new std::string("/kythe/edge/tagged"), 84 new std::string("/kythe/edge/property/reads"), 85 new std::string("/kythe/edge/property/writes"), 86 new std::string("/clang/usr"), 87 new std::string("/kythe/edge/ref/id"), 88 new std::string("/kythe/edge/ref/writes"), 89 new std::string("/kythe/edge/ref/writes/implicit"), 90 new std::string("/kythe/edge/influences"), 91 new std::string("/kythe/edge/ref/file"), 92 new std::string("/kythe/edge/tparam"), 93 new std::string("/kythe/edge/completedby"), 94 new std::string("/kythe/edge/ref/call/direct"), 95 new std::string("/kythe/edge/ref/call/direct/implicit"), 96 new std::string("/kythe/edge/denotes"), 97 new std::string("/kythe/edge/named"), 98 new std::string("/kythe/edge/exp/typed/init")}; 99 100 bool of_spelling(absl::string_view str, EdgeKindID* edge_id) { 101 size_t edge_index = 0; 102 for (auto* edge : kEdgeKindSpellings) { 103 if (*edge == str) { 104 *edge_id = static_cast<kythe::EdgeKindID>(edge_index); 105 return true; 106 } 107 ++edge_index; 108 } 109 return false; 110 } 111 112 static const std::string* const kPropertySpellings[] = { 113 new std::string("/kythe/loc"), 114 new std::string("/kythe/loc/uri"), 115 new std::string("/kythe/loc/start"), 116 new std::string("/kythe/loc/start/row"), 117 new std::string("/kythe/loc/start"), 118 new std::string("/kythe/loc/end"), 119 new std::string("/kythe/loc/end/row"), 120 new std::string("/kythe/loc/end"), 121 new std::string("/kythe/text"), 122 new std::string("/kythe/complete"), 123 new std::string("/kythe/subkind"), 124 new std::string("/kythe/node/kind"), 125 new std::string("/kythe/code"), 126 new std::string("/kythe/variance"), 127 new std::string("/kythe/param/default"), 128 new std::string("/kythe/tag/static"), 129 new std::string("/kythe/tag/deprecated"), 130 new std::string("/kythe/message"), 131 new std::string("/kythe/details"), 132 new std::string("/kythe/context/url"), 133 new std::string("/kythe/doc/uri"), 134 new std::string("/kythe/build/config"), 135 new std::string("/kythe/visibility"), 136 new std::string("/kythe/exp/code/flat"), 137 }; 138 139 static const std::string* const kEmptyStringSpelling = new std::string(""); 140 141 static const std::string* const kRootPropertySpelling = new std::string("/"); 142 143 absl::string_view spelling_of(PropertyID property_id) { 144 const auto* str = kPropertySpellings[static_cast<ptrdiff_t>(property_id)]; 145 return absl::string_view(str->data(), str->size()); 146 } 147 148 absl::string_view spelling_of(NodeKindID node_kind_id) { 149 const auto* str = kNodeKindSpellings[static_cast<ptrdiff_t>(node_kind_id)]; 150 return absl::string_view(str->data(), str->size()); 151 } 152 153 absl::string_view spelling_of(EdgeKindID edge_kind_id) { 154 const auto* str = kEdgeKindSpellings[static_cast<ptrdiff_t>(edge_kind_id)]; 155 return absl::string_view(str->data(), str->size()); 156 } 157 158 void KytheGraphRecorder::AddProperty(const VNameRef& node_vname, 159 PropertyID property_id, 160 absl::string_view property_value) { 161 stream_->Emit(FactRef{&node_vname, spelling_of(property_id), property_value}); 162 } 163 164 void KytheGraphRecorder::AddProperty(const VNameRef& node_vname, 165 PropertyID property_id, 166 const size_t property_value) { 167 AddProperty(node_vname, property_id, std::to_string(property_value)); 168 } 169 170 void KytheGraphRecorder::AddFlatSource(const VNameRef& node_vname, 171 std::string_view source) { 172 stream_->Emit( 173 FactRef{&node_vname, spelling_of(PropertyID::kFlatCode), source}); 174 } 175 176 void KytheGraphRecorder::AddMarkedSource(const VNameRef& node_vname, 177 const MarkedSource& marked_source) { 178 auto size = marked_source.ByteSizeLong(); 179 std::vector<char> buffer(size); 180 marked_source.SerializeToArray(buffer.data(), size); 181 stream_->Emit(FactRef{&node_vname, spelling_of(PropertyID::kCode), 182 absl::string_view(buffer.data(), buffer.size())}); 183 } 184 185 void KytheGraphRecorder::AddEdge(const VNameRef& edge_from, 186 EdgeKindID edge_kind_id, 187 const VNameRef& edge_to) { 188 stream_->Emit(EdgeRef{&edge_from, spelling_of(edge_kind_id), &edge_to}); 189 } 190 191 void KytheGraphRecorder::AddEdge(const VNameRef& edge_from, 192 EdgeKindID edge_kind_id, 193 const VNameRef& edge_to, uint32_t ordinal) { 194 stream_->Emit( 195 OrdinalEdgeRef{&edge_from, spelling_of(edge_kind_id), &edge_to, ordinal}); 196 } 197 198 void KytheGraphRecorder::AddFileContent(const VNameRef& file_vname, 199 absl::string_view file_content) { 200 AddProperty(file_vname, PropertyID::kNodeKind, 201 spelling_of(NodeKindID::kFile)); 202 AddProperty(file_vname, PropertyID::kText, file_content); 203 } 204 205 } // namespace kythe