1 //===--- Plugin.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CompilerDriver/Plugin.h"
15 #include "llvm/Support/ManagedStatic.h"
16 #include "llvm/System/Mutex.h"
22 // Registry::Add<> does not do lifetime management (probably issues
23 // with static constructor/destructor ordering), so we have to
26 // All this static registration/life-before-main model seems
27 // unnecessary convoluted to me.
29 static bool pluginListInitialized = false;
30 typedef std::vector<const llvmc::BasePlugin*> PluginList;
31 static PluginList Plugins;
32 static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > PluginMutex;
35 bool operator()(const llvmc::BasePlugin* lhs,
36 const llvmc::BasePlugin* rhs) {
37 return lhs->Priority() < rhs->Priority();
44 PluginLoader::PluginLoader() {
45 llvm::sys::SmartScopedLock<true> Lock(*PluginMutex);
46 if (!pluginListInitialized) {
47 for (PluginRegistry::iterator B = PluginRegistry::begin(),
48 E = PluginRegistry::end(); B != E; ++B)
49 Plugins.push_back(B->instantiate());
50 std::sort(Plugins.begin(), Plugins.end(), ByPriority());
52 pluginListInitialized = true;
55 PluginLoader::~PluginLoader() {
56 llvm::sys::SmartScopedLock<true> Lock(*PluginMutex);
57 if (pluginListInitialized) {
58 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end();
62 pluginListInitialized = false;
65 void PluginLoader::RunInitialization(LanguageMap& langMap,
66 CompilationGraph& graph) const
68 llvm::sys::SmartScopedLock<true> Lock(*PluginMutex);
69 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end();
71 const BasePlugin* BP = *B;
72 BP->PreprocessOptions();
73 BP->PopulateLanguageMap(langMap);
74 BP->PopulateCompilationGraph(graph);