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/PluginLoader.h"
00016 #include "llvm/Support/DynamicLibrary.h"
00017 #include "llvm/Support/ManagedStatic.h"
00018 #include "llvm/Support/Mutex.h"
00019 #include "llvm/Support/raw_ostream.h"
00020 #include <vector>
00021 using namespace llvm;
00022 
00023 static ManagedStatic<std::vector<std::string> > Plugins;
00024 static ManagedStatic<sys::SmartMutex<true> > PluginsLock;
00025 
00026 void PluginLoader::operator=(const std::string &Filename) {
00027   sys::SmartScopedLock<true> Lock(*PluginsLock);
00028   std::string Error;
00029   if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
00030     errs() << "Error opening '" << Filename << "': " << Error
00031            << "\n  -load request ignored.\n";
00032   } else {
00033     Plugins->push_back(Filename);
00034   }
00035 }
00036 
00037 unsigned PluginLoader::getNumPlugins() {
00038   sys::SmartScopedLock<true> Lock(*PluginsLock);
00039   return Plugins.isConstructed() ? Plugins->size() : 0;
00040 }
00041 
00042 std::string &PluginLoader::getPlugin(unsigned num) {
00043   sys::SmartScopedLock<true> Lock(*PluginsLock);
00044   assert(Plugins.isConstructed() && num < Plugins->size() &&
00045          "Asking for an out of bounds plugin");
00046   return (*Plugins)[num];
00047 }