Skip to content

Commit 5bec85a

Browse files
committed
[clangd] Fix a lifetime bug in QueryDriver
llvm-svn: 365134
1 parent e712295 commit 5bec85a

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

clang-tools-extra/clangd/QueryDriverDatabase.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "llvm/Support/Regex.h"
4949
#include "llvm/Support/ScopedPrinter.h"
5050
#include <algorithm>
51+
#include <map>
5152
#include <string>
5253
#include <vector>
5354

@@ -221,16 +222,19 @@ class QueryDriverDatabase : public GlobalCompilationDatabase {
221222

222223
llvm::SmallString<128> Driver(Cmd->CommandLine.front());
223224
llvm::sys::fs::make_absolute(Cmd->Directory, Driver);
225+
llvm::StringRef Ext = llvm::sys::path::extension(File).trim('.');
226+
auto Key = std::make_pair(Driver.str(), Ext);
224227

225-
llvm::ArrayRef<std::string> SystemIncludes;
228+
std::vector<std::string> SystemIncludes;
226229
{
227230
std::lock_guard<std::mutex> Lock(Mu);
228231

229-
llvm::StringRef Ext = llvm::sys::path::extension(File).trim('.');
230-
auto It = DriverToIncludesCache.try_emplace({Driver, Ext});
231-
if (It.second)
232-
It.first->second = extractSystemIncludes(Driver, Ext, QueryDriverRegex);
233-
SystemIncludes = It.first->second;
232+
auto It = DriverToIncludesCache.find(Key);
233+
if (It != DriverToIncludesCache.end())
234+
SystemIncludes = It->second;
235+
else
236+
DriverToIncludesCache[Key] = SystemIncludes =
237+
extractSystemIncludes(Key.first, Key.second, QueryDriverRegex);
234238
}
235239

236240
return addSystemIncludes(*Cmd, SystemIncludes);
@@ -239,8 +243,8 @@ class QueryDriverDatabase : public GlobalCompilationDatabase {
239243
private:
240244
mutable std::mutex Mu;
241245
// Caches includes extracted from a driver.
242-
mutable llvm::DenseMap<std::pair<StringRef, StringRef>,
243-
std::vector<std::string>>
246+
mutable std::map<std::pair<std::string, std::string>,
247+
std::vector<std::string>>
244248
DriverToIncludesCache;
245249
mutable llvm::Regex QueryDriverRegex;
246250

0 commit comments

Comments
 (0)