Skip to content

Commit

Permalink
Handle PluginAttrInstances using ManagedStatic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
john-brawn-arm committed Mar 4, 2020
1 parent 00c5793 commit 2bb3fb0
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions clang/lib/Sema/ParsedAttr.cpp
Expand Up @@ -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>
Expand Down Expand Up @@ -121,18 +122,19 @@ 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();
AttributeCommonInfo::Syntax SyntaxUsed = A.getSyntax();
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;
Expand Down

0 comments on commit 2bb3fb0

Please sign in to comment.