LLVM API Documentation

PluginLoader.cpp

Go to the documentation of this file.
00001 //===-- PluginLoader.cpp - Implement -load command line option ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the -load <plugin> command line option handler.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DONT_GET_PLUGIN_LOADER_OPTION
00015 #include "llvm/Support/ManagedStatic.h"
00016 #include "llvm/Support/PluginLoader.h"
00017 #include "llvm/Support/Streams.h"
00018 #include "llvm/System/DynamicLibrary.h"
00019 #include "llvm/System/Mutex.h"
00020 #include <ostream>
00021 #include <vector>
00022 using namespace llvm;
00023 
00024 static ManagedStatic<std::vector<std::string> > Plugins;
00025 static ManagedStatic<sys::SmartMutex<true> > PluginsLock;
00026 
00027 void PluginLoader::operator=(const std::string &Filename) {
00028   sys::SmartScopedLock<true> Lock(*PluginsLock);
00029   std::string Error;
00030   if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
00031     cerr << "Error opening '" << Filename << "': " << Error
00032          << "\n  -load request ignored.\n";
00033   } else {
00034     Plugins->push_back(Filename);
00035   }
00036 }
00037 
00038 unsigned PluginLoader::getNumPlugins() {
00039   sys::SmartScopedLock<true> Lock(*PluginsLock);
00040   return Plugins.isConstructed() ? Plugins->size() : 0;
00041 }
00042 
00043 std::string &PluginLoader::getPlugin(unsigned num) {
00044   sys::SmartScopedLock<true> Lock(*PluginsLock);
00045   assert(Plugins.isConstructed() && num < Plugins->size() &&
00046          "Asking for an out of bounds plugin");
00047   return (*Plugins)[num];
00048 }