add missing file
[llvm/avr.git] / lib / CompilerDriver / Plugin.cpp
blob7310d120bff58169ee325ffd14f525fb6a62903c
1 //===--- Plugin.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Plugin support.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CompilerDriver/Plugin.h"
15 #include "llvm/Support/ManagedStatic.h"
16 #include "llvm/System/Mutex.h"
17 #include <algorithm>
18 #include <vector>
20 namespace {
22 // Registry::Add<> does not do lifetime management (probably issues
23 // with static constructor/destructor ordering), so we have to
24 // implement it here.
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;
34 struct ByPriority {
35 bool operator()(const llvmc::BasePlugin* lhs,
36 const llvmc::BasePlugin* rhs) {
37 return lhs->Priority() < rhs->Priority();
42 namespace llvmc {
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();
59 B != E; ++B)
60 delete (*B);
62 pluginListInitialized = false;
65 void PluginLoader::PopulateLanguageMap(LanguageMap& langMap) {
66 llvm::sys::SmartScopedLock<true> Lock(*PluginMutex);
67 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end();
68 B != E; ++B)
69 (*B)->PopulateLanguageMap(langMap);
72 void PluginLoader::PopulateCompilationGraph(CompilationGraph& graph) {
73 llvm::sys::SmartScopedLock<true> Lock(*PluginMutex);
74 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end();
75 B != E; ++B)
76 (*B)->PopulateCompilationGraph(graph);