Skip to content

Commit 6d448ac

Browse files
fix: Use $ in version instead of Descriptor (#357)
When the descriptor contains an escaped identifier, it needs to contain a backtick at the start. This means that indiscriminately prefixing $ to a descriptor can create an ill-formed symbol name. So instead, include the $ in the version.
1 parent 0b54337 commit 6d448ac

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

indexer/SymbolFormatter.cc

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,19 @@ void DescriptorBuilder::formatTo(llvm::raw_ostream &out) const {
100100
void SymbolBuilder::formatTo(std::string &buf) const {
101101
llvm::raw_string_ostream out(buf);
102102
out << "cxx . "; // scheme manager
103-
for (auto text : {this->packageId.name, this->packageId.version}) {
104-
if (text.empty()) {
105-
out << ". ";
106-
continue;
107-
}
108-
::addSpaceEscaped(out, text);
103+
if (this->packageId.name.empty()) {
104+
out << ". ";
105+
} else {
106+
::addSpaceEscaped(out, this->packageId.name);
109107
out << ' ';
110108
}
111-
// NOTE(def: symbol-string-hack-for-forward-decls): Add a '$' prefix
112-
// after the space after the version, but before any descriptors.
113-
// When splitting the symbol, we check for the '$' preceded by a ' '.
114-
// Strictly speaking, it is possible that the same pattern is present
115-
// due to a funky filename which contains the pattern ' $', it's highly
116-
// improbable to cause any incorrect lookup by not colliding with an
117-
// actual symbol name.
118-
out << '$';
109+
if (!this->packageId.version.empty()) {
110+
::addSpaceEscaped(out, this->packageId.version);
111+
}
112+
out << "$ ";
113+
// NOTE(def: symbol-string-hack-for-forward-decls): Add a '$' suffix
114+
// after the version, but before the space for the symbol name.
115+
// When splitting the symbol, we check for the '$' followed by a ' '.
119116
for (auto &descriptor : this->descriptors) {
120117
descriptor.formatTo(out);
121118
}

indexer/SymbolName.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ std::optional<scip::SymbolSuffix>
2525
SymbolBuilder::getPackageAgnosticSuffix(scip::SymbolNameRef name) {
2626
// See NOTE(ref: symbol-string-hack-for-forward-decls)
2727
auto ix = name.value.find('$');
28-
if (ix == std::string::npos || ix == 0 || name.value[ix - 1] != ' ') {
28+
if (ix == std::string::npos || ix == name.value.size() - 1
29+
|| name.value[ix + 1] != ' ') {
2930
return std::nullopt;
3031
}
31-
return scip::SymbolSuffix{name.value.substr(ix + 1)};
32+
return scip::SymbolSuffix{name.value.substr(ix + 2)};
3233
}
3334

3435
// static
3536
scip::SymbolName SymbolBuilder::addFakePrefix(scip::SymbolSuffix suffix) {
3637
std::string buf;
37-
#define FAKE_SYMBOL_PREFIX "cxx . . . $"
38+
#define FAKE_SYMBOL_PREFIX "cxx . . $ "
3839
buf.reserve(sizeof(FAKE_SYMBOL_PREFIX) + suffix.value.size());
3940
buf.append(FAKE_SYMBOL_PREFIX);
4041
#undef FAKE_SYMBOL_PREFIX

test/Snapshot.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static std::vector<RootRelativePath> listFilesRecursive(const RootPath &root) {
9393
static std::string formatSymbol(const std::string &symbol) {
9494
// Strip out repeating information for cleaner snapshots.
9595
return absl::StrReplaceAll(symbol, {
96-
{"cxx . . . $", "[..] "},
96+
{"cxx . . $ ", "[..] "},
9797
{"cxx . ", ""}, // indexer prefix
9898
{"todo-pkg todo-version", "[..]"},
9999
});

0 commit comments

Comments
 (0)