Skip to content

Commit

Permalink
feat(cxx_indexer): Read the semantic field into metadata for #5037 (#…
Browse files Browse the repository at this point in the history
…5040)

This also fixes a possible problem where all fields of a Rule
may not be initialized in protobuf_metadata_file.cc.
  • Loading branch information
zrlk committed Aug 19, 2021
1 parent 2524ef6 commit 06c49fc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
19 changes: 16 additions & 3 deletions kythe/cxx/common/kythe_metadata_file.cc
Expand Up @@ -123,16 +123,27 @@ absl::optional<MetadataFile::Rule> MetadataFile::LoadMetaElement(
}

absl::string_view edge_string = mapping.edge();
if (edge_string.empty()) {
if (edge_string.empty() && !(mapping.type() == MappingRule::ANCHOR_DEFINES &&
mapping.semantic() != MappingRule::SEMA_NONE)) {
LOG(WARNING) << "When loading metadata: empty edge.";
return absl::nullopt;
}

bool reverse_edge = absl::ConsumePrefix(&edge_string, "%");
if (mapping.type() == MappingRule::ANCHOR_DEFINES) {
if (!CheckVName(mapping.vname())) {
return absl::nullopt;
}
Semantic sema;
switch (mapping.semantic()) {
case MappingRule::SEMA_WRITE:
sema = Semantic::kWrite;
break;
case MappingRule::SEMA_READ_WRITE:
sema = Semantic::kReadWrite;
break;
default:
sema = Semantic::kNone;
}
return MetadataFile::Rule{mapping.begin(),
mapping.end(),
kythe::common::schema::kDefinesBinding,
Expand All @@ -141,7 +152,9 @@ absl::optional<MetadataFile::Rule> MetadataFile::LoadMetaElement(
reverse_edge,
false,
0,
0};
0,
false,
sema};
} else if (mapping.type() == MappingRule::ANCHOR_ANCHOR) {
if (!CheckVName(mapping.source_vname())) {
return absl::nullopt;
Expand Down
8 changes: 8 additions & 0 deletions kythe/cxx/common/kythe_metadata_file.h
Expand Up @@ -31,6 +31,13 @@ namespace kythe {

class MetadataFile {
public:
/// \brief An additional semantic to apply to the given (C++) node.
enum class Semantic {
kNone, ///< No special semantics.
kWrite, ///< Write semantics.
kReadWrite, ///< Read+write semantics.
};

/// \brief A single metadata rule.
struct Rule {
unsigned begin; ///< Beginning of the range to match.
Expand All @@ -44,6 +51,7 @@ class MetadataFile {
unsigned anchor_begin; ///< The beginning of the anchor.
unsigned anchor_end; ///< The end of the anchor.
bool whole_file; ///< Whether to ignore begin/end
Semantic semantic; ///< Whether to apply special semantics.
};

/// Creates a new MetadataFile from a list of rules ranging from `begin` to
Expand Down
4 changes: 2 additions & 2 deletions kythe/cxx/common/protobuf_metadata_file.cc
Expand Up @@ -66,7 +66,7 @@ std::unique_ptr<kythe::MetadataFile> ProtobufMetadataSupport::ParseFile(
std::vector<MetadataFile::Rule> rules;
int file_rule = -1;
for (const auto& annotation : info.annotation()) {
MetadataFile::Rule rule;
MetadataFile::Rule rule{};
rule.whole_file = false;
rule.begin = annotation.begin();
rule.end = annotation.end();
Expand All @@ -90,7 +90,7 @@ std::unique_ptr<kythe::MetadataFile> ProtobufMetadataSupport::ParseFile(
// source file. (Other VNames end up due to the inclusion of
// forward-declarations early in the .h).
if (file_rule >= 0) {
MetadataFile::Rule rule;
MetadataFile::Rule rule{};
rule.whole_file = true;
rule.vname = rules[file_rule].vname;
rule.vname.set_signature("");
Expand Down
3 changes: 2 additions & 1 deletion kythe/cxx/indexer/cxx/KytheGraphObserver.cc
Expand Up @@ -400,7 +400,8 @@ void KytheGraphObserver::MetaHookDefines(const MetadataFile& meta,
const VNameRef& decl) {
auto rules = meta.rules().equal_range(range_begin);
for (auto rule = rules.first; rule != rules.second; ++rule) {
if (rule->second.begin == range_begin && rule->second.end == range_end &&
if (rule->second.semantic == MetadataFile::Semantic::kNone &&
rule->second.begin == range_begin && rule->second.end == range_end &&
(rule->second.edge_in == kythe::common::schema::kDefines ||
rule->second.edge_in == kythe::common::schema::kDefinesBinding)) {
VNameRef remote(rule->second.vname);
Expand Down

0 comments on commit 06c49fc

Please sign in to comment.