Handle PluginAttrInstances using ManagedStatic
authorJohn Brawn <john.brawn@arm.com>
Wed, 4 Mar 2020 13:25:06 +0000 (13:25 +0000)
committerJohn Brawn <john.brawn@arm.com>
Wed, 4 Mar 2020 15:03:13 +0000 (15:03 +0000)
We need to make sure that PluginAttrInstances is deleted before shared libraries
are unloaded, because otherwise when deleting its contents we'll try to access
a virtual destructor which no longer exists.

As shared libraries are managed using ManagedStatic we can do this by also using
ManagedStatic for PluginAttrInstances as ManagedStatics are deleted in reverse
order of construction and we know that PluginAttrInstances will only be
accessed, and thus constructed, after shared libraries have been loaded.

clang/lib/Sema/ParsedAttr.cpp

index 45df8cf..fa7b59d 100644 (file)
@@ -19,6 +19,7 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/ManagedStatic.h"
 #include <cassert>
 #include <cstddef>
 #include <utility>
@@ -121,10 +122,11 @@ const ParsedAttrInfo &ParsedAttrInfo::get(const AttributeCommonInfo &A) {
 
   // Otherwise this may be an attribute defined by a plugin. First instantiate
   // all plugin attributes if we haven't already done so.
-  static std::list<std::unique_ptr<ParsedAttrInfo>> PluginAttrInstances;
-  if (PluginAttrInstances.empty())
+  static llvm::ManagedStatic<std::list<std::unique_ptr<ParsedAttrInfo>>>
+      PluginAttrInstances;
+  if (PluginAttrInstances->empty())
     for (auto It : ParsedAttrInfoRegistry::entries())
-      PluginAttrInstances.emplace_back(It.instantiate());
+      PluginAttrInstances->emplace_back(It.instantiate());
 
   // Search for a ParsedAttrInfo whose name and syntax match.
   std::string FullName = A.getNormalizedFullName();
@@ -132,7 +134,7 @@ const ParsedAttrInfo &ParsedAttrInfo::get(const AttributeCommonInfo &A) {
   if (SyntaxUsed == AttributeCommonInfo::AS_ContextSensitiveKeyword)
     SyntaxUsed = AttributeCommonInfo::AS_Keyword;
 
-  for (auto &Ptr : PluginAttrInstances)
+  for (auto &Ptr : *PluginAttrInstances)
     for (auto &S : Ptr->Spellings)
       if (S.Syntax == SyntaxUsed && S.NormalizedFullName == FullName)
         return *Ptr;