LLVM API Documentation
00001 //===--- Plugin.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open 00006 // Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Plugin support. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CompilerDriver/Plugin.h" 00015 #include "llvm/Support/ManagedStatic.h" 00016 #include "llvm/System/Mutex.h" 00017 #include <algorithm> 00018 #include <vector> 00019 00020 namespace { 00021 00022 // Registry::Add<> does not do lifetime management (probably issues 00023 // with static constructor/destructor ordering), so we have to 00024 // implement it here. 00025 // 00026 // All this static registration/life-before-main model seems 00027 // unnecessary convoluted to me. 00028 00029 static bool pluginListInitialized = false; 00030 typedef std::vector<const llvmc::BasePlugin*> PluginList; 00031 static PluginList Plugins; 00032 static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > PluginMutex; 00033 00034 struct ByPriority { 00035 bool operator()(const llvmc::BasePlugin* lhs, 00036 const llvmc::BasePlugin* rhs) { 00037 return lhs->Priority() < rhs->Priority(); 00038 } 00039 }; 00040 } 00041 00042 namespace llvmc { 00043 00044 PluginLoader::PluginLoader() { 00045 llvm::sys::SmartScopedLock<true> Lock(*PluginMutex); 00046 if (!pluginListInitialized) { 00047 for (PluginRegistry::iterator B = PluginRegistry::begin(), 00048 E = PluginRegistry::end(); B != E; ++B) 00049 Plugins.push_back(B->instantiate()); 00050 std::sort(Plugins.begin(), Plugins.end(), ByPriority()); 00051 } 00052 pluginListInitialized = true; 00053 } 00054 00055 PluginLoader::~PluginLoader() { 00056 llvm::sys::SmartScopedLock<true> Lock(*PluginMutex); 00057 if (pluginListInitialized) { 00058 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end(); 00059 B != E; ++B) 00060 delete (*B); 00061 } 00062 pluginListInitialized = false; 00063 } 00064 00065 void PluginLoader::PopulateLanguageMap(LanguageMap& langMap) { 00066 llvm::sys::SmartScopedLock<true> Lock(*PluginMutex); 00067 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end(); 00068 B != E; ++B) 00069 (*B)->PopulateLanguageMap(langMap); 00070 } 00071 00072 void PluginLoader::PopulateCompilationGraph(CompilationGraph& graph) { 00073 llvm::sys::SmartScopedLock<true> Lock(*PluginMutex); 00074 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end(); 00075 B != E; ++B) 00076 (*B)->PopulateCompilationGraph(graph); 00077 } 00078 00079 }