-
Notifications
You must be signed in to change notification settings - Fork 214
/
kythe_facts.cc
171 lines (150 loc) · 5.83 KB
/
kythe_facts.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "verilog/tools/kythe/kythe_facts.h"
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
#include "absl/hash/hash.h"
#include "absl/log/check.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "common/util/spacer.h"
namespace verilog {
namespace kythe {
namespace {
// Returns a hash value produced by merging two hash values.
size_t CombineHash(size_t existing, size_t addition) {
// Taken from boost::hash_combine. Maybe replace with AbslHashValue::combine.
return existing ^ (addition + 0x9e3779b9 + (existing << 6) + (existing >> 2));
}
// Returns a rolling hash (https://en.wikipedia.org/wiki/Rolling_hash) of the
// signature names. NOTE: the first name (the file) is skipped and replaced with
// 0.
//
// The rolling hash of a vector produces a vector of an equal size where each
// element is a combined hash of all previous elements.
// res[0] = 0 // Global scope hash
// res[1] = hash(0, name[0])
// res[2] = hash(0, name[0], name[1])
// ...
// res[N] = hash(0, name[0], name[1], ..., name[N])
std::vector<size_t> RollingHash(const std::vector<absl::string_view> &names) {
if (names.size() <= 1) {
return {0}; // Global scope
}
std::vector<size_t> hashes = {0}; // Prefix with the global scope
hashes.reserve(names.size());
size_t previous = 0;
// Start from 2nd element --> skip the filename.
for (size_t i = 1; i < names.size(); ++i) {
previous = CombineHash(previous, absl::HashOf(names[i]));
hashes.push_back(previous);
}
CHECK_EQ(hashes.size(), names.size());
return hashes;
}
} // namespace
std::string Signature::ToString() const {
std::string signature;
for (absl::string_view name : names_) {
if (name.empty()) continue;
absl::StrAppend(&signature, name, "#");
}
return signature;
}
std::string Signature::ToBase64() const {
return absl::Base64Escape(ToString());
}
SignatureDigest Signature::Digest() const {
return SignatureDigest{.rolling_hash = RollingHash(Names())};
}
bool VName::operator==(const VName &other) const {
return path == other.path && root == other.root && corpus == other.corpus &&
signature == other.signature && language == other.language;
}
std::ostream &VName::FormatJSON(std::ostream &stream, bool debug,
int indentation) const {
// Output new line only in debug mode.
const std::string separator = debug ? "\n" : "";
stream << "{" << separator;
const verible::Spacer idt(indentation + 2);
stream << idt << "\"signature\": \""
<< (debug ? signature.ToString() : signature.ToBase64()) << "\","
<< separator << idt << "\"path\": \"" << path << "\"," << separator
<< idt << "\"language\": \"" << language << "\"," << separator << idt
<< "\"root\": \"" << root << "\"," << separator << idt
<< "\"corpus\": \"" << corpus << "\"" << separator
<< verible::Spacer(indentation) << "}";
return stream;
}
std::ostream &operator<<(std::ostream &stream, const VName &vname) {
return vname.FormatJSON(stream, /*debug=*/true);
}
bool Fact::operator==(const Fact &other) const {
return fact_value == other.fact_value && fact_name == other.fact_name &&
node_vname == other.node_vname;
}
std::ostream &Fact::FormatJSON(std::ostream &stream, bool debug,
int indentation) const {
// Output new line only in debug mode.
const std::string separator = debug ? "\n" : "";
// Indent entries in debug mode
const int indent_more = debug ? indentation + 2 : 0;
const verible::Spacer idt(indent_more);
stream << "{" << separator;
{
stream << idt << "\"source\": ";
node_vname.FormatJSON(stream, debug, indent_more) << "," << separator;
}
stream << idt << "\"fact_name\": \"" << fact_name << "\"," << separator //
<< idt << "\"fact_value\": \""
<< (debug ? fact_value : absl::Base64Escape(fact_value)) << "\""
<< separator //
<< verible::Spacer(indentation) << "}";
return stream;
}
std::ostream &operator<<(std::ostream &stream, const Fact &fact) {
return fact.FormatJSON(stream, /*debug=*/true);
}
bool Edge::operator==(const Edge &other) const {
return edge_name == other.edge_name && source_node == other.source_node &&
target_node == other.target_node;
}
std::ostream &Edge::FormatJSON(std::ostream &stream, bool debug,
int indentation) const {
// Output new line only in debug mode.
const std::string separator = debug ? "\n" : "";
// Indent entries in debug mode
const int indent_more = debug ? indentation + 2 : 0;
const verible::Spacer idt(indent_more);
stream << "{" << separator;
{
stream << idt << "\"source\": ";
source_node.FormatJSON(stream, debug, indent_more) << "," << separator;
}
{ stream << idt << "\"edge_kind\": \"" << edge_name << "\"," << separator; }
{
stream << idt << "\"target\": ";
target_node.FormatJSON(stream, debug, indent_more) << "," << separator;
}
{ stream << idt << "\"fact_name\": \"/\"" << separator; }
return stream << verible::Spacer(indentation) << "}";
}
std::ostream &operator<<(std::ostream &stream, const Edge &edge) {
return edge.FormatJSON(stream, /*debug=*/true);
}
} // namespace kythe
} // namespace verilog