Skip to content

Commit 8ca10dc

Browse files
committed
Replace std::multimap with llvm::StringMap (NFC)
While also being more efficient and shorter, this is done mainly in preparation for followup changes.
1 parent ff16a8d commit 8ca10dc

File tree

5 files changed

+59
-84
lines changed

5 files changed

+59
-84
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class LLDBExprNameLookup : public LLDBNameLookup {
219219
// kind lldb explicitly wants to globalize.
220220
if (shouldGlobalize(value_decl->getBaseName().getIdentifier(),
221221
value_decl->getKind()))
222-
m_staged_decls.AddDecl(value_decl, false, ConstString());
222+
m_staged_decls.AddDecl(value_decl, false, {});
223223
}
224224
}
225225

@@ -248,11 +248,10 @@ class LLDBExprNameLookup : public LLDBNameLookup {
248248
if (NameStr.empty())
249249
return false;
250250

251-
ConstString name_const_str(NameStr);
252-
if (m_log) {
253-
m_log->Printf("[LLDBExprNameLookup::lookupAdditions (%u)] Searching for %s",
254-
count, name_const_str.AsCString("<anonymous>"));
255-
}
251+
if (m_log)
252+
m_log->Printf(
253+
"[LLDBExprNameLookup::lookupAdditions (%u)] Searching for %s", count,
254+
NameStr.empty() ? "<anonymous>" : NameStr.str().c_str());
256255

257256
std::vector<swift::ValueDecl *> results;
258257

@@ -267,7 +266,7 @@ class LLDBExprNameLookup : public LLDBNameLookup {
267266
// Later, when we look for persistent decls, these staged decls
268267
// take precedence.
269268

270-
m_staged_decls.FindMatchingDecls(name_const_str, {}, results);
269+
m_staged_decls.FindMatchingDecls(NameStr, {}, results);
271270

272271
// Next look up persistent decls matching this name. Then, if we
273272
// aren't looking at a debugger variable, filter out persistent
@@ -294,7 +293,7 @@ class LLDBExprNameLookup : public LLDBNameLookup {
294293
size_t num_external_results = RV.size();
295294
if (!is_debugger_variable && num_external_results > 0) {
296295
std::vector<swift::ValueDecl *> persistent_results;
297-
m_persistent_vars->GetSwiftPersistentDecls(name_const_str, {},
296+
m_persistent_vars->GetSwiftPersistentDecls(NameStr, {},
298297
persistent_results);
299298

300299
size_t num_persistent_results = persistent_results.size();
@@ -336,8 +335,7 @@ class LLDBExprNameLookup : public LLDBNameLookup {
336335
results.push_back(value_decl);
337336
}
338337
} else {
339-
m_persistent_vars->GetSwiftPersistentDecls(name_const_str, results,
340-
results);
338+
m_persistent_vars->GetSwiftPersistentDecls(NameStr, results, results);
341339
}
342340
}
343341

@@ -401,11 +399,10 @@ class LLDBREPLNameLookup : public LLDBNameLookup {
401399
if (NameStr.empty())
402400
return false;
403401

404-
ConstString name_const_str(NameStr);
405-
if (m_log) {
406-
m_log->Printf("[LLDBREPLNameLookup::lookupAdditions (%u)] Searching for %s",
407-
count, name_const_str.AsCString("<anonymous>"));
408-
}
402+
if (m_log)
403+
m_log->Printf(
404+
"[LLDBREPLNameLookup::lookupAdditions (%u)] Searching for %s", count,
405+
NameStr.empty() ? "<anonymous>" : NameStr.str().c_str());
409406

410407
// Find decls that come from the current compilation.
411408
std::vector<swift::ValueDecl *> current_compilation_results;
@@ -420,9 +417,8 @@ class LLDBREPLNameLookup : public LLDBNameLookup {
420417
// decls from the current compilation. This makes the decls from
421418
// the current compilation take precedence.
422419
std::vector<swift::ValueDecl *> persistent_decl_results;
423-
m_persistent_vars->GetSwiftPersistentDecls(name_const_str,
424-
current_compilation_results,
425-
persistent_decl_results);
420+
m_persistent_vars->GetSwiftPersistentDecls(
421+
NameStr, current_compilation_results, persistent_decl_results);
426422

427423
// Append the persistent decls that we found to the result vector.
428424
for (auto result : persistent_decl_results) {

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionSourceCode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ Status SwiftExpressionSourceCode::GetText(
619619
}
620620
std::vector<swift::ValueDecl *> persistent_results;
621621
// Check if we have already declared the playground stub debug functions
622-
persistent_state->GetSwiftPersistentDecls(ConstString("__builtin_log_with_id"), {},
622+
persistent_state->GetSwiftPersistentDecls("__builtin_log_with_id", {},
623623
persistent_results);
624624

625625
size_t num_persistent_results = persistent_results.size();

lldb/source/Plugins/ExpressionParser/Swift/SwiftPersistentExpressionState.cpp

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -129,85 +129,66 @@ bool SwiftPersistentExpressionState::SwiftDeclMap::DeclsAreEquivalent(
129129
}
130130

131131
void SwiftPersistentExpressionState::SwiftDeclMap::AddDecl(
132-
swift::ValueDecl *value_decl, bool check_existing, ConstString alias) {
133-
std::string name_str;
132+
swift::ValueDecl *value_decl, bool check_existing, llvm::StringRef alias) {
133+
llvm::StringRef name;
134134

135-
if (alias.IsEmpty()) {
136-
name_str = value_decl->getBaseName().getIdentifier().str().str();
137-
} else {
138-
name_str.assign(alias.GetCString());
139-
}
135+
if (alias.empty())
136+
name = value_decl->getBaseName().getIdentifier().str();
137+
else
138+
name = alias;
140139

141-
if (!check_existing) {
142-
m_swift_decls.insert(std::make_pair(name_str, value_decl));
140+
auto it = m_swift_decls.find(name);
141+
if (it == m_swift_decls.end()) {
142+
m_swift_decls.insert({name, {value_decl}});
143143
return;
144144
}
145145

146-
SwiftDeclMapTy::iterator map_end = m_swift_decls.end();
147-
std::pair<SwiftDeclMapTy::iterator, SwiftDeclMapTy::iterator> found_range =
148-
m_swift_decls.equal_range(name_str);
146+
llvm::SmallVectorImpl<swift::ValueDecl *> &decls = it->second;
147+
if (check_existing)
148+
decls.erase(std::remove_if(decls.begin(), decls.end(),
149+
[&value_decl](swift::ValueDecl *cur_decl) {
150+
return DeclsAreEquivalent(cur_decl,
151+
value_decl);
152+
}),
153+
decls.end());
149154

150-
if (found_range.first == map_end) {
151-
m_swift_decls.insert(std::make_pair(name_str, value_decl));
152-
return;
153-
} else {
154-
SwiftDeclMapTy::iterator cur_item;
155-
bool done = false;
156-
for (cur_item = found_range.first; !done && cur_item != found_range.second;
157-
cur_item++) {
158-
swift::ValueDecl *cur_decl = (*cur_item).second;
159-
if (DeclsAreEquivalent(cur_decl, value_decl)) {
160-
m_swift_decls.erase(cur_item);
161-
break;
162-
}
163-
}
164-
165-
m_swift_decls.insert(std::make_pair(name_str, value_decl));
166-
}
155+
decls.push_back(value_decl);
167156
}
168157

169158
bool SwiftPersistentExpressionState::SwiftDeclMap::FindMatchingDecls(
170-
ConstString name,
159+
llvm::StringRef name,
171160
const std::vector<swift::ValueDecl *> &excluding_equivalents,
172161
std::vector<swift::ValueDecl *> &matches) {
173-
std::string name_str(name.AsCString());
162+
auto it = m_swift_decls.find(name);
163+
if (it == m_swift_decls.end())
164+
return false;
165+
llvm::SmallVectorImpl<swift::ValueDecl *> &decls = it->second;
166+
174167
size_t start_num_items = matches.size();
175-
size_t start_num_excluding_equivalents = excluding_equivalents.size();
176-
177-
std::pair<SwiftDeclMapTy::iterator, SwiftDeclMapTy::iterator> found_range =
178-
m_swift_decls.equal_range(name_str);
179-
for (SwiftDeclMapTy::iterator cur_item = found_range.first;
180-
cur_item != found_range.second; cur_item++) {
181-
bool add_it = true;
182-
swift::ValueDecl *cur_decl = (*cur_item).second;
183-
184-
// Iterate over only the elements of `excluding_equivalents` that were
185-
// originally there, in case `matches` aliases it.
186-
for (size_t idx = 0; idx < start_num_excluding_equivalents; idx++) {
187-
if (DeclsAreEquivalent(excluding_equivalents[idx], cur_decl)) {
188-
add_it = false;
189-
break;
190-
}
191-
}
192-
if (add_it)
193-
matches.push_back((*cur_item).second);
194-
}
168+
for (auto &cur_decl : decls)
169+
if (std::none_of(excluding_equivalents.begin(), excluding_equivalents.end(),
170+
[&](swift::ValueDecl *decl) {
171+
return DeclsAreEquivalent(cur_decl, decl);
172+
}))
173+
matches.push_back(cur_decl);
174+
195175
return start_num_items != matches.size();
196176
}
197177

198178
void SwiftPersistentExpressionState::SwiftDeclMap::CopyDeclsTo(
199179
SwiftPersistentExpressionState::SwiftDeclMap &target_map) {
200-
for (auto elem : m_swift_decls)
201-
target_map.AddDecl(elem.second, true, ConstString());
180+
for (auto &entry : m_swift_decls)
181+
for (auto &elem : entry.second)
182+
target_map.AddDecl(elem, true, {});
202183
}
203184

204185
void SwiftPersistentExpressionState::RegisterSwiftPersistentDecl(
205186
swift::ValueDecl *value_decl) {
206-
m_swift_persistent_decls.AddDecl(value_decl, true, ConstString());
187+
m_swift_persistent_decls.AddDecl(value_decl, true, {});
207188
}
208189

209190
void SwiftPersistentExpressionState::RegisterSwiftPersistentDeclAlias(
210-
swift::ValueDecl *value_decl, ConstString name) {
191+
swift::ValueDecl *value_decl, llvm::StringRef name) {
211192
m_swift_persistent_decls.AddDecl(value_decl, true, name);
212193
}
213194

@@ -217,7 +198,7 @@ void SwiftPersistentExpressionState::CopyInSwiftPersistentDecls(
217198
}
218199

219200
bool SwiftPersistentExpressionState::GetSwiftPersistentDecls(
220-
ConstString name,
201+
llvm::StringRef name,
221202
const std::vector<swift::ValueDecl *> &excluding_equivalents,
222203
std::vector<swift::ValueDecl *> &matches) {
223204
return m_swift_persistent_decls.FindMatchingDecls(name, excluding_equivalents,

lldb/source/Plugins/ExpressionParser/Swift/SwiftPersistentExpressionState.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,22 @@ class SwiftPersistentExpressionState : public PersistentExpressionState {
4545
public:
4646
class SwiftDeclMap {
4747
public:
48-
void AddDecl(swift::ValueDecl *decl, bool check_existing, ConstString name);
48+
void AddDecl(swift::ValueDecl *decl, bool check_existing,
49+
llvm::StringRef name);
4950

5051
/// Find decls matching `name`, excluding decls that are equivalent to
5152
/// decls in `excluding_equivalents`, and put the results in `matches`.
5253
/// Return true if there are any results.
5354
bool FindMatchingDecls(
54-
ConstString name,
55+
llvm::StringRef name,
5556
const std::vector<swift::ValueDecl *> &excluding_equivalents,
5657
std::vector<swift::ValueDecl *> &matches);
5758

5859
void CopyDeclsTo(SwiftDeclMap &target_map);
5960
static bool DeclsAreEquivalent(swift::Decl *lhs, swift::Decl *rhs);
6061

6162
private:
62-
typedef std::unordered_multimap<std::string, swift::ValueDecl *>
63-
SwiftDeclMapTy;
64-
typedef SwiftDeclMapTy::iterator iterator;
65-
SwiftDeclMapTy m_swift_decls;
63+
llvm::StringMap<llvm::SmallVector<swift::ValueDecl *, 1>> m_swift_decls;
6664
};
6765

6866
//----------------------------------------------------------------------
@@ -102,15 +100,15 @@ class SwiftPersistentExpressionState : public PersistentExpressionState {
102100
void RegisterSwiftPersistentDecl(swift::ValueDecl *value_decl);
103101

104102
void RegisterSwiftPersistentDeclAlias(swift::ValueDecl *value_decl,
105-
ConstString name);
103+
llvm::StringRef name);
106104

107105
void CopyInSwiftPersistentDecls(SwiftDeclMap &source_map);
108106

109107
/// Find decls matching `name`, excluding decls that are equivalent to decls
110108
/// in `excluding_equivalents`, and put the results in `matches`. Return true
111109
/// if there are any results.
112110
bool GetSwiftPersistentDecls(
113-
ConstString name,
111+
llvm::StringRef name,
114112
const std::vector<swift::ValueDecl *> &excluding_equivalents,
115113
std::vector<swift::ValueDecl *> &matches);
116114

lldb/source/Plugins/ExpressionParser/Swift/SwiftREPLMaterializer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class EntityREPLResultVariable : public Materializer::Entity {
186186

187187
if (m_swift_decl) {
188188
llvm::cast<SwiftPersistentExpressionState>(persistent_state)
189-
->RegisterSwiftPersistentDeclAlias(m_swift_decl, name);
189+
->RegisterSwiftPersistentDeclAlias(m_swift_decl, name.GetStringRef());
190190
}
191191

192192
return;

0 commit comments

Comments
 (0)