[clangd] Avoid range-loop init-list lifetime subtleties.
authorSam McCall <sam.mccall@gmail.com>
Fri, 23 Jul 2021 12:26:45 +0000 (14:26 +0200)
committerSam McCall <sam.mccall@gmail.com>
Fri, 23 Jul 2021 13:33:04 +0000 (15:33 +0200)
The original code appears to be OK per the spec, but we've had 3 reports of
crashes with certain unofficial builds of clangd that look a lot like old
compilers (GCC 5.4?) getting lifetime rules wrong.

Fixes https://github.com/clangd/clangd/issues/800

Differential Revision: https://reviews.llvm.org/D106654

clang-tools-extra/clangd/GlobalCompilationDatabase.cpp

index d830190..73f19ab 100644 (file)
@@ -276,7 +276,7 @@ bool DirectoryBasedGlobalCompilationDatabase::DirectoryCache::load(
   // For these, we know the files they read and cache their metadata so we can
   // cheaply validate whether they've changed, and hot-reload if they have.
   // (As a bonus, these are also VFS-clean)!
-  struct CDBFile {
+  struct {
     CachedFile *File;
     // Wrapper for {Fixed,JSON}CompilationDatabase::loadFromBuffer.
     llvm::function_ref<std::unique_ptr<tooling::CompilationDatabase>(
@@ -284,10 +284,12 @@ bool DirectoryBasedGlobalCompilationDatabase::DirectoryCache::load(
         /*Data*/ llvm::StringRef,
         /*ErrorMsg*/ std::string &)>
         Parser;
+  } Files[] = {
+      {&CompileCommandsJson, parseJSON},
+      {&BuildCompileCommandsJson, parseJSON},
+      {&CompileFlagsTxt, parseFixed},
   };
-  for (const auto &Entry : {CDBFile{&CompileCommandsJson, parseJSON},
-                            CDBFile{&BuildCompileCommandsJson, parseJSON},
-                            CDBFile{&CompileFlagsTxt, parseFixed}}) {
+  for (const auto &Entry : Files) {
     bool Active = ActiveCachedFile == Entry.File;
     auto Loaded = Entry.File->load(FS, Active);
     switch (Loaded.Result) {