[clang] 2bb3fb0 - Handle PluginAttrInstances using ManagedStatic

John Brawn via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 4 07:04:44 PST 2020


Author: John Brawn
Date: 2020-03-04T15:03:13Z
New Revision: 2bb3fb05e20e272790170e86fb76dac23a67f52c

URL: https://github.com/llvm/llvm-project/commit/2bb3fb05e20e272790170e86fb76dac23a67f52c
DIFF: https://github.com/llvm/llvm-project/commit/2bb3fb05e20e272790170e86fb76dac23a67f52c.diff

LOG: Handle PluginAttrInstances using ManagedStatic

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.

Added: 
    

Modified: 
    clang/lib/Sema/ParsedAttr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/ParsedAttr.cpp b/clang/lib/Sema/ParsedAttr.cpp
index 45df8cf5a85c..fa7b59de3e31 100644
--- a/clang/lib/Sema/ParsedAttr.cpp
+++ b/clang/lib/Sema/ParsedAttr.cpp
@@ -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;


        


More information about the cfe-commits mailing list